Repository: karaf Updated Branches: refs/heads/master e8a865024 -> c50929336
[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/c5092933 Tree: http://git-wip-us.apache.org/repos/asf/karaf/tree/c5092933 Diff: http://git-wip-us.apache.org/repos/asf/karaf/diff/c5092933 Branch: refs/heads/master Commit: c50929336d4c817616199ba8ed2104888583f4b6 Parents: e8a8650 Author: Guillaume Nodet <[email protected]> Authored: Wed Nov 2 16:00:11 2016 +0100 Committer: Guillaume Nodet <[email protected]> Committed: Wed Nov 2 16:00:30 2016 +0100 ---------------------------------------------------------------------- .../internal/region/CandidateComparator.java | 90 ++++++++------------ 1 file changed, 35 insertions(+), 55 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/karaf/blob/c5092933/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); } }
