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