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 java
.util
.Collections
;
20 import java
.util
.Date
;
21 import java
.util
.HashMap
;
23 import java
.util
.UUID
;
25 import org
.argeo
.slc
.SlcException
;
26 import org
.argeo
.slc
.execution
.ExecutionContext
;
27 import org
.springframework
.beans
.BeanWrapper
;
28 import org
.springframework
.beans
.BeanWrapperImpl
;
29 import org
.springframework
.beans
.BeansException
;
30 import org
.springframework
.context
.ApplicationContext
;
31 import org
.springframework
.context
.ApplicationContextAware
;
33 public class MapExecutionContext
implements ExecutionContext
,
34 ApplicationContextAware
{
35 private final Map
<String
, Object
> variables
= Collections
36 .synchronizedMap(new HashMap
<String
, Object
>());
38 private final String uuid
;
40 private ApplicationContext applicationContext
;
42 public MapExecutionContext() {
43 uuid
= UUID
.randomUUID().toString();
44 variables
.put(VAR_EXECUTION_CONTEXT_ID
, uuid
);
45 variables
.put(VAR_EXECUTION_CONTEXT_CREATION_DATE
, new Date());
48 public void setVariable(String key
, Object value
) {
49 // check if we do not refer to a bean
50 int lastInd
= key
.lastIndexOf('.');
51 if (applicationContext
!= null && lastInd
> 0) {
52 String beanName
= key
.substring(0, lastInd
);
53 String propertyName
= key
.substring(lastInd
+ 1);
54 if (applicationContext
.containsBean(beanName
)) {
55 BeanWrapper beanWrapper
= new BeanWrapperImpl(
56 applicationContext
.getBean(beanName
));
57 if (!beanWrapper
.isWritableProperty(propertyName
))
58 throw new SlcException("No writable property "
59 + propertyName
+ " in bean " + beanName
);
60 beanWrapper
.setPropertyValue(propertyName
, value
);
64 variables
.put(key
, value
);
67 public Object
getVariable(String key
) {
68 // check if we do not refer to a bean
69 int lastInd
= key
.lastIndexOf('.');
70 if (applicationContext
!= null && lastInd
> 0) {
71 String beanName
= key
.substring(0, lastInd
);
72 String propertyName
= key
.substring(lastInd
+ 1);
73 if (applicationContext
.containsBean(beanName
)) {
74 BeanWrapper beanWrapper
= new BeanWrapperImpl(
75 applicationContext
.getBean(beanName
));
76 if (!beanWrapper
.isReadableProperty(propertyName
))
77 throw new SlcException("No readable property "
78 + propertyName
+ " in bean " + beanName
);
79 Object obj
= beanWrapper
.getPropertyValue(propertyName
);
84 return variables
.get(key
);
87 public String
getUuid() {
92 public boolean equals(Object obj
) {
93 if (obj
instanceof ExecutionContext
)
94 return uuid
.equals(((ExecutionContext
) obj
).getUuid());
99 public String
toString() {
100 return getClass().getSimpleName() + "#" + uuid
;
103 public void setApplicationContext(ApplicationContext applicationContext
)
104 throws BeansException
{
105 this.applicationContext
= applicationContext
;