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
.ServiceRegistration
;
20 import org
.osgi
.framework
.launch
.Framework
;
21 import org
.osgi
.framework
.launch
.FrameworkFactory
;
23 /** An OSGi runtime context. */
24 public class OsgiRuntimeContext
implements RuntimeContext
, AutoCloseable
{
25 private Map
<String
, String
> config
;
26 private Framework framework
;
27 private OsgiBoot osgiBoot
;
29 @SuppressWarnings("rawtypes")
30 private ServiceRegistration
<Consumer
> loggingConfigurationSr
;
31 @SuppressWarnings("rawtypes")
32 private ServiceRegistration
<Flow
.Publisher
> logEntryPublisherSr
;
35 * Constructor to use when the runtime context will create the OSGi
38 public OsgiRuntimeContext(Map
<String
, String
> config
) {
43 * Constructor to use when the OSGi {@link Framework} has been created by other
46 OsgiRuntimeContext(BundleContext bundleContext
) {
52 ServiceLoader
<FrameworkFactory
> sl
= ServiceLoader
.load(FrameworkFactory
.class);
53 Optional
<FrameworkFactory
> opt
= sl
.findFirst();
55 throw new IllegalStateException("Cannot find OSGi framework");
56 framework
= opt
.get().newFramework(config
);
59 BundleContext bundleContext
= framework
.getBundleContext();
61 } catch (BundleException e
) {
62 throw new IllegalStateException("Cannot start OSGi framework", e
);
66 public void start(BundleContext bundleContext
) {
67 // Make sure LoggerFinder has been searched for, since it is lazily loaded
68 LoggerFinder
.getLoggerFinder();
71 loggingConfigurationSr
= bundleContext
.registerService(Consumer
.class,
72 ThinLoggerFinder
.getConfigurationConsumer(),
73 new Hashtable
<>(Collections
.singletonMap(Constants
.SERVICE_PID
, "argeo.logging.configuration")));
74 logEntryPublisherSr
= bundleContext
.registerService(Flow
.Publisher
.class,
75 ThinLoggerFinder
.getLogEntryPublisher(),
76 new Hashtable
<>(Collections
.singletonMap(Constants
.SERVICE_PID
, "argeo.logging.publisher")));
78 osgiBoot
= new OsgiBoot(bundleContext
);
79 osgiBoot
.bootstrap(config
);
83 public void update() {
84 Objects
.requireNonNull(osgiBoot
);
88 public void stop(BundleContext bundleContext
) {
89 // if (loggingConfigurationSr != null)
91 // loggingConfigurationSr.unregister();
92 // } catch (Exception e) {
95 // if (logEntryPublisherSr != null)
97 // logEntryPublisherSr.unregister();
98 // } catch (Exception e) {
104 public void waitForStop(long timeout
) throws InterruptedException
{
105 if (framework
== null)
106 throw new IllegalStateException("Framework is not initialised");
108 framework
.waitForStop(timeout
);
111 public void close() throws Exception
{
112 // TODO make shutdown of dynamic service more robust
113 Bundle scrBundle
= osgiBoot
.getBundlesBySymbolicName().get("org.apache.felix.scr");
114 if (scrBundle
!= null) {
116 while (!(scrBundle
.getState() <= Bundle
.RESOLVED
)) {
122 stop(framework
.getBundleContext());
123 if (framework
!= null)
128 public Framework
getFramework() {