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
.springframework
.beans
.BeanWrapper
;
12 import org
.springframework
.beans
.BeanWrapperImpl
;
13 import org
.springframework
.beans
.BeansException
;
14 import org
.springframework
.context
.ApplicationContext
;
15 import org
.springframework
.context
.ApplicationContextAware
;
17 public class MapExecutionContext
implements ExecutionContext
,
18 ApplicationContextAware
{
19 private final Map
<String
, Object
> variables
= Collections
20 .synchronizedMap(new HashMap
<String
, Object
>());
22 private final String uuid
;
24 private ApplicationContext applicationContext
;
26 public MapExecutionContext() {
27 uuid
= UUID
.randomUUID().toString();
28 variables
.put(VAR_EXECUTION_CONTEXT_ID
, uuid
);
29 variables
.put(VAR_EXECUTION_CONTEXT_CREATION_DATE
, new Date());
32 public void setVariable(String key
, Object value
) {
33 // check if we do not refer to a bean
34 int lastInd
= key
.lastIndexOf('.');
35 if (applicationContext
!= null && lastInd
> 0) {
36 String beanName
= key
.substring(0, lastInd
);
37 String propertyName
= key
.substring(lastInd
+ 1);
38 if (applicationContext
.containsBean(beanName
)) {
39 BeanWrapper beanWrapper
= new BeanWrapperImpl(
40 applicationContext
.getBean(beanName
));
41 if (!beanWrapper
.isWritableProperty(propertyName
))
42 throw new SlcException("No writable property "
43 + propertyName
+ " in bean " + beanName
);
44 beanWrapper
.setPropertyValue(propertyName
, value
);
48 variables
.put(key
, value
);
51 public Object
getVariable(String key
) {
52 // check if we do not refer to a bean
53 int lastInd
= key
.lastIndexOf('.');
54 if (applicationContext
!= null && lastInd
> 0) {
55 String beanName
= key
.substring(0, lastInd
);
56 String propertyName
= key
.substring(lastInd
+ 1);
57 if (applicationContext
.containsBean(beanName
)) {
58 BeanWrapper beanWrapper
= new BeanWrapperImpl(
59 applicationContext
.getBean(beanName
));
60 if (!beanWrapper
.isReadableProperty(propertyName
))
61 throw new SlcException("No readable property "
62 + propertyName
+ " in bean " + beanName
);
63 Object obj
= beanWrapper
.getPropertyValue(propertyName
);
68 return variables
.get(key
);
71 public String
getUuid() {
76 public boolean equals(Object obj
) {
77 if (obj
instanceof ExecutionContext
)
78 return uuid
.equals(((ExecutionContext
) obj
).getUuid());
83 public String
toString() {
84 return getClass().getSimpleName() + "#" + uuid
;
87 public void setApplicationContext(ApplicationContext applicationContext
)
88 throws BeansException
{
89 this.applicationContext
= applicationContext
;