Rename A2 framework package.
[lgpl/argeo-commons.git] / org.argeo.osgi.boot / src / org / argeo / osgi / a2 / OsgiContext.java
diff --git a/org.argeo.osgi.boot/src/org/argeo/osgi/a2/OsgiContext.java b/org.argeo.osgi.boot/src/org/argeo/osgi/a2/OsgiContext.java
new file mode 100644 (file)
index 0000000..8630dc2
--- /dev/null
@@ -0,0 +1,39 @@
+package org.argeo.osgi.a2;
+
+import org.argeo.osgi.boot.OsgiBootUtils;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.FrameworkUtil;
+import org.osgi.framework.Version;
+
+/** A running OSGi bundle context seen as a {@link AbstractProvisioningSource}. */
+class OsgiContext extends AbstractProvisioningSource {
+       private final BundleContext bc;
+
+       public OsgiContext(BundleContext bc) {
+               super();
+               this.bc = bc;
+       }
+
+       public OsgiContext() {
+               Bundle bundle = FrameworkUtil.getBundle(OsgiContext.class);
+               if (bundle == null)
+                       throw new IllegalArgumentException(
+                                       "OSGi Boot bundle must be started or a bundle context must be specified");
+               this.bc = bundle.getBundleContext();
+       }
+
+       void load() {
+               A2Contribution runtimeContribution = getOrAddContribution( A2Contribution.RUNTIME);
+               for (Bundle bundle : bc.getBundles()) {
+                       // OsgiBootUtils.debug(bundle.getDataFile("/"));
+                       String componentId = bundle.getSymbolicName();
+                       Version version = bundle.getVersion();
+                       A2Component component = runtimeContribution.getOrAddComponent(componentId);
+                       A2Module module = component.getOrAddModule(version, bundle);
+                       if (OsgiBootUtils.isDebug())
+                               OsgiBootUtils.debug("Registered " + module + " (location id: " + bundle.getLocation() + ")");
+               }
+
+       }
+}