1 package org
.argeo
.api
.a2
;
3 import java
.lang
.System
.Logger
;
4 import java
.lang
.System
.Logger
.Level
;
6 import org
.osgi
.framework
.Bundle
;
7 import org
.osgi
.framework
.BundleContext
;
8 import org
.osgi
.framework
.FrameworkUtil
;
9 import org
.osgi
.framework
.Version
;
12 * A running OSGi bundle context seen as a {@link AbstractProvisioningSource}.
14 class OsgiContext
extends AbstractProvisioningSource
{
15 private final static Logger logger
= System
.getLogger(OsgiContext
.class.getName());
17 private final BundleContext bc
;
19 private A2Contribution runtimeContribution
;
21 public OsgiContext(BundleContext bc
) {
24 runtimeContribution
= getOrAddContribution(A2Contribution
.RUNTIME
);
27 public OsgiContext() {
29 Bundle bundle
= FrameworkUtil
.getBundle(OsgiContext
.class);
31 throw new IllegalArgumentException(
32 "OSGi Boot bundle must be started or a bundle context must be specified");
33 this.bc
= bundle
.getBundleContext();
37 for (Bundle bundle
: bc
.getBundles()) {
38 registerBundle(bundle
);
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() + ")");