]> 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
Improve execution specs
[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.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 //&& !DefaultExecutionSpec.isInFlowInitialization()) {
33 //log.info("Skip parameter conversion for bean " + beanName);
34 return pvs;
35 } else {
36 //log.info("Execute parameter conversion for bean " + beanName);
37 }
38
39 Properties props = new Properties();
40 CustomPpc ppc = new CustomPpc(props);
41
42 for (PropertyValue pv : pvs.getPropertyValues()) {
43 // log.info(" PropertyValue pv " + pv.getValue() + " - "
44 // + pv.getValue().getClass());
45 String originalValue = null;
46 String convertedValue = null;
47 if (pv.getValue() instanceof TypedStringValue) {
48 TypedStringValue tsv = (TypedStringValue) pv.getValue();
49 originalValue = tsv.getValue();
50 convertedValue = ppc.process(originalValue);
51 tsv.setValue(convertedValue);
52 } else if (pv.getValue() instanceof String) {
53 originalValue = pv.getValue().toString();
54 convertedValue = ppc.process(originalValue);
55 pv.setConvertedValue(convertedValue);
56 }
57 if (convertedValue != null && log.isTraceEnabled()) {
58 if (!originalValue.equals(convertedValue))
59 log.trace("Converted field '" + pv.getName() + "': '"
60 + originalValue + "' to '" + convertedValue
61 + "' in bean " + beanName);
62 }
63 }
64
65 return pvs;
66 }
67
68 public void setPlaceholderPrefix(String placeholderPrefix) {
69 this.placeholderPrefix = placeholderPrefix;
70 }
71
72 public void setPlaceholderSuffix(String placeholderSuffix) {
73 this.placeholderSuffix = placeholderSuffix;
74 }
75
76 public void setNullValue(String nullValue) {
77 this.nullValue = nullValue;
78 }
79
80 private class CustomPpc extends PropertyPlaceholderConfigurer {
81 private final Properties props;
82
83 public CustomPpc(Properties props) {
84 super();
85 this.props = props;
86 setPlaceholderPrefix(placeholderPrefix);
87 setPlaceholderSuffix(placeholderSuffix);
88 setSystemPropertiesMode(SYSTEM_PROPERTIES_MODE_NEVER);
89 }
90
91 public String process(String strVal) {
92 String value = parseStringValue(strVal, this.props,
93 new HashSet<String>());
94 return (value.equals(nullValue) ? null : value);
95 }
96
97 @Override
98 protected String resolvePlaceholder(String placeholder, Properties props) {
99 //log.info("Try convert placeholder " + placeholder);
100 if (ExecutionContext.isExecuting())
101 return ExecutionContext.getVariable(placeholder).toString();
102 else if (DefaultExecutionSpec.isInFlowInitialization())
103 return DefaultExecutionSpec.getInitializingFlowParameter(
104 placeholder).toString();
105 else
106 return super.resolvePlaceholder(placeholder, props);
107 }
108
109 }
110 }