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] Better support for custom mbr/mbw comparators
Date Wed, 05 Aug 2015 11:18:05 GMT
Repository: cxf
Updated Branches:
  refs/heads/3.0.x-fixes 9fe0b1b71 -> 9956be7a4


[CXF-6515] Better support for custom mbr/mbw comparators


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

Branch: refs/heads/3.0.x-fixes
Commit: 9956be7a4723e766b3cf866b376e1bac8b905846
Parents: 9fe0b1b
Author: Sergey Beryozkin <sberyozkin@gmail.com>
Authored: Wed Aug 5 12:16:53 2015 +0100
Committer: Sergey Beryozkin <sberyozkin@gmail.com>
Committed: Wed Aug 5 12:17:48 2015 +0100

----------------------------------------------------------------------
 .../cxf/jaxrs/AbstractJAXRSFactoryBean.java     | 17 +++++++---
 .../cxf/jaxrs/provider/ProviderFactory.java     | 28 ++++++++--------
 .../cxf/jaxrs/provider/ProviderFactoryTest.java | 34 ++++++++++++++++++++
 3 files changed, 61 insertions(+), 18 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/9956be7a/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 b9dfbbf..d467689 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,7 +71,8 @@ public class AbstractJAXRSFactoryBean extends AbstractEndpointFactory {
     protected List<String> schemaLocations;
     protected JAXRSServiceFactoryBean serviceFactory;
     protected List<Object> entityProviders = new LinkedList<Object>();
-    private Comparator<?> providerComparator;
+    private Comparator<?> messageReaderComparator;
+    private Comparator<?> messageWriterComparator;
     
     protected AbstractJAXRSFactoryBean(JAXRSServiceFactoryBean serviceFactory) {
         this.serviceFactory = serviceFactory;
@@ -325,8 +326,11 @@ public class AbstractJAXRSFactoryBean extends AbstractEndpointFactory
{
     }
     
     protected void setupFactory(ProviderFactory factory, Endpoint ep) { 
-        if (providerComparator != null) {
-            factory.setProviderComparator(providerComparator);
+        if (messageReaderComparator != null) {
+            factory.setMessageReaderComparator(messageReaderComparator);
+        }
+        if (messageWriterComparator != null) {
+            factory.setMessageWriterComparator(messageWriterComparator);
         }
         if (entityProviders != null) {
             factory.setUserProviders(entityProviders); 
@@ -432,8 +436,11 @@ public class AbstractJAXRSFactoryBean extends AbstractEndpointFactory
{
         }
     }
 
-    public void setProviderComparator(Comparator<?> providerComparator) {
-        this.providerComparator = providerComparator;
+    public void setMessageWriterComparator(Comparator<?> messageWriterComparator) {
+        this.messageWriterComparator = messageWriterComparator;
+    }
+    public void setMessageReaderComparator(Comparator<?> messageReaderComparator) {
+        this.messageReaderComparator = messageReaderComparator;
     }
     
     

http://git-wip-us.apache.org/repos/asf/cxf/blob/9956be7a/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 30d6945..029fc6f 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
@@ -114,7 +114,8 @@ public abstract class ProviderFactory {
     
     private Bus bus;
     
-    private Comparator<?> providerComparator;
+    private Comparator<?> messageWriterComparator;
+    private Comparator<?> messageReaderComparator;
     
     protected ProviderFactory(Bus bus) {
         this.bus = bus;
@@ -562,25 +563,25 @@ public abstract class ProviderFactory {
      * x/y;q=1.0 < x/y;q=0.7.
      */    
     private void sortReaders() {
-        if (providerComparator == null) {
+        if (messageReaderComparator == null) {
             Collections.sort(messageReaders, new MessageBodyReaderComparator());
         } else {
-            doCustomSort(messageReaders);
+            doCustomSort(messageReaderComparator, messageReaders);
         }
     }
     private <T> void sortWriters() {
-        if (providerComparator == null) {
+        if (messageWriterComparator == null) {
             Collections.sort(messageWriters, new MessageBodyWriterComparator());
         } else {
-            doCustomSort(messageWriters);
+            doCustomSort(messageWriterComparator, messageWriters);
         }
     }
     
-    private <T> void doCustomSort(List<?> listOfProviders) {
+    private static <T> void doCustomSort(Comparator<?> comparator, List<?>
listOfProviders) {
         @SuppressWarnings("unchecked")
-        List<T> theProviders = (List<T>)messageReaders;
+        List<T> theProviders = (List<T>)listOfProviders;
         @SuppressWarnings("unchecked")
-        Comparator<? super T> theComparator = (Comparator<? super T>)providerComparator;
+        Comparator<? super T> theComparator = (Comparator<? super T>)comparator;
         Collections.sort((List<T>)theProviders, theComparator);
     }
     
@@ -1174,11 +1175,12 @@ public abstract class ProviderFactory {
         return null;
     }
 
-    public Comparator<?> getProviderComparator() {
-        return providerComparator;
+    public void setMessageWriterComparator(Comparator<?> messageWriterComparator) {
+        this.messageWriterComparator = messageWriterComparator;
+        sortWriters();
     }
-
-    public void setProviderComparator(Comparator<?> providerComparator) {
-        this.providerComparator = providerComparator;
+    public void setMessageReaderComparator(Comparator<?> messageReaderComparator) {
+        this.messageReaderComparator = messageReaderComparator;
+        sortReaders();
     }
 }

http://git-wip-us.apache.org/repos/asf/cxf/blob/9956be7a/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 2d1e74c..535b463 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
@@ -27,6 +27,7 @@ import java.lang.annotation.Annotation;
 import java.lang.reflect.Type;
 import java.util.ArrayList;
 import java.util.Collections;
+import java.util.Comparator;
 import java.util.List;
 import java.util.Map;
 
@@ -99,6 +100,39 @@ public class ProviderFactoryTest extends Assert {
     }
     
     @Test
+    public void testCustomProviderSorting() {
+        ProviderFactory pf = ServerProviderFactory.getInstance();
+        Comparator<?> comp = new Comparator<ProviderInfo<?>>() {
+
+            @Override
+            public int compare(ProviderInfo<?> o1, ProviderInfo<?> o2) {
+                Object provider1 = o1.getProvider();
+                Object provider2 = o2.getProvider();
+                if (provider1 instanceof StringTextProvider) {
+                    return 1;
+                } else if (provider2 instanceof StringTextProvider) {
+                    return -1;
+                } else {
+                    return 0;
+                }
+            }    
+        
+        };
+        // 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);
+    }
+    
+    @Test
     public void testCustomJaxbProvider() {
         ProviderFactory pf = ServerProviderFactory.getInstance();
         JAXBElementProvider<Book> provider = new JAXBElementProvider<Book>();


Mime
View raw message