X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.init%2Fsrc%2Forg%2Fargeo%2Finit%2Fosgi%2FOsgiRuntimeContext.java;h=64c588d19a347a0ac90204fc36faa336d59eff97;hb=8321a31f879e6998ed4fe9c70f3cd3faa21e4a0f;hp=6c3e40ce57eadf8189f9aa8c8687c4c3d2c1c27e;hpb=dea32138f55d1c80f75515793ed15be0e89e6d61;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.init/src/org/argeo/init/osgi/OsgiRuntimeContext.java b/org.argeo.init/src/org/argeo/init/osgi/OsgiRuntimeContext.java index 6c3e40ce5..64c588d19 100644 --- a/org.argeo.init/src/org/argeo/init/osgi/OsgiRuntimeContext.java +++ b/org.argeo.init/src/org/argeo/init/osgi/OsgiRuntimeContext.java @@ -1,5 +1,6 @@ package org.argeo.init.osgi; +import java.lang.System.LoggerFinder; import java.util.Collections; import java.util.Hashtable; import java.util.Map; @@ -11,6 +12,7 @@ import java.util.function.Consumer; import org.argeo.init.RuntimeContext; import org.argeo.init.logging.ThinLoggerFinder; +import org.osgi.framework.Bundle; import org.osgi.framework.BundleContext; import org.osgi.framework.BundleException; import org.osgi.framework.Constants; @@ -19,7 +21,7 @@ import org.osgi.framework.launch.Framework; import org.osgi.framework.launch.FrameworkFactory; /** An OSGi runtime context. */ -public class OsgiRuntimeContext implements RuntimeContext { +public class OsgiRuntimeContext implements RuntimeContext, AutoCloseable { private Map config; private Framework framework; private OsgiBoot osgiBoot; @@ -29,11 +31,19 @@ public class OsgiRuntimeContext implements RuntimeContext { @SuppressWarnings("rawtypes") private ServiceRegistration logEntryPublisherSr; + /** + * Constructor to use when the runtime context will create the OSGi + * {@link Framework}. + */ public OsgiRuntimeContext(Map config) { this.config = config; } - public OsgiRuntimeContext(BundleContext bundleContext) { + /** + * Constructor to use when the OSGi {@link Framework} has been created by other + * means. + */ + OsgiRuntimeContext(BundleContext bundleContext) { start(bundleContext); } @@ -54,6 +64,9 @@ public class OsgiRuntimeContext implements RuntimeContext { } public void start(BundleContext bundleContext) { + // Make sure LoggerFinder has been searched for, since it is lazily loaded + LoggerFinder.getLoggerFinder(); + // logging loggingConfigurationSr = bundleContext.registerService(Consumer.class, ThinLoggerFinder.getConfigurationConsumer(), @@ -63,7 +76,7 @@ public class OsgiRuntimeContext implements RuntimeContext { new Hashtable<>(Collections.singletonMap(Constants.SERVICE_PID, "argeo.logging.publisher"))); osgiBoot = new OsgiBoot(bundleContext); - osgiBoot.bootstrap(); + osgiBoot.bootstrap(config); } @@ -91,14 +104,29 @@ public class OsgiRuntimeContext implements RuntimeContext { public void waitForStop(long timeout) throws InterruptedException { if (framework == null) throw new IllegalStateException("Framework is not initialised"); + framework.waitForStop(timeout); } - @Override public void close() throws Exception { + // TODO make shutdown of dynamic service more robust + Bundle scrBundle = osgiBoot.getBundlesBySymbolicName().get("org.apache.felix.scr"); + if (scrBundle != null) { + scrBundle.stop(); + while (!(scrBundle.getState() <= Bundle.RESOLVED)) { + Thread.sleep(500); + } + Thread.sleep(1000); + } + stop(framework.getBundleContext()); if (framework != null) framework.stop(); + + } + + public Framework getFramework() { + return framework; } }