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
.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
;
18 public class ExecutionParameterPostProcessor
extends
19 InstantiationAwareBeanPostProcessorAdapter
{
20 private final static Log log
= LogFactory
21 .getLog(ExecutionParameterPostProcessor
.class);
23 private String placeholderPrefix
= "@{";
24 private String placeholderSuffix
= "}";
25 private String nullValue
;
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);
36 //log.info("Execute parameter conversion for bean " + beanName);
39 Properties props
= new Properties();
40 CustomPpc ppc
= new CustomPpc(props
);
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
);
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
);
68 public void setPlaceholderPrefix(String placeholderPrefix
) {
69 this.placeholderPrefix
= placeholderPrefix
;
72 public void setPlaceholderSuffix(String placeholderSuffix
) {
73 this.placeholderSuffix
= placeholderSuffix
;
76 public void setNullValue(String nullValue
) {
77 this.nullValue
= nullValue
;
80 private class CustomPpc
extends PropertyPlaceholderConfigurer
{
81 private final Properties props
;
83 public CustomPpc(Properties props
) {
86 setPlaceholderPrefix(placeholderPrefix
);
87 setPlaceholderSuffix(placeholderSuffix
);
88 setSystemPropertiesMode(SYSTEM_PROPERTIES_MODE_NEVER
);
91 public String
process(String strVal
) {
92 String value
= parseStringValue(strVal
, this.props
,
93 new HashSet
<String
>());
94 return (value
.equals(nullValue
) ?
null : value
);
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();
106 return super.resolvePlaceholder(placeholder
, props
);