]> 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
Fix issue with parameters
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.simple / src / main / java / org / argeo / slc / core / execution / ExecutionParameterPostProcessor.java
index d393d774f84c96de38737d93f6458e019fc64f96..5a00ed5aca89e01666546c3e907b04a5fa605910 100644 (file)
@@ -8,6 +8,7 @@ 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;
@@ -17,9 +18,37 @@ 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;
+
+       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;
@@ -28,48 +57,42 @@ public class ExecutionParameterPostProcessor extends
        public PropertyValues postProcessPropertyValues(PropertyValues pvs,
                        PropertyDescriptor[] pds, Object bean, String beanName)
                        throws BeansException {
-               if (!ExecutionContext.isExecuting())
-                       return pvs;
 
-//             ExecutionFlow currentFlow = ExecutionContext.getCurrentFlow();
+//             boolean inFlowInitialization = instantiationManager
+//                             .isInFlowInitialization();
 //
-//             Properties props = new Properties();
-//             Map<String, Object> attributes = currentFlow.getAttributes();
-//             Map<String, ExecutionSpecAttribute> specAttributes = currentFlow
-//                             .getExecutionSpec().getAttributes();
-//
-//             for (String key : specAttributes.keySet()) {
-//                     ExecutionSpecAttribute obj = specAttributes.get(key);
-//                     if (!(obj instanceof RefSpecAttribute)) {
-//                             if (!attributes.containsKey(key))
-//                                     throw new SlcException("Specified attribute " + key
-//                                                     + " is not set in " + currentFlow);
-//
-//                             props.setProperty(key, attributes.get(key).toString());
-//                             // if (log.isTraceEnabled())
-//                             // log.trace("Use attribute " + key);
-//                     }
+//             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);
 //             }
 
                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();
-                               String originalValue = tsv.getValue();
-                               String convertedValue = ppc.process(originalValue);
+                               originalValue = tsv.getValue();
+                               convertedValue = ppc.process(originalValue);
                                tsv.setValue(convertedValue);
-                               if (log.isTraceEnabled()) {
-                                       if (!originalValue.equals(convertedValue))
-                                               log.trace("Converted field '" + pv.getName() + "': '"
-                                                               + originalValue + "' to '" + convertedValue
-                                                               + "' in bean " + beanName);
-                               }
-                       } else {
-                               // if (log.isTraceEnabled())
-                               // log.trace(beanName + "[" + pv.getName() + "]: "
-                               // + pv.getValue().getClass());
+                       } else if (pv.getValue() instanceof String) {
+                               originalValue = pv.getValue().toString();
+                               convertedValue = ppc.process(originalValue);
+                               if (!convertedValue.equals(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);
                        }
                }
 
@@ -99,6 +122,7 @@ public class ExecutionParameterPostProcessor extends
                        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>());
@@ -107,14 +131,15 @@ public class ExecutionParameterPostProcessor extends
 
                @Override
                protected String resolvePlaceholder(String placeholder, Properties props) {
-                       if (ExecutionContext.isExecuting())
-                               return ExecutionContext.getVariable(placeholder).toString();
-                       else if (SimpleExecutionSpec.isInFlowInitialization())
-                               return SimpleExecutionSpec.getInitializingFlowParameter(
+                       // 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);
                }
-
        }
 }