1 package org
.argeo
.slc
.core
.execution
;
3 import org
.argeo
.api
.cms
.CmsLog
;
4 import org
.argeo
.slc
.execution
.ExecutionContext
;
5 import org
.argeo
.slc
.execution
.ExecutionFlow
;
6 import org
.argeo
.slc
.execution
.ExecutionStack
;
7 import org
.aspectj
.lang
.ProceedingJoinPoint
;
8 import org
.aspectj
.lang
.annotation
.Around
;
9 import org
.aspectj
.lang
.annotation
.Aspect
;
10 import org
.aspectj
.lang
.annotation
.Pointcut
;
13 /** Aspect intercepting calls on execution flows and contexts. */
14 public class ExecutionAspect
{
15 private final static CmsLog log
= CmsLog
.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 logStackEvent("=> ", executionFlow
);
35 // Actually execute the flow
38 logStackEvent("<= ", executionFlow
);
39 executionStack
.leaveFlow(executionFlow
);
43 @Around("getVariable()")
44 public Object
aroundGetVariable(ProceedingJoinPoint pjp
) throws Throwable
{
45 Object obj
= pjp
.proceed();
46 // if the variable was not found, look in the stack starting at the
49 String key
= pjp
.getArgs()[0].toString();
50 obj
= executionStack
.findLocalVariable(key
);
55 @Pointcut("execution(void org.argeo.slc.execution.ExecutionFlow.run())")
56 public void flowExecution() {
59 @Pointcut("execution(* org.argeo.slc.execution.ExecutionContext.getVariable(..))")
60 public void getVariable() {
63 public void setExecutionStack(ExecutionStack executionStack
) {
64 this.executionStack
= executionStack
;
67 public void setExecutionContext(ExecutionContext executionContext
) {
68 this.executionContext
= executionContext
;
71 protected void logStackEvent(String symbol
, ExecutionFlow executionFlow
) {
72 Integer stackSize
= executionStack
.getStackSize();
73 if (log
.isTraceEnabled())
74 log
.debug(depthSpaces(stackSize
) + symbol
+ executionFlow
+ " #"
75 + executionStack
.getCurrentStackLevelUuid() + ", depth="
77 if (log
.isDebugEnabled())
78 log
.debug(depthSpaces(stackSize
) + symbol
+ executionFlow
);
81 protected void logRunnableExecution(ExecutionFlow executionFlow
,
83 Integer stackSize
= executionStack
.getStackSize();
84 if (log
.isDebugEnabled())
85 log
.debug(depthSpaces(stackSize
+ 1)
86 + runnable
.getClass().getSimpleName() + " in "
90 private String
depthSpaces(int depth
) {
91 StringBuffer buf
= new StringBuffer(depth
* 2);
92 for (int i
= 0; i
< depth
; i
++)
94 return buf
.toString();