}
A2Branch getOrAddBranch(String branchId) {
- if (branches.containsKey(branchId))
- return branches.get(branchId);
- else
- return new A2Branch(this, branchId);
+ if (!branches.containsKey(branchId)) {
+ A2Branch a2Branch = new A2Branch(this, branchId);
+ branches.put(branchId, a2Branch);
+ }
+ return branches.get(branchId);
}
A2Module getOrAddModule(Version version, Object locator) {
import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.Version;
-/** A running OSGi bundle context seen as a {@link AbstractProvisioningSource}. */
+/**
+ * A running OSGi bundle context seen as a {@link AbstractProvisioningSource}.
+ */
class OsgiContext extends AbstractProvisioningSource {
private final BundleContext bc;
+ private A2Contribution runtimeContribution;
+
public OsgiContext(BundleContext bc) {
super(false);
this.bc = bc;
+ runtimeContribution = getOrAddContribution(A2Contribution.RUNTIME);
}
public OsgiContext() {
}
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() + ")");
+ 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);
+ if (OsgiBootUtils.isDebug())
+ OsgiBootUtils.debug("Registered bundle module " + module + " (location id: " + bundle.getLocation() + ")");
+
+ }
}
Version moduleVersion = module.getVersion();
A2Branch osgiBranch = osgiContext.findBranch(module.getBranch().getComponent().getId(), moduleVersion);
if (osgiBranch == null) {
-// Bundle bundle = bc.installBundle(module.getBranch().getCoordinates(),
-// moduleSource.newInputStream(module.getLocator()));
Bundle bundle = moduleSource.install(bc, module);
- if (OsgiBootUtils.isDebug())
- OsgiBootUtils.debug("Installed bundle " + bundle.getLocation() + " with version " + moduleVersion);
+ // TODO make it more dynamic, based on OSGi APIs
+ osgiContext.registerBundle(bundle);
+// if (OsgiBootUtils.isDebug())
+// OsgiBootUtils.debug("Installed bundle " + bundle.getLocation() + " with version " + moduleVersion);
return bundle;
} else {
A2Module lastOsgiModule = osgiBranch.last();
int compare = moduleVersion.compareTo(lastOsgiModule.getVersion());
if (compare > 0) {// update
Bundle bundle = (Bundle) lastOsgiModule.getLocator();
-// bundle.update(moduleSource.newInputStream(module.getLocator()));
moduleSource.update(bundle, module);
+ // TODO make it more dynamic, based on OSGi APIs
+ // TODO remove old module? Or keep update history?
+ osgiContext.registerBundle(bundle);
OsgiBootUtils.info("Updated bundle " + bundle.getLocation() + " to version " + moduleVersion);
return bundle;
+ } else {
+ if (OsgiBootUtils.isDebug())
+ OsgiBootUtils.debug("Did not install as bundle module " + module
+ + " since a module with higher version " + lastOsgiModule.getVersion()
+ + " is already installed for branch " + osgiBranch);
}
}
} catch (Exception e) {
for (String bsn : startLevels.get(level))
bundleStartLevels.put(bsn, level);
}
- for (Bundle bundle : bundleContext.getBundles()) {
+
+ // keep only bundles with the highest version
+ Map<String, Bundle> startableBundles = new HashMap<>();
+ bundles: for (Bundle bundle : bundleContext.getBundles()) {
+ if (bundle.getVersion() == null)
+ continue bundles;
+ String bsn = bundle.getSymbolicName();
+ if (!startableBundles.containsKey(bsn)) {
+ startableBundles.put(bsn, bundle);
+ } else {
+ if (bundle.getVersion().compareTo(startableBundles.get(bsn).getVersion()) > 0) {
+ startableBundles.put(bsn, bundle);
+ }
+ }
+ }
+
+ for (Bundle bundle : startableBundles.values()) {
String bsn = bundle.getSymbolicName();
if (bundleStartLevels.containsKey(bsn)) {
BundleStartLevel bundleStartLevel = bundle.adapt(BundleStartLevel.class);
OsgiBootUtils.error("Cannot mark " + bsn + " as started", e);
}
if (OsgiBootUtils.isDebug())
- OsgiBootUtils.debug(bsn + " starts at level " + level);
+ OsgiBootUtils.debug(bsn + " v" + bundle.getVersion() + " starts at level " + level);
}
}
}