]> git.argeo.org Git - gpl/argeo-slc.git/blob - legacy/org.argeo.slc.spring/src/org/argeo/slc/core/execution/ExecutionAspect.java
Massive Argeo APIs refactoring
[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.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;
11
12 @Aspect
13 /** Aspect intercepting calls on execution flows and contexts. */
14 public class ExecutionAspect {
15 private final static CmsLog log = CmsLog.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
23 // execution stack
24 executionContext.getUuid();
25
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());
32
33 logStackEvent("=> ", executionFlow);
34 try {
35 // Actually execute the flow
36 pjp.proceed();
37 } finally {
38 logStackEvent("<= ", executionFlow);
39 executionStack.leaveFlow(executionFlow);
40 }
41 }
42
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
47 // upper flows
48 if (obj == null) {
49 String key = pjp.getArgs()[0].toString();
50 obj = executionStack.findLocalVariable(key);
51 }
52 return obj;
53 }
54
55 @Pointcut("execution(void org.argeo.slc.execution.ExecutionFlow.run())")
56 public void flowExecution() {
57 }
58
59 @Pointcut("execution(* org.argeo.slc.execution.ExecutionContext.getVariable(..))")
60 public void getVariable() {
61 }
62
63 public void setExecutionStack(ExecutionStack executionStack) {
64 this.executionStack = executionStack;
65 }
66
67 public void setExecutionContext(ExecutionContext executionContext) {
68 this.executionContext = executionContext;
69 }
70
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="
76 + stackSize);
77 if (log.isDebugEnabled())
78 log.debug(depthSpaces(stackSize) + symbol + executionFlow);
79 }
80
81 protected void logRunnableExecution(ExecutionFlow executionFlow,
82 Runnable runnable) {
83 Integer stackSize = executionStack.getStackSize();
84 if (log.isDebugEnabled())
85 log.debug(depthSpaces(stackSize + 1)
86 + runnable.getClass().getSimpleName() + " in "
87 + executionFlow);
88 }
89
90 private String depthSpaces(int depth) {
91 StringBuffer buf = new StringBuffer(depth * 2);
92 for (int i = 0; i < depth; i++)
93 buf.append(" ");
94 return buf.toString();
95 }
96
97 }