Repository: karaf
Updated Branches:
  refs/heads/karaf-4.0.x 22a5cb7e5 -> e3c4d34b5


[KARAF-4192] java.lang.ClassCastException in 
org.apache.karaf.features.internal.region.CandidateComparator

Project: http://git-wip-us.apache.org/repos/asf/karaf/repo
Commit: http://git-wip-us.apache.org/repos/asf/karaf/commit/e3c4d34b
Tree: http://git-wip-us.apache.org/repos/asf/karaf/tree/e3c4d34b
Diff: http://git-wip-us.apache.org/repos/asf/karaf/diff/e3c4d34b

Branch: refs/heads/karaf-4.0.x
Commit: e3c4d34b516a6d1857b323e654123be3b5442676
Parents: 22a5cb7
Author: Guillaume Nodet <[email protected]>
Authored: Wed Nov 2 16:00:11 2016 +0100
Committer: Guillaume Nodet <[email protected]>
Committed: Wed Nov 2 16:01:42 2016 +0100

----------------------------------------------------------------------
 .../internal/region/CandidateComparator.java    | 90 ++++++++------------
 1 file changed, 35 insertions(+), 55 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/karaf/blob/e3c4d34b/features/core/src/main/java/org/apache/karaf/features/internal/region/CandidateComparator.java
----------------------------------------------------------------------
diff --git 
a/features/core/src/main/java/org/apache/karaf/features/internal/region/CandidateComparator.java
 
b/features/core/src/main/java/org/apache/karaf/features/internal/region/CandidateComparator.java
index d8bffb2..a3f6c51 100644
--- 
a/features/core/src/main/java/org/apache/karaf/features/internal/region/CandidateComparator.java
+++ 
b/features/core/src/main/java/org/apache/karaf/features/internal/region/CandidateComparator.java
@@ -16,7 +16,9 @@
  */
 package org.apache.karaf.features.internal.region;
 
+import java.util.Collections;
 import java.util.Comparator;
+import java.util.List;
 import java.util.Set;
 
 import org.apache.karaf.features.internal.resolver.ResolverUtil;
