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