1 package org
.argeo
.slc
.core
.execution
;
3 import java
.beans
.PropertyDescriptor
;
4 import java
.util
.HashSet
;
5 import java
.util
.Iterator
;
6 import java
.util
.LinkedHashMap
;
8 import java
.util
.Properties
;
10 import org
.apache
.commons
.logging
.Log
;
11 import org
.apache
.commons
.logging
.LogFactory
;
12 import org
.argeo
.slc
.SlcException
;
13 import org
.argeo
.slc
.execution
.ExecutionContext
;
14 import org
.springframework
.beans
.BeansException
;
15 import org
.springframework
.beans
.MutablePropertyValues
;
16 import org
.springframework
.beans
.PropertyValue
;
17 import org
.springframework
.beans
.PropertyValues
;
18 import org
.springframework
.beans
.factory
.config
.InstantiationAwareBeanPostProcessorAdapter
;
19 import org
.springframework
.beans
.factory
.config
.PropertyPlaceholderConfigurer
;
20 import org
.springframework
.beans
.factory
.config
.TypedStringValue
;
21 import org
.springframework
.beans
.factory
.support
.ManagedMap
;
23 public class ExecutionParameterPostProcessor
extends
24 InstantiationAwareBeanPostProcessorAdapter
{
26 private final static Log log
= LogFactory
27 .getLog(ExecutionParameterPostProcessor
.class);
29 // private CustomPpc ppc = new CustomPpc(new Properties());
31 private ExecutionContext executionContext
;
33 private ExecutionScope executionScope
;
35 private InstantiationManager instantiationManager
;
37 public InstantiationManager
getInstantiationManager() {
38 return instantiationManager
;
41 public void setInstantiationManager(
42 InstantiationManager instantiationManager
) {
43 this.instantiationManager
= instantiationManager
;
46 public void setExecutionScope(ExecutionScope executionScope
) {
47 this.executionScope
= executionScope
;
50 public ExecutionContext
getExecutionContext() {
51 return executionContext
;
54 public void setExecutionContext(ExecutionContext executionContext
) {
55 this.executionContext
= executionContext
;
58 private String placeholderPrefix
= "@{";
59 private String placeholderSuffix
= "}";
60 private String nullValue
;
62 protected Object
resolveValue(Object value
, CustomPpc ppc
) {
63 if (value
instanceof TypedStringValue
) {
64 TypedStringValue tsv
= (TypedStringValue
) value
;
65 return ppc
.process(tsv
.getValue());
67 else if (value
instanceof String
) {
68 return ppc
.process(value
.toString());
76 public PropertyValues
postProcessPropertyValues(PropertyValues pvs
,
77 PropertyDescriptor
[] pds
, Object bean
, String beanName
)
78 throws BeansException
{
80 //TODO: resolve at execution only if scope is execution
82 // boolean inFlowInitialization = instantiationManager
83 // .isInFlowInitialization();
85 // if (((executionScope == null) || (!executionScope.hasExecutionContext()))
86 // && !inFlowInitialization) {
87 // // log.info("Skip parameter conversion for bean " + beanName);
90 // // log.info("Execute parameter conversion for bean " + beanName);
93 // copy the property values
94 //MutablePropertyValues newPv = new MutablePropertyValues(pvs);
96 Properties props
= new Properties();
97 CustomPpc ppc
= new CustomPpc(props
);
99 for (PropertyValue pv
: pvs
.getPropertyValues()) {
100 // log.info(" PropertyValue pv " + pv.getValue() + " - "
101 // + pv.getValue().getClass());
102 String originalValue
= null;
103 String convertedValue
= null;
105 if (pv
.getValue() instanceof TypedStringValue
) {
106 TypedStringValue tsv
= (TypedStringValue
) pv
.getValue();
107 originalValue
= tsv
.getValue();
108 convertedValue
= ppc
.process(originalValue
);
109 if (!convertedValue
.equals(originalValue
))
110 tsv
.setValue(convertedValue
);
112 else if (pv
.getValue() instanceof String
) {
113 originalValue
= pv
.getValue().toString();
114 convertedValue
= ppc
.process(originalValue
);
115 // Setting the convertedValue can be problematic since
116 // calling setConvertedValue also sets a flag setConvertedValue
117 if (!convertedValue
.equals(originalValue
))
118 pv
.setConvertedValue(convertedValue
);
121 else if (pv
.getValue() instanceof ManagedMap
) {
123 Object obj
= pv
.getValue();
124 String name
= pv
.getName();
126 // log.info("##" + name + ":" + obj.getClass());
127 ManagedMap mapVal
= (ManagedMap
) pv
.getValue();
129 Map newContent
= new LinkedHashMap();
130 boolean entriesModified
= false;
131 for (Iterator it
= mapVal
.entrySet().iterator(); it
.hasNext();) {
132 Map
.Entry entry
= (Map
.Entry
) it
.next();
133 Object key
= entry
.getKey();
134 int keyHash
= (key
!= null ? key
.hashCode() : 0);
135 Object newKey
= resolveValue(key
,ppc
);
136 int newKeyHash
= (newKey
!= null ? newKey
.hashCode() : 0);
137 Object val
= entry
.getValue();
138 Object newVal
= resolveValue(val
,ppc
);
139 newContent
.put(newKey
, newVal
);
140 entriesModified
= entriesModified
|| (newVal
!= val
|| newKey
!= key
|| newKeyHash
!= keyHash
);
142 if (entriesModified
) {
144 mapVal
.putAll(newContent
);
148 if (convertedValue
!= null && log
.isTraceEnabled()) {
149 if (!originalValue
.equals(convertedValue
))
150 log
.trace("Converted field '" + pv
.getName() + "': '"
151 + originalValue
+ "' to '" + convertedValue
152 + "' in bean " + beanName
);
159 public void setPlaceholderPrefix(String placeholderPrefix
) {
160 this.placeholderPrefix
= placeholderPrefix
;
163 public void setPlaceholderSuffix(String placeholderSuffix
) {
164 this.placeholderSuffix
= placeholderSuffix
;
167 public void setNullValue(String nullValue
) {
168 this.nullValue
= nullValue
;
171 private class CustomPpc
extends PropertyPlaceholderConfigurer
{
172 private final Properties props
;
174 public CustomPpc(Properties props
) {
177 setPlaceholderPrefix(placeholderPrefix
);
178 setPlaceholderSuffix(placeholderSuffix
);
179 setSystemPropertiesMode(SYSTEM_PROPERTIES_MODE_NEVER
);
182 /** Public access to the internals of PropertyPlaceholderConfigurer*/
183 public String
process(String strVal
) {
184 String value
= parseStringValue(strVal
, this.props
,
185 new HashSet
<String
>());
186 return (value
.equals(nullValue
) ?
null : value
);
190 protected String
resolvePlaceholder(String placeholder
, Properties props
) {
191 // log.info("Try convert placeholder " + placeholder);
192 if ((executionScope
!= null)
193 && (executionScope
.hasExecutionContext()))
194 return executionContext
.getVariable(placeholder
).toString();
195 else if (instantiationManager
.isInFlowInitialization())
196 return instantiationManager
.getInitializingFlowParameter(
197 placeholder
).toString();
199 return super.resolvePlaceholder(placeholder
, props
);