]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.execution/src/main/java/org/argeo/slc/execution/ExecutionParameterPostProcessor.java
ActiveMQ and Castor integration
[gpl/argeo-slc.git] / runtime / org.argeo.slc.execution / src / main / java / org / argeo / slc / execution / ExecutionParameterPostProcessor.java
1 package org.argeo.slc.execution;
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
54 Properties props = new Properties();
55 CustomPpc ppc = new CustomPpc(props);
56
57 for (PropertyValue pv : pvs.getPropertyValues()) {
58 if (pv.getValue() instanceof TypedStringValue) {
59 TypedStringValue tsv = (TypedStringValue) pv.getValue();
60 String originalValue = tsv.getValue();
61 String convertedValue = ppc.process(originalValue);
62 tsv.setValue(convertedValue);
63 if (log.isTraceEnabled()) {
64 if (!originalValue.equals(convertedValue))
65 log.trace("Converted field '" + pv.getName() + "': '"
66 + originalValue + "' to '" + convertedValue
67 + "' in bean " + beanName);
68 }
69 } else {
70 // if (log.isTraceEnabled())
71 // log.trace(beanName + "[" + pv.getName() + "]: "
72 // + pv.getValue().getClass());
73 }
74 }
75
76 return pvs;
77 }
78
79 public void setPlaceholderPrefix(String placeholderPrefix) {
80 this.placeholderPrefix = placeholderPrefix;
81 }
82
83 public void setPlaceholderSuffix(String placeholderSuffix) {
84 this.placeholderSuffix = placeholderSuffix;
85 }
86
87 public void setNullValue(String nullValue) {
88 this.nullValue = nullValue;
89 }
90
91 private class CustomPpc extends PropertyPlaceholderConfigurer {
92 private final Properties props;
93
94 public CustomPpc(Properties props) {
95 super();
96 this.props = props;
97 setPlaceholderPrefix(placeholderPrefix);
98 setPlaceholderSuffix(placeholderSuffix);
99 setSystemPropertiesMode(SYSTEM_PROPERTIES_MODE_NEVER);
100 }
101
102 public String process(String strVal) {
103 String value = parseStringValue(strVal, this.props,
104 new HashSet<String>());
105 return (value.equals(nullValue) ? null : value);
106 }
107
108 @Override
109 protected String resolvePlaceholder(String placeholder, Properties props) {
110 if (ExecutionContext.isExecuting())
111 return ExecutionContext.getVariable(placeholder).toString();
112 else if (SimpleExecutionSpec.isInFlowInitialization())
113 return SimpleExecutionSpec.getInitializingFlowParameter(
114 placeholder).toString();
115 else
116 return super.resolvePlaceholder(placeholder, props);
117 }
118
119 }
120 }