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
.BundleContext
;
15 import org
.osgi
.framework
.BundleException
;
16 import org
.osgi
.framework
.Constants
;
17 import org
.osgi
.framework
.ServiceRegistration
;
18 import org
.osgi
.framework
.launch
.Framework
;
19 import org
.osgi
.framework
.launch
.FrameworkFactory
;
21 /** An OSGi runtime context. */
22 public class OsgiRuntimeContext
implements RuntimeContext
{
23 private Map
<String
, String
> config
;
24 private Framework framework
;
25 private OsgiBoot osgiBoot
;
27 @SuppressWarnings("rawtypes")
28 private ServiceRegistration
<Consumer
> loggingConfigurationSr
;
29 @SuppressWarnings("rawtypes")
30 private ServiceRegistration
<Flow
.Publisher
> logEntryPublisherSr
;
32 public OsgiRuntimeContext(Map
<String
, String
> config
) {
36 public OsgiRuntimeContext(BundleContext bundleContext
) {
42 ServiceLoader
<FrameworkFactory
> sl
= ServiceLoader
.load(FrameworkFactory
.class);
43 Optional
<FrameworkFactory
> opt
= sl
.findFirst();
45 throw new IllegalStateException("Cannot find OSGi framework");
46 framework
= opt
.get().newFramework(config
);
49 BundleContext bundleContext
= framework
.getBundleContext();
51 } catch (BundleException e
) {
52 throw new IllegalStateException("Cannot start OSGi framework", e
);
56 public void start(BundleContext bundleContext
) {
58 loggingConfigurationSr
= bundleContext
.registerService(Consumer
.class,
59 ThinLoggerFinder
.getConfigurationConsumer(),
60 new Hashtable
<>(Collections
.singletonMap(Constants
.SERVICE_PID
, "argeo.logging.configuration")));
61 logEntryPublisherSr
= bundleContext
.registerService(Flow
.Publisher
.class,
62 ThinLoggerFinder
.getLogEntryPublisher(),
63 new Hashtable
<>(Collections
.singletonMap(Constants
.SERVICE_PID
, "argeo.logging.publisher")));
65 osgiBoot
= new OsgiBoot(bundleContext
);
70 public void update() {
71 Objects
.requireNonNull(osgiBoot
);
75 public void stop(BundleContext bundleContext
) {
76 if (loggingConfigurationSr
!= null)
77 loggingConfigurationSr
.unregister();
78 if (logEntryPublisherSr
!= null)
79 logEntryPublisherSr
.unregister();
84 public void waitForStop(long timeout
) throws InterruptedException
{
85 if (framework
== null)
86 throw new IllegalStateException("Framework is not initialised");
87 stop(framework
.getBundleContext());
88 framework
.waitForStop(timeout
);
92 public void close() throws Exception
{
93 if (framework
!= null)