2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org
.argeo
.slc
.core
.execution
;
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
;
30 /** Aspect intercepting calls on execution flows and contexts. */
31 public class ExecutionAspect
{
32 private final static Log log
= LogFactory
.getLog(ExecutionAspect
.class);
34 private ExecutionStack executionStack
;
35 private ExecutionContext executionContext
;
37 @Around("flowExecution()")
38 public void aroundFlow(ProceedingJoinPoint pjp
) throws Throwable
{
39 // IMPORTANT: Make sure that the execution context is called before the
41 executionContext
.getUuid();
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());
50 logStackEvent("=> ", executionFlow
, false);
52 // Actually execute the flow
55 logStackEvent("<= ", executionFlow
, true);
56 executionStack
.leaveFlow(executionFlow
);
60 @Around("getVariable()")
61 public Object
aroundGetVariable(ProceedingJoinPoint pjp
) throws Throwable
{
62 Object obj
= pjp
.proceed();
63 // if the variable was not found, look in the stack starting at the
66 String key
= pjp
.getArgs()[0].toString();
67 obj
= executionStack
.findLocalVariable(key
);
72 @Pointcut("execution(void org.argeo.slc.execution.ExecutionFlow.run())")
73 public void flowExecution() {
76 @Pointcut("execution(* org.argeo.slc.execution.ExecutionContext.getVariable(..))")
77 public void getVariable() {
80 public void setExecutionStack(ExecutionStack executionStack
) {
81 this.executionStack
= executionStack
;
84 public void setExecutionContext(ExecutionContext executionContext
) {
85 this.executionContext
= executionContext
;
88 protected void logStackEvent(String symbol
, ExecutionFlow executionFlow
,
90 Integer stackSize
= executionStack
.getStackSize();
91 if (log
.isTraceEnabled())
92 log
.debug(depthSpaces(stackSize
) + symbol
+ executionFlow
+ " #"
93 + executionStack
.getCurrentStackLevelUuid() + ", depth="
95 else if (log
.isDebugEnabled() && !trace
)
96 log
.debug(depthSpaces(stackSize
) + symbol
+ executionFlow
);
97 else if (log
.isTraceEnabled() && trace
)
98 log
.trace(depthSpaces(stackSize
) + symbol
+ executionFlow
);
102 protected void logRunnableExecution(ExecutionFlow executionFlow
,
104 Integer stackSize
= executionStack
.getStackSize();
105 if (log
.isDebugEnabled())
106 log
.debug(depthSpaces(stackSize
+ 1)
107 + runnable
.getClass().getSimpleName() + " in "
111 private String
depthSpaces(int depth
) {
112 StringBuffer buf
= new StringBuffer(depth
* 2);
113 for (int i
= 0; i
< depth
; i
++)
115 return buf
.toString();