aries-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From csie...@apache.org
Subject [1/2] aries-jax-rs-whiteboard git commit: Reimplement resource ordering
Date Wed, 19 Jul 2017 17:18:07 GMT
Repository: aries-jax-rs-whiteboard
Updated Branches:
  refs/heads/master 006c69efc -> 3a2d75820


Reimplement resource ordering


Project: http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/repo
Commit: http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/commit/398ad838
Tree: http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/tree/398ad838
Diff: http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/diff/398ad838

Branch: refs/heads/master
Commit: 398ad838a442f5941cb425b74cccfb97cb51c789
Parents: 006c69e
Author: Carlos Sierra <csierra@apache.org>
Authored: Wed Jul 19 19:11:27 2017 +0200
Committer: Carlos Sierra <csierra@apache.org>
Committed: Wed Jul 19 19:11:27 2017 +0200

----------------------------------------------------------------------
 .../internal/CXFJaxRsServiceRegistrator.java    | 95 +++++++++++---------
 .../aries/jax/rs/whiteboard/internal/Utils.java | 93 +++++++++++--------
 2 files changed, 109 insertions(+), 79 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/398ad838/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/CXFJaxRsServiceRegistrator.java
----------------------------------------------------------------------
diff --git a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/CXFJaxRsServiceRegistrator.java
b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/CXFJaxRsServiceRegistrator.java
index 43c9f1a..8d6ee29 100644
--- a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/CXFJaxRsServiceRegistrator.java
+++ b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/CXFJaxRsServiceRegistrator.java
@@ -20,21 +20,24 @@ package org.apache.aries.jax.rs.whiteboard.internal;
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Collection;
-import java.util.Comparator;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
-import java.util.TreeSet;
 
 import javax.ws.rs.core.Application;
 
+import org.apache.aries.jax.rs.whiteboard.internal.Utils.ComparableResourceProvider;
 import org.apache.cxf.Bus;
 import org.apache.cxf.endpoint.Server;
 import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
+import org.apache.cxf.jaxrs.ext.ResourceComparator;
 import org.apache.cxf.jaxrs.lifecycle.ResourceProvider;
+import org.apache.cxf.jaxrs.model.ClassResourceInfo;
+import org.apache.cxf.jaxrs.model.OperationResourceInfo;
 import org.apache.cxf.jaxrs.provider.json.JSONProvider;
 import org.apache.cxf.jaxrs.utils.ResourceUtils;
+import org.apache.cxf.message.Message;
 import org.apache.cxf.service.model.EndpointInfo;
 import org.apache.cxf.transport.Destination;
 import org.apache.cxf.transport.DestinationFactory;
