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;h=eb6ce53473407a258fb5f1a6af52401be5201eb8;hb=6926b1fc72b36a5d8ec188a4981665fb7554a7d9;hp=c3d844c46c54c01bd267792e6ec1af8d23057d64;hpb=ee6c3543a0ff9403420ce6a9c647723269f14331;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 index c3d844c46..eb6ce5347 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,48 @@ 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) { + String type = ((PrimitiveSpecAttribute) attr).getType(); + Class clss = PrimitiveUtils.typeAsClass(type); + if (clss == null) + throw new SlcException("Cannot convert type " + type + + " to class."); + return clss; + } else + return null; } public Boolean isInFlowInitialization() { return (flowStack.get() != null) && !flowStack.get().empty(); - } + } }