cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject cxf git commit: [CXF-6515] Returning to a single custom comparator setter and checking the generic types as suggested by Romain
Date Wed, 12 Aug 2015 13:41:09 GMT
Repository: cxf
Updated Branches:
  refs/heads/master 507b390c5 -> 405059c0c


[CXF-6515] Returning to a single custom comparator setter and checking the generic types as
suggested by Romain


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

Branch: refs/heads/master
Commit: 405059c0c0801c84b4eb0e91c6c7819f57aad23d
Parents: 507b390
Author: Sergey Beryozkin <sberyozkin@talend.com>
Authored: Wed Aug 12 14:40:43 2015 +0100
Committer: Sergey Beryozkin <sberyozkin@talend.com>
Committed: Wed Aug 12 14:40:43 2015 +0100

----------------------------------------------------------------------
 .../cxf/jaxrs/AbstractJAXRSFactoryBean.java     | 18 ++---
 .../cxf/jaxrs/provider/ProviderFactory.java     | 45 +++++++++----
 .../cxf/jaxrs/provider/ProviderFactoryTest.java | 71 +++++++++++++++++++-
 3 files changed, 105 insertions(+), 29 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/405059c0/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/AbstractJAXRSFactoryBean.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/AbstractJAXRSFactoryBean.java
