X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=runtime%2Forg.argeo.slc.core%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fslc%2Fcore%2Fexecution%2FInstantiationManager.java;fp=runtime%2Forg.argeo.slc.core%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fslc%2Fcore%2Fexecution%2FInstantiationManager.java;h=c3d844c46c54c01bd267792e6ec1af8d23057d64;hb=ee6c3543a0ff9403420ce6a9c647723269f14331;hp=0000000000000000000000000000000000000000;hpb=9daa55ce316d52ffd8f30dc0d1b516ccf78a8c73;p=gpl%2Fargeo-slc.git 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 new file mode 100644 index 000000000..c3d844c46 --- /dev/null +++ b/runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/InstantiationManager.java @@ -0,0 +1,74 @@ +package org.argeo.slc.core.execution; + +import java.util.Stack; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.argeo.slc.SlcException; +import org.argeo.slc.execution.ExecutionFlow; + +public class InstantiationManager { + + 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()) { + 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) { + ((DefaultExecutionFlow) flow).setBeanName(flowName); + } + +// log.info("# flowInitializationStarted " + flowName); + // create a stack for this thread if there is none + if(flowStack.get() == null) { + flowStack.set(new Stack()); + } + flowStack.get().push(flow); + } + + public void flowInitializationFinished(ExecutionFlow flow, String flowName) { + if (log.isTraceEnabled()) + log.trace("Finish initialization of " + flow.hashCode() + " (" + + flow + " - " + flow.getClass() + ")"); + ExecutionFlow registeredFlow = flowStack.get().pop(); + if (registeredFlow != null) { + if (!flow.getName().equals(registeredFlow.getName())) + throw new SlcException("Current flow is " + flow); +// log.info("# flowInitializationFinished " + flowName); +// initializingFlow.set(null); + } + } + + public Object getInitializingFlowParameter(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); + } + } + throw new SlcException("Key " + key + " is not set as parameter in " + + flowStack.get().firstElement().toString() + " (stack size="+flowStack.get().size()+")"); + } + + public Boolean isInFlowInitialization() { + return (flowStack.get() != null) && !flowStack.get().empty(); + } +}