aries-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From csie...@apache.org
Subject aries-jax-rs-whiteboard git commit: Proper extension ordering following the spec
Date Wed, 21 Feb 2018 15:29:55 GMT
Repository: aries-jax-rs-whiteboard
Updated Branches:
  refs/heads/master 3f9371a50 -> 92dcd0118


Proper extension ordering following the spec

We need to use CXF models directly in order to be able to sort by
ServiceReference in case of a tie.

Basically in this I am copying the same registration as the Feature did
but using our own ProviderInfo subclass containing the
ServiceReference. This way the provider ProviderInfoComparator can solve
the ties.


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/92dcd011
Tree: http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/tree/92dcd011
Diff: http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/diff/92dcd011

Branch: refs/heads/master
Commit: 92dcd0118ad377cbcbf0485523ae3522c1aabe5f
Parents: 3f9371a
Author: Carlos Sierra <csierra@apache.org>
Authored: Wed Feb 21 16:27:15 2018 +0100
Committer: Carlos Sierra <csierra@apache.org>
Committed: Wed Feb 21 16:27:15 2018 +0100

----------------------------------------------------------------------
 .../internal/CxfJaxrsServiceRegistrator.java    | 166 ++++++++++++++-----
 1 file changed, 125 insertions(+), 41 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/92dcd011/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 098f93b..2f61c89 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,6 +20,7 @@ package org.apache.aries.jax.rs.whiteboard.internal;
 import static java.util.stream.Collectors.toMap;
 import static org.apache.aries.jax.rs.whiteboard.internal.Utils.canonicalize;
 import static org.apache.aries.jax.rs.whiteboard.internal.Whiteboard.SUPPORTED_EXTENSION_INTERFACES;
+import static org.apache.cxf.jaxrs.provider.ProviderFactory.DEFAULT_FILTER_NAME_BINDING;
 
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -29,8 +30,10 @@ 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.Priorities;
 import javax.ws.rs.RuntimeType;
 import javax.ws.rs.core.Application;
 import javax.ws.rs.core.Feature;
@@ -41,6 +44,7 @@ import org.apache.aries.jax.rs.whiteboard.internal.Utils.ServiceReferenceResourc
 import org.apache.aries.jax.rs.whiteboard.internal.Utils.ServiceTuple;
 import org.apache.aries.osgi.functional.CachingServiceReference;
 import org.apache.cxf.Bus;
+import org.apache.cxf.common.util.ClassHelper;
 import org.apache.cxf.endpoint.Server;
 import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
 import org.apache.cxf.jaxrs.JAXRSServiceFactoryBean;
@@ -49,8 +53,13 @@ import org.apache.cxf.jaxrs.impl.ConfigurableImpl;
 import org.apache.cxf.jaxrs.lifecycle.ResourceProvider;
 import org.apache.cxf.jaxrs.model.ApplicationInfo;
 import org.apache.cxf.jaxrs.model.ClassResourceInfo;
+import org.apache.cxf.jaxrs.model.FilterProviderInfo;
 import org.apache.cxf.jaxrs.model.OperationResourceInfo;
+import org.apache.cxf.jaxrs.model.ProviderInfo;
+import org.apache.cxf.jaxrs.provider.ProviderFactory;
+import org.apache.cxf.jaxrs.provider.ProviderFactory.ProviderInfoClassComparator;
 import org.apache.cxf.jaxrs.provider.ServerConfigurableFactory;
+import org.apache.cxf.jaxrs.utils.AnnotationUtils;
 import org.apache.cxf.message.Message;
 import org.osgi.framework.ServiceReference;
 
@@ -70,10 +79,6 @@ public class CxfJaxrsServiceRegistrator {
         rewire();
     }
 
