X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=runtime%2Forg.argeo.slc.support.osgi%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fslc%2Fosgi%2FOsgiExecutionModulesManager.java;h=58ae32a655ee9f7f521c98f7473a2a01636b8d03;hb=7b8669f41556f46a0f229192d05e2f2592c57d5f;hp=15a3f0de9bbcb27b56d75ad22fdb558e1975ce0a;hpb=699640c58cc0578beebfc3edd00dac9eed65ee0b;p=gpl%2Fargeo-slc.git diff --git a/runtime/org.argeo.slc.support.osgi/src/main/java/org/argeo/slc/osgi/OsgiExecutionModulesManager.java b/runtime/org.argeo.slc.support.osgi/src/main/java/org/argeo/slc/osgi/OsgiExecutionModulesManager.java index 15a3f0de9..58ae32a65 100644 --- a/runtime/org.argeo.slc.support.osgi/src/main/java/org/argeo/slc/osgi/OsgiExecutionModulesManager.java +++ b/runtime/org.argeo.slc.support.osgi/src/main/java/org/argeo/slc/osgi/OsgiExecutionModulesManager.java @@ -47,7 +47,9 @@ 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.BundleEvent; import org.osgi.framework.BundleException; +import org.osgi.framework.BundleListener; import org.osgi.framework.Constants; import org.osgi.framework.launch.Framework; import org.springframework.context.ApplicationContext; @@ -55,7 +57,8 @@ import org.springframework.osgi.service.importer.OsgiServiceLifecycleListener; /** Execution modules manager implementation based on an OSGi runtime. */ public class OsgiExecutionModulesManager extends - AbstractExecutionModulesManager implements OsgiServiceLifecycleListener { + AbstractExecutionModulesManager implements + OsgiServiceLifecycleListener, BundleListener { private final static Log log = LogFactory .getLog(OsgiExecutionModulesManager.class); @@ -68,9 +71,11 @@ public class OsgiExecutionModulesManager extends private List executionModulesListeners = new ArrayList(); - private Boolean registerFlowsToJmx = true; + private Boolean registerFlowsToJmx = false; public void init() throws Exception { + bundlesManager.getBundleContext().addBundleListener(this); + final String module = System.getProperty(UNIQUE_LAUNCH_MODULE_PROPERTY); final String flow = System.getProperty(UNIQUE_LAUNCH_FLOW_PROPERTY); if (module != null) { @@ -78,48 +83,51 @@ 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 { - 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) { - 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 { - // shutdown framework - framework.stop(); - // wait 1 min for shutdown - framework.waitForStop(60 * 1000); - // close VM - System.exit(0); - } catch (Exception e) { - e.printStackTrace(); - System.exit(1); - } - } + executeFlowAndExit(module, null, flow); } }.start(); } - } public void destroy() { + bundlesManager.getBundleContext().removeBundleListener(this); + } + /** Executes a single flow and stops the JVM */ + protected void executeFlowAndExit(final String module, + final String version, final String flow) { + if (log.isDebugEnabled()) + log.debug("Launch unique flow " + flow + " from module " + module); + try { + 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) { + 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 { + // shutdown framework + framework.stop(); + // wait 1 min for shutdown + framework.waitForStop(60 * 1000); + // close VM + System.exit(0); + } catch (Exception e) { + e.printStackTrace(); + System.exit(1); + } + } } public synchronized ExecutionModuleDescriptor getExecutionModuleDescriptor( @@ -383,8 +391,10 @@ public class OsgiExecutionModulesManager extends if (log.isTraceEnabled()) log.trace("Registered execution context from " + osgiBundle); // Notify + ModuleDescriptor md = osgiBundle.getModuleDescriptor(); + md.setStarted(true); for (ExecutionModulesListener listener : executionModulesListeners) - listener.executionModuleAdded(osgiBundle.getModuleDescriptor()); + listener.executionModuleAdded(md); } /** Unregisters an execution context. */ @@ -396,9 +406,10 @@ public class OsgiExecutionModulesManager extends if (log.isTraceEnabled()) log.trace("Removed execution context from " + osgiBundle); // Notify + ModuleDescriptor md = osgiBundle.getModuleDescriptor(); + md.setStarted(false); for (ExecutionModulesListener listener : executionModulesListeners) - listener.executionModuleRemoved(osgiBundle - .getModuleDescriptor()); + listener.executionModuleRemoved(md); } } @@ -473,6 +484,27 @@ public class OsgiExecutionModulesManager extends executionModulesListeners.remove(executionModulesListener); } + /* + * INTERFACE IMPLEMENTATIONS + */ + + public void bundleChanged(BundleEvent evt) { + Bundle bundle = evt.getBundle(); + if (bundle.getHeaders().get( + ExecutionModuleDescriptor.SLC_EXECUTION_MODULE) != null) { + OsgiBundle osgiBundle = new OsgiBundle(bundle); + if (evt.getType() == BundleEvent.INSTALLED) + for (ExecutionModulesListener listener : executionModulesListeners) + listener.executionModuleAdded(osgiBundle + .getModuleDescriptor()); + else if (evt.getType() == BundleEvent.UNINSTALLED) + for (ExecutionModulesListener listener : executionModulesListeners) + listener.executionModuleRemoved(osgiBundle + .getModuleDescriptor()); + } + + } + @SuppressWarnings({ "rawtypes" }) public synchronized void bind(Object service, Map properties) throws Exception {