View Javadoc
1   package org.argeo.osgi.a2;
2   
3   import org.argeo.osgi.boot.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   /** A running OSGi bundle context seen as a {@link AbstractProvisioningSource}. */
10  class OsgiContext extends AbstractProvisioningSource {
11  	private final BundleContext bc;
12  
13  	public OsgiContext(BundleContext bc) {
14  		super();
15  		this.bc = bc;
16  	}
17  
18  	public OsgiContext() {
19  		Bundle bundle = FrameworkUtil.getBundle(OsgiContext.class);
20  		if (bundle == null)
21  			throw new IllegalArgumentException(
22  					"OSGi Boot bundle must be started or a bundle context must be specified");
23  		this.bc = bundle.getBundleContext();
24  	}
25  
26  	void load() {
27  		A2Contribution runtimeContribution = getOrAddContribution( A2Contribution.RUNTIME);
28  		for (Bundle bundle : bc.getBundles()) {
29  			// OsgiBootUtils.debug(bundle.getDataFile("/"));
30  			String componentId = bundle.getSymbolicName();
31  			Version version = bundle.getVersion();
32  			A2Component component = runtimeContribution.getOrAddComponent(componentId);
33  			A2Module module = component.getOrAddModule(version, bundle);
34  			if (OsgiBootUtils.isDebug())
35  				OsgiBootUtils.debug("Registered " + module + " (location id: " + bundle.getLocation() + ")");
36  		}
37  
38  	}
39  }