From e1bcfe3f3ae705c10bf2de9a533fb1a13f5727c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?JB=20Onofr=C3=A9?= Date: Fri, 20 Nov 2026 16:28:34 +0100 Subject: [PATCH] [4.4.x] Keep raw version when feature version is not a valid OSGi version Follow-up backport of #2978 (commit c172c9f6ab) to karaf-4.4.x. PR #2993 already backported the initial commit of #2978 (0e2b7b03) to karaf-4.4.x, but missed the subsequent review fixes before #2978 was merged: - ResourceUtils.getFeatureId(name, version) falls back to the raw version when VersionTable.getVersion throws IllegalArgumentException, avoiding failures on unrelated features when a registered repository contains an invalid version. - FeaturesServiceImpl.normalize delegates directly to ResourceUtils.getFeatureId without redundant VersionCleaner.clean. - Deployer reuses the featuresById entry key when installing configs/libraries. - Adds testFeatureWithInvalidVersionDoesNotBreakOtherFeatures regression test. (backport of #2978 follow-up commits from c172c9f6ab) --- .../internal/resolver/ResourceUtils.java | 12 ++- .../features/internal/service/Deployer.java | 5 +- .../internal/service/FeaturesServiceImpl.java | 3 +- .../service/FeaturesServiceImplTest.java | 94 ++++++++++++++++--- .../internal/service/invalidversion/bad.xml | 26 +++++ .../internal/service/invalidversion/f.xml | 25 +++++ 6 files changed, 145 insertions(+), 20 deletions(-) create mode 100644 features/core/src/test/resources/org/apache/karaf/features/internal/service/invalidversion/bad.xml create mode 100644 features/core/src/test/resources/org/apache/karaf/features/internal/service/invalidversion/f.xml diff --git a/features/core/src/main/java/org/apache/karaf/features/internal/resolver/ResourceUtils.java b/features/core/src/main/java/org/apache/karaf/features/internal/resolver/ResourceUtils.java index 8929504eb14..6d0d16f5f0e 100644 --- a/features/core/src/main/java/org/apache/karaf/features/internal/resolver/ResourceUtils.java +++ b/features/core/src/main/java/org/apache/karaf/features/internal/resolver/ResourceUtils.java @@ -94,10 +94,18 @@ public static String getFeatureId(Resource resource) { /** * Returns the feature ID in the same form as {@link #getFeatureId(Resource)}, which is the form used as key * in the deployment state. The version is normalized the OSGi way, so 1.0.01 becomes - * 1.0.1. + * 1.0.1. A version that is not a valid OSGi version, even after cleaning, is kept as is. */ public static String getFeatureId(String name, String version) { - return version != null ? name + "/" + VersionTable.getVersion(version) : name; + if (version == null) { + return name; + } + try { + return name + "/" + VersionTable.getVersion(version); + } catch (IllegalArgumentException e) { + // not a valid OSGi version even after cleaning, keep it as is + return name + "/" + version; + } } /** diff --git a/features/core/src/main/java/org/apache/karaf/features/internal/service/Deployer.java b/features/core/src/main/java/org/apache/karaf/features/internal/service/Deployer.java index 9c5c71f6a54..a88e16abf4c 100644 --- a/features/core/src/main/java/org/apache/karaf/features/internal/service/Deployer.java +++ b/features/core/src/main/java/org/apache/karaf/features/internal/service/Deployer.java @@ -957,8 +957,9 @@ && isUpdateable(resource) && !deployment.bundleChecksums.containsKey(bundle.getB // if (!newFeatures.isEmpty()) { Set featureIds = flatten(newFeatures); - for (Feature feature : dstate.featuresById.values()) { - if (featureIds.contains(getFeatureId(feature.getName(), feature.getVersion()))) { + for (Map.Entry entry : dstate.featuresById.entrySet()) { + Feature feature = entry.getValue(); + if (featureIds.contains(entry.getKey())) { callback.installConfigs(feature); callback.installLibraries(feature); } diff --git a/features/core/src/main/java/org/apache/karaf/features/internal/service/FeaturesServiceImpl.java b/features/core/src/main/java/org/apache/karaf/features/internal/service/FeaturesServiceImpl.java index 37165a9a98a..a169b926157 100644 --- a/features/core/src/main/java/org/apache/karaf/features/internal/service/FeaturesServiceImpl.java +++ b/features/core/src/main/java/org/apache/karaf/features/internal/service/FeaturesServiceImpl.java @@ -51,7 +51,6 @@ import java.util.stream.Collectors; import java.util.stream.Stream; -import org.apache.felix.utils.version.VersionCleaner; import org.apache.karaf.features.BundleInfo; import org.apache.karaf.features.DeploymentEvent; import org.apache.karaf.features.DeploymentListener; @@ -954,7 +953,7 @@ private String normalize(String feature) { String name = feature.substring(0, idx); String version = feature.substring(idx + 1); // same form as the ids in the deployment state, so 1.0.01 matches 1.0.1 - return ResourceUtils.getFeatureId(name, VersionCleaner.clean(version)); + return ResourceUtils.getFeatureId(name, version); } /** diff --git a/features/core/src/test/java/org/apache/karaf/features/internal/service/FeaturesServiceImplTest.java b/features/core/src/test/java/org/apache/karaf/features/internal/service/FeaturesServiceImplTest.java index 1e74f796305..cd022ee5723 100644 --- a/features/core/src/test/java/org/apache/karaf/features/internal/service/FeaturesServiceImplTest.java +++ b/features/core/src/test/java/org/apache/karaf/features/internal/service/FeaturesServiceImplTest.java @@ -23,6 +23,7 @@ import java.io.OutputStream; import java.lang.reflect.Field; import java.net.*; +import java.util.Arrays; import java.util.Collections; import java.util.EnumSet; import java.util.HashMap; @@ -288,15 +289,7 @@ public void testInstallAndStopFeatureWithLeadingZeroInVersion() throws Exception expect(installSupport.installBundle(EasyMock.eq("root"), EasyMock.eq("a100"), anyObject())).andReturn(bundle); installSupport.startBundle(bundle); expectLastCall(); - expect(bundle.getBundleId()).andReturn(1L).anyTimes(); - expect(bundle.getSymbolicName()).andReturn("a").anyTimes(); - expect(bundle.getVersion()).andReturn(new Version("1.0.0")).anyTimes(); - expect(bundle.getHeaders()).andReturn(new Hashtable<>()).anyTimes(); - expect(bundle.adapt(BundleStartLevel.class)).andReturn(bundleStartLevel).anyTimes(); - expect(bundle.adapt(BundleRevision.class)).andReturn(bundleRevision).anyTimes(); - expect(bundleRevision.getBundle()).andReturn(bundle).anyTimes(); - expect(bundleRevision.getCapabilities(null)).andReturn(Collections.emptyList()).anyTimes(); - expect(bundleRevision.getRequirements(null)).andReturn(Collections.emptyList()).anyTimes(); + expectBundleA(bundle, bundleStartLevel, bundleRevision); EasyMock.replay(installSupport, bundle, bundleStartLevel, bundleRevision); FeaturesService featureService = new FeaturesServiceImpl(new Storage(), null, null, this.resolver, installSupport, null, cfg) { @@ -321,6 +314,84 @@ protected DownloadManager createDownloadManager() throws IOException { expect(installSupport.getInfo()).andReturn(dummyInfo).anyTimes(); installSupport.stopBundle(EasyMock.capture(stoppedBundle), EasyMock.anyInt()); expectLastCall(); + expectBundleA(bundle, bundleStartLevel, bundleRevision); + EasyMock.replay(installSupport, bundle, bundleRevision, bundleStartLevel); + + featureService.updateFeaturesState(states, EnumSet.noneOf(Option.class)); + assertSame(bundle, stoppedBundle.getValue()); + assertEquals(FeatureState.Resolved, featureService.getState(feature.getId())); + } + + /** + * A feature whose version is not a valid OSGi version, even after cleaning, must not break listing, stopping and + * uninstalling a feature installed before its repository was added, nor querying its own state. + */ + @Test + public void testFeatureWithInvalidVersionDoesNotBreakOtherFeatures() throws Exception { + Capture stoppedBundle = Capture.newInstance(); + + Bundle bundle = EasyMock.niceMock(Bundle.class); + BundleStartLevel bundleStartLevel = EasyMock.niceMock(BundleStartLevel.class); + BundleRevision bundleRevision = EasyMock.niceMock(BundleRevision.class); + + FeaturesServiceConfig cfg = new FeaturesServiceConfig(); + BundleInstallSupport installSupport = EasyMock.niceMock(BundleInstallSupport.class); + FrameworkInfo dummyInfo = new FrameworkInfo(); + expect(installSupport.getInfo()).andReturn(dummyInfo).atLeastOnce(); + expect(installSupport.installBundle(EasyMock.eq("root"), EasyMock.eq("a100"), anyObject())).andReturn(bundle); + installSupport.startBundle(bundle); + expectLastCall(); + expectBundleA(bundle, bundleStartLevel, bundleRevision); + EasyMock.replay(installSupport, bundle, bundleStartLevel, bundleRevision); + FeaturesService featureService = new FeaturesServiceImpl(new Storage(), null, null, this.resolver, + installSupport, null, cfg) { + @Override + protected DownloadManager createDownloadManager() throws IOException { + return new TestDownloadManager(FeaturesServiceImplTest.class, "data1"); + } + }; + + featureService.addRepository(URI.create("custom:invalidversion/f.xml")); + Feature feature = featureService.getFeature("f", "1.0.0"); + installFeature(featureService, feature); + assertInstalled(featureService, feature); + + // the repository with the invalid version is added after f was installed + featureService.addRepository(URI.create("custom:invalidversion/bad.xml")); + assertEquals(1, featureService.listInstalledFeatures().length); + assertEquals(FeatureState.Uninstalled, featureService.getState("bad/1.0.0.beta+1")); + Feature bad = Arrays.stream(featureService.listFeatures()) + .filter(f -> "bad".equals(f.getName())).findFirst().orElseThrow(AssertionError::new); + assertFalse(featureService.isInstalled(bad)); + + // stop + dummyInfo.bundles.put(1L, bundle); + Map> states = new HashMap<>(); + states.computeIfAbsent("root", k -> new HashMap<>()).put(feature.getId(), FeatureState.Resolved); + EasyMock.reset(installSupport, bundle, bundleRevision, bundleStartLevel); + expect(installSupport.getInfo()).andReturn(dummyInfo).anyTimes(); + installSupport.stopBundle(EasyMock.capture(stoppedBundle), EasyMock.anyInt()); + expectLastCall(); + expectBundleA(bundle, bundleStartLevel, bundleRevision); + EasyMock.replay(installSupport, bundle, bundleRevision, bundleStartLevel); + featureService.updateFeaturesState(states, EnumSet.noneOf(Option.class)); + assertSame(bundle, stoppedBundle.getValue()); + assertEquals(FeatureState.Resolved, featureService.getState(feature.getId())); + + // uninstall + EasyMock.reset(installSupport, bundle, bundleRevision, bundleStartLevel); + expect(installSupport.getInfo()).andReturn(dummyInfo).anyTimes(); + installSupport.uninstall(bundle); + expectLastCall(); + expectBundleA(bundle, bundleStartLevel, bundleRevision); + EasyMock.replay(installSupport, bundle, bundleRevision, bundleStartLevel); + featureService.uninstallFeature("f", "1.0.0", EnumSet.noneOf(Option.class)); + assertNotInstalled(featureService, feature); + EasyMock.verify(installSupport); + } + + private static void expectBundleA(Bundle bundle, BundleStartLevel bundleStartLevel, + BundleRevision bundleRevision) { expect(bundle.getBundleId()).andReturn(1L).anyTimes(); expect(bundle.getSymbolicName()).andReturn("a").anyTimes(); expect(bundle.getVersion()).andReturn(new Version("1.0.0")).anyTimes(); @@ -330,11 +401,6 @@ protected DownloadManager createDownloadManager() throws IOException { expect(bundleRevision.getBundle()).andReturn(bundle).anyTimes(); expect(bundleRevision.getCapabilities(null)).andReturn(Collections.emptyList()).anyTimes(); expect(bundleRevision.getRequirements(null)).andReturn(Collections.emptyList()).anyTimes(); - EasyMock.replay(installSupport, bundle, bundleRevision, bundleStartLevel); - - featureService.updateFeaturesState(states, EnumSet.noneOf(Option.class)); - assertSame(bundle, stoppedBundle.getValue()); - assertEquals(FeatureState.Resolved, featureService.getState(feature.getId())); } @Test diff --git a/features/core/src/test/resources/org/apache/karaf/features/internal/service/invalidversion/bad.xml b/features/core/src/test/resources/org/apache/karaf/features/internal/service/invalidversion/bad.xml new file mode 100644 index 00000000000..8e98c0db97b --- /dev/null +++ b/features/core/src/test/resources/org/apache/karaf/features/internal/service/invalidversion/bad.xml @@ -0,0 +1,26 @@ + + + + + + + a100 + + + diff --git a/features/core/src/test/resources/org/apache/karaf/features/internal/service/invalidversion/f.xml b/features/core/src/test/resources/org/apache/karaf/features/internal/service/invalidversion/f.xml new file mode 100644 index 00000000000..eda33a2824e --- /dev/null +++ b/features/core/src/test/resources/org/apache/karaf/features/internal/service/invalidversion/f.xml @@ -0,0 +1,25 @@ + + + + + + a100 + + +