]> git.argeo.org Git - gpl/argeo-slc.git/blob - sandbox/argeo.slc.executionflow/src/main/java/org/argeo/slc/executionflow/ExecutionParameterPostProcessor.java
Introduce executor and event notifications
[gpl/argeo-slc.git] / sandbox / argeo.slc.executionflow / src / main / java / org / argeo / slc / executionflow / ExecutionParameterPostProcessor.java
1 package org.argeo.slc.executionflow;
2
3 import java.beans.PropertyDescriptor;
4 import java.util.HashSet;
5 import java.util.Map;
6 import java.util.Properties;
7
8 import org.apache.commons.logging.Log;
9 import org.apache.commons.logging.LogFactory;
10 import org.argeo.slc.SlcException;
11 import org.springframework.beans.BeansException;
12 import org.springframework.beans.PropertyValue;
13 import org.springframework.beans.PropertyValues;
14 import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter;
15 import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
16 import org.springframework.beans.factory.config.TypedStringValue;
17
18 public class ExecutionParameterPostProcessor extends
19 InstantiationAwareBeanPostProcessorAdapter {
20 private final static Log log = LogFactory
21 .getLog(ExecutionParameterPostProcessor.class);
22
23 private String placeholderPrefix = "@{";
24 private String placeholderSuffix = "}";
25 private String nullValue;
26
27 @Override
28 public PropertyValues postProcessPropertyValues(PropertyValues pvs,
29 PropertyDescriptor[] pds, Object bean, String beanName)
30 throws BeansException {
31 if (!ExecutionContext.isExecuting())
32 return pvs;
33
34 ExecutionFlow currentFlow = ExecutionContext.getCurrentFlow();
35
36 Properties props = new Properties();
37 Map<String, Object> attributes = currentFlow.getAttributes();
38 Map<String, ExecutionSpecAttribute> specAttributes = currentFlow
39 .getExecutionSpec().getAttributes();
40
41 for (String key : specAttributes.keySet()) {
42 ExecutionSpecAttribute obj = specAttributes.get(key);
43 if (!(obj instanceof RefSpecAttribute)) {
44 if (!attributes.containsKey(key))
45 throw new SlcException("Specified attribute " + key
46 + " is not set in " + currentFlow);
47
48 props.setProperty(key, attributes.get(key).toString());
49 // if (log.isTraceEnabled())
50 // log.trace("Use attribute " + key);
51 }
52 }
53 CustomPpc ppc = new CustomPpc(props);
54
55 for (PropertyValue pv : pvs.getPropertyValues()) {
56 if (pv.getValue() instanceof TypedStringValue) {
57 TypedStringValue tsv = (TypedStringValue) pv.getValue();
58 String originalValue = tsv.getValue();
59 String convertedValue = ppc.process(originalValue);
60 tsv.setValue(convertedValue);
61 if (log.isTraceEnabled()) {
62 if (!originalValue.equals(convertedValue))
63 log.trace("Converted field '" + pv.getName() + "': '"
64 + originalValue + "' to '" + convertedValue
65 + "' in bean " + beanName);
66 }
67 } else {
68 // if (log.isTraceEnabled())
69 // log.trace(beanName + "[" + pv.getName() + "]: "
70 // + pv.getValue().getClass());
71 }
72 }
73
74 return pvs;
75 }
76
77 public void setPlaceholderPrefix(String placeholderPrefix) {
78 this.placeholderPrefix = placeholderPrefix;
79 }
80
81 public void setPlaceholderSuffix(String placeholderSuffix) {
82 this.placeholderSuffix = placeholderSuffix;
83 }
84
85 public void setNullValue(String nullValue) {
86 this.nullValue = nullValue;
87 }
88
89 private class CustomPpc extends PropertyPlaceholderConfigurer {
90 private final Properties props;
91
92 public CustomPpc(Properties props) {
93 super();
94 this.props = props;
95 setPlaceholderPrefix(placeholderPrefix);
96 setPlaceholderSuffix(placeholderSuffix);
97 setSystemPropertiesMode(SYSTEM_PROPERTIES_MODE_NEVER);
98 }
99
100 public String process(String strVal) {
101 String value = parseStringValue(strVal, this.props,
102 new HashSet<String>());
103 return (value.equals(nullValue) ? null : value);
104 }
105 }
106 }