1 package org
.argeo
.slc
.core
.execution
;
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
;
14 public class ExecutionAspect
{
15 private final static Log log
= LogFactory
.getLog(ExecutionAspect
.class);
17 private ExecutionStack executionStack
;
18 private ExecutionContext executionContext
;
20 @Around("flowExecution()")
21 public void aroundFlow(ProceedingJoinPoint pjp
) throws Throwable
{
22 // IMPORTANT: Make sure that the execution context is called before the
24 executionContext
.getUuid();
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());
33 if (log
.isDebugEnabled())
34 logStackEvent("=> ", executionFlow
);
37 // Actually execute the flow
40 if (log
.isDebugEnabled())
41 logStackEvent("<= ", executionFlow
);
43 executionStack
.leaveFlow(executionFlow
);
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
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
63 String key
= pjp
.getArgs()[0].toString();
64 obj
= executionStack
.findLocalVariable(key
);
69 @Pointcut("execution(void org.argeo.slc.execution.ExecutionFlow.run())")
70 public void flowExecution() {
73 @Pointcut("execution(void org.argeo.slc.execution.ExecutionFlow.doExecuteRunnable(..))")
74 public void runnableExecution() {
77 @Pointcut("execution(* org.argeo.slc.execution.ExecutionContext.getVariable(..))")
78 public void getVariable() {
81 public void setExecutionStack(ExecutionStack executionStack
) {
82 this.executionStack
= executionStack
;
85 public void setExecutionContext(ExecutionContext executionContext
) {
86 this.executionContext
= executionContext
;
89 protected void logStackEvent(String symbol
, ExecutionFlow executionFlow
) {
90 Integer stackSize
= executionStack
.getStackSize();
91 log
.debug(depthSpaces(stackSize
) + symbol
+ executionFlow
+ " #"
92 + executionStack
.getCurrentStackLevelUuid() + ", depth="
96 protected void logRunnableExecution(ExecutionFlow executionFlow
,
98 Integer stackSize
= executionStack
.getStackSize();
99 log
.debug(depthSpaces(stackSize
+ 1)
100 + runnable
.getClass().getSimpleName() + " in " + executionFlow
);
103 private String
depthSpaces(int depth
) {
104 StringBuffer buf
= new StringBuffer(depth
* 2);
105 for (int i
= 0; i
< depth
; i
++)
107 return buf
.toString();