]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/MapExecutionContext.java
Document and improve execution model
[gpl/argeo-slc.git] / runtime / org.argeo.slc.core / src / main / java / org / argeo / slc / core / execution / MapExecutionContext.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 package org.argeo.slc.core.execution;
18
19 import java.util.Collections;
20 import java.util.Date;
21 import java.util.HashMap;
22 import java.util.Map;
23 import java.util.UUID;
24
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;
32
33 public class MapExecutionContext implements ExecutionContext,
34 ApplicationContextAware {
35 private final Map<String, Object> variables = Collections
36 .synchronizedMap(new HashMap<String, Object>());
37
38 private final String uuid;
39
40 private ApplicationContext applicationContext;
41
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());
46 }
47
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);
61 }
62 }
63
64 variables.put(key, value);
65 }
66
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);
80 return obj;
81 }
82 }
83
84 return variables.get(key);
85 }
86
87 public String getUuid() {
88 return uuid;
89 }
90
91 @Override
92 public boolean equals(Object obj) {
93 if (obj instanceof ExecutionContext)
94 return uuid.equals(((ExecutionContext) obj).getUuid());
95 return false;
96 }
97
98 @Override
99 public String toString() {
100 return getClass().getSimpleName() + "#" + uuid;
101 }
102
103 public void setApplicationContext(ApplicationContext applicationContext)
104 throws BeansException {
105 this.applicationContext = applicationContext;
106 }
107
108 }