-package org.argeo.slc.core.execution;
-
-import java.beans.PropertyDescriptor;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.LinkedHashMap;
-import java.util.Map;
-import java.util.Properties;
-
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.argeo.slc.SlcException;
-import org.argeo.slc.execution.ExecutionContext;
-import org.springframework.beans.BeansException;
-import org.springframework.beans.MutablePropertyValues;
-import org.springframework.beans.PropertyValue;
-import org.springframework.beans.PropertyValues;
-import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter;
-import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
-import org.springframework.beans.factory.config.TypedStringValue;
-import org.springframework.beans.factory.support.ManagedMap;
-
-public class ExecutionParameterPostProcessor extends
- InstantiationAwareBeanPostProcessorAdapter {
-
- private final static Log log = LogFactory
- .getLog(ExecutionParameterPostProcessor.class);
-
-// private CustomPpc ppc = new CustomPpc(new Properties());
-
- private ExecutionContext executionContext;
-
- private ExecutionScope executionScope;
-
- private InstantiationManager instantiationManager;
-
- public InstantiationManager getInstantiationManager() {
- return instantiationManager;
- }
-
- public void setInstantiationManager(
- InstantiationManager instantiationManager) {
- this.instantiationManager = instantiationManager;
- }
-
- public void setExecutionScope(ExecutionScope executionScope) {
- this.executionScope = executionScope;
- }
-
- public ExecutionContext getExecutionContext() {
- return executionContext;
- }
-
- public void setExecutionContext(ExecutionContext executionContext) {
- this.executionContext = executionContext;
- }
-
- private String placeholderPrefix = "@{";
- private String placeholderSuffix = "}";
- private String nullValue;
-
- protected Object resolveValue(Object value, CustomPpc ppc) {
- if (value instanceof TypedStringValue) {
- TypedStringValue tsv = (TypedStringValue) value;
- return ppc.process(tsv.getValue());
- }
- else if (value instanceof String) {
- return ppc.process(value.toString());
- }
- else {
- return value;
- }
- }
-
- @Override
- public PropertyValues postProcessPropertyValues(PropertyValues pvs,
- PropertyDescriptor[] pds, Object bean, String beanName)
- throws BeansException {
-
- //TODO: resolve at execution only if scope is execution
-
-// boolean inFlowInitialization = instantiationManager
-// .isInFlowInitialization();
-//
-// if (((executionScope == null) || (!executionScope.hasExecutionContext()))
-// && !inFlowInitialization) {
-// // log.info("Skip parameter conversion for bean " + beanName);
-// return pvs;
-// } else {
-// // log.info("Execute parameter conversion for bean " + beanName);
-// }
-
- // copy the property values
- //MutablePropertyValues newPv = new MutablePropertyValues(pvs);
-
- Properties props = new Properties();
- CustomPpc ppc = new CustomPpc(props);
-
- for (PropertyValue pv : pvs.getPropertyValues()) {
- // log.info(" PropertyValue pv " + pv.getValue() + " - "
- // + pv.getValue().getClass());
- String originalValue = null;
- String convertedValue = null;
-
- if (pv.getValue() instanceof TypedStringValue) {
- TypedStringValue tsv = (TypedStringValue) pv.getValue();
- originalValue = tsv.getValue();
- convertedValue = ppc.process(originalValue);
- if (!convertedValue.equals(originalValue))
- tsv.setValue(convertedValue);
- }
- else if (pv.getValue() instanceof String) {
- originalValue = pv.getValue().toString();
- convertedValue = ppc.process(originalValue);
- // Setting the convertedValue can be problematic since
- // calling setConvertedValue also sets a flag setConvertedValue
- if (!convertedValue.equals(originalValue))
- pv.setConvertedValue(convertedValue);
- }
-
- else if (pv.getValue() instanceof ManagedMap) {
- //debug
- Object obj = pv.getValue();
- String name = pv.getName();
-
-// log.info("##" + name + ":" + obj.getClass());
- ManagedMap mapVal = (ManagedMap) pv.getValue();
-
- Map newContent = new LinkedHashMap();
- boolean entriesModified = false;
- for (Iterator it = mapVal.entrySet().iterator(); it.hasNext();) {
- Map.Entry entry = (Map.Entry) it.next();
- Object key = entry.getKey();
- int keyHash = (key != null ? key.hashCode() : 0);
- Object newKey = resolveValue(key,ppc);
- int newKeyHash = (newKey != null ? newKey.hashCode() : 0);
- Object val = entry.getValue();
- Object newVal = resolveValue(val,ppc);
- newContent.put(newKey, newVal);
- entriesModified = entriesModified || (newVal != val || newKey != key || newKeyHash != keyHash);
- }
- if (entriesModified) {
- mapVal.clear();
- mapVal.putAll(newContent);
- }
- }
-
- if (convertedValue != null && log.isTraceEnabled()) {
- if (!originalValue.equals(convertedValue))
- log.trace("Converted field '" + pv.getName() + "': '"
- + originalValue + "' to '" + convertedValue
- + "' in bean " + beanName);
- }
- }
-
- return pvs;
- }
-
- public void setPlaceholderPrefix(String placeholderPrefix) {
- this.placeholderPrefix = placeholderPrefix;
- }
-
- public void setPlaceholderSuffix(String placeholderSuffix) {
- this.placeholderSuffix = placeholderSuffix;
- }
-
- public void setNullValue(String nullValue) {
- this.nullValue = nullValue;
- }
-
- private class CustomPpc extends PropertyPlaceholderConfigurer {
- private final Properties props;
-
- public CustomPpc(Properties props) {
- super();
- this.props = props;
- setPlaceholderPrefix(placeholderPrefix);
- setPlaceholderSuffix(placeholderSuffix);
- setSystemPropertiesMode(SYSTEM_PROPERTIES_MODE_NEVER);
- }
-
- /** Public access to the internals of PropertyPlaceholderConfigurer*/
- public String process(String strVal) {
- String value = parseStringValue(strVal, this.props,
- new HashSet<String>());
- return (value.equals(nullValue) ? null : value);
- }
-
- @Override
- protected String resolvePlaceholder(String placeholder, Properties props) {
- // log.info("Try convert placeholder " + placeholder);
- if ((executionScope != null)
- && (executionScope.hasExecutionContext()))
- return executionContext.getVariable(placeholder).toString();
- else if (instantiationManager.isInFlowInitialization())
- return instantiationManager.getInitializingFlowParameter(
- placeholder).toString();
- else
- return super.resolvePlaceholder(placeholder, props);
- }
- }
-}
+package org.argeo.slc.core.execution;\r
+\r
+import java.beans.PropertyDescriptor;\r
+import java.util.HashSet;\r
+import java.util.Iterator;\r
+import java.util.List;\r
+import java.util.Map;\r
+import java.util.Properties;\r
+import java.util.Set;\r
+\r
+import org.apache.commons.logging.Log;\r
+import org.apache.commons.logging.LogFactory;\r
+import org.argeo.slc.SlcException;\r
+import org.argeo.slc.execution.ExecutionContext;\r
+import org.springframework.beans.BeansException;\r
+import org.springframework.beans.MutablePropertyValues;\r
+import org.springframework.beans.PropertyValue;\r
+import org.springframework.beans.PropertyValues;\r
+import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter;\r
+import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;\r
+import org.springframework.beans.factory.config.TypedStringValue;\r
+import org.springframework.beans.factory.support.ManagedList;\r
+import org.springframework.beans.factory.support.ManagedMap;\r
+import org.springframework.beans.factory.support.ManagedSet;\r
+import org.springframework.util.ObjectUtils;\r
+\r
+public class ExecutionParameterPostProcessor extends\r
+ InstantiationAwareBeanPostProcessorAdapter {\r
+\r
+ private final static Log log = LogFactory\r
+ .getLog(ExecutionParameterPostProcessor.class);\r
+ \r
+ private ExecutionContext executionContext;\r
+\r
+ private ExecutionScope executionScope;\r
+\r
+ private InstantiationManager instantiationManager;\r
+ \r
+ public InstantiationManager getInstantiationManager() {\r
+ return instantiationManager;\r
+ }\r
+\r
+ public void setInstantiationManager(\r
+ InstantiationManager instantiationManager) {\r
+ this.instantiationManager = instantiationManager;\r
+ }\r
+\r
+ public void setExecutionScope(ExecutionScope executionScope) {\r
+ this.executionScope = executionScope;\r
+ }\r
+\r
+ public ExecutionContext getExecutionContext() {\r
+ return executionContext;\r
+ }\r
+\r
+ public void setExecutionContext(ExecutionContext executionContext) {\r
+ this.executionContext = executionContext;\r
+ }\r
+\r
+ private String placeholderPrefix = "@{";\r
+ private String placeholderSuffix = "}";\r
+ private String nullValue;\r
+\r
+ @Override\r
+ public PropertyValues postProcessPropertyValues(PropertyValues pvs,\r
+ PropertyDescriptor[] pds, Object bean, String beanName)\r
+ throws BeansException {\r
+\r
+ //TODO: resolve at execution only if scope is execution\r
+ //TODO: deal with placeholders in RuntimeBeanReference and RuntimeBeanNameReference\r
+ \r
+ MutablePropertyValues newPvs = new MutablePropertyValues();\r
+ \r
+ boolean changesOccured = false;\r
+ \r
+ CustomPpc ppc = new CustomPpc(beanName);\r
+ \r
+ for(PropertyValue pv : pvs.getPropertyValues()) {\r
+ Object convertedValue = ppc.resolveValue(pv.getValue());\r
+ newPvs.addPropertyValue(new PropertyValue(pv, convertedValue));\r
+ if(convertedValue != pv.getValue()) {\r
+ changesOccured = true;\r
+ }\r
+ }\r
+ \r
+ return changesOccured ? newPvs : pvs;\r
+ }\r
+\r
+ public void setPlaceholderPrefix(String placeholderPrefix) {\r
+ this.placeholderPrefix = placeholderPrefix;\r
+ }\r
+\r
+ public void setPlaceholderSuffix(String placeholderSuffix) {\r
+ this.placeholderSuffix = placeholderSuffix;\r
+ }\r
+\r
+ public void setNullValue(String nullValue) {\r
+ this.nullValue = nullValue;\r
+ }\r
+ \r
+ private class CustomPpc extends PropertyPlaceholderConfigurer {\r
+ private final Properties props;\r
+ String beanName;\r
+ \r
+ public CustomPpc(String beanName) {\r
+ super();\r
+ this.props = new Properties();\r
+ this.beanName = beanName;\r
+ setPlaceholderPrefix(placeholderPrefix);\r
+ setPlaceholderSuffix(placeholderSuffix);\r
+ setSystemPropertiesMode(SYSTEM_PROPERTIES_MODE_NEVER);\r
+ }\r
+\r
+ /** Public access to the internals of PropertyPlaceholderConfigurer*/\r
+ public String resolveString(String strVal) {\r
+ String value = parseStringValue(strVal, this.props,\r
+ new HashSet<String>());\r
+ return (value.equals(nullValue) ? null : value);\r
+ }\r
+ \r
+ public Object resolveValue(Object value) {\r
+ if (value instanceof TypedStringValue) {\r
+ TypedStringValue tsv = (TypedStringValue) value;\r
+ String originalValue = tsv.getValue();\r
+\r
+ String convertedValue = resolveString(originalValue);\r
+ return convertedValue.equals(originalValue) ? value : new TypedStringValue(convertedValue);\r
+ }\r
+ else if (value instanceof String) {\r
+ String originalValue = value.toString(); \r
+ String convertedValue = resolveString(originalValue);\r
+ return convertedValue.equals(originalValue) ? value : convertedValue;\r
+ } \r
+ else if (value instanceof ManagedMap) {\r
+ Map mapVal = (Map) value;\r
+ \r
+ Map newContent = new ManagedMap();\r
+ boolean entriesModified = false;\r
+ for (Iterator it = mapVal.entrySet().iterator(); it.hasNext();) {\r
+ Map.Entry entry = (Map.Entry) it.next();\r
+ Object key = entry.getKey();\r
+ int keyHash = (key != null ? key.hashCode() : 0);\r
+ Object newKey = resolveValue(key);\r
+ int newKeyHash = (newKey != null ? newKey.hashCode() : 0);\r
+ Object val = entry.getValue();\r
+ Object newVal = resolveValue(val);\r
+ newContent.put(newKey, newVal);\r
+ entriesModified = entriesModified || (newVal != val || newKey != key || newKeyHash != keyHash);\r
+ }\r
+ \r
+ return entriesModified ? newContent : value;\r
+ }\r
+ else if (value instanceof ManagedList) {\r
+ List listVal = (List) value;\r
+ List newContent = new ManagedList();\r
+ boolean valueModified = false;\r
+ \r
+ for (int i = 0; i < listVal.size(); i++) {\r
+ Object elem = listVal.get(i);\r
+ Object newVal = resolveValue(elem);\r
+ newContent.add(newVal);\r
+ if (!ObjectUtils.nullSafeEquals(newVal, elem)) {\r
+ valueModified = true;\r
+ }\r
+ } \r
+ return valueModified ? newContent : value;\r
+ }\r
+ else if (value instanceof ManagedSet) {\r
+ Set setVal = (Set) value;\r
+ Set newContent = new ManagedSet();\r
+ boolean entriesModified = false;\r
+ for (Iterator it = setVal.iterator(); it.hasNext();) {\r
+ Object elem = it.next();\r
+ int elemHash = (elem != null ? elem.hashCode() : 0);\r
+ Object newVal = resolveValue(elem);\r
+ int newValHash = (newVal != null ? newVal.hashCode() : 0);\r
+ newContent.add(newVal);\r
+ entriesModified = entriesModified || (newVal != elem || newValHash != elemHash);\r
+ } \r
+ return entriesModified ? newContent : value;\r
+ }\r
+ else {\r
+ return value;\r
+ } \r
+ }\r
+\r
+ @Override\r
+ protected String resolvePlaceholder(String placeholder, Properties props) { \r
+ if ((executionScope != null)\r
+ && (executionScope.hasExecutionContext())) {\r
+ Object obj = executionContext.findVariable(placeholder);\r
+ if(obj != null) {\r
+ return obj.toString();\r
+ }\r
+ }\r
+ if (instantiationManager.isInFlowInitialization())\r
+ return instantiationManager.getInitializingFlowParameter(\r
+ placeholder).toString();\r
+ else\r
+ throw new SlcException("Could not resolve placeholder '" \r
+ + placeholder + "' in bean '" + beanName + "'");\r
+ }\r
+ }\r
+}\r