]> git.argeo.org Git - gpl/argeo-slc.git/blob - legacy/org.argeo.slc.spring/src/org/argeo/slc/core/execution/MapExecutionContext.java
Merge remote-tracking branch 'origin/master' into testing
[gpl/argeo-slc.git] / legacy / org.argeo.slc.spring / src / 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.argeo.slc.execution.ExecutionFlow;
12 import org.argeo.slc.execution.ExecutionStack;
13 import org.springframework.beans.BeanWrapper;
14 import org.springframework.beans.BeanWrapperImpl;
15 import org.springframework.beans.BeansException;
16 import org.springframework.context.ApplicationContext;
17 import org.springframework.context.ApplicationContextAware;
18
19 public class MapExecutionContext implements ExecutionContext,
20 ApplicationContextAware {
21 private final Map<String, Object> variables = Collections
22 .synchronizedMap(new HashMap<String, Object>());
23
24 private final String uuid;
25
26 private ApplicationContext applicationContext;
27 private ExecutionStack executionStack;
28
29 public MapExecutionContext() {
30 uuid = UUID.randomUUID().toString();
31 variables.put(VAR_EXECUTION_CONTEXT_ID, uuid);
32 variables.put(VAR_EXECUTION_CONTEXT_CREATION_DATE, new Date());
33 }
34
35 public void setVariable(String key, Object value) {
36 // check if we do not refer to a bean
37 int lastInd = key.lastIndexOf('.');
38 if (applicationContext != null && lastInd > 0) {
39 String beanName = key.substring(0, lastInd);
40 String propertyName = key.substring(lastInd + 1);
41 if (applicationContext.containsBean(beanName)) {
42 BeanWrapper beanWrapper = new BeanWrapperImpl(
43 applicationContext.getBean(beanName));
44 if (!beanWrapper.isWritableProperty(propertyName))
45 throw new SlcException("No writable property "
46 + propertyName + " in bean " + beanName);
47 beanWrapper.setPropertyValue(propertyName, value);
48 }
49 }
50
51 variables.put(key, value);
52 }
53
54 public Object getVariable(String key) {
55 // check if we do not refer to a bean
56 int lastInd = key.lastIndexOf('.');
57 if (applicationContext != null && lastInd > 0) {
58 String beanName = key.substring(0, lastInd);
59 String propertyName = key.substring(lastInd + 1);
60 if (applicationContext.containsBean(beanName)) {
61 BeanWrapper beanWrapper = new BeanWrapperImpl(
62 applicationContext.getBean(beanName));
63 if (!beanWrapper.isReadableProperty(propertyName))
64 throw new SlcException("No readable property "
65 + propertyName + " in bean " + beanName);
66 Object obj = beanWrapper.getPropertyValue(propertyName);
67 return obj;
68 }
69 }
70
71 Object value = variables.get(key);
72 // try system property in last resort
73 if (value == null)
74 value = System.getProperty(key);
75
76 // if the variable was not found, look in the stack starting at the
77 // upper flows
78 if (value == null) {
79 value = executionStack.findLocalVariable(key);
80 }
81 return value;
82 }
83
84 public String getUuid() {
85 return uuid;
86 }
87
88 @Override
89 public void beforeFlow(ExecutionFlow executionFlow) {
90 // getUuid();
91 executionStack.enterFlow(executionFlow);
92 setVariable(ExecutionContext.VAR_FLOW_ID,
93 executionStack.getCurrentStackLevelUuid());
94 setVariable(ExecutionContext.VAR_FLOW_NAME, executionFlow.getName());
95 }
96
97 @Override
98 public void afterFlow(ExecutionFlow executionFlow) {
99 executionStack.leaveFlow(executionFlow);
100 }
101
102 @Override
103 public boolean equals(Object obj) {
104 if (obj instanceof ExecutionContext)
105 return uuid.equals(((ExecutionContext) obj).getUuid());
106 return false;
107 }
108
109 @Override
110 public String toString() {
111 return getClass().getSimpleName() + "#" + uuid;
112 }
113
114 public void setApplicationContext(ApplicationContext applicationContext)
115 throws BeansException {
116 this.applicationContext = applicationContext;
117 }
118
119 public void setExecutionStack(ExecutionStack executionStack) {
120 this.executionStack = executionStack;
121 }
122
123 }