From: Mathieu Baudier Date: Fri, 24 Jul 2009 12:32:09 +0000 (+0000) Subject: Change the way parameterRef is implemented X-Git-Tag: argeo-slc-2.1.7~1637 X-Git-Url: http://git.argeo.org/?a=commitdiff_plain;ds=inline;h=ef08ee82dcb187f6e96114fc9a9568c1da329eca;p=gpl%2Fargeo-slc.git Change the way parameterRef is implemented git-svn-id: https://svn.argeo.org/slc/trunk@2750 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc --- diff --git a/runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/InstantiationManager.java b/runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/InstantiationManager.java index c3d844c46..1f1ff73d2 100644 --- a/runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/InstantiationManager.java +++ b/runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/InstantiationManager.java @@ -6,36 +6,38 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.argeo.slc.SlcException; import org.argeo.slc.execution.ExecutionFlow; +import org.argeo.slc.execution.ExecutionSpecAttribute; public class InstantiationManager { - private final static Log log = LogFactory.getLog(InstantiationManager.class); - - private ThreadLocal > flowStack = new ThreadLocal >(); - + private final static Log log = LogFactory + .getLog(InstantiationManager.class); + + private ThreadLocal> flowStack = new ThreadLocal>(); + public Object createRef(String name) { - - if((flowStack.get() == null) || flowStack.get().empty()) { + + if ((flowStack.get() == null) || flowStack.get().empty()) { throw new SlcException("No flow is currently initializing." + " Declare ParameterRef as inner beans or prototypes."); } - + return getInitializingFlowParameter(name); - } - + } + public void flowInitializationStarted(ExecutionFlow flow, String flowName) { if (log.isTraceEnabled()) log.trace("Start initialization of " + flow.hashCode() + " (" + flow + " - " + flow.getClass() + ")"); - + // set the flow name if it is DefaultExecutionFlow - if(flow instanceof DefaultExecutionFlow) { + if (flow instanceof DefaultExecutionFlow) { ((DefaultExecutionFlow) flow).setBeanName(flowName); } - -// log.info("# flowInitializationStarted " + flowName); + + // log.info("# flowInitializationStarted " + flowName); // create a stack for this thread if there is none - if(flowStack.get() == null) { + if (flowStack.get() == null) { flowStack.set(new Stack()); } flowStack.get().push(flow); @@ -49,26 +51,43 @@ public class InstantiationManager { if (registeredFlow != null) { if (!flow.getName().equals(registeredFlow.getName())) throw new SlcException("Current flow is " + flow); -// log.info("# flowInitializationFinished " + flowName); -// initializingFlow.set(null); + // log.info("# flowInitializationFinished " + flowName); + // initializingFlow.set(null); } - } - - public Object getInitializingFlowParameter(String key) { + } + + protected ExecutionFlow findInitializingFlowWithParameter(String key) { if ((flowStack.get() == null) || flowStack.get().empty()) throw new SlcException("No initializing flow available."); - + // first look in the outer flow (that may override parameters) - for(int i = 0; i < flowStack.get().size(); i++) { - if(flowStack.get().elementAt(i).isSetAsParameter(key)) { - return flowStack.get().elementAt(i).getParameter(key); + for (int i = 0; i < flowStack.get().size(); i++) { + if (flowStack.get().elementAt(i).isSetAsParameter(key)) { + return flowStack.get().elementAt(i); } } throw new SlcException("Key " + key + " is not set as parameter in " - + flowStack.get().firstElement().toString() + " (stack size="+flowStack.get().size()+")"); + + flowStack.get().firstElement().toString() + " (stack size=" + + flowStack.get().size() + ")"); + + } + + public Object getInitializingFlowParameter(String key) { + return findInitializingFlowWithParameter(key).getParameter(key); + } + + public Class getInitializingFlowParameterClass(String key) { + ExecutionSpecAttribute attr = findInitializingFlowWithParameter(key) + .getExecutionSpec().getAttributes().get(key); + if (attr instanceof RefSpecAttribute) + return ((RefSpecAttribute) attr).getTargetClass(); + else if (attr instanceof PrimitiveSpecAttribute) + return ((PrimitiveSpecAttribute) attr).getTypeAsClass(); + else + return null; } public Boolean isInFlowInitialization() { return (flowStack.get() != null) && !flowStack.get().empty(); - } + } } diff --git a/runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/ParameterRef.java b/runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/ParameterRef.java new file mode 100644 index 000000000..d86af02a9 --- /dev/null +++ b/runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/ParameterRef.java @@ -0,0 +1,53 @@ +package org.argeo.slc.core.execution; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.springframework.beans.factory.FactoryBean; + +public class ParameterRef implements FactoryBean { + private final static Log log = LogFactory.getLog(ParameterRef.class); + + private InstantiationManager instantiationManager; + private String name; + + /** Cached object. */ + private Object object; + + public ParameterRef() { + } + + /** @deprecated for backward compatibility with pre v0.11.4 approach. */ + public ParameterRef(String name) { + this.name = name; + } + + public Object getObject() throws Exception { + if (log.isTraceEnabled()) + log.debug("Parameter ref called for " + name); + + if (object == null) + object = instantiationManager.getInitializingFlowParameter(name); + return object; + } + + public Class getObjectType() { + if (object == null) + return instantiationManager.getInitializingFlowParameterClass(name); + else + return object.getClass(); + } + + public boolean isSingleton() { + return true; + } + + public void setInstantiationManager( + InstantiationManager instantiationManager) { + this.instantiationManager = instantiationManager; + } + + public void setName(String name) { + this.name = name; + } + +} diff --git a/runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/PrimitiveSpecAttribute.java b/runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/PrimitiveSpecAttribute.java index 27910bf90..0186a1c3c 100644 --- a/runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/PrimitiveSpecAttribute.java +++ b/runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/PrimitiveSpecAttribute.java @@ -1,5 +1,6 @@ package org.argeo.slc.core.execution; +import org.argeo.slc.SlcException; public class PrimitiveSpecAttribute extends AbstractSpecAttribute implements PrimitiveAccessor { @@ -29,8 +30,33 @@ public class PrimitiveSpecAttribute extends AbstractSpecAttribute implements return type; } + public Class getTypeAsClass() { + return typeAsClass(type); + } + public void setType(String type) { this.type = type; + + // check whether type is recognized. + // TODO: make validation cleaner + typeAsClass(type); + } + + public static Class typeAsClass(String type) { + if (TYPE_STRING.equals(type)) + return String.class; + else if (TYPE_INTEGER.equals(type)) + return Integer.class; + else if (TYPE_LONG.equals(type)) + return Long.class; + else if (TYPE_FLOAT.equals(type)) + return Float.class; + else if (TYPE_DOUBLE.equals(type)) + return Double.class; + else if (TYPE_BOOLEAN.equals(type)) + return Boolean.class; + else + throw new SlcException("Unrecognized type " + type); } } diff --git a/runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/tasks/Echo.java b/runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/tasks/Echo.java index 52195903c..87140246f 100644 --- a/runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/tasks/Echo.java +++ b/runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/tasks/Echo.java @@ -14,7 +14,7 @@ public class Echo implements Runnable { private Resource writeTo = null; private Log log; - private String message; + private Object message; public void run() { log().info(message); @@ -24,7 +24,8 @@ public class Echo implements Runnable { File file = writeTo.getFile(); if (log().isDebugEnabled()) log().debug("Write to " + file); - FileUtils.writeStringToFile(file, message); + if (message != null) + FileUtils.writeStringToFile(file, message.toString()); } catch (IOException e) { throw new SlcException("Could not write to " + writeTo, e); } @@ -35,7 +36,7 @@ public class Echo implements Runnable { return log != null ? log : defaultLog; } - public void setMessage(String message) { + public void setMessage(Object message) { this.message = message; } diff --git a/runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/test/context/ContextUtils.java b/runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/test/context/ContextUtils.java index 8d795405c..ff3b1095f 100644 --- a/runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/test/context/ContextUtils.java +++ b/runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/test/context/ContextUtils.java @@ -107,7 +107,7 @@ public class ContextUtils { parent.getExpectedValues()); synchronize(parent, expectedValuesCommon); if (log.isDebugEnabled()) - log.debug("Synchonized context " + parent); + log.debug("Synchronized context " + parent); } diff --git a/runtime/org.argeo.slc.core/src/main/resources/org/argeo/slc/core/execution/simple.xml b/runtime/org.argeo.slc.core/src/main/resources/org/argeo/slc/core/execution/simple.xml index a6887e3d9..d4db7a3c0 100644 --- a/runtime/org.argeo.slc.core/src/main/resources/org/argeo/slc/core/execution/simple.xml +++ b/runtime/org.argeo.slc.core/src/main/resources/org/argeo/slc/core/execution/simple.xml @@ -25,7 +25,12 @@ - + + + \ No newline at end of file diff --git a/runtime/org.argeo.slc.core/src/test/java/org/argeo/slc/core/execution/ExceptionIfInitCalledTwice.java b/runtime/org.argeo.slc.core/src/test/java/org/argeo/slc/core/execution/ExceptionIfInitCalledTwice.java new file mode 100644 index 000000000..7ba7c826a --- /dev/null +++ b/runtime/org.argeo.slc.core/src/test/java/org/argeo/slc/core/execution/ExceptionIfInitCalledTwice.java @@ -0,0 +1,27 @@ +package org.argeo.slc.core.execution; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.argeo.slc.SlcException; +import org.springframework.beans.factory.InitializingBean; + +public class ExceptionIfInitCalledTwice implements Runnable, InitializingBean { + private final static Log log = LogFactory + .getLog(ExceptionIfInitCalledTwice.class); + + private Boolean calledOnce = false; + + public void run() { + log.info(getClass().getSimpleName() + " ran properly"); + } + + public void afterPropertiesSet() throws Exception { + log.info(getClass().getSimpleName() + " init method called"); + + if (calledOnce) + throw new SlcException(getClass().getSimpleName() + + "init method called twice."); + else + calledOnce = true; + } +} diff --git a/runtime/org.argeo.slc.core/src/test/java/org/argeo/slc/core/execution/ParameterRefTest.java b/runtime/org.argeo.slc.core/src/test/java/org/argeo/slc/core/execution/ParameterRefTest.java new file mode 100644 index 000000000..4d9c9220c --- /dev/null +++ b/runtime/org.argeo.slc.core/src/test/java/org/argeo/slc/core/execution/ParameterRefTest.java @@ -0,0 +1,21 @@ +package org.argeo.slc.core.execution; + +import org.argeo.slc.core.test.SimpleTestResult; +import org.argeo.slc.execution.ExecutionFlow; +import org.argeo.slc.test.TestStatus; +import org.springframework.context.ConfigurableApplicationContext; + +public class ParameterRefTest extends AbstractExecutionFlowTestCase { + public void test001() throws Exception { + ConfigurableApplicationContext applicationContext = createApplicationContext("parameterRef.xml"); + ((ExecutionFlow) applicationContext.getBean("parameterRef.001")).run(); + + SimpleTestResult res = (SimpleTestResult) applicationContext + .getBean("parameterRef.testResult"); + assertEquals(res.getParts().get(0).getStatus(), TestStatus.PASSED); + assertEquals(res.getParts().get(1).getStatus(), TestStatus.FAILED); + + applicationContext.close(); + } + +} diff --git a/runtime/org.argeo.slc.core/src/test/resources/org/argeo/slc/core/execution/parameterRef.xml b/runtime/org.argeo.slc.core/src/test/resources/org/argeo/slc/core/execution/parameterRef.xml new file mode 100644 index 000000000..9f454539b --- /dev/null +++ b/runtime/org.argeo.slc.core/src/test/resources/org/argeo/slc/core/execution/parameterRef.xml @@ -0,0 +1,109 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file