]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.init/src/org/argeo/init/osgi/InitActivator.java
Simplify multi-runtime
[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.util.Objects;
6
7 import org.argeo.api.init.RuntimeManager;
8 import org.argeo.init.ServiceMain;
9 import org.argeo.init.logging.ThinLoggerFinder;
10 import org.osgi.framework.BundleActivator;
11 import org.osgi.framework.BundleContext;
12
13 /**
14 * An OSGi configurator. See
15 * <a href="http://wiki.eclipse.org/Configurator">http:
16 * //wiki.eclipse.org/Configurator</a>
17 */
18 public class InitActivator implements BundleActivator {
19 static {
20 // must be called first
21 ThinLoggerFinder.lazyInit();
22 }
23 private final static Logger logger = System.getLogger(InitActivator.class.getName());
24
25 private Long checkpoint = null;
26
27 private boolean argeoInit = false;
28 /** Not null if we created it ourselves. */
29 private OsgiRuntimeContext runtimeContext;
30
31 private static OsgiRuntimeManager runtimeManager;
32
33 public void start(final BundleContext bundleContext) throws Exception {
34 // The OSGi runtime was created by us, and therefore already initialized
35 argeoInit = Boolean.parseBoolean(bundleContext.getProperty(ServiceMain.PROP_ARGEO_INIT_MAIN));
36 if (!argeoInit) {
37 if (runtimeContext == null) {
38 runtimeContext = new OsgiRuntimeContext(bundleContext);
39 logger.log(Level.DEBUG, () -> "Argeo init via OSGi activator");
40 }
41
42 // admin thread
43 // Thread adminThread = new AdminThread(bundleContext);
44 // adminThread.start();
45
46 // bootstrap
47 // OsgiBoot osgiBoot = new OsgiBoot(bundleContext);
48 if (checkpoint == null) {
49 // osgiBoot.bootstrap();
50 checkpoint = System.currentTimeMillis();
51 } else {
52 runtimeContext.update();
53 checkpoint = System.currentTimeMillis();
54 }
55 }
56
57 if (runtimeManager != null)
58 throw new IllegalArgumentException("Runtime manager is already set");
59 runtimeManager = new OsgiRuntimeManager(bundleContext);
60 }
61
62 public void stop(BundleContext context) throws Exception {
63 if (!argeoInit) {
64 Objects.nonNull(runtimeContext);
65 runtimeContext.stop(context);
66 runtimeContext = null;
67 }
68 runtimeManager = null;
69 }
70
71 public static RuntimeManager getRuntimeManager() {
72 return runtimeManager;
73 }
74
75 }