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
);
35 // Actually execute the flow
37 if (log
.isDebugEnabled())
38 logStackEvent("<= ", executionFlow
);
40 executionStack
.leaveFlow(executionFlow
);
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
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
59 String key
= pjp
.getArgs()[0].toString();
60 obj
= executionStack
.findLocalVariable(key
);
65 @Pointcut("execution(void org.argeo.slc.execution.ExecutionFlow.run())")
66 public void flowExecution() {
69 @Pointcut("execution(void org.argeo.slc.execution.ExecutionFlow.doExecuteRunnable(..))")
70 public void runnableExecution() {
73 @Pointcut("execution(* org.argeo.slc.execution.ExecutionContext.getVariable(..))")
74 public void getVariable() {
77 public void setExecutionStack(ExecutionStack executionStack
) {
78 this.executionStack
= executionStack
;
81 public void setExecutionContext(ExecutionContext executionContext
) {
82 this.executionContext
= executionContext
;
85 protected void logStackEvent(String symbol
, ExecutionFlow executionFlow
) {
86 Integer stackSize
= executionStack
.getStackSize();
87 log
.debug(depthSpaces(stackSize
) + symbol
+ executionFlow
+ " #"
88 + executionStack
.getCurrentStackLevelUuid() + ", depth="
92 protected void logRunnableExecution(ExecutionFlow executionFlow
,
94 Integer stackSize
= executionStack
.getStackSize();
95 log
.debug(depthSpaces(stackSize
+ 1)
96 + runnable
.getClass().getSimpleName() + " in " + executionFlow
);
99 private String
depthSpaces(int depth
) {
100 StringBuffer buf
= new StringBuffer(depth
* 2);
101 for (int i
= 0; i
< depth
; i
++)
103 return buf
.toString();