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(InstantiationManager instantiationManager
) {
36 this.instantiationManager
= instantiationManager
;
39 public void setExecutionScope(ExecutionScope executionScope
) {
40 this.executionScope
= executionScope
;
43 public ExecutionContext
getExecutionContext() {
44 return executionContext
;
47 public void setExecutionContext(ExecutionContext executionContext
) {
48 this.executionContext
= executionContext
;
51 private String placeholderPrefix
= "@{";
52 private String placeholderSuffix
= "}";
53 private String nullValue
;
56 public PropertyValues
postProcessPropertyValues(PropertyValues pvs
,
57 PropertyDescriptor
[] pds
, Object bean
, String beanName
)
58 throws BeansException
{
61 // boolean inFlowInitialization = DefaultExecutionSpec.isInFlowInitialization();
63 // if (((executionScope == null) || (!executionScope.hasExecutionContext()))
64 // && !inFlowInitialization) {
65 // //log.info("Skip parameter conversion for bean " + beanName);
68 // //log.info("Execute parameter conversion for bean " + beanName);
71 Properties props
= new Properties();
72 CustomPpc ppc
= new CustomPpc(props
);
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
);
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
);
100 public void setPlaceholderPrefix(String placeholderPrefix
) {
101 this.placeholderPrefix
= placeholderPrefix
;
104 public void setPlaceholderSuffix(String placeholderSuffix
) {
105 this.placeholderSuffix
= placeholderSuffix
;
108 public void setNullValue(String nullValue
) {
109 this.nullValue
= nullValue
;
112 private class CustomPpc
extends PropertyPlaceholderConfigurer
{
113 private final Properties props
;
115 public CustomPpc(Properties props
) {
118 setPlaceholderPrefix(placeholderPrefix
);
119 setPlaceholderSuffix(placeholderSuffix
);
120 setSystemPropertiesMode(SYSTEM_PROPERTIES_MODE_NEVER
);
123 public String
process(String strVal
) {
124 String value
= parseStringValue(strVal
, this.props
,
125 new HashSet
<String
>());
126 return (value
.equals(nullValue
) ?
null : value
);
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();
136 // TODO: check scope of the bean
137 // throw exception if trying to resolve placeholder on bean of scope singleton
139 if (instantiationManager
.isInFlowInitialization()) {
140 String resolved
= instantiationManager
.getInitializingFlowParameter(
141 placeholder
).toString();
142 // log.info("Initialization placeholder resolution " + placeholder
143 // + ">>" + resolved);
147 // return super.resolvePlaceholder(placeholder, props);
148 throw new SlcException("Placeholder '" + placeholder
149 + "' can not be resolved outside of Flow Initialization or Flow Execution.");