]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - runtime/org.argeo.slc.support.osgi/src/main/java/org/argeo/slc/osgi/OsgiExecutionModulesManager.java
Introduce module meta data
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.osgi / src / main / java / org / argeo / slc / osgi / OsgiExecutionModulesManager.java
index c5d23589ef03ee09c5a53429359a7e36a412567d..15a3f0de9bbcb27b56d75ad22fdb558e1975ce0a 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
+ * Copyright (C) 2007-2012 Mathieu Baudier
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-
 package org.argeo.slc.osgi;
 
 import java.lang.management.ManagementFactory;
 import java.util.ArrayList;
+import java.util.Dictionary;
+import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Iterator;
@@ -48,6 +49,8 @@ import org.argeo.slc.process.RealizedFlow;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleException;
 import org.osgi.framework.Constants;
+import org.osgi.framework.launch.Framework;
+import org.springframework.context.ApplicationContext;
 import org.springframework.osgi.service.importer.OsgiServiceLifecycleListener;
 
 /** Execution modules manager implementation based on an OSGi runtime. */
@@ -75,26 +78,37 @@ public class OsgiExecutionModulesManager extends
                        new Thread("Unique Flow") {
                                @Override
                                public void run() {
+                                       if (log.isDebugEnabled())
+                                               log.debug("Launch unique flow " + flow
+                                                               + " from module " + module);
                                        try {
-                                               bundlesManager.startSynchronous(bundlesManager
-                                                               .findRelatedBundle(bundlesManager
-                                                                               .findFromPattern(module)));
+                                               OsgiBundle osgiBundle = bundlesManager
+                                                               .findFromPattern(module);
+                                               Bundle moduleBundle = bundlesManager
+                                                               .findRelatedBundle(osgiBundle);
+                                               bundlesManager.startSynchronous(moduleBundle);
                                                RealizedFlow lastLaunch = findRealizedFlow(module, flow);
                                                if (lastLaunch == null)
                                                        throw new SlcException("Cannot find launch for "
                                                                        + module + " " + flow);
                                                execute(lastLaunch);
                                        } catch (Exception e) {
-                                               throw new SlcException(
-                                                               "Error when executing unique flow " + flow
-                                                                               + " on " + module, e);
+                                               log.error("Error in unique flow " + flow
+                                                               + " from module " + module, e);
                                        } finally {
+                                               if (log.isDebugEnabled())
+                                                       log.debug("Shutdown OSGi runtime...");
+                                               Framework framework = (Framework) bundlesManager
+                                                               .getBundleContext().getBundle(0);
                                                try {
-                                                       bundlesManager.getBundleContext().getBundle(0)
-                                                                       .stop();
+                                                       // shutdown framework
+                                                       framework.stop();
+                                                       // wait 1 min for shutdown
+                                                       framework.waitForStop(60 * 1000);
+                                                       // close VM
                                                        System.exit(0);
                                                } catch (Exception e) {
-                                                       log.error("Cannot shutdown equinox.", e);
+                                                       e.printStackTrace();
                                                        System.exit(1);
                                                }
                                        }
@@ -170,7 +184,7 @@ public class OsgiExecutionModulesManager extends
                String filter = "(&(Bundle-SymbolicName=" + moduleName
                                + ")(org.springframework.osgi.bean.name=" + flowName + "))";
                return bundlesManager.getSingleServiceStrict(ExecutionFlow.class,
-                               filter);
+                               filter, true);
        }
 
        protected ExecutionContext findExecutionContext(String moduleName,
@@ -178,7 +192,7 @@ public class OsgiExecutionModulesManager extends
                String filter = "(&(Bundle-SymbolicName=" + moduleName
                                + ")(Bundle-Version=" + moduleVersion + "))";
                return bundlesManager.getSingleServiceStrict(ExecutionContext.class,
-                               filter);
+                               filter, true);
        }
 
        protected ExecutionFlowDescriptorConverter findExecutionFlowDescriptorConverter(
@@ -270,7 +284,23 @@ public class OsgiExecutionModulesManager extends
                        Bundle bundle = bundlesManager.findRelatedBundle(new OsgiBundle(
                                        nameVersion));
                        bundlesManager.startSynchronous(bundle);
-               } catch (BundleException e) {
+                       boolean isSpringInstrumented = bundle.getEntryPaths(
+                                       "/META-INF/spring").hasMoreElements()
+                                       || bundle.getHeaders().get("Spring-Context") == null;
+                       if (isSpringInstrumented) {
+                               // Wait for Spring application context to be ready
+                               String filter = "(Bundle-SymbolicName="
+                                               + bundle.getSymbolicName() + ")";
+                               try {
+                                       bundlesManager.getServiceRefSynchronous(
+                                                       ApplicationContext.class.getName(), filter);
+                               } catch (Exception e) {
+                                       // stop if application context not found
+                                       bundle.stop();
+                                       throw e;
+                               }
+                       }
+               } catch (Exception e) {
                        throw new SlcException("Cannot start " + nameVersion, e);
                }
        }
@@ -310,6 +340,23 @@ public class OsgiExecutionModulesManager extends
                md.setVersion(getHeaderSafe(bdl, Constants.BUNDLE_VERSION));
                md.setTitle(getHeaderSafe(bdl, Constants.BUNDLE_NAME));
                md.setDescription(getHeaderSafe(bdl, Constants.BUNDLE_DESCRIPTION));
+
+               // copy manifets header to meta data
+               Dictionary<?, ?> headers = bundle.getHeaders();
+               Enumeration<?> keys = headers.keys();
+               while (keys.hasMoreElements()) {
+                       Object key = keys.nextElement();
+                       Object value = headers.get(key);
+                       if (value != null)
+                               md.getMetadata().put(key.toString(), value.toString());
+               }
+
+               // check if started
+               if (bundle.getState() == Bundle.ACTIVE
+                               || bundle.getState() == Bundle.STARTING)
+                       md.setStarted(true);
+               else
+                       md.setStarted(false);
        }
 
        private String getHeaderSafe(Bundle bundle, Object key) {
@@ -496,7 +543,7 @@ public class OsgiExecutionModulesManager extends
                String path = executionFlow.getPath();
                String name = executionFlow.getName();
                if (path == null && name.indexOf('/') >= 0) {
-                       path = name.substring(0, name.lastIndexOf('/') - 1);
+                       path = name.substring(0, name.lastIndexOf('/'));
                        name = name.substring(name.lastIndexOf('/'));
                }