b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/AbstractJAXRSFactoryBean.java
index d467689..0fb8fae 100644
--- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/AbstractJAXRSFactoryBean.java
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/AbstractJAXRSFactoryBean.java
@@ -71,8 +71,7 @@ public class AbstractJAXRSFactoryBean extends AbstractEndpointFactory {
     protected List<String> schemaLocations;
     protected JAXRSServiceFactoryBean serviceFactory;
     protected List<Object> entityProviders = new LinkedList<Object>();
-    private Comparator<?> messageReaderComparator;
-    private Comparator<?> messageWriterComparator;
+    private Comparator<?> providerComparator;
     
     protected AbstractJAXRSFactoryBean(JAXRSServiceFactoryBean serviceFactory) {
         this.serviceFactory = serviceFactory;
@@ -326,11 +325,8 @@ public class AbstractJAXRSFactoryBean extends AbstractEndpointFactory
{
     }
     
     protected void setupFactory(ProviderFactory factory, Endpoint ep) { 
-        if (messageReaderComparator != null) {
-            factory.setMessageReaderComparator(messageReaderComparator);
-        }
-        if (messageWriterComparator != null) {
-            factory.setMessageWriterComparator(messageWriterComparator);
+        if (providerComparator != null) {
+            factory.setProviderComparator(providerComparator);
         }
         if (entityProviders != null) {
             factory.setUserProviders(entityProviders); 
@@ -435,12 +431,8 @@ public class AbstractJAXRSFactoryBean extends AbstractEndpointFactory
{
             serviceFactory.setUserResourcesWithServiceClass(resources, sClasses);
         }
     }
-
-    public void setMessageWriterComparator(Comparator<?> messageWriterComparator) {
-        this.messageWriterComparator = messageWriterComparator;
-    }
-    public void setMessageReaderComparator(Comparator<?> messageReaderComparator) {
-        this.messageReaderComparator = messageReaderComparator;
+    public void setProviderComparator(Comparator<?> providerComparator) {
+        this.providerComparator = providerComparator;
     }
     
     

http://git-wip-us.apache.org/repos/asf/cxf/blob/405059c0/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java
b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java
index 70333b7..4529ceb 100644
--- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java
@@ -25,6 +25,7 @@ import java.lang.reflect.Method;
 import java.lang.reflect.ParameterizedType;
 import java.lang.reflect.Type;
 import java.lang.reflect.TypeVariable;
+import java.lang.reflect.WildcardType;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
@@ -114,8 +115,7 @@ public abstract class ProviderFactory {
     
     private Bus bus;
     
-    private Comparator<?> messageWriterComparator;
-    private Comparator<?> messageReaderComparator;
+    private Comparator<?> providerComparator;
     
     protected ProviderFactory(Bus bus) {
         this.bus = bus;
@@ -564,25 +564,45 @@ public abstract class ProviderFactory {
      * x/y;q=1.0 < x/y;q=0.7.
      */    
     private void sortReaders() {
-        if (messageReaderComparator == null) {
+        if (!customComparatorAvailable(MessageBodyReader.class)) {
             Collections.sort(messageReaders, new MessageBodyReaderComparator());
         } else {
-            doCustomSort(messageReaderComparator, messageReaders);
+            doCustomSort(messageReaders);
         }
     }
     private <T> void sortWriters() {
-        if (messageWriterComparator == null) {
+        if (!customComparatorAvailable(MessageBodyWriter.class)) {
             Collections.sort(messageWriters, new MessageBodyWriterComparator());
         } else {
-            doCustomSort(messageWriterComparator, messageWriters);
+            doCustomSort(messageWriters);
+        }
+    }
+    
+    private boolean customComparatorAvailable(Class<?> providerClass) {
+        if (providerComparator != null) {
+            Type type = ((ParameterizedType)providerComparator.getClass()
+                .getGenericInterfaces()[0]).getActualTypeArguments()[0];
+            if (type instanceof ParameterizedType) {
+                ParameterizedType pt = (ParameterizedType)type;
+                if (pt.getRawType() == ProviderInfo.class) {
+                    Type type2 = pt.getActualTypeArguments()[0];
+                    if (type2 == providerClass
+                        || type2 instanceof WildcardType
+                        || type2 instanceof ParameterizedType 
+                           && ((ParameterizedType)type2).getRawType() == providerClass)
{
+                        return true;
+                    }
+                }
+            }
         }
+        return false;
     }
     
-    private static <T> void doCustomSort(Comparator<?> comparator, List<?>
listOfProviders) {
+    private <T> void doCustomSort(List<?> listOfProviders) {
         @SuppressWarnings("unchecked")
         List<T> theProviders = (List<T>)listOfProviders;
         @SuppressWarnings("unchecked")
-        Comparator<? super T> theComparator = (Comparator<? super T>)comparator;
+        Comparator<? super T> theComparator = (Comparator<? super T>)providerComparator;
         Collections.sort((List<T>)theProviders, theComparator);
     }
     
@@ -1185,12 +1205,9 @@ public abstract class ProviderFactory {
         return null;
     }
 
-    public void setMessageWriterComparator(Comparator<?> messageWriterComparator) {
-        this.messageWriterComparator = messageWriterComparator;
-        sortWriters();
-    }
-    public void setMessageReaderComparator(Comparator<?> messageReaderComparator) {
-        this.messageReaderComparator = messageReaderComparator;
+    public void setProviderComparator(Comparator<?> providerComparator) {
+        this.providerComparator = providerComparator;
         sortReaders();
+        sortWriters();
     }
 }

http://git-wip-us.apache.org/repos/asf/cxf/blob/405059c0/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/ProviderFactoryTest.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/ProviderFactoryTest.java
b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/ProviderFactoryTest.java
index ddedbc1..199f227 100644
--- a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/ProviderFactoryTest.java
+++ b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/ProviderFactoryTest.java
@@ -120,19 +120,86 @@ public class ProviderFactoryTest extends Assert {
             }    
         
         };
+        pf.setProviderComparator(comp);
+        
         // writers
-        pf.setMessageWriterComparator(comp);
         List<ProviderInfo<MessageBodyWriter<?>>> writers = pf.getMessageWriters();
         assertEquals(8, writers.size());
         Object lastWriter = writers.get(7).getProvider();
         assertTrue(lastWriter instanceof StringTextProvider);
         //readers
-        pf.setMessageReaderComparator(comp);
         List<ProviderInfo<MessageBodyReader<?>>> readers = pf.getMessageReaders();
         assertEquals(8, readers.size());
         Object lastReader = readers.get(7).getProvider();
         assertTrue(lastReader instanceof StringTextProvider);
     }
+    @SuppressWarnings("rawtypes")
+    @Test
+    public void testCustomProviderSortingMBROnly() {
+        ProviderFactory pf = ServerProviderFactory.getInstance();
+        Comparator<ProviderInfo<MessageBodyReader>> comp = 
+            new Comparator<ProviderInfo<MessageBodyReader>>() {
+
+            @Override
+            public int compare(ProviderInfo<MessageBodyReader> o1, ProviderInfo<MessageBodyReader>
o2) {
+                MessageBodyReader<?> provider1 = o1.getProvider();
+                MessageBodyReader<?> provider2 = o2.getProvider();
+                if (provider1 instanceof StringTextProvider) {
+                    return 1;
+                } else if (provider2 instanceof StringTextProvider) {
+                    return -1;
+                } else {
+                    return 0;
+                }
+            }    
+        
+        };
+        pf.setProviderComparator(comp);
+        
+        // writers
+        List<ProviderInfo<MessageBodyWriter<?>>> writers = pf.getMessageWriters();
+        assertEquals(8, writers.size());
+        Object lastWriter = writers.get(7).getProvider();
+        assertFalse(lastWriter instanceof StringTextProvider);
+        //readers
+        List<ProviderInfo<MessageBodyReader<?>>> readers = pf.getMessageReaders();
+        assertEquals(8, readers.size());
+        Object lastReader = readers.get(7).getProvider();
+        assertTrue(lastReader instanceof StringTextProvider);
+    }
+    @Test
+    public void testCustomProviderSortingMBWOnly() {
+        ProviderFactory pf = ServerProviderFactory.getInstance();
+        Comparator<ProviderInfo<MessageBodyWriter<?>>> comp = 
+            new Comparator<ProviderInfo<MessageBodyWriter<?>>>() {
+
+            @Override
+            public int compare(ProviderInfo<MessageBodyWriter<?>> o1, ProviderInfo<MessageBodyWriter<?>>
o2) {
+                MessageBodyWriter<?> provider1 = o1.getProvider();
+                MessageBodyWriter<?> provider2 = o2.getProvider();
+                if (provider1 instanceof StringTextProvider) {
+                    return 1;
+                } else if (provider2 instanceof StringTextProvider) {
+                    return -1;
+                } else {
+                    return 0;
+                }
+            }    
+        
+        };
+        pf.setProviderComparator(comp);
+        
+        // writers
+        List<ProviderInfo<MessageBodyWriter<?>>> writers = pf.getMessageWriters();
+        assertEquals(8, writers.size());
+        Object lastWriter = writers.get(7).getProvider();
+        assertTrue(lastWriter instanceof StringTextProvider);
+        //readers
+        List<ProviderInfo<MessageBodyReader<?>>> readers = pf.getMessageReaders();
+        assertEquals(8, readers.size());
+        Object lastReader = readers.get(7).getProvider();
+        assertFalse(lastReader instanceof StringTextProvider);
+    }
     
     @Test
     public void testCustomProviderSortingWithBus() {


Mime
View raw message