]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/core/execution/ExecutionParameterPostProcessor.java
ExecutionParameterPostProcessor improved (placeholder resolved for each instance)
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.simple / src / main / java / org / argeo / slc / core / execution / ExecutionParameterPostProcessor.java
index a76820465d452692bbe310010e5237b3d39445c2..175ef778c68ce5cc502dd576616d57567eb82d1d 100644 (file)
-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.LinkedHashSet;
-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 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 if (value instanceof Map) {
-                       Map mapVal = (Map) value;
-                       
-                       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);
-                       }
-                       return mapVal;
-               }
-               else if (value instanceof List) {
-                       List listVal = (List) value;
-                       for (int i = 0; i < listVal.size(); i++) {
-                               Object elem = listVal.get(i);
-                               Object newVal = resolveValue(elem,ppc);
-                               if (!ObjectUtils.nullSafeEquals(newVal, elem)) {
-                                       listVal.set(i, newVal);
-                               }
-                       }                       
-                       return value;
-               }
-               else if (value instanceof Set) {
-                       Set setVal = (Set) value;
-                       Set newContent = new LinkedHashSet();
-                       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,ppc);
-                               int newValHash = (newVal != null ? newVal.hashCode() : 0);
-                               newContent.add(newVal);
-                               entriesModified = entriesModified || (newVal != elem || newValHash != elemHash);
-                       }
-                       if (entriesModified) {
-                               setVal.clear();
-                               setVal.addAll(newContent);
-                       }       
-                       return value;
-               }
-               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)
-                                       ||(pv.getValue() instanceof ManagedList)
-                                       ||(pv.getValue() instanceof ManagedSet)){
-                               resolveValue(pv.getValue(),ppc);                        
-                       }
-                       
-                       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.ArrayList;\r
+import java.util.HashMap;\r
+import java.util.HashSet;\r
+import java.util.Iterator;\r
+import java.util.LinkedHashMap;\r
+import java.util.LinkedHashSet;\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
+       private Map<String, PropertyValues> storedPvsMap = new HashMap<String, PropertyValues>();\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
+                       \r
+               // if \r
+               PropertyValues sourcePvs = pvs;\r
+               if(storedPvsMap.containsKey(beanName)) {\r
+                       sourcePvs = storedPvsMap.get(beanName);\r
+                       log.info("Use storedPvsMap for bean " + beanName);\r
+               }\r
+               \r
+               MutablePropertyValues newPvs = new MutablePropertyValues();\r
+               \r
+               boolean changesOccured = false;\r
+                               \r
+               CustomPpc ppc = new CustomPpc(beanName);\r
+               \r
+               for(int i=0; i<sourcePvs.getPropertyValues().length; i++) {\r
+                       \r
+                       PropertyValue pv = pvs.getPropertyValues()[i];\r
+                       \r
+                       if (pv.getValue() instanceof TypedStringValue) {\r
+                               TypedStringValue tsv = (TypedStringValue) pv.getValue();\r
+                               String originalValue = tsv.getValue();\r
+                               String convertedValue = ppc.resolveString(originalValue);\r
+                               // add a new Property value to newPvs, identical to pv\r
+                               // except for the value\r
+                               newPvs.addPropertyValue(new PropertyValue(pv, new TypedStringValue(convertedValue)));\r
+                               if (!convertedValue.equals(originalValue)) {\r
+                                       changesOccured = true;\r
+                               }\r
+                       }\r
+                       else if (pv.getValue() instanceof String) {\r
+                               String originalValue = pv.getValue().toString();                        \r
+                               String convertedValue = ppc.resolveString(originalValue);\r
+                               newPvs.addPropertyValue(new PropertyValue(pv, convertedValue));\r
+                               if (!convertedValue.equals(originalValue)) {\r
+                                       changesOccured = true;\r
+                               }\r
+                       }               \r
+                       else if ((pv.getValue() instanceof ManagedMap)\r
+                                       ||(pv.getValue() instanceof ManagedList)\r
+                                       ||(pv.getValue() instanceof ManagedSet)){\r
+\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
+                       else {\r
+                               newPvs.addPropertyValue(new PropertyValue(pv));\r
+                       }\r
+               }\r
+               \r
+               if(changesOccured) {\r
+                       storedPvsMap.put(beanName, pvs);\r
+                       log.info("Add storedPvsMap for Bean " + beanName);\r
+                       return newPvs;\r
+               }\r
+               else {\r
+                       // no change, return pvs\r
+                       return pvs;\r
+               }\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 Map) {\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 List) {\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 Set) {\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