]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/MapExecutionContext.java
Move default agent to execution package
[gpl/argeo-slc.git] / runtime / org.argeo.slc.core / src / main / java / org / argeo / slc / core / execution / MapExecutionContext.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
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 package org.argeo.slc.core.execution;
17
18 import java.util.Collections;
19 import java.util.Date;
20 import java.util.HashMap;
21 import java.util.Map;
22 import java.util.UUID;
23
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;
31
32 public class MapExecutionContext implements ExecutionContext,
33 ApplicationContextAware {
34 private final Map<String, Object> variables = Collections
35 .synchronizedMap(new HashMap<String, Object>());
36
37 private final String uuid;
38
39 private ApplicationContext applicationContext;
40
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());
45 }
46
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);
60 }
61 }
62
63 variables.put(key, value);
64 }
65
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);
79 return obj;
80 }
81 }
82
83 Object value = variables.get(key);
84 // try system property in last resort
85 if (value == null)
86 value = System.getProperty(key);
87 return value;
88 }
89
90 public String getUuid() {
91 return uuid;
92 }
93
94 @Override
95 public boolean equals(Object obj) {
96 if (obj instanceof ExecutionContext)
97 return uuid.equals(((ExecutionContext) obj).getUuid());
98 return false;
99 }
100
101 @Override
102 public String toString() {
103 return getClass().getSimpleName() + "#" + uuid;
104 }
105
106 public void setApplicationContext(ApplicationContext applicationContext)
107 throws BeansException {
108 this.applicationContext = applicationContext;
109 }
110
111 }