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 private InstantiationManager instantiationManager
;
31 public InstantiationManager
getInstantiationManager() {
32 return instantiationManager
;
35 public void setInstantiationManager(
36 InstantiationManager instantiationManager
) {
37 this.instantiationManager
= instantiationManager
;
40 public void setExecutionScope(ExecutionScope executionScope
) {
41 this.executionScope
= executionScope
;
44 public ExecutionContext
getExecutionContext() {
45 return executionContext
;
48 public void setExecutionContext(ExecutionContext executionContext
) {
49 this.executionContext
= executionContext
;
52 private String placeholderPrefix
= "@{";
53 private String placeholderSuffix
= "}";
54 private String nullValue
;
57 public PropertyValues
postProcessPropertyValues(PropertyValues pvs
,
58 PropertyDescriptor
[] pds
, Object bean
, String beanName
)
59 throws BeansException
{
61 // boolean inFlowInitialization = instantiationManager
62 // .isInFlowInitialization();
64 // if (((executionScope == null) || (!executionScope.hasExecutionContext()))
65 // && !inFlowInitialization) {
66 // // log.info("Skip parameter conversion for bean " + beanName);
69 // // log.info("Execute parameter conversion for bean " + beanName);
72 Properties props
= new Properties();
73 CustomPpc ppc
= new CustomPpc(props
);
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
);
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
);
102 public void setPlaceholderPrefix(String placeholderPrefix
) {
103 this.placeholderPrefix
= placeholderPrefix
;
106 public void setPlaceholderSuffix(String placeholderSuffix
) {
107 this.placeholderSuffix
= placeholderSuffix
;
110 public void setNullValue(String nullValue
) {
111 this.nullValue
= nullValue
;
114 private class CustomPpc
extends PropertyPlaceholderConfigurer
{
115 private final Properties props
;
117 public CustomPpc(Properties props
) {
120 setPlaceholderPrefix(placeholderPrefix
);
121 setPlaceholderSuffix(placeholderSuffix
);
122 setSystemPropertiesMode(SYSTEM_PROPERTIES_MODE_NEVER
);
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
);
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();
142 return super.resolvePlaceholder(placeholder
, props
);