2 * Copyright (C) 2007-2012 Argeo GmbH
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.
16 package org
.argeo
.slc
.core
.execution
;
18 import java
.util
.Collections
;
19 import java
.util
.Date
;
20 import java
.util
.HashMap
;
22 import java
.util
.UUID
;
24 import org
.argeo
.slc
.SlcException
;
25 import org
.argeo
.slc
.execution
.ExecutionContext
;
26 import org
.springframework
.beans
.BeanWrapper
;
27 import org
.springframework
.beans
.BeanWrapperImpl
;
28 import org
.springframework
.beans
.BeansException
;
29 import org
.springframework
.context
.ApplicationContext
;
30 import org
.springframework
.context
.ApplicationContextAware
;
32 public class MapExecutionContext
implements ExecutionContext
,
33 ApplicationContextAware
{
34 private final Map
<String
, Object
> variables
= Collections
35 .synchronizedMap(new HashMap
<String
, Object
>());
37 private final String uuid
;
39 private ApplicationContext applicationContext
;
41 public MapExecutionContext() {
42 uuid
= UUID
.randomUUID().toString();
43 variables
.put(VAR_EXECUTION_CONTEXT_ID
, uuid
);
44 variables
.put(VAR_EXECUTION_CONTEXT_CREATION_DATE
, new Date());
47 public void setVariable(String key
, Object value
) {
48 // check if we do not refer to a bean
49 int lastInd
= key
.lastIndexOf('.');
50 if (applicationContext
!= null && lastInd
> 0) {
51 String beanName
= key
.substring(0, lastInd
);
52 String propertyName
= key
.substring(lastInd
+ 1);
53 if (applicationContext
.containsBean(beanName
)) {
54 BeanWrapper beanWrapper
= new BeanWrapperImpl(
55 applicationContext
.getBean(beanName
));
56 if (!beanWrapper
.isWritableProperty(propertyName
))
57 throw new SlcException("No writable property "
58 + propertyName
+ " in bean " + beanName
);
59 beanWrapper
.setPropertyValue(propertyName
, value
);
63 variables
.put(key
, value
);
66 public Object
getVariable(String key
) {
67 // check if we do not refer to a bean
68 int lastInd
= key
.lastIndexOf('.');
69 if (applicationContext
!= null && lastInd
> 0) {
70 String beanName
= key
.substring(0, lastInd
);
71 String propertyName
= key
.substring(lastInd
+ 1);
72 if (applicationContext
.containsBean(beanName
)) {
73 BeanWrapper beanWrapper
= new BeanWrapperImpl(
74 applicationContext
.getBean(beanName
));
75 if (!beanWrapper
.isReadableProperty(propertyName
))
76 throw new SlcException("No readable property "
77 + propertyName
+ " in bean " + beanName
);
78 Object obj
= beanWrapper
.getPropertyValue(propertyName
);
83 Object value
= variables
.get(key
);
84 // try system property in last resort
86 value
= System
.getProperty(key
);
90 public String
getUuid() {
95 public boolean equals(Object obj
) {
96 if (obj
instanceof ExecutionContext
)
97 return uuid
.equals(((ExecutionContext
) obj
).getUuid());
102 public String
toString() {
103 return getClass().getSimpleName() + "#" + uuid
;
106 public void setApplicationContext(ApplicationContext applicationContext
)
107 throws BeansException
{
108 this.applicationContext
= applicationContext
;