1 package org
.argeo
.init
.osgi
;
3 import java
.lang
.System
.Logger
;
4 import java
.lang
.System
.Logger
.Level
;
5 import java
.lang
.reflect
.InvocationTargetException
;
6 import java
.util
.Objects
;
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
;
17 * An OSGi configurator. See
18 * <a href="http://wiki.eclipse.org/Configurator">http:
19 * //wiki.eclipse.org/Configurator</a>
21 public class InitActivator
implements BundleActivator
{
23 // must be called first
24 ThinLoggerFinder
.lazyInit();
26 private final static Logger logger
= System
.getLogger(InitActivator
.class.getName());
28 private Long checkpoint
= null;
30 // TODO use framework factory SR
32 // private boolean argeoInit = false;
33 /** Not null if we created it ourselves. */
34 private OsgiRuntimeContext runtimeContext
;
35 private ServiceRegistration
<ConnectFrameworkFactory
> frameworkFactorySr
= null;
37 private static OsgiRuntimeManager runtimeManager
;
39 public void start(final BundleContext bundleContext
) throws Exception
{
40 ConnectFrameworkFactory frameworkFactory
= OsgiRuntimeContext
.getFrameworkFactory(bundleContext
);
41 if (frameworkFactory
== null) {
43 frameworkFactory
= newFrameworkFactory();
44 frameworkFactorySr
= bundleContext
.registerService(ConnectFrameworkFactory
.class, frameworkFactory
, null);
47 // The OSGi runtime was created by us, and therefore already initialized
48 // argeoInit = Boolean.parseBoolean(bundleContext.getProperty(ServiceMain.PROP_ARGEO_INIT_MAIN));
50 if (runtimeContext
== null) {
51 runtimeContext
= new OsgiRuntimeContext(bundleContext
);
52 logger
.log(Level
.DEBUG
, () -> "Argeo init via OSGi activator");
56 // Thread adminThread = new AdminThread(bundleContext);
57 // adminThread.start();
60 // OsgiBoot osgiBoot = new OsgiBoot(bundleContext);
61 if (checkpoint
== null) {
62 // osgiBoot.bootstrap();
63 checkpoint
= System
.currentTimeMillis();
65 runtimeContext
.update();
66 checkpoint
= System
.currentTimeMillis();
70 if (runtimeManager
!= null)
71 throw new IllegalArgumentException("Runtime manager is already set");
72 runtimeManager
= new OsgiRuntimeManager(bundleContext
);
76 public void stop(BundleContext context
) throws Exception
{
78 frameworkFactorySr
.unregister();
79 Objects
.nonNull(runtimeContext
);
80 runtimeContext
.stop(context
);
81 runtimeContext
= null;
83 runtimeManager
= null;
86 /** Whether it wa sinitialised by an Argeo Init main class. */
87 private boolean isArgeoInit() {
88 return frameworkFactorySr
== null;
91 public static RuntimeManager
getRuntimeManager() {
92 return runtimeManager
;
96 * Workaround to explicitly instantiate an Equinox
97 * {@link ConnectFrameworkFactory} when running in a pure OSGi runtime.
99 private ConnectFrameworkFactory
newFrameworkFactory() {
100 final String EQUINOX_FRAMEWORK_FACTORY_CLASS
= "org.eclipse.osgi.launch.EquinoxFactory";
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
);