]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - runtime/org.argeo.slc.support.osgi/src/main/java/org/argeo/slc/osgi/OsgiExecutionModulesManager.java
Improve Javadocs
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.osgi / src / main / java / org / argeo / slc / osgi / OsgiExecutionModulesManager.java
index ca8fabaeee77e830b75dee88aff9ba1223e913b8..2cfad6683cdc7404daaa076b945ac1c6902b3b44 100644 (file)
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
+ *
+ * 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.osgi;
 
 import java.util.ArrayList;
@@ -7,11 +23,14 @@ import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
+import java.util.Properties;
 import java.util.Set;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.argeo.slc.SlcException;
+import org.argeo.slc.build.BasicNameVersion;
+import org.argeo.slc.build.NameVersion;
 import org.argeo.slc.core.execution.AbstractExecutionModulesManager;
 import org.argeo.slc.core.execution.DefaultExecutionFlowDescriptorConverter;
 import org.argeo.slc.deploy.ModuleDescriptor;
@@ -20,6 +39,7 @@ import org.argeo.slc.execution.ExecutionFlow;
 import org.argeo.slc.execution.ExecutionFlowDescriptor;
 import org.argeo.slc.execution.ExecutionFlowDescriptorConverter;
 import org.argeo.slc.execution.ExecutionModuleDescriptor;
+import org.argeo.slc.execution.ExecutionModulesListener;
 import org.argeo.slc.process.RealizedFlow;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.Constants;
