]> 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
5a00ed5aca89e01666546c3e907b04a5fa605910
[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 private InstantiationManager instantiationManager;
30
31 public InstantiationManager getInstantiationManager() {
32 return instantiationManager;
33 }
34
35 public void setInstantiationManager(
36 InstantiationManager instantiationManager) {
37 this.instantiationManager = instantiationManager;
38 }
39
40 public void setExecutionScope(ExecutionScope executionScope) {
41 this.executionScope = executionScope;
42 }
43
44 public ExecutionContext getExecutionContext() {
45 return executionContext;
46 }
47
48 public void setExecutionContext(ExecutionContext executionContext) {
49 this.executionContext = executionContext;
50 }
51
52 private String placeholderPrefix = "@{";
53 private String placeholderSuffix = "}";
54 private String nullValue;
55
56 @Override
57 public PropertyValues postProcessPropertyValues(PropertyValues pvs,
58 PropertyDescriptor[] pds, Object bean, String beanName)
59 throws BeansException {
60
61 // boolean inFlowInitialization = instantiationManager
62 // .isInFlowInitialization();
63 //
64 // if (((executionScope == null) || (!executionScope.hasExecutionContext()))
65 // && !inFlowInitialization) {
66 // // log.info("Skip parameter conversion for bean " + beanName);
67 // return pvs;
68 // } else {
69 // // log.info("Execute parameter conversion for bean " + beanName);
70 // }
71
72 Properties props = new Properties();
73 CustomPpc ppc = new CustomPpc(props);
74
75 for (PropertyValue pv : pvs.getPropertyValues()) {
76 // log.info(" PropertyValue pv " + pv.getValue() + " - "
77 // + pv.getValue().getClass());
78 String originalValue = null;
79 String convertedValue = null;
80 if (pv.getValue() instanceof TypedStringValue) {
81 TypedStringValue tsv = (TypedStringValue) pv.getValue();
82 originalValue = tsv.getValue();
83 convertedValue = ppc.process(originalValue);
84 tsv.setValue(convertedValue);
85 } else if (pv.getValue() instanceof String) {
86 originalValue = pv.getValue().toString();
87 convertedValue = ppc.process(originalValue);
88 if (!convertedValue.equals(originalValue))
89 pv.setConvertedValue(convertedValue);
90 }
91 if (convertedValue != null && log.isTraceEnabled()) {
92 if (!originalValue.equals(convertedValue))
93 log.trace("Converted field '" + pv.getName() + "': '"
94 + originalValue + "' to '" + convertedValue
95 + "' in bean " + beanName);
96 }
97 }
98
99 return pvs;
100 }
101
102 public void setPlaceholderPrefix(String placeholderPrefix) {
103 this.placeholderPrefix = placeholderPrefix;
104 }
105
106 public void setPlaceholderSuffix(String placeholderSuffix) {
107 this.placeholderSuffix = placeholderSuffix;
108 }
109
110 public void setNullValue(String nullValue) {
111 this.nullValue = nullValue;
112 }
113
114 private class CustomPpc extends PropertyPlaceholderConfigurer {
115 private final Properties props;
116
117 public CustomPpc(Properties props) {
118 super();
119 this.props = props;
120 setPlaceholderPrefix(placeholderPrefix);
121 setPlaceholderSuffix(placeholderSuffix);
122 setSystemPropertiesMode(SYSTEM_PROPERTIES_MODE_NEVER);
123 }
124
125 /** Public access to the internals of PropertyPlaceholderConfigurer*/
126 public String process(String strVal) {
127 String value = parseStringValue(strVal, this.props,
128 new HashSet<String>());
129 return (value.equals(nullValue) ? null : value);
130 }
131
132 @Override
133 protected String resolvePlaceholder(String placeholder, Properties props) {
134 // log.info("Try convert placeholder " + placeholder);
135 if ((executionScope != null)
136 && (executionScope.hasExecutionContext()))
137 return executionContext.getVariable(placeholder).toString();
138 else if (instantiationManager.isInFlowInitialization())
139 return instantiationManager.getInitializingFlowParameter(
140 placeholder).toString();
141 else
142 return super.resolvePlaceholder(placeholder, props);
143 }
144 }
145 }