]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.init/src/org/argeo/init/osgi/InitActivator.java
Improve runtime manager
[lgpl/argeo-commons.git] / org.argeo.init / src / org / argeo / init / osgi / InitActivator.java
1 package org.argeo.init.osgi;
2
3 import java.lang.System.Logger;
4 import java.lang.System.Logger.Level;
5 import java.lang.reflect.InvocationTargetException;
6 import java.util.Objects;
7
8 import org.argeo.api.init.RuntimeManager;
9 import org.argeo.init.logging.ThinLoggerFinder;
10 import org.osgi.framework.BundleActivator;
11 import org.osgi.framework.BundleContext;
12 import org.osgi.framework.ServiceRegistration;
13 import org.osgi.framework.connect.ConnectFrameworkFactory;
14 import org.osgi.framework.launch.Framework;
15
16 /**
17 * An OSGi configurator. See
18 * <a href="http://wiki.eclipse.org/Configurator">http:
19 * //wiki.eclipse.org/Configurator</a>
20 */
21 public class InitActivator implements BundleActivator {
22 static {
23 // must be called first
24 ThinLoggerFinder.lazyInit();
25 }
26 private final static Logger logger = System.getLogger(InitActivator.class.getName());
27
28 private Long checkpoint = null;
29
30 // TODO use framework factory SR
31 // @Deprecated
32 // private boolean argeoInit = false;
33 /** Not null if we created it ourselves. */
34 private OsgiRuntimeContext runtimeContext;
35 private ServiceRegistration<ConnectFrameworkFactory> frameworkFactorySr = null;
36
37 private static OsgiRuntimeManager runtimeManager;
38
39 public void start(final BundleContext bundleContext) throws Exception {
40 ConnectFrameworkFactory frameworkFactory = OsgiRuntimeContext.getFrameworkFactory(bundleContext);
41 if (frameworkFactory == null) {
42 // argeoInit = false;
43 frameworkFactory = newFrameworkFactory();
44 frameworkFactorySr = bundleContext.registerService(ConnectFrameworkFactory.class, frameworkFactory, null);
45 }
46
47 // The OSGi runtime was created by us, and therefore already initialized
48 // argeoInit = Boolean.parseBoolean(bundleContext.getProperty(ServiceMain.PROP_ARGEO_INIT_MAIN));
49 if (!isArgeoInit()) {
50 if (runtimeContext == null) {
51 runtimeContext = new OsgiRuntimeContext(bundleContext);
52 logger.log(Level.DEBUG, () -> "Argeo init via OSGi activator");
53 }
54
55 // admin thread
56 // Thread adminThread = new AdminThread(bundleContext);
57 // adminThread.start();
58
59 // bootstrap
60 // OsgiBoot osgiBoot = new OsgiBoot(bundleContext);
61 if (checkpoint == null) {
62 // osgiBoot.bootstrap();
63 checkpoint = System.currentTimeMillis();
64 } else {
65 runtimeContext.update();
66 checkpoint = System.currentTimeMillis();
67 }
68 } else {
69
70 if (runtimeManager != null)
71 throw new IllegalArgumentException("Runtime manager is already set");
72 runtimeManager = new OsgiRuntimeManager(bundleContext);
73 }
74 }
75
76 public void stop(BundleContext context) throws Exception {
77 if (!isArgeoInit()) {
78 frameworkFactorySr.unregister();
79 Objects.nonNull(runtimeContext);
80 runtimeContext.stop(context);
81 runtimeContext = null;
82 }
83 runtimeManager = null;
84 }
85
86 /** Whether it wa sinitialised by an Argeo Init main class. */
87 private boolean isArgeoInit() {
88 return frameworkFactorySr == null;
89 }
90
91 public static RuntimeManager getRuntimeManager() {
92 return runtimeManager;
93 }
94
95 /**
96 * Workaround to explicitly instantiate an Equinox
97 * {@link ConnectFrameworkFactory} when running in a pure OSGi runtime.
98 */
99 private ConnectFrameworkFactory newFrameworkFactory() {
100 final String EQUINOX_FRAMEWORK_FACTORY_CLASS = "org.eclipse.osgi.launch.EquinoxFactory";
101 try {
102 @SuppressWarnings("unchecked")
103 Class<? extends ConnectFrameworkFactory> frameworkFactoryClass = (Class<? extends ConnectFrameworkFactory>) Framework.class
104 .getClassLoader().loadClass(EQUINOX_FRAMEWORK_FACTORY_CLASS);
105 return frameworkFactoryClass.getConstructor().newInstance();
106 } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | IllegalArgumentException
107 | InvocationTargetException | NoSuchMethodException | SecurityException e) {
108 throw new IllegalStateException("Cannot create OSGi framework factory", e);
109 }
110 }
111 }