1 package org
.argeo
.slc
.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())
34 // ExecutionFlow currentFlow = ExecutionContext.getCurrentFlow();
36 // Properties props = new Properties();
37 // Map<String, Object> attributes = currentFlow.getAttributes();
38 // Map<String, ExecutionSpecAttribute> specAttributes = currentFlow
39 // .getExecutionSpec().getAttributes();
41 // for (String key : specAttributes.keySet()) {
42 // ExecutionSpecAttribute obj = specAttributes.get(key);
43 // if (!(obj instanceof RefSpecAttribute)) {
44 // if (!attributes.containsKey(key))
45 // throw new SlcException("Specified attribute " + key
46 // + " is not set in " + currentFlow);
48 // props.setProperty(key, attributes.get(key).toString());
49 // // if (log.isTraceEnabled())
50 // // log.trace("Use attribute " + key);
54 Properties props
= new Properties();
55 CustomPpc ppc
= new CustomPpc(props
);
57 for (PropertyValue pv
: pvs
.getPropertyValues()) {
58 if (pv
.getValue() instanceof TypedStringValue
) {
59 TypedStringValue tsv
= (TypedStringValue
) pv
.getValue();
60 String originalValue
= tsv
.getValue();
61 String convertedValue
= ppc
.process(originalValue
);
62 tsv
.setValue(convertedValue
);
63 if (log
.isTraceEnabled()) {
64 if (!originalValue
.equals(convertedValue
))
65 log
.trace("Converted field '" + pv
.getName() + "': '"
66 + originalValue
+ "' to '" + convertedValue
67 + "' in bean " + beanName
);
70 // if (log.isTraceEnabled())
71 // log.trace(beanName + "[" + pv.getName() + "]: "
72 // + pv.getValue().getClass());
79 public void setPlaceholderPrefix(String placeholderPrefix
) {
80 this.placeholderPrefix
= placeholderPrefix
;
83 public void setPlaceholderSuffix(String placeholderSuffix
) {
84 this.placeholderSuffix
= placeholderSuffix
;
87 public void setNullValue(String nullValue
) {
88 this.nullValue
= nullValue
;
91 private class CustomPpc
extends PropertyPlaceholderConfigurer
{
92 private final Properties props
;
94 public CustomPpc(Properties props
) {
97 setPlaceholderPrefix(placeholderPrefix
);
98 setPlaceholderSuffix(placeholderSuffix
);
99 setSystemPropertiesMode(SYSTEM_PROPERTIES_MODE_NEVER
);
102 public String
process(String strVal
) {
103 String value
= parseStringValue(strVal
, this.props
,
104 new HashSet
<String
>());
105 return (value
.equals(nullValue
) ?
null : value
);
109 protected String
resolvePlaceholder(String placeholder
, Properties props
) {
110 if (ExecutionContext
.isExecuting())
111 return ExecutionContext
.getVariable(placeholder
).toString();
112 else if (SimpleExecutionSpec
.isInFlowInitialization())
113 return SimpleExecutionSpec
.getInitializingFlowParameter(
114 placeholder
).toString();
116 return super.resolvePlaceholder(placeholder
, props
);