X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=org.argeo.slc.runtime%2Fsrc%2Forg%2Fargeo%2Fslc%2Fruntime%2FInstantiationManager.java;h=911e95aaa305681e05f4c210efb59307086a1300;hb=09c9e5093fe1353aaac344ac8a8caf2e1dcc0778;hp=0571e8307d1f83c3e84ea6b8c84b17111b58e227;hpb=810d3b29b3678cbe4b9a6af8e281757c6e820ee4;p=gpl%2Fargeo-slc.git diff --git a/org.argeo.slc.runtime/src/org/argeo/slc/runtime/InstantiationManager.java b/org.argeo.slc.runtime/src/org/argeo/slc/runtime/InstantiationManager.java index 0571e8307..911e95aaa 100644 --- a/org.argeo.slc.runtime/src/org/argeo/slc/runtime/InstantiationManager.java +++ b/org.argeo.slc.runtime/src/org/argeo/slc/runtime/InstantiationManager.java @@ -1,123 +1,102 @@ -/* - * Copyright (C) 2007-2012 Argeo GmbH - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.argeo.slc.runtime; - -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; -import org.argeo.slc.execution.ExecutionSpecAttribute; -import org.argeo.slc.execution.RefSpecAttribute; -import org.argeo.slc.primitive.PrimitiveSpecAttribute; -import org.argeo.slc.primitive.PrimitiveUtils; - -/** Manage parameters that need to be set during the instantiation of a flow */ -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) { - // set the flow name if it is DefaultExecutionFlow - if (flow instanceof DefaultExecutionFlow) { - ((DefaultExecutionFlow) flow).setName(flowName); - } - - if (log.isTraceEnabled()) - log.trace("Start initialization of " + flow.hashCode() + " (" - + flow + " - " + flow.getClass() + ")"); - - // 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() + ")"); - - if (flowStack.get() != null) { - 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); - } - } else { - // happens for flows imported as services - log.warn("flowInitializationFinished - Flow Stack is null"); - } - } - - 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); - } - } - throw new SlcException("Key " + key + " is not set as parameter in " - + 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(); - } -} +package org.argeo.slc.runtime; + +import java.lang.System.Logger.Level; +import java.util.Stack; + +import org.argeo.api.slc.SlcException; +import org.argeo.api.slc.execution.ExecutionFlow; +import org.argeo.api.slc.execution.ExecutionSpecAttribute; +import org.argeo.api.slc.execution.RefSpecAttribute; +import org.argeo.api.slc.primitive.PrimitiveSpecAttribute; +import org.argeo.api.slc.primitive.PrimitiveUtils; + +/** Manage parameters that need to be set during the instantiation of a flow */ +public class InstantiationManager { + + private final static System.Logger logger = System.getLogger(InstantiationManager.class.getName()); + + 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) { + // set the flow name if it is DefaultExecutionFlow + if (flow instanceof DefaultExecutionFlow) { + ((DefaultExecutionFlow) flow).setName(flowName); + } + + logger.log(Level.TRACE, + () -> "Start initialization of " + flow.hashCode() + " (" + flow + " - " + flow.getClass() + ")"); + + // 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) { + logger.log(Level.TRACE, + () -> "Finish initialization of " + flow.hashCode() + " (" + flow + " - " + flow.getClass() + ")"); + + if (flowStack.get() != null) { + 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); + } + } else { + // happens for flows imported as services + logger.log(Level.WARNING, "flowInitializationFinished - Flow Stack is null"); + } + } + + 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); + } + } + throw new SlcException("Key " + key + " is not set as parameter in " + 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(); + } +}