1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.dozer.stats; |
17 | |
|
18 | |
import org.dozer.util.MappingUtils; |
19 | |
|
20 | |
import java.lang.reflect.InvocationHandler; |
21 | |
import java.lang.reflect.InvocationTargetException; |
22 | |
import java.lang.reflect.Method; |
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
public class StatisticsInterceptor implements InvocationHandler { |
31 | |
private final Object delegate; |
32 | |
private final StatisticsManager statsMgr; |
33 | |
|
34 | 2 | public StatisticsInterceptor(Object delegate, StatisticsManager statsMgr) { |
35 | 2 | this.delegate = delegate; |
36 | 2 | this.statsMgr = statsMgr; |
37 | 2 | } |
38 | |
|
39 | |
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { |
40 | 2 | long start = System.currentTimeMillis(); |
41 | |
|
42 | |
try { |
43 | 2 | Object result = method.invoke(delegate, args); |
44 | |
|
45 | 1 | long stop = System.currentTimeMillis(); |
46 | 1 | statsMgr.increment(StatisticType.MAPPING_SUCCESS_COUNT); |
47 | 1 | statsMgr.increment(StatisticType.MAPPING_TIME, (stop - start)); |
48 | |
|
49 | 1 | return result; |
50 | 1 | } catch (InvocationTargetException e) { |
51 | 1 | Throwable ex = e.getTargetException(); |
52 | |
|
53 | 1 | statsMgr.increment(StatisticType.MAPPING_FAILURE_COUNT); |
54 | 1 | Throwable rootCause = MappingUtils.getRootCause(ex); |
55 | 1 | statsMgr.increment(StatisticType.MAPPING_FAILURE_EX_TYPE_COUNT, rootCause.getClass()); |
56 | 1 | incrementClassMappingFailureTypeStat(args); |
57 | 1 | throw ex; |
58 | |
} |
59 | |
} |
60 | |
|
61 | |
private void incrementClassMappingFailureTypeStat(Object[] args) { |
62 | |
|
63 | 1 | String srcClassName = null; |
64 | 1 | if (args[0] != null) { |
65 | 1 | srcClassName = args[0].getClass().getName(); |
66 | |
} |
67 | 1 | String destClassName = null; |
68 | 1 | if (args[1] != null) { |
69 | 1 | if (args[1] instanceof Class) { |
70 | 0 | destClassName = ((Class<?>) args[1]).getName(); |
71 | |
} else { |
72 | 1 | destClassName = args[1].getClass().getName(); |
73 | |
} |
74 | |
} |
75 | 1 | statsMgr.increment(StatisticType.MAPPING_FAILURE_TYPE_COUNT, srcClassName + "-->" + destClassName); |
76 | 1 | } |
77 | |
} |