]> git.argeo.org Git - gpl/argeo-slc.git/blob - sandbox/argeo.slc.executionflow/src/main/java/org/argeo/slc/executionflow/ExecutionParameterPostProcessor.java
Introduce aspects and execution parameters
[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 ExecutionFlow currentFlow = ExecutionContext.getCurrentFlow();
32 if (currentFlow == null)
33 return pvs;
34
35 Properties props = new Properties();
36 Map<String, Object> attributes = currentFlow.getAttributes();
37 Map<String, ExecutionSpecAttribute> specAttributes = currentFlow
38 .getExecutionSpec().getAttributes();
39
40 for (String key : specAttributes.keySet()) {
41 ExecutionSpecAttribute obj = specAttributes.get(key);
42 if (!(obj instanceof RefSpecAttribute)) {
43 if (!attributes.containsKey(key))
44 throw new SlcException("Specified attribute " + key
45 + " is not set in " + currentFlow);
46
47 props.setProperty(key, attributes.get(key).toString());
48 // if (log.isTraceEnabled())
49 // log.trace("Use attribute " + key);
50 }
51 }
52 CustomPpc ppc = new CustomPpc(props);
53
54 for (PropertyValue pv : pvs.getPropertyValues()) {
55 if (pv.getValue() instanceof TypedStringValue) {
56 TypedStringValue tsv = (TypedStringValue) pv.getValue();
57 String originalValue = tsv.getValue();
58 String convertedValue = ppc.process(originalValue);
59 tsv.setValue(convertedValue);
60 if (log.isTraceEnabled()) {
61 if (!originalValue.equals(convertedValue))
62 log.trace("Converted " + beanName + "[" + pv.getName()
63 + "]: " + originalValue + " to "
64 + convertedValue);
65 }
66 } else {
67 // if (log.isTraceEnabled())
68 // log.trace(beanName + "[" + pv.getName() + "]: "
69 // + pv.getValue().getClass());
70 }
71 }
72
73 return pvs;
74 }
75
76 public void setPlaceholderPrefix(String placeholderPrefix) {
77 this.placeholderPrefix = placeholderPrefix;
78 }
79
80 public void setPlaceholderSuffix(String placeholderSuffix) {
81 this.placeholderSuffix = placeholderSuffix;
82 }
83
84 public void setNullValue(String nullValue) {
85 this.nullValue = nullValue;
86 }
87
88 private class CustomPpc extends PropertyPlaceholderConfigurer {
89 private final Properties props;
90
91 public CustomPpc(Properties props) {
92 super();
93 this.props = props;
94 setPlaceholderPrefix(placeholderPrefix);
95 setPlaceholderSuffix(placeholderSuffix);
96 setSystemPropertiesMode(SYSTEM_PROPERTIES_MODE_NEVER);
97 }
98
99 public String process(String strVal) {
100 String value = parseStringValue(strVal, this.props, new HashSet<String>());
101 return (value.equals(nullValue) ? null : value);
102 }
103 }
104 }