Refactor Argeo API
[gpl/argeo-slc.git] / org.argeo.slc.runtime / src / org / argeo / slc / runtime / InstantiationManager.java
index 0571e8307d1f83c3e84ea6b8c84b17111b58e227..911e95aaa305681e05f4c210efb59307086a1300 100644 (file)
-/*\r
- * Copyright (C) 2007-2012 Argeo GmbH\r
- *\r
- * Licensed under the Apache License, Version 2.0 (the "License");\r
- * you may not use this file except in compliance with the License.\r
- * You may obtain a copy of the License at\r
- *\r
- *         http://www.apache.org/licenses/LICENSE-2.0\r
- *\r
- * Unless required by applicable law or agreed to in writing, software\r
- * distributed under the License is distributed on an "AS IS" BASIS,\r
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * See the License for the specific language governing permissions and\r
- * limitations under the License.\r
- */\r
-package org.argeo.slc.runtime;\r
-\r
-import java.util.Stack;\r
-\r
-import org.apache.commons.logging.Log;\r
-import org.apache.commons.logging.LogFactory;\r
-import org.argeo.slc.SlcException;\r
-import org.argeo.slc.execution.ExecutionFlow;\r
-import org.argeo.slc.execution.ExecutionSpecAttribute;\r
-import org.argeo.slc.execution.RefSpecAttribute;\r
-import org.argeo.slc.primitive.PrimitiveSpecAttribute;\r
-import org.argeo.slc.primitive.PrimitiveUtils;\r
-\r
-/** Manage parameters that need to be set during the instantiation of a flow */\r
-public class InstantiationManager {\r
-\r
-       private final static Log log = LogFactory\r
-                       .getLog(InstantiationManager.class);\r
-\r
-       private ThreadLocal<Stack<ExecutionFlow>> flowStack = new ThreadLocal<Stack<ExecutionFlow>>();\r
-\r
-       public Object createRef(String name) {\r
-\r
-               if ((flowStack.get() == null) || flowStack.get().empty()) {\r
-                       throw new SlcException("No flow is currently initializing."\r
-                                       + " Declare ParameterRef as inner beans or prototypes.");\r
-               }\r
-\r
-               return getInitializingFlowParameter(name);\r
-       }\r
-\r
-       public void flowInitializationStarted(ExecutionFlow flow, String flowName) {\r
-               // set the flow name if it is DefaultExecutionFlow\r
-               if (flow instanceof DefaultExecutionFlow) {\r
-                       ((DefaultExecutionFlow) flow).setName(flowName);\r
-               }\r
-\r
-               if (log.isTraceEnabled())\r
-                       log.trace("Start initialization of " + flow.hashCode() + " ("\r
-                                       + flow + " - " + flow.getClass() + ")");\r
-\r
-               // log.info("# flowInitializationStarted " + flowName);\r
-               // create a stack for this thread if there is none\r
-               if (flowStack.get() == null) {\r
-                       flowStack.set(new Stack<ExecutionFlow>());\r
-               }\r
-               flowStack.get().push(flow);\r
-       }\r
-\r
-       public void flowInitializationFinished(ExecutionFlow flow, String flowName) {\r
-               if (log.isTraceEnabled())\r
-                       log.trace("Finish initialization of " + flow.hashCode() + " ("\r
-                                       + flow + " - " + flow.getClass() + ")");\r
-\r
-               if (flowStack.get() != null) {\r
-                       ExecutionFlow registeredFlow = flowStack.get().pop();\r
-                       if (registeredFlow != null) {\r
-                               if (!flow.getName().equals(registeredFlow.getName()))\r
-                                       throw new SlcException("Current flow is " + flow);\r
-                               // log.info("# flowInitializationFinished " + flowName);\r
-                               // initializingFlow.set(null);\r
-                       }\r
-               } else {\r
-                       // happens for flows imported as services\r
-                       log.warn("flowInitializationFinished - Flow Stack is null");\r
-               }\r
-       }\r
-\r
-       protected ExecutionFlow findInitializingFlowWithParameter(String key) {\r
-               if ((flowStack.get() == null) || flowStack.get().empty())\r
-                       throw new SlcException("No initializing flow available.");\r
-\r
-               // first look in the outer flow (that may override parameters)\r
-               for (int i = 0; i < flowStack.get().size(); i++) {\r
-                       if (flowStack.get().elementAt(i).isSetAsParameter(key)) {\r
-                               return flowStack.get().elementAt(i);\r
-                       }\r
-               }\r
-               throw new SlcException("Key " + key + " is not set as parameter in "\r
-                               + flowStack.get().firstElement().toString() + " (stack size="\r
-                               + flowStack.get().size() + ")");\r
-\r
-       }\r
-\r
-       public Object getInitializingFlowParameter(String key) {\r
-               return findInitializingFlowWithParameter(key).getParameter(key);\r
-       }\r
-\r
-       public Class<?> getInitializingFlowParameterClass(String key) {\r
-               ExecutionSpecAttribute attr = findInitializingFlowWithParameter(key)\r
-                               .getExecutionSpec().getAttributes().get(key);\r
-               if (attr instanceof RefSpecAttribute)\r
-                       return ((RefSpecAttribute) attr).getTargetClass();\r
-               else if (attr instanceof PrimitiveSpecAttribute) {\r
-                       String type = ((PrimitiveSpecAttribute) attr).getType();\r
-                       Class<?> clss = PrimitiveUtils.typeAsClass(type);\r
-                       if (clss == null)\r
-                               throw new SlcException("Cannot convert type " + type\r
-                                               + " to class.");\r
-                       return clss;\r
-               } else\r
-                       return null;\r
-       }\r
-\r
-       public Boolean isInFlowInitialization() {\r
-               return (flowStack.get() != null) && !flowStack.get().empty();\r
-       }\r
-}\r
+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<Stack<ExecutionFlow>> flowStack = new ThreadLocal<Stack<ExecutionFlow>>();
+
+       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<ExecutionFlow>());
+               }
+               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();
+       }
+}