]> git.argeo.org Git - gpl/argeo-slc.git/blob - legacy/org.argeo.slc.spring/src/org/argeo/slc/core/execution/ExecutionAspect.java
Adapt to changes in Argeo Commons
[gpl/argeo-slc.git] / legacy / org.argeo.slc.spring / src / 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 /** Aspect intercepting calls on execution flows and contexts. */
15 public class ExecutionAspect {
16 private final static Log log = LogFactory.getLog(ExecutionAspect.class);
17
18 private ExecutionStack executionStack;
19 private ExecutionContext executionContext;
20
21 @Around("flowExecution()")
22 public void aroundFlow(ProceedingJoinPoint pjp) throws Throwable {
23 // IMPORTANT: Make sure that the execution context is called before the
24 // execution stack
25 executionContext.getUuid();
26
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());
33
34 logStackEvent("=> ", executionFlow);
35 try {
36 // Actually execute the flow
37 pjp.proceed();
38 } finally {
39 logStackEvent("<= ", executionFlow);
40 executionStack.leaveFlow(executionFlow);
41 }
42 }
43
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
48 // upper flows
49 if (obj == null) {
50 String key = pjp.getArgs()[0].toString();
51 obj = executionStack.findLocalVariable(key);
52 }
53 return obj;
54 }
55
56 @Pointcut("execution(void org.argeo.slc.execution.ExecutionFlow.run())")
57 public void flowExecution() {
58 }
59
60 @Pointcut("execution(* org.argeo.slc.execution.ExecutionContext.getVariable(..))")
61 public void getVariable() {
62 }
63
64 public void setExecutionStack(ExecutionStack executionStack) {
65 this.executionStack = executionStack;
66 }
67
68 public void setExecutionContext(ExecutionContext executionContext) {
69 this.executionContext = executionContext;
70 }
71
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="
77 + stackSize);
78 if (log.isDebugEnabled())
79 log.debug(depthSpaces(stackSize) + symbol + executionFlow);
80 }
81
82 protected void logRunnableExecution(ExecutionFlow executionFlow,
83 Runnable runnable) {
84 Integer stackSize = executionStack.getStackSize();
85 if (log.isDebugEnabled())
86 log.debug(depthSpaces(stackSize + 1)
87 + runnable.getClass().getSimpleName() + " in "
88 + executionFlow);
89 }
90
91 private String depthSpaces(int depth) {
92 StringBuffer buf = new StringBuffer(depth * 2);
93 for (int i = 0; i < depth; i++)
94 buf.append(" ");
95 return buf.toString();
96 }
97
98 }