1 | |
package org.jtheque.core.spring.aspect; |
2 | |
|
3 | |
import org.apache.log4j.Logger; |
4 | |
import org.aspectj.lang.JoinPoint; |
5 | |
import org.aspectj.lang.annotation.After; |
6 | |
import org.aspectj.lang.annotation.Aspect; |
7 | |
import org.aspectj.lang.annotation.Before; |
8 | |
import org.aspectj.lang.annotation.Pointcut; |
9 | |
import org.jtheque.core.managers.Managers; |
10 | |
import org.jtheque.core.managers.lifecycle.JThequeCoreTimer; |
11 | |
import org.jtheque.core.managers.log.ILoggingManager; |
12 | |
|
13 | |
import java.util.HashMap; |
14 | |
import java.util.Map; |
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
@Aspect |
38 | 0 | public final class PhasesCounterAndLoggingAspect { |
39 | 0 | private final Map<String, Long> startTimes = new HashMap<String, Long>(3); |
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
@Pointcut("execution(* org.jtheque.core.managers.lifecycle.phases.*Phase.run())") |
45 | 0 | public void run() { } |
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
@Before("run()") |
53 | |
public void startCounter(JoinPoint joinPoint) { |
54 | 0 | String phase = getPhase(joinPoint); |
55 | |
|
56 | 0 | Logger.getLogger(getClass()).trace("Phase " + phase + " started"); |
57 | |
|
58 | 0 | startTimes.put(phase, System.currentTimeMillis()); |
59 | 0 | } |
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
@After("run()") |
67 | |
public void stopCounter(JoinPoint joinPoint) { |
68 | 0 | String phase = getPhase(joinPoint); |
69 | |
|
70 | 0 | long time = System.currentTimeMillis() - startTimes.get(phase); |
71 | |
|
72 | 0 | Managers.getManager(ILoggingManager.class).getLogger(getClass()).trace("Phase {} finished in {}ms", phase, time); |
73 | |
|
74 | 0 | if ("Third".equals(phase)) { |
75 | 0 | JThequeCoreTimer.stop(); |
76 | |
} |
77 | 0 | } |
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
private static String getPhase(JoinPoint joinPoint) { |
86 | 0 | return joinPoint.getSourceLocation().getWithinType().getSimpleName().replace("Phase", ""); |
87 | |
} |
88 | |
} |