X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.init%2Fsrc%2Forg%2Fargeo%2Finit%2Fosgi%2FOsgiRuntimeContext.java;h=7f44f6b2746dccc914312180b493d56e7328ce3c;hb=1010bff4f1ffadbf3f7988fbd3eba70f5d672c88;hp=91756e32b6759921484c0c1bf6b937408ccd4ab7;hpb=8be06a167888a8ba1f262bbe34c70385807d11c0;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 91756e32b..7f44f6b27 100644 --- a/org.argeo.init/src/org/argeo/init/osgi/OsgiRuntimeContext.java +++ b/org.argeo.init/src/org/argeo/init/osgi/OsgiRuntimeContext.java @@ -1,24 +1,42 @@ package org.argeo.init.osgi; +import java.util.Collections; +import java.util.Hashtable; import java.util.Map; +import java.util.Objects; import java.util.Optional; import java.util.ServiceLoader; +import java.util.concurrent.Flow; +import java.util.function.Consumer; import org.argeo.init.RuntimeContext; +import org.argeo.init.logging.ThinLoggerFinder; import org.osgi.framework.BundleContext; import org.osgi.framework.BundleException; +import org.osgi.framework.Constants; +import org.osgi.framework.ServiceRegistration; import org.osgi.framework.launch.Framework; import org.osgi.framework.launch.FrameworkFactory; +/** An OSGi runtime context. */ public class OsgiRuntimeContext implements RuntimeContext { private Map config; private Framework framework; private OsgiBoot osgiBoot; + @SuppressWarnings("rawtypes") + private ServiceRegistration loggingConfigurationSr; + @SuppressWarnings("rawtypes") + private ServiceRegistration logEntryPublisherSr; + public OsgiRuntimeContext(Map config) { this.config = config; } + public OsgiRuntimeContext(BundleContext bundleContext) { + start(bundleContext); + } + @Override public void run() { ServiceLoader sl = ServiceLoader.load(FrameworkFactory.class); @@ -29,17 +47,44 @@ public class OsgiRuntimeContext implements RuntimeContext { try { framework.start(); BundleContext bundleContext = framework.getBundleContext(); - osgiBoot = new OsgiBoot(bundleContext); - osgiBoot.bootstrap(); + start(bundleContext); } catch (BundleException e) { throw new IllegalStateException("Cannot start OSGi framework", e); } } + public void start(BundleContext bundleContext) { + // logging + loggingConfigurationSr = bundleContext.registerService(Consumer.class, + ThinLoggerFinder.getConfigurationConsumer(), + new Hashtable<>(Collections.singletonMap(Constants.SERVICE_PID, "argeo.logging.configuration"))); + logEntryPublisherSr = bundleContext.registerService(Flow.Publisher.class, + ThinLoggerFinder.getLogEntryPublisher(), + new Hashtable<>(Collections.singletonMap(Constants.SERVICE_PID, "argeo.logging.publisher"))); + + osgiBoot = new OsgiBoot(bundleContext); + osgiBoot.bootstrap(); + + } + + public void update() { + Objects.requireNonNull(osgiBoot); + osgiBoot.update(); + } + + public void stop(BundleContext bundleContext) { + if (loggingConfigurationSr != null) + loggingConfigurationSr.unregister(); + if (logEntryPublisherSr != null) + logEntryPublisherSr.unregister(); + + } + @Override public void waitForStop(long timeout) throws InterruptedException { if (framework == null) throw new IllegalStateException("Framework is not initialised"); + stop(framework.getBundleContext()); framework.waitForStop(timeout); }