1 package org
.argeo
.slc
.core
.execution
;
3 import java
.util
.Collections
;
5 import java
.util
.HashMap
;
9 import org
.argeo
.slc
.SlcException
;
10 import org
.argeo
.slc
.execution
.ExecutionContext
;
11 import org
.argeo
.slc
.execution
.ExecutionFlow
;
12 import org
.argeo
.slc
.execution
.ExecutionStack
;
13 import org
.springframework
.beans
.BeanWrapper
;
14 import org
.springframework
.beans
.BeanWrapperImpl
;
15 import org
.springframework
.beans
.BeansException
;
16 import org
.springframework
.context
.ApplicationContext
;
17 import org
.springframework
.context
.ApplicationContextAware
;
19 public class MapExecutionContext
implements ExecutionContext
,
20 ApplicationContextAware
{
21 private final Map
<String
, Object
> variables
= Collections
22 .synchronizedMap(new HashMap
<String
, Object
>());
24 private final String uuid
;
26 private ApplicationContext applicationContext
;
27 private ExecutionStack executionStack
;
29 public MapExecutionContext() {
30 uuid
= UUID
.randomUUID().toString();
31 variables
.put(VAR_EXECUTION_CONTEXT_ID
, uuid
);
32 variables
.put(VAR_EXECUTION_CONTEXT_CREATION_DATE
, new Date());
35 public void setVariable(String key
, Object value
) {
36 // check if we do not refer to a bean
37 int lastInd
= key
.lastIndexOf('.');
38 if (applicationContext
!= null && lastInd
> 0) {
39 String beanName
= key
.substring(0, lastInd
);
40 String propertyName
= key
.substring(lastInd
+ 1);
41 if (applicationContext
.containsBean(beanName
)) {
42 BeanWrapper beanWrapper
= new BeanWrapperImpl(
43 applicationContext
.getBean(beanName
));
44 if (!beanWrapper
.isWritableProperty(propertyName
))
45 throw new SlcException("No writable property "
46 + propertyName
+ " in bean " + beanName
);
47 beanWrapper
.setPropertyValue(propertyName
, value
);
51 variables
.put(key
, value
);
54 public Object
getVariable(String key
) {
55 // check if we do not refer to a bean
56 int lastInd
= key
.lastIndexOf('.');
57 if (applicationContext
!= null && lastInd
> 0) {
58 String beanName
= key
.substring(0, lastInd
);
59 String propertyName
= key
.substring(lastInd
+ 1);
60 if (applicationContext
.containsBean(beanName
)) {
61 BeanWrapper beanWrapper
= new BeanWrapperImpl(
62 applicationContext
.getBean(beanName
));
63 if (!beanWrapper
.isReadableProperty(propertyName
))
64 throw new SlcException("No readable property "
65 + propertyName
+ " in bean " + beanName
);
66 Object obj
= beanWrapper
.getPropertyValue(propertyName
);
71 Object value
= variables
.get(key
);
72 // try system property in last resort
74 value
= System
.getProperty(key
);
76 // if the variable was not found, look in the stack starting at the
79 value
= executionStack
.findLocalVariable(key
);
84 public String
getUuid() {
89 public void beforeFlow(ExecutionFlow executionFlow
) {
91 executionStack
.enterFlow(executionFlow
);
92 setVariable(ExecutionContext
.VAR_FLOW_ID
,
93 executionStack
.getCurrentStackLevelUuid());
94 setVariable(ExecutionContext
.VAR_FLOW_NAME
, executionFlow
.getName());
98 public void afterFlow(ExecutionFlow executionFlow
) {
99 executionStack
.leaveFlow(executionFlow
);
103 public boolean equals(Object obj
) {
104 if (obj
instanceof ExecutionContext
)
105 return uuid
.equals(((ExecutionContext
) obj
).getUuid());
110 public String
toString() {
111 return getClass().getSimpleName() + "#" + uuid
;
114 public void setApplicationContext(ApplicationContext applicationContext
)
115 throws BeansException
{
116 this.applicationContext
= applicationContext
;
119 public void setExecutionStack(ExecutionStack executionStack
) {
120 this.executionStack
= executionStack
;