]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.init/src/org/argeo/init/osgi/OsgiRuntimeContext.java
Introduce cms ping command
[lgpl/argeo-commons.git] / org.argeo.init / src / org / argeo / init / osgi / OsgiRuntimeContext.java
1 package org.argeo.init.osgi;
2
3 import java.lang.System.LoggerFinder;
4 import java.util.Collections;
5 import java.util.Hashtable;
6 import java.util.Map;
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;
12
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.launch.Framework;
20 import org.osgi.framework.launch.FrameworkFactory;
21
22 /** An OSGi runtime context. */
23 public class OsgiRuntimeContext implements RuntimeContext, AutoCloseable {
24 private Map<String, String> config;
25 private Framework framework;
26 private OsgiBoot osgiBoot;
27
28 /**
29 * Constructor to use when the runtime context will create the OSGi
30 * {@link Framework}.
31 */
32 public OsgiRuntimeContext(Map<String, String> config) {
33 this.config = config;
34 }
35
36 /**
37 * Constructor to use when the OSGi {@link Framework} has been created by other
38 * means.
39 */
40 OsgiRuntimeContext(BundleContext bundleContext) {
41 start(bundleContext);
42 }
43
44 @Override
45 public void run() {
46 ServiceLoader<FrameworkFactory> sl = ServiceLoader.load(FrameworkFactory.class);
47 Optional<FrameworkFactory> opt = sl.findFirst();
48 if (opt.isEmpty())
49 throw new IllegalStateException("Cannot find OSGi framework");
50 framework = opt.get().newFramework(config);
51 try {
52 framework.start();
53 BundleContext bundleContext = framework.getBundleContext();
54 start(bundleContext);
55 } catch (BundleException e) {
56 throw new IllegalStateException("Cannot start OSGi framework", e);
57 }
58 }
59
60 public void start(BundleContext bundleContext) {
61 // preferences
62 // SystemRootPreferences systemRootPreferences = ThinPreferencesFactory.getInstance().getSystemRootPreferences();
63 // bundleContext.registerService(AbstractPreferences.class, systemRootPreferences, new Hashtable<>());
64
65 // Make sure LoggerFinder has been searched for, since it is lazily loaded
66 LoggerFinder.getLoggerFinder();
67
68 // logging
69 bundleContext.registerService(Consumer.class, ThinLoggerFinder.getConfigurationConsumer(),
70 new Hashtable<>(Collections.singletonMap(Constants.SERVICE_PID, "argeo.logging.configuration")));
71 bundleContext.registerService(Flow.Publisher.class, ThinLoggerFinder.getLogEntryPublisher(),
72 new Hashtable<>(Collections.singletonMap(Constants.SERVICE_PID, "argeo.logging.publisher")));
73
74 osgiBoot = new OsgiBoot(bundleContext);
75 osgiBoot.bootstrap(config);
76
77 }
78
79 public void update() {
80 Objects.requireNonNull(osgiBoot);
81 osgiBoot.update();
82 }
83
84 public void stop(BundleContext bundleContext) {
85 // if (loggingConfigurationSr != null)
86 // try {
87 // loggingConfigurationSr.unregister();
88 // } catch (Exception e) {
89 // // silent
90 // }
91 // if (logEntryPublisherSr != null)
92 // try {
93 // logEntryPublisherSr.unregister();
94 // } catch (Exception e) {
95 // // silent
96 // }
97 }
98
99 @Override
100 public void waitForStop(long timeout) throws InterruptedException {
101 if (framework == null)
102 throw new IllegalStateException("Framework is not initialised");
103
104 framework.waitForStop(timeout);
105 }
106
107 public void close() throws Exception {
108 // TODO make shutdown of dynamic service more robust
109 Bundle scrBundle = osgiBoot.getBundlesBySymbolicName().get("org.apache.felix.scr");
110 if (scrBundle != null) {
111 scrBundle.stop();
112 while (!(scrBundle.getState() <= Bundle.RESOLVED)) {
113 Thread.sleep(500);
114 }
115 Thread.sleep(1000);
116 }
117
118 stop(framework.getBundleContext());
119 if (framework != null)
120 framework.stop();
121
122 }
123
124 public Framework getFramework() {
125 return framework;
126 }
127
128 }