]> git.argeo.org Git - lgpl/argeo-commons.git/blob - OsgiRuntimeContext.java
6c3e40ce57eadf8189f9aa8c8687c4c3d2c1c27e
[lgpl/argeo-commons.git] / OsgiRuntimeContext.java
1 package org.argeo.init.osgi;
2
3 import java.util.Collections;
4 import java.util.Hashtable;
5 import java.util.Map;
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;
11
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;
20
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;
26
27 @SuppressWarnings("rawtypes")
28 private ServiceRegistration<Consumer> loggingConfigurationSr;
29 @SuppressWarnings("rawtypes")
30 private ServiceRegistration<Flow.Publisher> logEntryPublisherSr;
31
32 public OsgiRuntimeContext(Map<String, String> config) {
33 this.config = config;
34 }
35
36 public OsgiRuntimeContext(BundleContext bundleContext) {
37 start(bundleContext);
38 }
39
40 @Override
41 public void run() {
42 ServiceLoader<FrameworkFactory> sl = ServiceLoader.load(FrameworkFactory.class);
43 Optional<FrameworkFactory> opt = sl.findFirst();
44 if (opt.isEmpty())
45 throw new IllegalStateException("Cannot find OSGi framework");
46 framework = opt.get().newFramework(config);
47 try {
48 framework.start();
49 BundleContext bundleContext = framework.getBundleContext();
50 start(bundleContext);
51 } catch (BundleException e) {
52 throw new IllegalStateException("Cannot start OSGi framework", e);
53 }
54 }
55
56 public void start(BundleContext bundleContext) {
57 // logging
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")));
64
65 osgiBoot = new OsgiBoot(bundleContext);
66 osgiBoot.bootstrap();
67
68 }
69
70 public void update() {
71 Objects.requireNonNull(osgiBoot);
72 osgiBoot.update();
73 }
74
75 public void stop(BundleContext bundleContext) {
76 // if (loggingConfigurationSr != null)
77 // try {
78 // loggingConfigurationSr.unregister();
79 // } catch (Exception e) {
80 // // silent
81 // }
82 // if (logEntryPublisherSr != null)
83 // try {
84 // logEntryPublisherSr.unregister();
85 // } catch (Exception e) {
86 // // silent
87 // }
88 }
89
90 @Override
91 public void waitForStop(long timeout) throws InterruptedException {
92 if (framework == null)
93 throw new IllegalStateException("Framework is not initialised");
94 framework.waitForStop(timeout);
95 }
96
97 @Override
98 public void close() throws Exception {
99 stop(framework.getBundleContext());
100 if (framework != null)
101 framework.stop();
102 }
103
104 }