Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 84488200BB3 for ; Wed, 2 Nov 2016 16:00:55 +0100 (CET) Received: by cust-asf.ponee.io (Postfix) id 8303F160AFB; Wed, 2 Nov 2016 15:00:55 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id A3797160AEA for ; Wed, 2 Nov 2016 16:00:54 +0100 (CET) Received: (qmail 64101 invoked by uid 500); 2 Nov 2016 15:00:53 -0000 Mailing-List: contact commits-help@karaf.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@karaf.apache.org Delivered-To: mailing list commits@karaf.apache.org Received: (qmail 64092 invoked by uid 99); 2 Nov 2016 15:00:53 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 02 Nov 2016 15:00:53 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id B9AC8E09DE; Wed, 2 Nov 2016 15:00:53 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: gnodet@apache.org To: commits@karaf.apache.org Message-Id: <81c9eef1bb70467ab93b366986e372ed@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: karaf git commit: [KARAF-4192] java.lang.ClassCastException in org.apache.karaf.features.internal.region.CandidateComparator Date: Wed, 2 Nov 2016 15:00:53 +0000 (UTC) archived-at: Wed, 02 Nov 2016 15:00:55 -0000 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 Authored: Wed Nov 2 16:00:11 2016 +0100 Committer: Guillaume Nodet 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 { } // Compare revision capabilities. if ((c == 0) && cap1.getNamespace().equals(BundleNamespace.BUNDLE_NAMESPACE)) { - c = ((Comparable) 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) 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) 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 { 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 l1 = o1 instanceof List ? (List) o1 : Collections.singletonList((String) o1); + List 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); } }