1 package org
.argeo
.init
.osgi
;
3 import java
.lang
.System
.LoggerFinder
;
4 import java
.util
.Collections
;
5 import java
.util
.Hashtable
;
7 import java
.util
.Objects
;
8 import java
.util
.Optional
;
9 import java
.util
.ServiceLoader
;
10 import java
.util
.concurrent
.Flow
;
11 import java
.util
.function
.Consumer
;
13 import org
.argeo
.init
.RuntimeContext
;
14 import org
.argeo
.init
.logging
.ThinLoggerFinder
;
15 import org
.osgi
.framework
.Bundle
;
16 import org
.osgi
.framework
.BundleContext
;
17 import org
.osgi
.framework
.BundleException
;
18 import org
.osgi
.framework
.Constants
;
19 import org
.osgi
.framework
.launch
.Framework
;
20 import org
.osgi
.framework
.launch
.FrameworkFactory
;
22 /** An OSGi runtime context. */
23 public class OsgiRuntimeContext
implements RuntimeContext
, AutoCloseable
{
24 private Map
<String
, String
> config
;
25 private Framework framework
;
26 private OsgiBoot osgiBoot
;
29 * Constructor to use when the runtime context will create the OSGi
32 public OsgiRuntimeContext(Map
<String
, String
> config
) {
37 * Constructor to use when the OSGi {@link Framework} has been created by other
40 OsgiRuntimeContext(BundleContext bundleContext
) {
46 ServiceLoader
<FrameworkFactory
> sl
= ServiceLoader
.load(FrameworkFactory
.class);
47 Optional
<FrameworkFactory
> opt
= sl
.findFirst();
49 throw new IllegalStateException("Cannot find OSGi framework");
50 framework
= opt
.get().newFramework(config
);
53 BundleContext bundleContext
= framework
.getBundleContext();
55 } catch (BundleException e
) {
56 throw new IllegalStateException("Cannot start OSGi framework", e
);
60 public void start(BundleContext bundleContext
) {
62 // SystemRootPreferences systemRootPreferences = ThinPreferencesFactory.getInstance().getSystemRootPreferences();
63 // bundleContext.registerService(AbstractPreferences.class, systemRootPreferences, new Hashtable<>());
65 // Make sure LoggerFinder has been searched for, since it is lazily loaded
66 LoggerFinder
.getLoggerFinder();
69 bundleContext
.registerService(Consumer
.class, ThinLoggerFinder
.getConfigurationConsumer(),
70 new Hashtable
<>(Collections
.singletonMap(Constants
.SERVICE_PID
, "argeo.logging.configuration")));
71 bundleContext
.registerService(Flow
.Publisher
.class, ThinLoggerFinder
.getLogEntryPublisher(),
72 new Hashtable
<>(Collections
.singletonMap(Constants
.SERVICE_PID
, "argeo.logging.publisher")));
74 osgiBoot
= new OsgiBoot(bundleContext
);
75 osgiBoot
.bootstrap(config
);
79 public void update() {
80 Objects
.requireNonNull(osgiBoot
);
84 public void stop(BundleContext bundleContext
) {
85 // if (loggingConfigurationSr != null)
87 // loggingConfigurationSr.unregister();
88 // } catch (Exception e) {
91 // if (logEntryPublisherSr != null)
93 // logEntryPublisherSr.unregister();
94 // } catch (Exception e) {
100 public void waitForStop(long timeout
) throws InterruptedException
{
101 if (framework
== null)
102 throw new IllegalStateException("Framework is not initialised");
104 framework
.waitForStop(timeout
);
107 public void close() throws Exception
{
108 // TODO make shutdown of dynamic service more robust
109 Bundle scrBundle
= osgiBoot
.getBundlesBySymbolicName().get("org.apache.felix.scr");
110 if (scrBundle
!= null) {
112 while (!(scrBundle
.getState() <= Bundle
.RESOLVED
)) {
118 stop(framework
.getBundleContext());
119 if (framework
!= null)
124 public Framework
getFramework() {