]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/MapExecutionContext.java
ExcecutionScopeDecorator: change default to proxy interfaces
[gpl/argeo-slc.git] / runtime / org.argeo.slc.core / src / main / java / org / argeo / slc / core / execution / MapExecutionContext.java
1 package org.argeo.slc.core.execution;
2
3 import java.util.Collections;
4 import java.util.Date;
5 import java.util.HashMap;
6 import java.util.Map;
7 import java.util.UUID;
8
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;
16
17 public class MapExecutionContext implements ExecutionContext,
18 ApplicationContextAware {
19 private final Map<String, Object> variables = Collections
20 .synchronizedMap(new HashMap<String, Object>());
21
22 private final String uuid;
23
24 private ApplicationContext applicationContext;
25
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());
30 }
31
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);
45 }
46 }
47
48 variables.put(key, value);
49 }
50
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);
64 return obj;
65 }
66 }
67
68 return variables.get(key);
69 }
70
71 public String getUuid() {
72 return uuid;
73 }
74
75 @Override
76 public boolean equals(Object obj) {
77 if (obj instanceof ExecutionContext)
78 return uuid.equals(((ExecutionContext) obj).getUuid());
79 return false;
80 }
81
82 @Override
83 public String toString() {
84 return getClass().getSimpleName() + "#" + uuid;
85 }
86
87 public void setApplicationContext(ApplicationContext applicationContext)
88 throws BeansException {
89 this.applicationContext = applicationContext;
90 }
91
92 }