X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=runtime%2Forg.argeo.slc.support.simple%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fslc%2Fcore%2Fexecution%2FExecutionParameterPostProcessor.java;h=1bdd4f29a6d46a984443be4b213e7100bffbfda6;hb=9daa55ce316d52ffd8f30dc0d1b516ccf78a8c73;hp=c5d484bbfafbb190362c8cd900db5991c31d663e;hpb=ae98a7536a842021e433a43427db79a18abf3a6c;p=gpl%2Fargeo-slc.git diff --git a/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/core/execution/ExecutionParameterPostProcessor.java b/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/core/execution/ExecutionParameterPostProcessor.java index c5d484bbf..1bdd4f29a 100644 --- a/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/core/execution/ExecutionParameterPostProcessor.java +++ b/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/core/execution/ExecutionParameterPostProcessor.java @@ -1,127 +1,204 @@ -package org.argeo.slc.core.execution; - -import java.beans.PropertyDescriptor; -import java.util.HashSet; -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.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; - -public class ExecutionParameterPostProcessor extends - InstantiationAwareBeanPostProcessorAdapter { - - private final static Log log = LogFactory - .getLog(ExecutionParameterPostProcessor.class); - - private ExecutionContext executionContext; - - private ExecutionScope executionScope; - - 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; - - @Override - public PropertyValues postProcessPropertyValues(PropertyValues pvs, - PropertyDescriptor[] pds, Object bean, String beanName) - throws BeansException { - if ((executionScope == null) || (!executionScope.hasExecutionContext())){ - //&& !DefaultExecutionSpec.isInFlowInitialization()) { - //log.info("Skip parameter conversion for bean " + beanName); - return pvs; - } else { - //log.info("Execute parameter conversion for bean " + beanName); - } - - 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); - tsv.setValue(convertedValue); - } else if (pv.getValue() instanceof String) { - originalValue = pv.getValue().toString(); - convertedValue = ppc.process(originalValue); - pv.setConvertedValue(convertedValue); - } - 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 String process(String strVal) { - String value = parseStringValue(strVal, this.props, - new HashSet()); - 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 (DefaultExecutionSpec.isInFlowInitialization()) - return DefaultExecutionSpec.getInitializingFlowParameter( - placeholder).toString(); - else - return super.resolvePlaceholder(placeholder, props); - } - } -} +package org.argeo.slc.core.execution; + +import java.beans.PropertyDescriptor; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Properties; +import java.util.Set; + +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.ManagedList; +import org.springframework.beans.factory.support.ManagedMap; +import org.springframework.beans.factory.support.ManagedSet; +import org.springframework.util.ObjectUtils; + +public class ExecutionParameterPostProcessor extends + InstantiationAwareBeanPostProcessorAdapter { + + private final static Log log = LogFactory + .getLog(ExecutionParameterPostProcessor.class); + + 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; + + @Override + public PropertyValues postProcessPropertyValues(PropertyValues pvs, + PropertyDescriptor[] pds, Object bean, String beanName) + throws BeansException { + + //TODO: resolve at execution only if scope is execution + //TODO: deal with placeholders in RuntimeBeanReference and RuntimeBeanNameReference + + MutablePropertyValues newPvs = new MutablePropertyValues(); + + boolean changesOccured = false; + + CustomPpc ppc = new CustomPpc(beanName); + + for(PropertyValue pv : pvs.getPropertyValues()) { + Object convertedValue = ppc.resolveValue(pv.getValue()); + newPvs.addPropertyValue(new PropertyValue(pv, convertedValue)); + if(convertedValue != pv.getValue()) { + changesOccured = true; + } + } + + return changesOccured ? newPvs : 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; + String beanName; + + public CustomPpc(String beanName) { + super(); + this.props = new Properties(); + this.beanName = beanName; + setPlaceholderPrefix(placeholderPrefix); + setPlaceholderSuffix(placeholderSuffix); + setSystemPropertiesMode(SYSTEM_PROPERTIES_MODE_NEVER); + } + + /** Public access to the internals of PropertyPlaceholderConfigurer*/ + public String resolveString(String strVal) { + String value = parseStringValue(strVal, this.props, + new HashSet()); + return (value.equals(nullValue) ? null : value); + } + + public Object resolveValue(Object value) { + if (value instanceof TypedStringValue) { + TypedStringValue tsv = (TypedStringValue) value; + String originalValue = tsv.getValue(); + + String convertedValue = resolveString(originalValue); + return convertedValue.equals(originalValue) ? value : new TypedStringValue(convertedValue); + } + else if (value instanceof String) { + String originalValue = value.toString(); + String convertedValue = resolveString(originalValue); + return convertedValue.equals(originalValue) ? value : convertedValue; + } + else if (value instanceof ManagedMap) { + Map mapVal = (Map) value; + + Map newContent = new ManagedMap(); + 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); + int newKeyHash = (newKey != null ? newKey.hashCode() : 0); + Object val = entry.getValue(); + Object newVal = resolveValue(val); + newContent.put(newKey, newVal); + entriesModified = entriesModified || (newVal != val || newKey != key || newKeyHash != keyHash); + } + + return entriesModified ? newContent : value; + } + else if (value instanceof ManagedList) { + List listVal = (List) value; + List newContent = new ManagedList(); + boolean valueModified = false; + + for (int i = 0; i < listVal.size(); i++) { + Object elem = listVal.get(i); + Object newVal = resolveValue(elem); + newContent.add(newVal); + if (!ObjectUtils.nullSafeEquals(newVal, elem)) { + valueModified = true; + } + } + return valueModified ? newContent : value; + } + else if (value instanceof ManagedSet) { + Set setVal = (Set) value; + Set newContent = new ManagedSet(); + boolean entriesModified = false; + for (Iterator it = setVal.iterator(); it.hasNext();) { + Object elem = it.next(); + int elemHash = (elem != null ? elem.hashCode() : 0); + Object newVal = resolveValue(elem); + int newValHash = (newVal != null ? newVal.hashCode() : 0); + newContent.add(newVal); + entriesModified = entriesModified || (newVal != elem || newValHash != elemHash); + } + return entriesModified ? newContent : value; + } + else { + return value; + } + } + + @Override + protected String resolvePlaceholder(String placeholder, Properties props) { + if ((executionScope != null) + && (executionScope.hasExecutionContext())) { + Object obj = executionContext.findVariable(placeholder); + if(obj != null) { + return obj.toString(); + } + } + if (instantiationManager.isInFlowInitialization()) + return instantiationManager.getInitializingFlowParameter( + placeholder).toString(); + else + throw new SlcException("Could not resolve placeholder '" + + placeholder + "' in bean '" + beanName + "'"); + } + } +}