]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/ExecutionAspect.java
6d0b9a64e5ad3bbf064c992b84b15e9ee427f2a9
[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
36 try {
37 // Actually execute the flow
38 pjp.proceed();
39 } finally {
40 if (log.isDebugEnabled())
41 logStackEvent("<= ", executionFlow);
42
43 executionStack.leaveFlow(executionFlow);
44 }
45 }
46
47 @Around("runnableExecution()")
48 public void aroundRunnable(ProceedingJoinPoint pjp) throws Throwable {
49 ExecutionFlow executionFlow = (ExecutionFlow) pjp.getTarget();
50 Runnable runnable = (Runnable) pjp.getArgs()[0];
51 if (log.isDebugEnabled())
52 logRunnableExecution(executionFlow, runnable);
53 // Actually execute the runnable
54 pjp.proceed();
55 }
56
57 @Around("getVariable()")
58 public Object aroundGetVariable(ProceedingJoinPoint pjp) throws Throwable {
59 Object obj = pjp.proceed();
60 // if the variable was not found, look in the stack starting at the
61 // upper flows
62 if (obj == null) {
63 String key = pjp.getArgs()[0].toString();
64 obj = executionStack.findLocalVariable(key);
65 }
66 return obj;
67 }
68
69 @Pointcut("execution(void org.argeo.slc.execution.ExecutionFlow.run())")
70 public void flowExecution() {
71 }
72
73 @Pointcut("execution(void org.argeo.slc.execution.ExecutionFlow.doExecuteRunnable(..))")
74 public void runnableExecution() {
75 }
76
77 @Pointcut("execution(* org.argeo.slc.execution.ExecutionContext.getVariable(..))")
78 public void getVariable() {
79 }
80
81 public void setExecutionStack(ExecutionStack executionStack) {
82 this.executionStack = executionStack;
83 }
84
85 public void setExecutionContext(ExecutionContext executionContext) {
86 this.executionContext = executionContext;
87 }
88
89 protected void logStackEvent(String symbol, ExecutionFlow executionFlow) {
90 Integer stackSize = executionStack.getStackSize();
91 log.debug(depthSpaces(stackSize) + symbol + executionFlow + " #"
92 + executionStack.getCurrentStackLevelUuid() + ", depth="
93 + stackSize);
94 }
95
96 protected void logRunnableExecution(ExecutionFlow executionFlow,
97 Runnable runnable) {
98 Integer stackSize = executionStack.getStackSize();
99 log.debug(depthSpaces(stackSize + 1)
100 + runnable.getClass().getSimpleName() + " in " + executionFlow);
101 }
102
103 private String depthSpaces(int depth) {
104 StringBuffer buf = new StringBuffer(depth * 2);
105 for (int i = 0; i < depth; i++)
106 buf.append(" ");
107 return buf.toString();
108 }
109
110 }