1 package org
.argeo
.init
.osgi
;
3 import java
.util
.Collections
;
4 import java
.util
.Hashtable
;
6 import java
.util
.Objects
;
7 import java
.util
.Optional
;
8 import java
.util
.ServiceLoader
;
9 import java
.util
.concurrent
.Flow
;
10 import java
.util
.function
.Consumer
;
12 import org
.argeo
.init
.RuntimeContext
;
13 import org
.argeo
.init
.logging
.ThinLoggerFinder
;
14 import org
.osgi
.framework
.Bundle
;
15 import org
.osgi
.framework
.BundleContext
;
16 import org
.osgi
.framework
.BundleException
;
17 import org
.osgi
.framework
.Constants
;
18 import org
.osgi
.framework
.ServiceRegistration
;
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
{
24 private Map
<String
, String
> config
;
25 private Framework framework
;
26 private OsgiBoot osgiBoot
;
28 @SuppressWarnings("rawtypes")
29 private ServiceRegistration
<Consumer
> loggingConfigurationSr
;
30 @SuppressWarnings("rawtypes")
31 private ServiceRegistration
<Flow
.Publisher
> logEntryPublisherSr
;
33 public OsgiRuntimeContext(Map
<String
, String
> config
) {
37 public OsgiRuntimeContext(BundleContext bundleContext
) {
43 ServiceLoader
<FrameworkFactory
> sl
= ServiceLoader
.load(FrameworkFactory
.class);
44 Optional
<FrameworkFactory
> opt
= sl
.findFirst();
46 throw new IllegalStateException("Cannot find OSGi framework");
47 framework
= opt
.get().newFramework(config
);
50 BundleContext bundleContext
= framework
.getBundleContext();
52 } catch (BundleException e
) {
53 throw new IllegalStateException("Cannot start OSGi framework", e
);
57 public void start(BundleContext bundleContext
) {
59 loggingConfigurationSr
= bundleContext
.registerService(Consumer
.class,
60 ThinLoggerFinder
.getConfigurationConsumer(),
61 new Hashtable
<>(Collections
.singletonMap(Constants
.SERVICE_PID
, "argeo.logging.configuration")));
62 logEntryPublisherSr
= bundleContext
.registerService(Flow
.Publisher
.class,
63 ThinLoggerFinder
.getLogEntryPublisher(),
64 new Hashtable
<>(Collections
.singletonMap(Constants
.SERVICE_PID
, "argeo.logging.publisher")));
66 osgiBoot
= new OsgiBoot(bundleContext
);
71 public void update() {
72 Objects
.requireNonNull(osgiBoot
);
76 public void stop(BundleContext bundleContext
) {
77 // if (loggingConfigurationSr != null)
79 // loggingConfigurationSr.unregister();
80 // } catch (Exception e) {
83 // if (logEntryPublisherSr != null)
85 // logEntryPublisherSr.unregister();
86 // } catch (Exception e) {
92 public void waitForStop(long timeout
) throws InterruptedException
{
93 if (framework
== null)
94 throw new IllegalStateException("Framework is not initialised");
96 framework
.waitForStop(timeout
);
99 public void close() throws Exception
{
100 // TODO make shutdown of dynamic service more robust
101 Bundle scrBundle
= osgiBoot
.getBundlesBySymbolicName().get("org.apache.felix.scr");
102 if (scrBundle
!= null) {
104 while (!(scrBundle
.getState() <= Bundle
.RESOLVED
)) {
110 stop(framework
.getBundleContext());
111 if (framework
!= null)