@@ -33,6 +53,28 @@ import org.springframework.osgi.service.importer.OsgiServiceLifecycleListener;
 public class OsgiExecutionModulesManager extends
                AbstractExecutionModulesManager implements InitializingBean,
                DisposableBean, OsgiServiceLifecycleListener {
+
+//     static {
+//             // Force usage of vanilla Xalan when in OSGi
+//             // We would like to do it in a cleaner way
+//             // but the integration of Xalan and Xerces in the JRE
+//             // makes it very difficult
+//             // Suggestions welcome!
+//             Properties systemProperties = System.getProperties();
+//             // if (!systemProperties
+//             // .containsKey("javax.xml.parsers.DocumentBuilderFactory"))
+//             // System.setProperty("javax.xml.parsers.DocumentBuilderFactory",
+//             // "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");
+//             // if
+//             // (!systemProperties.containsKey("javax.xml.parsers.SAXParserFactory"))
+//             // System.setProperty("javax.xml.parsers.SAXParserFactory",
+//             // "org.apache.xerces.jaxp.SAXParserFactoryImpl");
+//             if (!systemProperties
+//                             .containsKey("javax.xml.transform.TransformerFactory"))
+//                     System.setProperty("javax.xml.transform.TransformerFactory",
+//                                     "org.apache.xalan.processor.TransformerFactoryImpl");
+//     }
+
        private final static String PROPERTY_CACHE_SERVICES = "slc.osgi.execution.cacheServices";
 
        private final static Log log = LogFactory
@@ -48,12 +90,33 @@ public class OsgiExecutionModulesManager extends
        private Boolean useCachedServices = Boolean.parseBoolean(System
                        .getProperty(PROPERTY_CACHE_SERVICES, "true"));
 
-       public ExecutionModuleDescriptor getExecutionModuleDescriptor(
+       public synchronized ExecutionModuleDescriptor getExecutionModuleDescriptor(
                        String moduleName, String version) {
                ExecutionModuleDescriptor md = new ExecutionModuleDescriptor();
-               md.setName(moduleName);
-               md.setVersion(version);
-               setMetadataFromBundle(md, null);
+               if (useCachedServices) {
+                       OsgiBundle osgiBundle = null;
+                       BasicNameVersion nameVersion = new BasicNameVersion(moduleName,
+                                       version);
+                       bundles: for (Iterator<OsgiBundle> iterator = executionContexts
+                                       .keySet().iterator(); iterator.hasNext();) {
+                               OsgiBundle ob = iterator.next();
+                               if (ob.equals(nameVersion)) {
+                                       osgiBundle = ob;
+                                       break bundles;
+                               }
+                       }
+                       if (osgiBundle == null)
+                               throw new SlcException("No execution module registered for "
+                                               + nameVersion);
+                       md.setName(osgiBundle.getName());
+                       md.setVersion(osgiBundle.getVersion());
+                       md.setLabel(osgiBundle.getLabel());
+                       md.setDescription(osgiBundle.getDescription());
+               } else {
+                       md.setName(moduleName);
+                       md.setVersion(version);
+                       setMetadataFromBundle(md, null);
+               }
                ExecutionFlowDescriptorConverter executionFlowDescriptorConverter = getExecutionFlowDescriptorConverter(
                                moduleName, version);
                if (executionFlowDescriptorConverter == null)
@@ -100,8 +163,11 @@ public class OsgiExecutionModulesManager extends
 
                Map<String, ExecutionFlow> flows = new HashMap<String, ExecutionFlow>();
                if (useCachedServices) {
-                       Set<ExecutionFlow> flowsT = executionFlows.get(new OsgiBundle(
-                                       moduleName, moduleVersion));
+                       OsgiBundle key = new OsgiBundle(
+                                       moduleName, moduleVersion);
+                       if(!executionFlows.containsKey(key))
+                               return flows;
+                       Set<ExecutionFlow> flowsT = executionFlows.get(key);
                        for (ExecutionFlow flow : flowsT)
                                flows.put(flow.getName(), flow);
                } else {
@@ -212,10 +278,9 @@ public class OsgiExecutionModulesManager extends
                }
        }
 
-       public void updateAndExecute(RealizedFlow realizedFlow) {
-               OsgiBundle osgiBundle = new OsgiBundle(realizedFlow);
+       public void upgrade(NameVersion nameVersion) {
+               OsgiBundle osgiBundle = new OsgiBundle(nameVersion);
                bundlesManager.upgradeSynchronous(osgiBundle);
-               execute(realizedFlow);
        }
 
        protected synchronized ExecutionFlowDescriptorConverter getExecutionFlowDescriptorConverter(
@@ -237,33 +302,6 @@ public class OsgiExecutionModulesManager extends
                }
        }
 
-       public void execute(RealizedFlow realizedFlow) {
-               if (log.isTraceEnabled())
-                       log.trace("Executing " + realizedFlow);
-
-               String moduleName = realizedFlow.getModuleName();
-               String moduleVersion = realizedFlow.getModuleVersion();
-
-               Map<? extends String, ? extends Object> variablesToAdd = getExecutionFlowDescriptorConverter(
-                               moduleName, moduleVersion).convertValues(
-                               realizedFlow.getFlowDescriptor());
-               ExecutionContext executionContext = findExecutionContext(moduleName,
-                               moduleVersion);
-               for (String key : variablesToAdd.keySet())
-                       executionContext.setVariable(key, variablesToAdd.get(key));
-
-               ExecutionFlow flow = findExecutionFlow(moduleName, moduleVersion,
-                               realizedFlow.getFlowDescriptor().getName());
-
-               //
-               // Actually runs the flow, IN THIS THREAD
-               //
-               flow.run();
-               //
-               //
-               //
-       }
-
        public ModuleDescriptor getModuleDescriptor(String moduleName,
                        String version) {
                return getExecutionModuleDescriptor(moduleName, version);
@@ -321,9 +359,17 @@ public class OsgiExecutionModulesManager extends
                if (service instanceof ExecutionContext) {
                        ExecutionContext executionContext = (ExecutionContext) service;
                        OsgiBundle osgiBundle = asOsgiBundle(properties);
+                       Bundle bundle = bundlesManager.findRelatedBundle(osgiBundle);
+                       osgiBundle.setLabel(getHeaderSafe(bundle, Constants.BUNDLE_NAME));
+                       osgiBundle.setDescription(getHeaderSafe(bundle,
+                                       Constants.BUNDLE_DESCRIPTION));
                        executionContexts.put(osgiBundle, executionContext);
                        if (log.isTraceEnabled())
                                log.debug("Registered execution context from " + osgiBundle);
+                       // Notify
+                       for (ExecutionModulesListener listener : getExecutionModulesListeners())
+                               listener.executionModuleAdded(osgiBundle, executionContext);
+
                } else if (service instanceof ExecutionFlow) {
                        ExecutionFlow executionFlow = (ExecutionFlow) service;
                        OsgiBundle osgiBundle = asOsgiBundle(properties);
@@ -335,6 +381,8 @@ public class OsgiExecutionModulesManager extends
                                log
                                                .debug("Registered " + executionFlow + " from "
                                                                + osgiBundle);
+                       for (ExecutionModulesListener listener : getExecutionModulesListeners())
+                               listener.executionFlowAdded(osgiBundle, executionFlow);
 
                } else if (service instanceof ExecutionFlowDescriptorConverter) {
                        ExecutionFlowDescriptorConverter executionFlowDescriptorConverter = (ExecutionFlowDescriptorConverter) service;
@@ -356,18 +404,31 @@ public class OsgiExecutionModulesManager extends
                if (service instanceof ExecutionContext) {
                        OsgiBundle osgiBundle = asOsgiBundle(properties);
                        if (executionContexts.containsKey(osgiBundle)) {
-                               executionContexts.remove(osgiBundle);
+                               ExecutionContext executionContext = executionContexts
+                                               .remove(osgiBundle);
                                if (log.isTraceEnabled())
                                        log.debug("Removed execution context from " + osgiBundle);
+                               // Notify
+                               for (ExecutionModulesListener listener : getExecutionModulesListeners())
+                                       listener.executionModuleRemoved(osgiBundle,
+                                                       executionContext);
                        }
                } else if (service instanceof ExecutionFlow) {
                        ExecutionFlow executionFlow = (ExecutionFlow) service;
                        OsgiBundle osgiBundle = asOsgiBundle(properties);
                        if (executionFlows.containsKey(osgiBundle)) {
-                               executionFlows.get(osgiBundle).remove(executionFlow);
+                               Set flows = executionFlows.get(osgiBundle);
+                               flows.remove(executionFlow);
                                if (log.isTraceEnabled())
                                        log.debug("Removed " + executionFlow + " from "
                                                        + osgiBundle);
+                               if (flows.size() == 0) {
+                                       executionFlows.remove(osgiBundle);
+                                       if (log.isTraceEnabled())
+                                               log.debug("Removed flows set from " + osgiBundle);
+                               }
+                               for (ExecutionModulesListener listener : getExecutionModulesListeners())
+                                       listener.executionFlowRemoved(osgiBundle, executionFlow);
                        }
                } else if (service instanceof ExecutionFlowDescriptorConverter) {
                        OsgiBundle osgiBundle = asOsgiBundle(properties);