]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/ExecutionAspect.java
Introduce JMX agent
[gpl/argeo-slc.git] / runtime / org.argeo.slc.core / src / main / java / org / argeo / slc / core / execution / ExecutionAspect.java
1 package org.argeo.slc.core.execution;
2
3 import org.apache.commons.logging.Log;
4 import org.apache.commons.logging.LogFactory;
5 import org.argeo.slc.execution.ExecutionContext;
6 import org.argeo.slc.execution.ExecutionFlow;
7 import org.argeo.slc.execution.ExecutionStack;
8 import org.aspectj.lang.ProceedingJoinPoint;
9 import org.aspectj.lang.annotation.Around;
10 import org.aspectj.lang.annotation.Aspect;
11 import org.aspectj.lang.annotation.Pointcut;
12
13 @Aspect
14 public class ExecutionAspect {
15 private final static Log log = LogFactory.getLog(ExecutionAspect.class);
16
17 private ExecutionStack executionStack;
18 private ExecutionContext executionContext;
19
20 @Around("flowExecution()")
21 public void aroundFlow(ProceedingJoinPoint pjp) throws Throwable {
22 // IMPORTANT: Make sure that the execution context is called before the
23 // execution stack
24 executionContext.getUuid();
25
26 ExecutionFlow executionFlow = (ExecutionFlow) pjp.getTarget();
27 executionStack.enterFlow(executionFlow);
28 executionContext.setVariable(ExecutionContext.VAR_FLOW_ID,
29 executionStack.getCurrentStackLevelUuid());
30 executionContext.setVariable(ExecutionContext.VAR_FLOW_NAME,
31 executionFlow.getName());
32
33 if (log.isDebugEnabled())
34 logStackEvent("=> ", executionFlow);
35 // Actually execute the flow
36 pjp.proceed();
37 if (log.isDebugEnabled())
38 logStackEvent("<= ", executionFlow);
39
40 executionStack.leaveFlow(executionFlow);
41 }
42
43 @Around("runnableExecution()")
44 public void aroundRunnable(ProceedingJoinPoint pjp) throws Throwable {
45 ExecutionFlow executionFlow = (ExecutionFlow) pjp.getTarget();
46 Runnable runnable = (Runnable) pjp.getArgs()[0];
47 if (log.isDebugEnabled())
48 logRunnableExecution(executionFlow, runnable);
49 // Actually execute the runnable
50 pjp.proceed();
51 }
52
53 @Around("getVariable()")
54 public Object aroundGetVariable(ProceedingJoinPoint pjp) throws Throwable {
55 Object obj = pjp.proceed();
56 // if the variable was not found, look in the stack starting at the
57 // upper flows
58 if (obj == null) {
59 String key = pjp.getArgs()[0].toString();
60 obj = executionStack.findLocalVariable(key);
61 }
62 return obj;
63 }
64
65 @Pointcut("execution(void org.argeo.slc.execution.ExecutionFlow.run())")
66 public void flowExecution() {
67 }
68
69 @Pointcut("execution(void org.argeo.slc.execution.ExecutionFlow.doExecuteRunnable(..))")
70 public void runnableExecution() {
71 }
72
73 @Pointcut("execution(* org.argeo.slc.execution.ExecutionContext.getVariable(..))")
74 public void getVariable() {
75 }
76
77 public void setExecutionStack(ExecutionStack executionStack) {
78 this.executionStack = executionStack;
79 }
80
81 public void setExecutionContext(ExecutionContext executionContext) {
82 this.executionContext = executionContext;
83 }
84
85 protected void logStackEvent(String symbol, ExecutionFlow executionFlow) {
86 Integer stackSize = executionStack.getStackSize();
87 log.debug(depthSpaces(stackSize) + symbol + executionFlow + " #"
88 + executionStack.getCurrentStackLevelUuid() + ", depth="
89 + stackSize);
90 }
91
92 protected void logRunnableExecution(ExecutionFlow executionFlow,
93 Runnable runnable) {
94 Integer stackSize = executionStack.getStackSize();
95 log.debug(depthSpaces(stackSize + 1)
96 + runnable.getClass().getSimpleName() + " in " + executionFlow);
97 }
98
99 private String depthSpaces(int depth) {
100 StringBuffer buf = new StringBuffer(depth * 2);
101 for (int i = 0; i < depth; i++)
102 buf.append(" ");
103 return buf.toString();
104 }
105
106 }