]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/core/execution/ExecutionParameterPostProcessor.java
c5d484bbfafbb190362c8cd900db5991c31d663e
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.simple / src / main / java / org / argeo / slc / core / execution / ExecutionParameterPostProcessor.java
1 package org.argeo.slc.core.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.argeo.slc.execution.ExecutionContext;
12 import org.springframework.beans.BeansException;
13 import org.springframework.beans.PropertyValue;
14 import org.springframework.beans.PropertyValues;
15 import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter;
16 import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
17 import org.springframework.beans.factory.config.TypedStringValue;
18
19 public class ExecutionParameterPostProcessor extends
20 InstantiationAwareBeanPostProcessorAdapter {
21
22 private final static Log log = LogFactory
23 .getLog(ExecutionParameterPostProcessor.class);
24
25 private ExecutionContext executionContext;
26
27 private ExecutionScope executionScope;
28
29 public void setExecutionScope(ExecutionScope executionScope) {
30 this.executionScope = executionScope;
31 }
32
33 public ExecutionContext getExecutionContext() {
34 return executionContext;
35 }
36
37 public void setExecutionContext(ExecutionContext executionContext) {
38 this.executionContext = executionContext;
39 }
40
41 private String placeholderPrefix = "@{";
42 private String placeholderSuffix = "}";
43 private String nullValue;
44
45 @Override
46 public PropertyValues postProcessPropertyValues(PropertyValues pvs,
47 PropertyDescriptor[] pds, Object bean, String beanName)
48 throws BeansException {
49 if ((executionScope == null) || (!executionScope.hasExecutionContext())){
50 //&& !DefaultExecutionSpec.isInFlowInitialization()) {
51 //log.info("Skip parameter conversion for bean " + beanName);
52 return pvs;
53 } else {
54 //log.info("Execute parameter conversion for bean " + beanName);
55 }
56
57 Properties props = new Properties();
58 CustomPpc ppc = new CustomPpc(props);
59
60 for (PropertyValue pv : pvs.getPropertyValues()) {
61 // log.info(" PropertyValue pv " + pv.getValue() + " - "
62 // + pv.getValue().getClass());
63 String originalValue = null;
64 String convertedValue = null;
65 if (pv.getValue() instanceof TypedStringValue) {
66 TypedStringValue tsv = (TypedStringValue) pv.getValue();
67 originalValue = tsv.getValue();
68 convertedValue = ppc.process(originalValue);
69 tsv.setValue(convertedValue);
70 } else if (pv.getValue() instanceof String) {
71 originalValue = pv.getValue().toString();
72 convertedValue = ppc.process(originalValue);
73 pv.setConvertedValue(convertedValue);
74 }
75 if (convertedValue != null && log.isTraceEnabled()) {
76 if (!originalValue.equals(convertedValue))
77 log.trace("Converted field '" + pv.getName() + "': '"
78 + originalValue + "' to '" + convertedValue
79 + "' in bean " + beanName);
80 }
81 }
82
83 return pvs;
84 }
85
86 public void setPlaceholderPrefix(String placeholderPrefix) {
87 this.placeholderPrefix = placeholderPrefix;
88 }
89
90 public void setPlaceholderSuffix(String placeholderSuffix) {
91 this.placeholderSuffix = placeholderSuffix;
92 }
93
94 public void setNullValue(String nullValue) {
95 this.nullValue = nullValue;
96 }
97
98 private class CustomPpc extends PropertyPlaceholderConfigurer {
99 private final Properties props;
100
101 public CustomPpc(Properties props) {
102 super();
103 this.props = props;
104 setPlaceholderPrefix(placeholderPrefix);
105 setPlaceholderSuffix(placeholderSuffix);
106 setSystemPropertiesMode(SYSTEM_PROPERTIES_MODE_NEVER);
107 }
108
109 public String process(String strVal) {
110 String value = parseStringValue(strVal, this.props,
111 new HashSet<String>());
112 return (value.equals(nullValue) ? null : value);
113 }
114
115 @Override
116 protected String resolvePlaceholder(String placeholder, Properties props) {
117 //log.info("Try convert placeholder " + placeholder);
118 if ((executionScope != null) && (executionScope.hasExecutionContext()))
119 return executionContext.getVariable(placeholder).toString();
120 else if (DefaultExecutionSpec.isInFlowInitialization())
121 return DefaultExecutionSpec.getInitializingFlowParameter(
122 placeholder).toString();
123 else
124 return super.resolvePlaceholder(placeholder, props);
125 }
126 }
127 }