@@ -54,60 +56,25 @@ public class CandidateComparator implements 
Comparator<Capability> {
         }
         // Compare revision capabilities.
         if ((c == 0) && 
cap1.getNamespace().equals(BundleNamespace.BUNDLE_NAMESPACE)) {
-            c = ((Comparable<Object>) 
cap1.getAttributes().get(BundleNamespace.BUNDLE_NAMESPACE))
-                    
.compareTo(cap2.getAttributes().get(BundleNamespace.BUNDLE_NAMESPACE));
+            c = compareNames(cap1, cap2, BundleNamespace.BUNDLE_NAMESPACE);
             if (c == 0) {
-                Version v1 = 
(!cap1.getAttributes().containsKey(BundleNamespace.CAPABILITY_BUNDLE_VERSION_ATTRIBUTE))
-                        ? Version.emptyVersion
-                        : (Version) 
cap1.getAttributes().get(BundleNamespace.CAPABILITY_BUNDLE_VERSION_ATTRIBUTE);
-                Version v2 = 
(!cap2.getAttributes().containsKey(BundleNamespace.CAPABILITY_BUNDLE_VERSION_ATTRIBUTE))
-                        ? Version.emptyVersion
-                        : (Version) 
cap2.getAttributes().get(BundleNamespace.CAPABILITY_BUNDLE_VERSION_ATTRIBUTE);
-                // Compare these in reverse order, since we want
-                // highest version to have priority.
-                c = compareVersions(v2, v1);
+                c = compareVersions(cap1, cap2, 
BundleNamespace.CAPABILITY_BUNDLE_VERSION_ATTRIBUTE);
             }
         // Compare package capabilities.
         } else if ((c == 0) && 
cap1.getNamespace().equals(PackageNamespace.PACKAGE_NAMESPACE)) {
-            c = ((Comparable<Object>) 
cap1.getAttributes().get(PackageNamespace.PACKAGE_NAMESPACE))
-                    
.compareTo(cap2.getAttributes().get(PackageNamespace.PACKAGE_NAMESPACE));
+            c = compareNames(cap1, cap2, PackageNamespace.PACKAGE_NAMESPACE);
             if (c == 0) {
-                Version v1 = 
(!cap1.getAttributes().containsKey(PackageNamespace.CAPABILITY_VERSION_ATTRIBUTE))
-                        ? Version.emptyVersion
-                        : (Version) 
cap1.getAttributes().get(PackageNamespace.CAPABILITY_VERSION_ATTRIBUTE);
-                Version v2 = 
(!cap2.getAttributes().containsKey(PackageNamespace.CAPABILITY_VERSION_ATTRIBUTE))
-                        ? Version.emptyVersion
-                        : (Version) 
cap2.getAttributes().get(PackageNamespace.CAPABILITY_VERSION_ATTRIBUTE);
-                // Compare these in reverse order, since we want
-                // highest version to have priority.
-                c = compareVersions(v2, v1);
+                c = compareVersions(cap1, cap2, 
PackageNamespace.CAPABILITY_VERSION_ATTRIBUTE);
                 // if same version, rather compare on the bundle version
                 if (c == 0) {
-                    v1 = 
(!cap1.getAttributes().containsKey(BundleNamespace.CAPABILITY_BUNDLE_VERSION_ATTRIBUTE))
-                            ? Version.emptyVersion
-                            : (Version) 
cap1.getAttributes().get(BundleNamespace.CAPABILITY_BUNDLE_VERSION_ATTRIBUTE);
-                    v2 = 
(!cap2.getAttributes().containsKey(BundleNamespace.CAPABILITY_BUNDLE_VERSION_ATTRIBUTE))
-                            ? Version.emptyVersion
-                            : (Version) 
cap2.getAttributes().get(BundleNamespace.CAPABILITY_BUNDLE_VERSION_ATTRIBUTE);
-                    // Compare these in reverse order, since we want
-                    // highest version to have priority.
-                    c = compareVersions(v2, v1);
+                    c = compareVersions(cap1, cap2, 
BundleNamespace.CAPABILITY_BUNDLE_VERSION_ATTRIBUTE);
                 }
             }
         // Compare feature capabilities
         } else if ((c == 0) && 
cap1.getNamespace().equals(IdentityNamespace.IDENTITY_NAMESPACE)) {
-            c = ((Comparable<Object>) 
cap1.getAttributes().get(IdentityNamespace.IDENTITY_NAMESPACE))
-                    
.compareTo(cap2.getAttributes().get(IdentityNamespace.IDENTITY_NAMESPACE));
+            c = compareNames(cap1, cap2, IdentityNamespace.IDENTITY_NAMESPACE);
             if (c == 0) {
-                Version v1 = 
(!cap1.getAttributes().containsKey(IdentityNamespace.CAPABILITY_VERSION_ATTRIBUTE))
-                        ? Version.emptyVersion
-                        : (Version) 
cap1.getAttributes().get(IdentityNamespace.CAPABILITY_VERSION_ATTRIBUTE);
-                Version v2 = 
(!cap2.getAttributes().containsKey(IdentityNamespace.CAPABILITY_VERSION_ATTRIBUTE))
-                        ? Version.emptyVersion
-                        : (Version) 
cap2.getAttributes().get(IdentityNamespace.CAPABILITY_VERSION_ATTRIBUTE);
-                // Compare these in reverse order, since we want
-                // highest version to have priority.
-                c = compareVersions(v2, v1);
+                c = compareVersions(cap1, cap2, 
IdentityNamespace.CAPABILITY_VERSION_ATTRIBUTE);
             }
         }
         if (c == 0) {
@@ -119,20 +86,33 @@ public class CandidateComparator implements 
Comparator<Capability> {
         return c;
     }
 
-    private int compareVersions(Version v1, Version v2) {
-        int c = v1.getMajor() - v2.getMajor();
-        if (c != 0) {
-            return c;
-        }
-        c = v1.getMinor() - v2.getMinor();
-        if (c != 0) {
-            return c;
-        }
-        c = v1.getMicro() - v2.getMicro();
-        if (c != 0) {
-            return c;
+    private int compareNames(Capability cap1, Capability cap2, String 
attribute) {
+        Object o1 = cap1.getAttributes().get(attribute);
+        Object o2 = cap2.getAttributes().get(attribute);
+        if (o1 instanceof List || o2 instanceof List) {
+            List<String> l1 = o1 instanceof List ? (List) o1 : 
Collections.singletonList((String) o1);
+            List<String> l2 = o2 instanceof List ? (List) o2 : 
Collections.singletonList((String) o2);
+            for (String s : l1) {
+                if (l2.contains(s)) {
+                    return 0;
+                }
+            }
+            return l1.get(0).compareTo(l2.get(0));
+        } else {
+            return((String) o1).compareTo((String) o2);
         }
-        return v1.getQualifier().compareTo(v2.getQualifier());
+    }
+
+    private int compareVersions(Capability cap1, Capability cap2, String 
attribute) {
+        Version v1 = (!cap1.getAttributes().containsKey(attribute))
+                ? Version.emptyVersion
+                : (Version) cap1.getAttributes().get(attribute);
+        Version v2 = (!cap2.getAttributes().containsKey(attribute))
+                ? Version.emptyVersion
+                : (Version) cap2.getAttributes().get(attribute);
+        // Compare these in reverse order, since we want
+        // highest version to have priority.
+        return v2.compareTo(v1);
     }
 
 }

Reply via email to