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 /** Aspect intercepting calls on execution flows and contexts. */
15 public class ExecutionAspect
{
16 private final static Log log
= LogFactory
.getLog(ExecutionAspect
.class);
18 private ExecutionStack executionStack
;
19 private ExecutionContext executionContext
;
21 @Around("flowExecution()")
22 public void aroundFlow(ProceedingJoinPoint pjp
) throws Throwable
{
23 // IMPORTANT: Make sure that the execution context is called before the
25 executionContext
.getUuid();
27 ExecutionFlow executionFlow
= (ExecutionFlow
) pjp
.getTarget();
28 executionStack
.enterFlow(executionFlow
);
29 executionContext
.setVariable(ExecutionContext
.VAR_FLOW_ID
,
30 executionStack
.getCurrentStackLevelUuid());
31 executionContext
.setVariable(ExecutionContext
.VAR_FLOW_NAME
,
32 executionFlow
.getName());
34 logStackEvent("=> ", executionFlow
);
36 // Actually execute the flow
39 logStackEvent("<= ", executionFlow
);
40 executionStack
.leaveFlow(executionFlow
);
44 @Around("getVariable()")
45 public Object
aroundGetVariable(ProceedingJoinPoint pjp
) throws Throwable
{
46 Object obj
= pjp
.proceed();
47 // if the variable was not found, look in the stack starting at the
50 String key
= pjp
.getArgs()[0].toString();
51 obj
= executionStack
.findLocalVariable(key
);
56 @Pointcut("execution(void org.argeo.slc.execution.ExecutionFlow.run())")
57 public void flowExecution() {
60 @Pointcut("execution(* org.argeo.slc.execution.ExecutionContext.getVariable(..))")
61 public void getVariable() {
64 public void setExecutionStack(ExecutionStack executionStack
) {
65 this.executionStack
= executionStack
;
68 public void setExecutionContext(ExecutionContext executionContext
) {
69 this.executionContext
= executionContext
;
72 protected void logStackEvent(String symbol
, ExecutionFlow executionFlow
) {
73 Integer stackSize
= executionStack
.getStackSize();
74 if (log
.isTraceEnabled())
75 log
.debug(depthSpaces(stackSize
) + symbol
+ executionFlow
+ " #"
76 + executionStack
.getCurrentStackLevelUuid() + ", depth="
78 if (log
.isDebugEnabled())
79 log
.debug(depthSpaces(stackSize
) + symbol
+ executionFlow
);
82 protected void logRunnableExecution(ExecutionFlow executionFlow
,
84 Integer stackSize
= executionStack
.getStackSize();
85 if (log
.isDebugEnabled())
86 log
.debug(depthSpaces(stackSize
+ 1)
87 + runnable
.getClass().getSimpleName() + " in "
91 private String
depthSpaces(int depth
) {
92 StringBuffer buf
= new StringBuffer(depth
* 2);
93 for (int i
= 0; i
< depth
; i
++)
95 return buf
.toString();