]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/ExecutionAspect.java
Document and improve execution model
[gpl/argeo-slc.git] / runtime / org.argeo.slc.core / src / main / java / org / argeo / slc / core / execution / ExecutionAspect.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package org.argeo.slc.core.execution;
18
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21 import org.argeo.slc.execution.ExecutionContext;
22 import org.argeo.slc.execution.ExecutionFlow;
23 import org.argeo.slc.execution.ExecutionStack;
24 import org.aspectj.lang.ProceedingJoinPoint;
25 import org.aspectj.lang.annotation.Around;
26 import org.aspectj.lang.annotation.Aspect;
27 import org.aspectj.lang.annotation.Pointcut;
28
29 @Aspect
30 /** Aspect intercepting calls on execution flows and contexts. */
31 public class ExecutionAspect {
32 private final static Log log = LogFactory.getLog(ExecutionAspect.class);
33
34 private ExecutionStack executionStack;
35 private ExecutionContext executionContext;
36
37 @Around("flowExecution()")
38 public void aroundFlow(ProceedingJoinPoint pjp) throws Throwable {
39 // IMPORTANT: Make sure that the execution context is called before the
40 // execution stack
41 executionContext.getUuid();
42
43 ExecutionFlow executionFlow = (ExecutionFlow) pjp.getTarget();
44 executionStack.enterFlow(executionFlow);
45 executionContext.setVariable(ExecutionContext.VAR_FLOW_ID,
46 executionStack.getCurrentStackLevelUuid());
47 executionContext.setVariable(ExecutionContext.VAR_FLOW_NAME,
48 executionFlow.getName());
49
50 if (log.isDebugEnabled())
51 logStackEvent("=> ", executionFlow);
52
53 try {
54 // Actually execute the flow
55 pjp.proceed();
56 } finally {
57 if (log.isDebugEnabled())
58 logStackEvent("<= ", executionFlow);
59
60 executionStack.leaveFlow(executionFlow);
61 }
62 }
63
64 @Around("getVariable()")
65 public Object aroundGetVariable(ProceedingJoinPoint pjp) throws Throwable {
66 Object obj = pjp.proceed();
67 // if the variable was not found, look in the stack starting at the
68 // upper flows
69 if (obj == null) {
70 String key = pjp.getArgs()[0].toString();
71 obj = executionStack.findLocalVariable(key);
72 }
73 return obj;
74 }
75
76 @Pointcut("execution(void org.argeo.slc.execution.ExecutionFlow.run())")
77 public void flowExecution() {
78 }
79
80 @Pointcut("execution(* org.argeo.slc.execution.ExecutionContext.getVariable(..))")
81 public void getVariable() {
82 }
83
84 public void setExecutionStack(ExecutionStack executionStack) {
85 this.executionStack = executionStack;
86 }
87
88 public void setExecutionContext(ExecutionContext executionContext) {
89 this.executionContext = executionContext;
90 }
91
92 protected void logStackEvent(String symbol, ExecutionFlow executionFlow) {
93 Integer stackSize = executionStack.getStackSize();
94 if (log.isTraceEnabled())
95 log.debug(depthSpaces(stackSize) + symbol + executionFlow + " #"
96 + executionStack.getCurrentStackLevelUuid() + ", depth="
97 + stackSize);
98 else if (log.isDebugEnabled())
99 log.debug(depthSpaces(stackSize) + symbol + executionFlow);
100 }
101
102 protected void logRunnableExecution(ExecutionFlow executionFlow,
103 Runnable runnable) {
104 Integer stackSize = executionStack.getStackSize();
105 if (log.isDebugEnabled())
106 log.debug(depthSpaces(stackSize + 1)
107 + runnable.getClass().getSimpleName() + " in "
108 + executionFlow);
109 }
110
111 private String depthSpaces(int depth) {
112 StringBuffer buf = new StringBuffer(depth * 2);
113 for (int i = 0; i < depth; i++)
114 buf.append(" ");
115 return buf.toString();
116 }
117
118 }