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 <[email protected]> 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<ServiceReference<AdapterFactory>>() { + @Override + public int compare(ServiceReference<AdapterFactory> o1, ServiceReference<AdapterFactory> 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 "[email protected]" <[email protected]>.
