Repository navigation
Expand file tree
/
Copy pathLoggingProxy.java
More file actions
56 lines (47 loc) · 1.88 KB
/
Copy pathLoggingProxy.java
File metadata and controls
56 lines (47 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.*;
/**
* Created by user on 20.10.2017.
*/
public class LoggingProxy implements InvocationHandler {
private final Object delegate;
HashMap<ArrayList<Object>, ResultAndTme> cash = new HashMap<>();
public LoggingProxy(Object delegate) {
this.delegate = delegate;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Cashe annotation = method.getAnnotation(Cashe.class);
int i = annotation.expireInSec();
if (args != null){
System.out.print("input ");
for (Object arg: args) System.out.print(arg + ", ");
System.out.println();
}
ArrayList<Object> methAndParam = new ArrayList<>();
methAndParam.add(method);
Collections.addAll(methAndParam, args);
if (cash.containsKey(methAndParam)) addElementWhichExist(cash, methAndParam);
long startTime = System.currentTimeMillis();
Object result = method.invoke(delegate, args);
ResultAndTme resultAndTme = new ResultAndTme(result, startTime);
cash.put(methAndParam, resultAndTme);
return resultAndTme.getResult();
}
public static Object addElementWhichExist(Map<ArrayList<Object>, ResultAndTme> map, ArrayList<Object> methAndParam){
if (map.containsKey(methAndParam)){
long time = map.get(methAndParam).getStartTime();
long currentTime = System.currentTimeMillis();
if ((currentTime - time)*1000 < i){
System.out.println("contain");
System.out.println("Time is not up");
return map.get(methAndParam).getResult();
}
else {
System.out.println("Time is up");
map.remove(methAndParam);
}
}
}
}