]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.init/src/org/argeo/init/a2/OsgiContext.java
Prepare next development cycle
[lgpl/argeo-commons.git] / org.argeo.init / src / org / argeo / init / a2 / OsgiContext.java
1 package org.argeo.init.a2;
2
3 import org.argeo.init.osgi.OsgiBootUtils;
4 import org.osgi.framework.Bundle;
5 import org.osgi.framework.BundleContext;
6 import org.osgi.framework.FrameworkUtil;
7 import org.osgi.framework.Version;
8
9 /**
10 * A running OSGi bundle context seen as a {@link AbstractProvisioningSource}.
11 */
12 class OsgiContext extends AbstractProvisioningSource {
13 private final BundleContext bc;
14
15 private A2Contribution runtimeContribution;
16
17 public OsgiContext(BundleContext bc) {
18 super(false);
19 this.bc = bc;
20 runtimeContribution = getOrAddContribution(A2Contribution.RUNTIME);
21 }
22
23 public OsgiContext() {
24 super(false);
25 Bundle bundle = FrameworkUtil.getBundle(OsgiContext.class);
26 if (bundle == null)
27 throw new IllegalArgumentException(
28 "OSGi Boot bundle must be started or a bundle context must be specified");
29 this.bc = bundle.getBundleContext();
30 }
31
32 void load() {
33 for (Bundle bundle : bc.getBundles()) {
34 registerBundle(bundle);
35 }
36
37 }
38
39 void registerBundle(Bundle bundle) {
40 String componentId = bundle.getSymbolicName();
41 Version version = bundle.getVersion();
42 A2Component component = runtimeContribution.getOrAddComponent(componentId);
43 A2Module module = component.getOrAddModule(version, bundle);
44 if (OsgiBootUtils.isDebug())
45 OsgiBootUtils.debug("Registered bundle module " + module + " (location id: " + bundle.getLocation() + ")");
46
47 }
48 }