]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/ExecutionAspect.java
Modular distributions
[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 execution stack
23 executionContext.getUuid();
24
25 ExecutionFlow executionFlow = (ExecutionFlow) pjp.getTarget();
26 executionStack.enterFlow(executionFlow);
27 executionContext.setVariable(ExecutionContext.VAR_FLOW_ID,
28 executionStack.getCurrentStackLevelUuid());
29 executionContext.setVariable(ExecutionContext.VAR_FLOW_NAME,
30 executionFlow.getName());
31
32 if (log.isDebugEnabled())
33 logStackEvent("=> ", executionFlow);
34 // Actually execute the flow
35 pjp.proceed();
36 if (log.isDebugEnabled())
37 logStackEvent("<= ", executionFlow);
38
39 executionStack.leaveFlow(executionFlow);
40 }
41
42 @Around("getVariable()")
43 public Object aroundGetVariable(ProceedingJoinPoint pjp) throws Throwable {
44 Object obj = pjp.proceed();
45 // if the variable was not found, look in the stack starting at the
46 // upper flows
47 if (obj == null) {
48 String key = pjp.getArgs()[0].toString();
49 obj = executionStack.findLocalVariable(key);
50 }
51 return obj;
52 }
53
54 @Pointcut("execution(void org.argeo.slc.execution.ExecutionFlow.run())")
55 public void flowExecution() {
56 }
57
58 @Pointcut("execution(* org.argeo.slc.execution.ExecutionContext.getVariable(..))")
59 public void getVariable() {
60 }
61
62 public void setExecutionStack(ExecutionStack executionStack) {
63 this.executionStack = executionStack;
64 }
65
66 public void setExecutionContext(ExecutionContext executionContext) {
67 this.executionContext = executionContext;
68 }
69
70 protected void logStackEvent(String symbol, ExecutionFlow executionFlow) {
71 Integer stackSize = executionStack.getStackSize();
72 log.debug(depthSpaces(stackSize) + symbol + executionFlow + " #"
73 + executionStack.getCurrentStackLevelUuid() + ", depth="
74 + stackSize);
75 }
76
77 private String depthSpaces(int depth) {
78 StringBuffer buf = new StringBuffer(depth * 2);
79 for (int i = 0; i < depth; i++)
80 buf.append(" ");
81 return buf.toString();
82 }
83
84 }