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