1 package org
.argeo
.init
.osgi
;
3 import static java
.lang
.System
.Logger
.Level
.INFO
;
5 import java
.io
.InputStream
;
6 import java
.lang
.System
.Logger
;
8 import java
.nio
.file
.Path
;
9 import java
.nio
.file
.Paths
;
10 import java
.util
.Collections
;
11 import java
.util
.HashMap
;
12 import java
.util
.Iterator
;
14 import java
.util
.UUID
;
16 import org
.argeo
.api
.init
.InitConstants
;
17 import org
.argeo
.api
.init
.RuntimeManager
;
18 import org
.osgi
.framework
.Bundle
;
19 import org
.osgi
.framework
.BundleActivator
;
20 import org
.osgi
.framework
.BundleContext
;
21 import org
.osgi
.framework
.Constants
;
22 import org
.osgi
.framework
.FrameworkEvent
;
23 import org
.osgi
.framework
.connect
.ConnectFrameworkFactory
;
24 import org
.osgi
.framework
.connect
.ModuleConnector
;
25 import org
.osgi
.framework
.launch
.Framework
;
27 public class SubFrameworkActivator
implements BundleActivator
{
28 private final static Logger logger
= System
.getLogger(SubFrameworkActivator
.class.getName());
30 // private final static String EQUINOX_FRAMEWORK_CLASS = "org.eclipse.osgi.launch.Equinox";
31 private final static String EQUINOX_FRAMEWORK_FACTORY_CLASS
= "org.eclipse.osgi.launch.EquinoxFactory";
33 // private ClassLoader bundleClassLoader;
34 // private ClassLoader subFrameworkClassLoader;
35 private BundleContext foreignBundleContext
;
37 private ConnectFrameworkFactory frameworkFactory
;
39 private Map
<UUID
, Framework
> subFrameworks
= Collections
.synchronizedMap(new HashMap
<>());
41 private UUID foreignFrameworkUuid
;
44 public void start(BundleContext context
) throws Exception
{
45 this.foreignBundleContext
= context
;
46 foreignFrameworkUuid
= UUID
.fromString(foreignBundleContext
.getProperty(Constants
.FRAMEWORK_UUID
));
49 // Bundle bundle = context.getBundle();
50 // ClassLoader bundleClassLoader = bundle.adapt(BundleWiring.class).getClassLoader();
51 // subFrameworkClassLoader = new URLClassLoader(new URL[0], bundleClassLoader);
53 @SuppressWarnings("unchecked")
54 Class
<?
extends ConnectFrameworkFactory
> frameworkFactoryClass
= (Class
<?
extends ConnectFrameworkFactory
>) Framework
.class
55 .getClassLoader().loadClass(EQUINOX_FRAMEWORK_FACTORY_CLASS
);
56 frameworkFactory
= frameworkFactoryClass
.getConstructor().newInstance();
64 for (int i
= 0; i
< 5; i
++) {
65 Map
<String
, String
> config
= new HashMap
<>();
66 Path basePase
= Paths
.get(System
.getProperty("user.home"), ".config/argeo/test/",
68 config
.put(InitConstants
.PROP_OSGI_CONFIGURATION_AREA
,
69 basePase
.resolve(RuntimeManager
.STATE
).toString());
70 config
.put(InitConstants
.PROP_OSGI_INSTANCE_AREA
,
71 basePase
.resolve(RuntimeManager
.DATA
).toString());
72 config
.put("argeo.host", "host" + i
);
73 config
.put("osgi.console", "host" + i
+ ":2023");
74 createFramework(config
);
79 } catch (Exception e
) {
85 Framework
createFramework(Map
<String
, String
> config
) {
87 URL bundleConfigUrl
= foreignBundleContext
.getBundle().getEntry("config.ini");
88 try (InputStream in
= bundleConfigUrl
.openStream()) {
89 RuntimeManager
.loadConfig(in
, config
);
93 // config.put("osgi.frameworkParentClassloader", "current");
94 // config.put("osgi.parentClassLoader", "app");
95 // config.put("osgi.contextClassLoaderParent", "app");
97 ModuleConnector moduleConnector
= new ForeignModuleConnector(foreignBundleContext
, null);
99 // URL frameworkUrl = URI.create(bundleContext.getProperty("osgi.framework")).toURL();
100 // URLClassLoader frameworkClassLoader = new URLClassLoader(new URL[] { frameworkUrl, });
101 // Class<? extends Framework> frameworkClass = (Class<? extends Framework>) frameworkClassLoader
102 // .loadClass(EQUINOX_FRAMEWORK_CLASS);
103 // Framework framework = frameworkClass.getConstructor(Map.class, ModuleConnector.class).newInstance(config,
106 config
.put(InitConstants
.PROP_ARGEO_OSGI_PARENT_UUID
, foreignFrameworkUuid
.toString());
107 Framework framework
= frameworkFactory
.newFramework(config
, moduleConnector
);
109 framework
.init((e
) -> {
110 UUID frameworkUuid
= UUID
111 .fromString(framework
.getBundleContext().getProperty(Constants
.FRAMEWORK_UUID
));
112 if (e
.getType() == FrameworkEvent
.STOPPED
) {
113 subFrameworks
.remove(frameworkUuid
);
114 logger
.log(INFO
, "Removed subframework " + frameworkUuid
+ " in parent " + foreignFrameworkUuid
);
118 for (Bundle b
: foreignBundleContext
.getBundles()) {
119 if (b
.getBundleId() == 0)
121 String location
= b
.getLocation();
122 if (location
.contains("/org.argeo.tp/") //
123 || location
.contains("/org.argeo.tp.sys/") //
124 || location
.contains("/org.argeo.tp.httpd/") //
125 || location
.contains("/org.argeo.tp.sshd/") //
127 framework
.getBundleContext().installBundle(b
.getLocation());
131 OsgiBoot osgiBoot
= new OsgiBoot(framework
.getBundleContext());
133 // OsgiBoot.uninstallBundles(osgiBoot.getBundleContext(), "org.argeo.api.cms");
134 // OsgiBoot.uninstallBundles(osgiBoot.getBundleContext(), "org.osgi.service.useradmin");
135 // osgiBoot.getBundleContext()
136 // .installBundle("initial@reference:file:../../../../../argeo-commons/org.argeo.api.cms/");
137 // osgiBoot.getBundleContext().installBundle(
138 // "reference:file:/usr/local/share/a2/osgi/equinox/org.argeo.tp.osgi/org.osgi.service.useradmin.1.1.jar");
141 osgiBoot
.startBundles();
143 // for (Bundle b : framework.getBundleContext().getBundles()) {
144 // BundleContext bc = b.getBundleContext();
146 // System.err.println(b.getSymbolicName() + " BC null");
149 UUID frameworkUuid
= UUID
.fromString(framework
.getBundleContext().getProperty(Constants
.FRAMEWORK_UUID
));
150 subFrameworks
.put(frameworkUuid
, framework
);
151 logger
.log(INFO
, "Created subframework " + frameworkUuid
+ " in parent " + foreignFrameworkUuid
);
153 } catch (Exception e
) {
154 throw new IllegalStateException("Cannot start framework", e
);
159 public void stop(BundleContext context
) throws Exception
{
160 for (Iterator
<Framework
> it
= subFrameworks
.values().iterator(); it
.hasNext();) {
161 Framework framework
= it
.next();
166 // for (Framework framework : subFrameworks.values()) {
169 subFrameworks
.clear();
170 foreignBundleContext
= null;
171 frameworkFactory
= null;