@@ -51,8 +54,7 @@ public class CXFJaxRsServiceRegistrator {
     private final Map<String, Object> _properties;
     private final Collection<Object> _providers = new ArrayList<>();
     private Server _server;
-    private final Collection<ResourceInformation<ServiceReference<?>>>
-        _services = new TreeSet<>(Comparator.reverseOrder());
+    private final Collection<ResourceProvider> _services = new ArrayList<>();
 
     private static final String CXF_ENDPOINT_ADDRESS = "CXF_ENDPOINT_ADDRESS";
 
@@ -81,13 +83,12 @@ public class CXFJaxRsServiceRegistrator {
         return properties;
     }
 
-    public void add(
-        ResourceInformation<ServiceReference<?>> resourceInformation) {
+    public void add(ResourceProvider resourceProvider) {
         if (_closed) {
             return;
         }
 
-        _services.add(resourceInformation);
+        _services.add(resourceProvider);
 
         rewire();
     }
@@ -114,13 +115,12 @@ public class CXFJaxRsServiceRegistrator {
         _closed = true;
     }
 
-    public void remove(
-        ResourceInformation<ServiceReference<?>> resourceInformation) {
+    public void remove(ResourceProvider resourceProvider) {
         if (_closed) {
             return;
         }
 
-        _services.remove(resourceInformation);
+        _services.remove(resourceProvider);
 
         rewire();
     }
@@ -135,6 +135,44 @@ public class CXFJaxRsServiceRegistrator {
         rewire();
     }
 
+    private static class ComparableResourceComparator
+        implements ResourceComparator {
+
+        @Override
+        public int compare(
+            ClassResourceInfo cri1, ClassResourceInfo cri2, Message message) {
+
+            ResourceProvider rp1 = cri1.getResourceProvider();
+            ResourceProvider rp2 = cri2.getResourceProvider();
+
+            if (rp1 instanceof ComparableResourceProvider &&
+                rp2 instanceof ComparableResourceProvider) {
+
+                return -((ComparableResourceProvider) rp1).compareTo(
+                    (ComparableResourceProvider) rp2);
+            }
+
+            if (rp1 instanceof ComparableResourceProvider) {
+                return 1;
+            }
+
+            if (rp2 instanceof ComparableResourceProvider) {
+                return -1;
+            }
+
+            return 0;
+        }
+
+        @Override
+        public int compare(
+            OperationResourceInfo oper1, OperationResourceInfo oper2,
+            Message message) {
+
+            return 0;
+        }
+
+    }
+
     protected synchronized void rewire() {
         if (_server != null) {
             _server.destroy();
@@ -176,13 +214,15 @@ public class CXFJaxRsServiceRegistrator {
             jaxRsServerFactoryBean.setProvider(provider);
         }
 
-        for (ResourceInformation<?> resourceInformation : _services) {
-            jaxRsServerFactoryBean.setResourceProvider(
-                resourceInformation.getResourceProvider());
+        for (ResourceProvider resourceProvider: _services) {
+            jaxRsServerFactoryBean.setResourceProvider(resourceProvider);
         }
 
         jaxRsServerFactoryBean.setAddress(address);
 
+        jaxRsServerFactoryBean.setResourceComparator(
+            new ComparableResourceComparator());
+
         _server = jaxRsServerFactoryBean.create();
 
         _server.start();
@@ -198,35 +238,6 @@ public class CXFJaxRsServiceRegistrator {
         return endpointType.cast(server);
     }
 
-    public static class ResourceInformation<T extends Comparable<? super T>>
-        implements Comparable<ResourceInformation<T>> {
-
-        private final T _comparable;
-        private final ResourceProvider _resourceProvider;
-
-        public ResourceInformation(
-            T comparable, ResourceProvider resourceProvider) {
-            _comparable = comparable;
-
-            _resourceProvider = resourceProvider;
-        }
-
-        public ResourceProvider getResourceProvider() {
-            return _resourceProvider;
-        }
-
-        @Override
-        public int compareTo(ResourceInformation<T> resourceInformation) {
-
-            if (resourceInformation == null) {
-                return 1;
-            }
-
-            return _comparable.compareTo(resourceInformation._comparable);
-        }
-
-    }
-
     /**
      * This class exists as a workaround for
      * https://issues.apache.org/jira/browse/CXF-7409

http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/398ad838/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/Utils.java
----------------------------------------------------------------------
diff --git a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/Utils.java
b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/Utils.java
index 7b44394..69bbe93 100644
--- a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/Utils.java
+++ b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/Utils.java
@@ -17,7 +17,6 @@
 
 package org.apache.aries.jax.rs.whiteboard.internal;
 
-import org.apache.aries.jax.rs.whiteboard.internal.CXFJaxRsServiceRegistrator.ResourceInformation;
 import org.apache.aries.osgi.functional.Event;
 import org.apache.aries.osgi.functional.OSGi;
 import org.apache.cxf.Bus;
@@ -127,17 +126,15 @@ public class Utils {
         return program.route(new RepeatInOrderRouter<>());
     }
 
-    public static <T> OSGi<ResourceInformation<ServiceReference<?>>>
+    public static <T> OSGi<ResourceProvider>
         registerEndpoint(
             ServiceReference<?> serviceReference,
             CXFJaxRsServiceRegistrator registrator,
             ServiceObjects<T> serviceObjects) {
 
         ResourceProvider resourceProvider = getResourceProvider(serviceObjects);
-        ResourceInformation<ServiceReference<?>> resourceInformation =
-            new ResourceInformation<>(serviceReference, resourceProvider);
-        registrator.add(resourceInformation);
-        return just(resourceInformation);
+        registrator.add(resourceProvider);
+        return just(resourceProvider);
     }
 
     public static String safeToString(Object object) {
@@ -147,43 +144,16 @@ public class Utils {
     public static <T> ResourceProvider getResourceProvider(
         ServiceObjects<T> serviceObjects) {
 
-        return new ResourceProvider() {
+        ServiceReference<T> serviceReference = serviceObjects.getServiceReference();
 
-            @Override
-            public Object getInstance(Message m) {
-                return serviceObjects.getService();
-            }
-
-            @Override
-            @SuppressWarnings("unchecked")
-            public void releaseInstance(Message m, Object o) {
-                serviceObjects.ungetService((T)o);
-            }
-
-            @Override
-            public Class<?> getResourceClass() {
-                T service = serviceObjects.getService();
-
-                try {
-                    return service.getClass();
-                }
-                finally {
-                    serviceObjects.ungetService(service);
-                }
-            }
-
-            @Override
-            public boolean isSingleton() {
-                return false;
-            }
-        };
+        return new ComparableResourceProvider(serviceReference, serviceObjects);
     }
 
     public static void unregisterEndpoint(
         CXFJaxRsServiceRegistrator registrator,
-        ResourceInformation<ServiceReference<?>> resourceInformation) {
+        ResourceProvider resourceProvider) {
 
-        registrator.remove(resourceInformation);
+        registrator.remove(resourceProvider);
     }
 
     private static class RepeatInOrderRouter<T extends Comparable<? super T>>
@@ -229,4 +199,53 @@ public class Utils {
 
     }
 
+    public static class ComparableResourceProvider
+        implements ResourceProvider, Comparable<ComparableResourceProvider> {
+
+        private ServiceReference<?> _serviceReference;
+        private final ServiceObjects<?> _serviceObjects;
+
+        public ComparableResourceProvider(
+            ServiceReference<?> serviceReference,
+            ServiceObjects<?> serviceObjects) {
+            _serviceReference = serviceReference;
+
+            _serviceObjects = serviceObjects;
+        }
+
+        @Override
+        public int compareTo(ComparableResourceProvider o) {
+            return _serviceReference.compareTo(o._serviceReference);
+        }
+
+        @Override
+        public Object getInstance(Message m) {
+            return _serviceObjects.getService();
+        }
+
+        @Override
+        @SuppressWarnings("unchecked")
+        public void releaseInstance(Message m, Object o) {
+            ((ServiceObjects)_serviceObjects).ungetService(o);
+        }
+
+        @Override
+        public Class<?> getResourceClass() {
+            Object service = _serviceObjects.getService();
+
+            try {
+                return service.getClass();
+            }
+            finally {
+                ((ServiceObjects)_serviceObjects).ungetService(service);
+            }
+        }
+
+        @Override
+        public boolean isSingleton() {
+            return false;
+        }
+
+    }
+
 }


Mime
View raw message