jbonofre commented on code in PR #2978:
URL: https://github.com/apache/karaf/pull/2978#discussion_r4561711557


##########
features/core/src/main/java/org/apache/karaf/features/internal/resolver/ResourceUtils.java:
##########
@@ -91,6 +91,23 @@ public static String getFeatureId(Resource resource) {
         return null;
     }
 
+    /**
+     * 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 
<code>1.0.01</code> becomes
+     * <code>1.0.1</code>.
+     */
+    public static String getFeatureId(String name, String version) {
+        return version != null ? name + "/" + VersionTable.getVersion(version) 
: name;

Review Comment:
   `VersionTable.getVersion` throws `IllegalStateException` when the cleaned 
version still is not a valid OSGi version. A few inputs that survive 
`VersionCleaner.clean` and then fail:
   
   | version | cleaned | result |
   |---------|---------|-------|
   | `1.0.0.beta+1` | `1.0.0.beta+1` | `invalid qualifier "beta+1" ` |
   | `1.0.0.rc 1` | `1.0.0.rc 1` | `invalid qualifier "rc 1"` |
   | `20231112093000` | `20231112093000.0.0` | `non-numeric "20231112093000"` |
   
   Before this PR, these ids were only cleaned, never parsed, so the method 
couldn't throw. Since it's now called for every available feature (see the 
comments on `normalize()` and `partitionFeatures`), it needs to fall back 
instead of failing. Such a feature can't be in the deployment state anyway 
(`FeatureResource` would fail to parse it too), so keeping the raw version as 
the key is safe:
   
   
   ```suggestion
           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;
           }
   ```
   
   Could we also add a regression test? A repository with an unused feature 
that has an invalid version, next to the one being installed:
   
   ```xml
   <feature name="f" version="1.0.0">
       <bundle>a100</bundle>
   </feature>
   <feature name="bad" version="1.0.0.beta+1">
       <bundle>a100</bundle>
   </feature>
   ```
   
   Installing `f/1.0.0`, `listInstalledFeatures()`, stopping and uninstalling 
`f` should all still work.



##########
features/core/src/main/java/org/apache/karaf/features/internal/service/Deployer.java:
##########
@@ -230,7 +230,8 @@ public void partitionFeatures(Collection<Feature> 
featuresList) {
             featuresById = new HashMap<>();
             for (Feature feature : featuresList) {
                 features.computeIfAbsent(feature.getName(), name -> new 
ArrayList<>()).add(feature);
-                featuresById.put(feature.getId(), feature);
+                // keyed like the deployment state, which uses normalized 
versions (1.0.01 -> 1.0.1)
+                featuresById.put(getFeatureId(feature.getName(), 
feature.getVersion()), feature);

Review Comment:
   Same issue on the deployment path. Before, only features whose name matched 
a requirement had their version parsed (in `Subsystem`/`FeatureReq`). This loop 
now parses the version of every available feature. If `f` was installed first 
and a repository with `bad/1.0.0.beta+1` is added later, every deployment 
fails, including stopping or uninstalling `f`:
   
   ```
   java.lang.IllegalArgumentException: invalid version "1.0.0.beta+1": invalid 
qualifier "beta+1"
       at 
o.a.k.features.internal.resolver.ResourceUtils.getFeatureId(ResourceUtils.java:100)
       at 
o.a.k.features.internal.service.Deployer$DeploymentState.partitionFeatures(Deployer.java:234)
       at 
o.a.k.features.internal.service.FeaturesServiceImpl.getDeploymentState(FeaturesServiceImpl.java:1038)
       at 
o.a.k.features.internal.service.FeaturesServiceImpl.doProvision(FeaturesServiceImpl.java:1071)
   ```
   
   `partitionFeatures` is also used by `AssemblyDeployCallback` and 
`VerifyMojo`, so profile assembly and `karaf:verify` are affected too.
   
   The fallback in `ResourceUtils.getFeatureId` covers this as well.



##########
features/core/src/main/java/org/apache/karaf/features/internal/service/FeaturesServiceImpl.java:
##########
@@ -957,7 +958,8 @@ private String normalize(String feature) {
         }
         String name = feature.substring(0, idx);
         String version = feature.substring(idx + 1);
-        return name + VERSION_SEPARATOR + VersionCleaner.clean(version);
+        // same form as the ids in the deployment state, so 1.0.01 matches 
1.0.1
+        return ResourceUtils.getFeatureId(name, VersionCleaner.clean(version));

Review Comment:
   This is where it breaks for users. `listInstalledFeatures()` calls 
`isInstalled()` -> `normalize()` on every cached feature, and 
`installFeatures()` calls `listInstalledFeatures()` via 
`computeFeaturesToAdd()`. With an unused `bad/1.0.0.beta+1` in any registered 
repository, installing an unrelated feature fails:
   
   ```
   java.lang.IllegalArgumentException: invalid version "1.0.0.beta+1": invalid 
qualifier "beta+1"
       at 
o.a.k.features.internal.resolver.ResourceUtils.getFeatureId(ResourceUtils.java:100)
       at 
o.a.k.features.internal.service.FeaturesServiceImpl.normalize(FeaturesServiceImpl.java:962)
       at 
o.a.k.features.internal.service.FeaturesServiceImpl.isInstalled(FeaturesServiceImpl.java:688)
       at 
o.a.k.features.internal.service.FeaturesServiceImpl.flattenFeatures(FeaturesServiceImpl.java:683)
       at 
o.a.k.features.internal.service.FeaturesServiceImpl.listInstalledFeatures(FeaturesServiceImpl.java:666)
       at 
o.a.k.features.internal.service.FeaturesServiceImpl.computeFeaturesToAdd(FeaturesServiceImpl.java:835)
       at 
o.a.k.features.internal.service.FeaturesServiceImpl.installFeatures(FeaturesServiceImpl.java:807)
   ```
   
   `feature:list`, the JMX MBean and `getState()` fail the same way. The 
fallback in `ResourceUtils.getFeatureId` fixes it.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to