From: Mathieu Baudier Date: Sun, 17 Dec 2023 11:31:20 +0000 (+0100) Subject: Fix override of installed bundles X-Git-Tag: v2.3.27~4 X-Git-Url: https://git.argeo.org/?p=lgpl%2Fargeo-commons.git;a=commitdiff_plain;h=71d0bea8d7aa3592f7f2b7c89bb8dd91b6d8bb6d Fix override of installed bundles --- diff --git a/org.argeo.init/src/org/argeo/init/a2/A2Component.java b/org.argeo.init/src/org/argeo/init/a2/A2Component.java index cc2f56471..894270630 100644 --- a/org.argeo.init/src/org/argeo/init/a2/A2Component.java +++ b/org.argeo.init/src/org/argeo/init/a2/A2Component.java @@ -28,10 +28,11 @@ public class A2Component implements Comparable { } 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) { diff --git a/org.argeo.init/src/org/argeo/init/a2/OsgiContext.java b/org.argeo.init/src/org/argeo/init/a2/OsgiContext.java index 0064ab9ed..7f1133f67 100644 --- a/org.argeo.init/src/org/argeo/init/a2/OsgiContext.java +++ b/org.argeo.init/src/org/argeo/init/a2/OsgiContext.java @@ -6,13 +6,18 @@ 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}. */ +/** + * 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() { @@ -25,16 +30,19 @@ class OsgiContext extends AbstractProvisioningSource { } 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() + ")"); + + } } diff --git a/org.argeo.init/src/org/argeo/init/a2/ProvisioningManager.java b/org.argeo.init/src/org/argeo/init/a2/ProvisioningManager.java index 6a0836bdf..92df47ea8 100644 --- a/org.argeo.init/src/org/argeo/init/a2/ProvisioningManager.java +++ b/org.argeo.init/src/org/argeo/init/a2/ProvisioningManager.java @@ -149,21 +149,28 @@ public class ProvisioningManager { 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) { diff --git a/org.argeo.init/src/org/argeo/init/osgi/OsgiBoot.java b/org.argeo.init/src/org/argeo/init/osgi/OsgiBoot.java index 2e96db3b3..f5b260cab 100644 --- a/org.argeo.init/src/org/argeo/init/osgi/OsgiBoot.java +++ b/org.argeo.init/src/org/argeo/init/osgi/OsgiBoot.java @@ -381,7 +381,23 @@ public class OsgiBoot implements OsgiBootConstants { for (String bsn : startLevels.get(level)) bundleStartLevels.put(bsn, level); } - for (Bundle bundle : bundleContext.getBundles()) { + + // keep only bundles with the highest version + Map 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); @@ -394,7 +410,7 @@ public class OsgiBoot implements OsgiBootConstants { 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); } } }