1 package org
.argeo
.slc
.core
.execution
;
3 import java
.beans
.PropertyDescriptor
;
4 import java
.util
.HashSet
;
6 import java
.util
.Properties
;
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
;
19 public class ExecutionParameterPostProcessor
extends
20 InstantiationAwareBeanPostProcessorAdapter
{
22 private final static Log log
= LogFactory
23 .getLog(ExecutionParameterPostProcessor
.class);
25 private ExecutionContext executionContext
;
27 private ExecutionScope executionScope
;
29 public void setExecutionScope(ExecutionScope executionScope
) {
30 this.executionScope
= executionScope
;
33 public ExecutionContext
getExecutionContext() {
34 return executionContext
;
37 public void setExecutionContext(ExecutionContext executionContext
) {
38 this.executionContext
= executionContext
;
41 private String placeholderPrefix
= "@{";
42 private String placeholderSuffix
= "}";
43 private String nullValue
;
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);
54 //log.info("Execute parameter conversion for bean " + beanName);
57 Properties props
= new Properties();
58 CustomPpc ppc
= new CustomPpc(props
);
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
);
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
);
86 public void setPlaceholderPrefix(String placeholderPrefix
) {
87 this.placeholderPrefix
= placeholderPrefix
;
90 public void setPlaceholderSuffix(String placeholderSuffix
) {
91 this.placeholderSuffix
= placeholderSuffix
;
94 public void setNullValue(String nullValue
) {
95 this.nullValue
= nullValue
;
98 private class CustomPpc
extends PropertyPlaceholderConfigurer
{
99 private final Properties props
;
101 public CustomPpc(Properties props
) {
104 setPlaceholderPrefix(placeholderPrefix
);
105 setPlaceholderSuffix(placeholderSuffix
);
106 setSystemPropertiesMode(SYSTEM_PROPERTIES_MODE_NEVER
);
109 public String
process(String strVal
) {
110 String value
= parseStringValue(strVal
, this.props
,
111 new HashSet
<String
>());
112 return (value
.equals(nullValue
) ?
null : value
);
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();
124 return super.resolvePlaceholder(placeholder
, props
);