]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.init/src/org/argeo/init/osgi/OsgiRuntimeContext.java
Remove unused configurations in build.properties
[lgpl/argeo-commons.git] / org.argeo.init / src / org / argeo / init / osgi / 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 loggingConfigurationSr.unregister();
78 if (logEntryPublisherSr != null)
79 logEntryPublisherSr.unregister();
80
81 }
82
83 @Override
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);
89 }
90
91 @Override
92 public void close() throws Exception {
93 if (framework != null)
94 framework.stop();
95 }
96
97 }