Merge tag 'v2.3.28' into testing
[lgpl/argeo-commons.git] / org.argeo.init / src / org / argeo / api / a2 / OsgiContext.java
diff --git a/org.argeo.init/src/org/argeo/api/a2/OsgiContext.java b/org.argeo.init/src/org/argeo/api/a2/OsgiContext.java
new file mode 100644 (file)
index 0000000..4ec186b
--- /dev/null
@@ -0,0 +1,52 @@
+package org.argeo.api.a2;
+
+import java.lang.System.Logger;
+import java.lang.System.Logger.Level;
+
+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 static Logger logger = System.getLogger(OsgiContext.class.getName());
+
+       private final BundleContext bc;
+
+       private A2Contribution runtimeContribution;
+
+       public OsgiContext(BundleContext bc) {
+               super(false);
+               this.bc = bc;
+               runtimeContribution = getOrAddContribution(A2Contribution.RUNTIME);
+       }
+
+       public OsgiContext() {
+               super(false);
+               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() {
+               for (Bundle bundle : bc.getBundles()) {
+                       registerBundle(bundle);
+               }
+
+       }
+
+       void registerBundle(Bundle bundle) {
+               String componentId = bundle.getSymbolicName();
+               Version version = bundle.getVersion();
+               A2Component component = runtimeContribution.getOrAddComponent(componentId);
+               A2Module module = component.getOrAddModule(version, bundle);
+               logger.log(Level.TRACE,
+                               () -> "Registered bundle module " + module + " (location id: " + bundle.getLocation() + ")");
+
+       }
+}