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 ABCB3200D4E for ; Thu, 7 Dec 2017 17:50:02 +0100 (CET) Received: by cust-asf.ponee.io (Postfix) id AA629160C08; Thu, 7 Dec 2017 16:50:02 +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 A38C7160C0C for ; Thu, 7 Dec 2017 17:50:01 +0100 (CET) Received: (qmail 28642 invoked by uid 500); 7 Dec 2017 16:50:00 -0000 Mailing-List: contact commits-help@sling.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@sling.apache.org Delivered-To: mailing list commits@sling.apache.org Received: (qmail 28616 invoked by uid 99); 7 Dec 2017 16:50:00 -0000 Received: from ec2-52-202-80-70.compute-1.amazonaws.com (HELO gitbox.apache.org) (52.202.80.70) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 07 Dec 2017 16:50:00 +0000 Received: by gitbox.apache.org (ASF Mail Server at gitbox.apache.org, from userid 33) id C0ABE82015; Thu, 7 Dec 2017 16:49:59 +0000 (UTC) Date: Thu, 07 Dec 2017 16:50:00 +0000 To: "commits@sling.apache.org" Subject: [sling-org-apache-sling-adapter] 01/01: SLING-7194 AdapterManager sorts AdapterFactory implementations lowest ranking first MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit From: sseifert@apache.org In-Reply-To: <151266539952.23148.10405430566111332350@gitbox.apache.org> References: <151266539952.23148.10405430566111332350@gitbox.apache.org> X-Git-Host: gitbox.apache.org X-Git-Repo: sling-org-apache-sling-adapter X-Git-Refname: refs/heads/feature/SLING-7194-service-ranking X-Git-Reftype: branch X-Git-Rev: 4d88efa8563498416a3effefc9802cfa30fe15dc X-Git-NotificationType: diff X-Git-Multimail-Version: 1.5.dev Auto-Submitted: auto-generated Message-Id: <20171207164959.C0ABE82015@gitbox.apache.org> archived-at: Thu, 07 Dec 2017 16:50:02 -0000 This is an automated email from the ASF dual-hosted git repository. sseifert pushed a commit to branch feature/SLING-7194-service-ranking in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-adapter.git commit 4d88efa8563498416a3effefc9802cfa30fe15dc Author: sseifert AuthorDate: Thu Dec 7 17:49:48 2017 +0100 SLING-7194 AdapterManager sorts AdapterFactory implementations lowest ranking first --- .../internal/AdapterFactoryDescriptorMap.java | 13 ++++++ .../sling/adapter/internal/AdapterManagerTest.java | 48 +++++++++++----------- 2 files changed, 37 insertions(+), 24 deletions(-) diff --git a/src/main/java/org/apache/sling/adapter/internal/AdapterFactoryDescriptorMap.java b/src/main/java/org/apache/sling/adapter/internal/AdapterFactoryDescriptorMap.java index 4de3d08..c36f7dc 100644 --- a/src/main/java/org/apache/sling/adapter/internal/AdapterFactoryDescriptorMap.java +++ b/src/main/java/org/apache/sling/adapter/internal/AdapterFactoryDescriptorMap.java @@ -18,6 +18,7 @@ */ package org.apache.sling.adapter.internal; +import java.util.Comparator; import java.util.TreeMap; import org.apache.sling.api.adapter.AdapterFactory; @@ -41,4 +42,16 @@ public class AdapterFactoryDescriptorMap extends private static final long serialVersionUID = 2L; + public AdapterFactoryDescriptorMap() { + super(new Comparator>() { + @Override + public int compare(ServiceReference o1, ServiceReference o2) { + // compareTo of ServiceReference implements service-ranking lowest-first/service id highest-first + // but we want service-ranking highest-first/service id lowest-first as it is done e.g. for BundleContext.getService + // so explicitly reverse the comparison (SLING-7194) + return o2.compareTo(o1); + } + }); + } + } diff --git a/src/test/java/org/apache/sling/adapter/internal/AdapterManagerTest.java b/src/test/java/org/apache/sling/adapter/internal/AdapterManagerTest.java index 1208c85..635ab83 100644 --- a/src/test/java/org/apache/sling/adapter/internal/AdapterManagerTest.java +++ b/src/test/java/org/apache/sling/adapter/internal/AdapterManagerTest.java @@ -395,8 +395,8 @@ public class AdapterManagerTest { } @org.junit.Test public void testAdaptMultipleAdapterFactoriesServiceRanking() throws Exception { - final ServiceReference firstAdaptable = new ServiceReferenceImpl(1, new String[]{AdapterObject.class.getName()}, new String[]{ ParentInterface.class.getName(), FirstImplementation.class.getName()}); - final ServiceReference secondAdaptable = new ServiceReferenceImpl(2, new String[]{ AdapterObject.class.getName() }, new String[]{ParentInterface.class.getName(), SecondImplementation.class.getName()}); + final ServiceReference firstAdaptable = new ServiceReferenceImpl(2, new String[]{AdapterObject.class.getName()}, new String[]{ ParentInterface.class.getName(), FirstImplementation.class.getName()}); + final ServiceReference secondAdaptable = new ServiceReferenceImpl(1, new String[]{ AdapterObject.class.getName() }, new String[]{ParentInterface.class.getName(), SecondImplementation.class.getName()}); am.activate(this.createMultipleAdaptersComponentContext(firstAdaptable, secondAdaptable)); @@ -410,21 +410,21 @@ public class AdapterManagerTest { am.bindAdapterFactory(secondAdaptable); Object adapter = am.getAdapter(first, ParentInterface.class); - assertNotNull("Did not get an adapter back for first implementation (from ParentInterface), service ranking 1", adapter); - assertTrue("Did not get the correct adaptable back for first implementation, service ranking 1, ", adapter instanceof FirstImplementation); + assertNotNull("Did not get an adapter back for first implementation (from ParentInterface), service ranking 2", adapter); + assertTrue("Did not get the correct adaptable back for first implementation, service ranking 2, ", adapter instanceof FirstImplementation); adapter = am.getAdapter(first, FirstImplementation.class); - assertNotNull("Did not get an adapter back for first implementation, service ranking 1", adapter); - assertTrue("Did not get the correct adaptable back for first implementation, service ranking 1, ", adapter instanceof FirstImplementation); + assertNotNull("Did not get an adapter back for first implementation, service ranking 2", adapter); + assertTrue("Did not get the correct adaptable back for first implementation, service ranking 2, ", adapter instanceof FirstImplementation); adapter = am.getAdapter(second, SecondImplementation.class); - assertNotNull("Did not get an adapter back for second implementation, service ranking 2", adapter); - assertTrue("Did not get the correct adaptable back for second implementation, service ranking 2, ", adapter instanceof SecondImplementation); + assertNotNull("Did not get an adapter back for second implementation, service ranking 1", adapter); + assertTrue("Did not get the correct adaptable back for second implementation, service ranking 1, ", adapter instanceof SecondImplementation); } @org.junit.Test public void testAdaptMultipleAdapterFactoriesServiceRankingSecondHigherOrder() throws Exception { - final ServiceReference firstAdaptable = new ServiceReferenceImpl(2, new String[]{AdapterObject.class.getName()}, new String[]{ ParentInterface.class.getName(), FirstImplementation.class.getName()}); - final ServiceReference secondAdaptable = new ServiceReferenceImpl(1, new String[]{ AdapterObject.class.getName() }, new String[]{ParentInterface.class.getName(), SecondImplementation.class.getName()}); + final ServiceReference firstAdaptable = new ServiceReferenceImpl(1, new String[]{AdapterObject.class.getName()}, new String[]{ ParentInterface.class.getName(), FirstImplementation.class.getName()}); + final ServiceReference secondAdaptable = new ServiceReferenceImpl(2, new String[]{ AdapterObject.class.getName() }, new String[]{ParentInterface.class.getName(), SecondImplementation.class.getName()}); am.activate(this.createMultipleAdaptersComponentContext(firstAdaptable, secondAdaptable)); @@ -438,21 +438,21 @@ public class AdapterManagerTest { am.bindAdapterFactory(secondAdaptable); Object adapter = am.getAdapter(first, ParentInterface.class); - assertNotNull("Did not get an adapter back for second implementation (from ParentInterface), service ranking 1", adapter); - assertTrue("Did not get the correct adaptable back for second implementation, service ranking 1, ", adapter instanceof SecondImplementation); + assertNotNull("Did not get an adapter back for second implementation (from ParentInterface), service ranking 2", adapter); + assertTrue("Did not get the correct adaptable back for second implementation, service ranking 2, ", adapter instanceof SecondImplementation); adapter = am.getAdapter(first, FirstImplementation.class); - assertNotNull("Did not get an adapter back for first implementation, service ranking 1", adapter); - assertTrue("Did not get the correct adaptable back for first implementation, service ranking 1, ", adapter instanceof FirstImplementation); + assertNotNull("Did not get an adapter back for first implementation, service ranking 2", adapter); + assertTrue("Did not get the correct adaptable back for first implementation, service ranking 2, ", adapter instanceof FirstImplementation); adapter = am.getAdapter(second, SecondImplementation.class); - assertNotNull("Did not get an adapter back for second implementation, service ranking 2", adapter); - assertTrue("Did not get the correct adaptable back for second implementation, service ranking 2, ", adapter instanceof SecondImplementation); + assertNotNull("Did not get an adapter back for second implementation, service ranking 1", adapter); + assertTrue("Did not get the correct adaptable back for second implementation, service ranking 1, ", adapter instanceof SecondImplementation); } @org.junit.Test public void testAdaptMultipleAdapterFactoriesServiceRankingReverse() throws Exception { - final ServiceReference firstAdaptable = new ServiceReferenceImpl(1, new String[]{AdapterObject.class.getName()}, new String[]{ ParentInterface.class.getName(), FirstImplementation.class.getName()}); - final ServiceReference secondAdaptable = new ServiceReferenceImpl(2, new String[]{ AdapterObject.class.getName() }, new String[]{ParentInterface.class.getName(), SecondImplementation.class.getName()}); + final ServiceReference firstAdaptable = new ServiceReferenceImpl(2, new String[]{AdapterObject.class.getName()}, new String[]{ ParentInterface.class.getName(), FirstImplementation.class.getName()}); + final ServiceReference secondAdaptable = new ServiceReferenceImpl(1, new String[]{ AdapterObject.class.getName() }, new String[]{ParentInterface.class.getName(), SecondImplementation.class.getName()}); am.activate(this.createMultipleAdaptersComponentContext(firstAdaptable, secondAdaptable)); @@ -467,16 +467,16 @@ public class AdapterManagerTest { am.bindAdapterFactory(firstAdaptable); Object adapter = am.getAdapter(first, ParentInterface.class); - assertNotNull("Did not get an adapter back for first implementation (from ParentInterface), service ranking 1", adapter); - assertTrue("Did not get the correct adaptable back for first implementation, service ranking 1, ", adapter instanceof FirstImplementation); + assertNotNull("Did not get an adapter back for first implementation (from ParentInterface), service ranking 2", adapter); + assertTrue("Did not get the correct adaptable back for first implementation, service ranking 2, ", adapter instanceof FirstImplementation); adapter = am.getAdapter(first, FirstImplementation.class); - assertNotNull("Did not get an adapter back for first implementation, service ranking 1", adapter); - assertTrue("Did not get the correct adaptable back for first implementation, service ranking 1, ", adapter instanceof FirstImplementation); + assertNotNull("Did not get an adapter back for first implementation, service ranking 2", adapter); + assertTrue("Did not get the correct adaptable back for first implementation, service ranking 2, ", adapter instanceof FirstImplementation); adapter = am.getAdapter(second, SecondImplementation.class); - assertNotNull("Did not get an adapter back for second implementation, service ranking 2", adapter); - assertTrue("Did not get the correct adaptable back for second implementation, service ranking 2, ", adapter instanceof SecondImplementation); + assertNotNull("Did not get an adapter back for second implementation, service ranking 1", adapter); + assertTrue("Did not get the correct adaptable back for second implementation, service ranking 1, ", adapter instanceof SecondImplementation); } -- To stop receiving notification emails like this one, please contact "commits@sling.apache.org" .