-    public Bus getBus() {
-        return _bus;
-    }
-
     public void add(ResourceProvider resourceProvider) {
         if (_closed) {
             return;
@@ -134,6 +139,10 @@ public class CxfJaxrsServiceRegistrator {
         return endpointType.cast(server);
     }
 
+    public Bus getBus() {
+        return _bus;
+    }
+
     public void remove(ResourceProvider resourceProvider) {
         if (_closed) {
             return;
@@ -154,14 +163,22 @@ public class CxfJaxrsServiceRegistrator {
         rewire();
     }
 
+    protected static Set<String> getFilterNameBindings(Bus bus, Object provider) {
+        Class<?> pClass = ClassHelper.getRealClass(bus, provider);
+        Set<String> names = AnnotationUtils.getNameBindings(pClass.getAnnotations());
+        if (names.isEmpty()) {
+            names = Collections.singleton(DEFAULT_FILTER_NAME_BINDING);
+        }
+        return names;
+    }
     private final Application _application;
     private final Bus _bus;
     private final Collection<ServiceTuple<?>> _providers;
     private final Collection<ResourceProvider> _services = new ArrayList<>();
-    private Map<String, Object> _properties;
     private volatile boolean _closed = false;
-    private Server _server;
     private JAXRSServerFactoryBean _jaxRsServerFactoryBean;
+    private Map<String, Object> _properties;
+    private Server _server;
 
     private static class ComparableResourceComparator
         implements ResourceComparator {
@@ -208,6 +225,65 @@ public class CxfJaxrsServiceRegistrator {
 
     }
 
+    private static class ProviderInfoComparator implements Comparator<ProviderInfo<?>>
{
+        public ProviderInfoComparator(
+            ProviderInfoClassComparator providerInfoClassComparator) {
+
+            _providerInfoClassComparator = providerInfoClassComparator;
+        }
+
+        @Override
+        public int compare(ProviderInfo<?> pi1, ProviderInfo<?> pi2) {
+            if (pi1 instanceof ServiceReferenceFilterProviderInfo<?>) {
+                if (pi2 instanceof ServiceReferenceFilterProviderInfo<?>) {
+                    ServiceReference serviceReference1 =
+                        ((ServiceReferenceFilterProviderInfo) pi1).
+                            getServiceReference();
+                    ServiceReference serviceReference2 =
+                        ((ServiceReferenceFilterProviderInfo) pi2).
+                            getServiceReference();
+
+                    return serviceReference1.compareTo(serviceReference2);
+                }
+                else {
+                    return -1;
+                }
+            }
+            else {
+                if (pi2 instanceof ServiceReferenceFilterProviderInfo<?>) {
+                    return 1;
+                }
+            }
+
+            return _providerInfoClassComparator.compare(pi1, pi2);
+        }
+
+        private final ProviderInfoClassComparator
+            _providerInfoClassComparator;
+    }
+
+    private static class ServiceReferenceFilterProviderInfo<T>
+        extends FilterProviderInfo<T> {
+
+        public ServiceReferenceFilterProviderInfo(
+            ServiceReference<?> serviceReference,
+            Class<?> resourceClass, Class<?> serviceClass, T provider, Bus bus,
+            Set<String> nameBindings, boolean dynamic, Map<Class<?>, Integer>
+                supportedContracts) {
+
+            super(resourceClass, serviceClass, provider, bus, nameBindings,
+                dynamic, supportedContracts);
+
+            _serviceReference = serviceReference;
+        }
+
+        public ServiceReference<?> getServiceReference() {
+            return _serviceReference;
+        }
+
+        private ServiceReference<?> _serviceReference;
+    }
+
     protected synchronized void rewire() {
         if (_server != null) {
             _server.destroy();
@@ -242,42 +318,43 @@ public class CxfJaxrsServiceRegistrator {
 
         _jaxRsServerFactoryBean.setStart(false);
 
-        _jaxRsServerFactoryBean.setProvider(
-            (Feature) featureContext -> {
-                for (ServiceTuple<?> provider : _providers) {
-                    CachingServiceReference<?> cachingServiceReference =
-                        provider.getCachingServiceReference();
-
-                    ServiceReference<?> serviceReference =
-                        cachingServiceReference.getServiceReference();
-
-                    int ranking = Utils.getRanking(cachingServiceReference);
-
-                    Class<?>[] interfaces = Arrays.stream(canonicalize(
-                        serviceReference.getProperty("objectClass")))
-                            .filter(SUPPORTED_EXTENSION_INTERFACES::containsKey)
-                            .map(SUPPORTED_EXTENSION_INTERFACES::get)
-                            .toArray(Class[]::new);
-
-                    if (ranking != 0) {
-                        Map<Class<?>, Integer> classesWithPriorities=
-                            Arrays.stream(interfaces).collect(
-                                toMap(c -> c, __ -> ranking)
-                            );
-
-                        featureContext.register(
-                            provider.getService(), classesWithPriorities);
-                    }
-                    else {
-                        featureContext.register(
-                                    provider.getService(),
-                                    interfaces);
-                    }
+        for (ServiceTuple<?> provider : _providers) {
+            CachingServiceReference<?> cachingServiceReference =
+                provider.getCachingServiceReference();
 
-                }
-                
-                return true;
-            });
+            ServiceReference<?> serviceReference =
+                cachingServiceReference.getServiceReference();
+
+            Object service = provider.getService();
+
+            if (service instanceof Feature) {
+                _jaxRsServerFactoryBean.setProvider(service);
+
+                continue;
+            }
+
+            Class<?> realClass = ClassHelper.getRealClass(getBus(), service);
+
+            Class<?>[] interfaces = Arrays.stream(canonicalize(
+                serviceReference.getProperty("objectClass")))
+                .filter(SUPPORTED_EXTENSION_INTERFACES::containsKey)
+                .map(SUPPORTED_EXTENSION_INTERFACES::get)
+                .toArray(Class[]::new);
+
+            Map<Class<?>, Integer> classesWithPriorities=
+                Arrays.stream(interfaces).collect(
+                    toMap(
+                        c -> c,
+                        __ -> AnnotationUtils.getBindingPriority(
+                            realClass))
+                );
+
+            _jaxRsServerFactoryBean.setProvider(
+                new ServiceReferenceFilterProviderInfo<>(
+                    serviceReference, realClass, realClass, service, getBus(),
+                    getFilterNameBindings(getBus(), service), false,
+                    classesWithPriorities));
+        }
 
         for (ResourceProvider resourceProvider: _services) {
             _jaxRsServerFactoryBean.setResourceProvider(resourceProvider);
@@ -290,6 +367,13 @@ public class CxfJaxrsServiceRegistrator {
         _jaxRsServerFactoryBean.setResourceComparator(
             new ComparableResourceComparator());
 
+        ProviderInfoClassComparator providerInfoClassComparator =
+            new ProviderInfoClassComparator(Object.class);
+
+        _jaxRsServerFactoryBean.setProviderComparator(
+            new ProviderInfoComparator(providerInfoClassComparator)
+        );
+
         _server = _jaxRsServerFactoryBean.create();
 
         ApplicationInfo applicationInfo = (ApplicationInfo)


Mime
View raw message