Repository: aries-jax-rs-whiteboard
Updated Branches:
  refs/heads/master 082b64a6f -> 56dd71be4


Name clashing must happen among all types of services

Section 151.3


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

Branch: refs/heads/master
Commit: 0ab8db67bc71d83f0581f770f656c6122874b007
Parents: 082b64a
Author: Carlos Sierra <[email protected]>
Authored: Mon Feb 19 11:56:55 2018 +0100
Committer: Carlos Sierra <[email protected]>
Committed: Mon Feb 19 11:56:55 2018 +0100

----------------------------------------------------------------------
 .../internal/AriesJaxrsServiceRuntime.java      |  81 +++++++---
 .../jax/rs/whiteboard/internal/Whiteboard.java  | 161 +++++++++++++------
 2 files changed, 172 insertions(+), 70 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/0ab8db67/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/AriesJaxrsServiceRuntime.java
----------------------------------------------------------------------
diff --git 
a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/AriesJaxrsServiceRuntime.java
 
b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/AriesJaxrsServiceRuntime.java
index 11cb1ba..b40fedc 100644
--- 
a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/AriesJaxrsServiceRuntime.java
+++ 
b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/AriesJaxrsServiceRuntime.java
@@ -117,11 +117,23 @@ public class AriesJaxrsServiceRuntime implements 
JaxrsServiceRuntime {
     }
 
     public void addClashingApplication(
-        CachingServiceReference<Application> serviceReference) {
+        CachingServiceReference<?> serviceReference) {
 
         _clashingApplications.add(serviceReference);
     }
 
+    public void addClashingExtension(
+        CachingServiceReference<?> serviceReference) {
+
+        _clashingExtensions.add(serviceReference);
+    }
+
+    public void addClashingResource(
+        CachingServiceReference<?> serviceReference) {
+
+        _clashingResources.add(serviceReference);
+    }
+
     public void addDependentApplication(
         CachingServiceReference<Application> applicationReference) {
 
@@ -231,24 +243,29 @@ public class AriesJaxrsServiceRuntime implements 
JaxrsServiceRuntime {
 
         runtimeDTO.failedResourceDTOs =
             Stream.concat(
-                unreferenciableEndpointsDTOStream(),
+                clashingResourcesDTOStream(),
                 Stream.concat(
-                    dependentServiceStreamDTO(),
+                    unreferenciableEndpointsDTOStream(),
                     Stream.concat(
-                        applicationDependentResourcesDTOStream(),
-                        erroredEndpointsStreamDTO()))
+                        dependentServiceStreamDTO(),
+                        Stream.concat(
+                            applicationDependentResourcesDTOStream(),
+                            erroredEndpointsStreamDTO())))
             ).toArray(
                 FailedResourceDTO[]::new
             );
 
-        runtimeDTO.failedExtensionDTOs = Stream.concat(
-                unreferenciableExtensionsDTOStream(),
+        runtimeDTO.failedExtensionDTOs =
+            Stream.concat(
+                clashingExtensionsDTOStream(),
                 Stream.concat(
-                    applicationDependentExtensionsDTOStream(),
+                    unreferenciableExtensionsDTOStream(),
                     Stream.concat(
-                        erroredExtensionsDTOStream(),
-                        Stream.concat(dependentExtensionsStreamDTO(),
-                            invalidExtensionsDTOStream())))
+                        applicationDependentExtensionsDTOStream(),
+                        Stream.concat(
+                            erroredExtensionsDTOStream(),
+                            Stream.concat(dependentExtensionsStreamDTO(),
+                                invalidExtensionsDTOStream()))))
             ).toArray(
                 FailedExtensionDTO[]::new
             );
@@ -291,11 +308,17 @@ public class AriesJaxrsServiceRuntime implements 
JaxrsServiceRuntime {
     }
 
     public void removeClashingApplication(
-        CachingServiceReference<Application> serviceReference) {
+        CachingServiceReference<?> serviceReference) {
 
         _clashingApplications.remove(serviceReference);
     }
 
+    public void removeClashingExtension(
+        CachingServiceReference<?> serviceReference) {
+
+        _clashingExtensions.remove(serviceReference);
+    }
+
     public void removeClashingResource(
         CachingServiceReference<?> serviceReference) {
 
@@ -397,7 +420,11 @@ public class AriesJaxrsServiceRuntime implements 
JaxrsServiceRuntime {
         _applicationExtensions = new ConcurrentHashMap<>();
     private ConcurrentHashMap<String, ApplicationRuntimeInformation>
         _applications = new ConcurrentHashMap<>();
-    private Collection<CachingServiceReference<Application>> 
_clashingApplications =
+    private Collection<CachingServiceReference<?>> _clashingApplications =
+        new CopyOnWriteArrayList<>();
+    private Collection<CachingServiceReference<?>> _clashingExtensions =
+        new CopyOnWriteArrayList<>();
+    private Collection<CachingServiceReference<?>> _clashingResources =
         new CopyOnWriteArrayList<>();
     private volatile ApplicationRuntimeInformation 
_defaultApplicationProperties;
     private Set<CachingServiceReference<Application>> _dependentApplications =
@@ -424,7 +451,7 @@ public class AriesJaxrsServiceRuntime implements 
JaxrsServiceRuntime {
         new CopyOnWriteArrayList<>();
 
     private static FailedApplicationDTO buildFailedApplicationDTO(
-        int reason, CachingServiceReference<Application> serviceReference) {
+        int reason, CachingServiceReference<?> serviceReference) {
 
         FailedApplicationDTO failedApplicationDTO = new FailedApplicationDTO();
 
@@ -462,7 +489,7 @@ public class AriesJaxrsServiceRuntime implements 
JaxrsServiceRuntime {
         baseDTO.name = getApplicationName(serviceReference::getProperty);
         baseDTO.serviceId = (Long)serviceReference.getProperty(
             "service.id");
-        
+
         return baseDTO;
     }
 
@@ -702,6 +729,18 @@ public class AriesJaxrsServiceRuntime implements 
JaxrsServiceRuntime {
         );
     }
 
+    private Stream<FailedExtensionDTO> clashingExtensionsDTOStream() {
+        return _clashingExtensions.stream().map(
+            sr -> buildFailedExtensionDTO(
+                DTOConstants.FAILURE_REASON_DUPLICATE_NAME, sr));
+    }
+
+    private Stream<FailedResourceDTO> clashingResourcesDTOStream() {
+        return _clashingResources.stream().map(
+            sr -> buildFailedResourceDTO(
+                DTOConstants.FAILURE_REASON_DUPLICATE_NAME, sr));
+    }
+
     private Stream<FailedApplicationDTO> dependentApplicationsDTOStream() {
         return _dependentApplications.stream().map(
             sr -> buildFailedApplicationDTO(
@@ -823,7 +862,7 @@ public class AriesJaxrsServiceRuntime implements 
JaxrsServiceRuntime {
         public int hashCode() {
             return _cachingServiceReference.hashCode();
         }
-        CachingServiceReference _cachingServiceReference;        @Override
+        CachingServiceReference _cachingServiceReference;                Bus 
_bus;@Override
         public boolean equals(Object o) {
             if (this == o) return true;
             if (o == null || getClass() != o.getClass()) return false;
@@ -833,9 +872,9 @@ public class AriesJaxrsServiceRuntime implements 
JaxrsServiceRuntime {
             return _cachingServiceReference.equals(
                 that._cachingServiceReference);
         }
-        Bus _bus;
         Class<?> _class;
 
+
     }
 
     private static class ExtensionRuntimeInformation {
@@ -846,11 +885,11 @@ public class AriesJaxrsServiceRuntime implements 
JaxrsServiceRuntime {
             _cachingServiceReference = cachingServiceReference;
             _class = aClass;
         }
-        CachingServiceReference _cachingServiceReference;        @Override
+        CachingServiceReference _cachingServiceReference;        Class<?> 
_class;@Override
         public int hashCode() {
             return _cachingServiceReference.hashCode();
         }
-        Class<?> _class;        @Override
+                @Override
         public boolean equals(Object o) {
             if (this == o) return true;
             if (o == null || getClass() != o.getClass()) return false;
@@ -873,11 +912,11 @@ public class AriesJaxrsServiceRuntime implements 
JaxrsServiceRuntime {
             _cachingServiceReference = cachingServiceReference;
             _cxfJaxRsServiceRegistrator = cxfJaxRsServiceRegistrator;
         }
-        CachingServiceReference _cachingServiceReference;        @Override
+        CachingServiceReference _cachingServiceReference;        
CxfJaxrsServiceRegistrator _cxfJaxRsServiceRegistrator;@Override
         public int hashCode() {
             return _cachingServiceReference.hashCode();
         }
-        CxfJaxrsServiceRegistrator _cxfJaxRsServiceRegistrator;        
@Override
+                @Override
         public boolean equals(Object o) {
             if (this == o) return true;
             if (o == null || getClass() != o.getClass()) return false;

http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/0ab8db67/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/Whiteboard.java
----------------------------------------------------------------------
diff --git 
a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/Whiteboard.java
 
b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/Whiteboard.java
index b74faa0..b7e2433 100644
--- 
a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/Whiteboard.java
+++ 
b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/Whiteboard.java
@@ -29,6 +29,7 @@ import org.apache.cxf.transport.servlet.CXFNonSpringServlet;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.Constants;
 import org.osgi.framework.Filter;
+import org.osgi.framework.FrameworkUtil;
 import org.osgi.framework.InvalidSyntaxException;
 import org.osgi.framework.ServiceReference;
 import org.osgi.framework.ServiceRegistration;
@@ -94,7 +95,6 @@ import static 
org.osgi.service.http.whiteboard.HttpWhiteboardConstants.HTTP_WHIT
 import static 
org.osgi.service.jaxrs.runtime.JaxrsServiceRuntimeConstants.JAX_RS_SERVICE_ENDPOINT;
 import static 
org.osgi.service.jaxrs.whiteboard.JaxrsWhiteboardConstants.JAX_RS_APPLICATION_BASE;
 import static 
org.osgi.service.jaxrs.whiteboard.JaxrsWhiteboardConstants.JAX_RS_APPLICATION_SELECT;
-import static 
org.osgi.service.jaxrs.whiteboard.JaxrsWhiteboardConstants.JAX_RS_DEFAULT_APPLICATION;
 import static 
org.osgi.service.jaxrs.whiteboard.JaxrsWhiteboardConstants.JAX_RS_EXTENSION;
 import static 
org.osgi.service.jaxrs.whiteboard.JaxrsWhiteboardConstants.JAX_RS_EXTENSION_SELECT;
 import static 
org.osgi.service.jaxrs.whiteboard.JaxrsWhiteboardConstants.JAX_RS_NAME;
@@ -122,8 +122,6 @@ public class Whiteboard {
     static final String DEFAULT_NAME = ".default";
     private static final Function<CachingServiceReference<Application>, String>
         APPLICATION_BASE = sr -> getApplicationBase(sr::getProperty);
-    private static final Function<CachingServiceReference<Application>, String>
-        APPLICATION_NAME = sr -> getApplicationName(sr::getProperty);
 
     private final AriesJaxrsServiceRuntime _runtime;
     private final Map<String, ?> _configurationMap;
@@ -149,10 +147,8 @@ public class Whiteboard {
         _program =
             all(
                 ignore(registerDefaultApplication()),
-                ignore(applications()),
-                ignore(applicationResources()),
-                ignore(applicationExtensions()
-            ));
+                ignore(getAllServices())
+            );
     }
 
     public static Whiteboard createWhiteboard(
@@ -180,11 +176,12 @@ public class Whiteboard {
         }
     }
 
-    private OSGi<?> applicationExtensions() {
+    private OSGi<?> applicationExtensions(
+        OSGi<CachingServiceReference<Object>> extensions) {
+
         return
             onlySupportedInterfaces(
-                    countChanges(
-                        getApplicationExtensionsForWhiteboard(), _counter),
+                    extensions,
                     _runtime::addInvalidExtension,
                     _runtime::removeInvalidExtension).
                 flatMap(resourceReference ->
@@ -202,10 +199,11 @@ public class Whiteboard {
         )));
     }
 
-    private OSGi<?> applicationResources() {
+    private OSGi<?> applicationResources(
+        OSGi<CachingServiceReference<Object>> resources) {
+
         return
-            countChanges(getResourcesForWhiteboard(), _counter).
-                flatMap(resourceReference ->
+            resources.flatMap(resourceReference ->
             chooseApplication(
                 resourceReference, this::defaultApplication,
                 _runtime::addApplicationDependentResource,
@@ -220,22 +218,72 @@ public class Whiteboard {
             )));
     }
 
-    private OSGi<?> applications() {
+    @SuppressWarnings("unchecked")
+    private OSGi<?> getAllServices() {
+        return
+            highestPer(
+                sr -> getApplicationName(sr::getProperty),
+                all(
+                    countChanges(getResourcesForWhiteboard(), _counter),
+                    countChanges(getApplicationExtensionsForWhiteboard(), 
_counter),
+                    countChanges(getApplicationsForWhiteboard(), _counter)
+                ),
+                this::registerShadowedService,
+                this::unregisterShadowedService
+            ).distribute(
+                p -> ignore(applications(p.filter(this::isApplication))),
+                p -> ignore(applicationResources(p.filter(this::isResource))),
+                p -> ignore(applicationExtensions(p.filter(this::isExtension)))
+            );
+    }
+
+    private boolean isApplication(CachingServiceReference<?> sr) {
+        return _applicationsFilter.match(sr.getServiceReference());
+    }
+
+    private boolean isExtension(CachingServiceReference<?> sr) {
+        return _extensionsFilter.match(sr.getServiceReference());
+    }
+
+    private boolean isResource(CachingServiceReference<?> sr) {
+        return _resourcesFilter.match(sr.getServiceReference());
+    }
+
+    private void registerShadowedService(CachingServiceReference<?> sr) {
+        if (isApplication(sr)) {
+            _runtime.addClashingApplication(sr);
+        }
+        if (isExtension(sr)) {
+            _runtime.addClashingExtension(sr);
+        }
+        if (isResource(sr)) {
+            _runtime.addClashingResource(sr);
+        }
+    }
+
+    private void unregisterShadowedService(CachingServiceReference<?> sr) {
+        if (isApplication(sr)) {
+            _runtime.removeClashingApplication(sr);
+        }
+        if (isExtension(sr)) {
+            _runtime.removeClashingExtension(sr);
+        }
+        if (isApplication(sr)) {
+            _runtime.removeClashingResource(sr);
+        }
+    }
+
+    private OSGi<?> applications(
+        OSGi<CachingServiceReference<Object>> applications) {
+
         OSGi<CachingServiceReference<Application>> applicationsForWhiteboard =
-            countChanges(getApplicationsForWhiteboard(), _counter).flatMap(
+            applications.flatMap(
                 this::waitForApplicationDependencies
             );
 
-        OSGi<CachingServiceReference<Application>> highestRankedPerName =
-            highestPer(
-                APPLICATION_NAME, applicationsForWhiteboard,
-                _runtime::addClashingApplication,
-                _runtime::removeClashingApplication
-        );
-
         OSGi<CachingServiceReference<Application>> highestRankedPerPath =
             highestPer(
-                APPLICATION_BASE, highestRankedPerName,
+                APPLICATION_BASE, applicationsForWhiteboard,
                 _runtime::addShadowedApplication,
                 _runtime::removeShadowedApplication
         );
@@ -342,20 +390,20 @@ public class Whiteboard {
     private OSGi<CachingServiceReference<Object>>
         getApplicationExtensionsForWhiteboard() {
 
-        return serviceReferences(getApplicationExtensionsFilter()).
+        return serviceReferences(_applicationExtensionsFilter.toString()).
             filter(new TargetFilter<>(_runtimeReference));
     }
 
-    private OSGi<CachingServiceReference<Application>>
+    private OSGi<CachingServiceReference<Object>>
         getApplicationsForWhiteboard() {
 
         return
-            serviceReferences(Application.class, getApplicationFilter()).
-            filter(new TargetFilter<>(_runtimeReference));
+            serviceReferences(_applicationsFilter.toString()).
+                filter(new TargetFilter<>(_runtimeReference));
     }
 
     private OSGi<CachingServiceReference<Object>> getResourcesForWhiteboard() {
-        return serviceReferences(getResourcesFilter()).
+        return serviceReferences(_resourcesFilter.toString()).
             filter(
                 new TargetFilter<>(_runtimeReference));
     }
@@ -502,10 +550,13 @@ public class Whiteboard {
 
     private OSGi<CachingServiceReference<Application>>
         waitForApplicationDependencies(
-            CachingServiceReference<Application> applicationReference) {
+            CachingServiceReference<?> objectReference) {
 
         String[] extensionDependencies = canonicalize(
-            applicationReference.getProperty(JAX_RS_EXTENSION_SELECT));
+            objectReference.getProperty(JAX_RS_EXTENSION_SELECT));
+
+        CachingServiceReference<Application> applicationReference =
+            (CachingServiceReference<Application>) objectReference;
 
         OSGi<CachingServiceReference<Application>> program = just(
             applicationReference);
@@ -692,25 +743,6 @@ public class Whiteboard {
         return cxfNonSpringServlet;
     }
 
-    private static String getApplicationExtensionsFilter() {
-        return format(
-            "(&(!(objectClass=%s))(%s=%s)%s)",
-            ApplicationExtensionRegistration.class.getName(),
-            JAX_RS_EXTENSION, true, getExtensionsFilter());
-    }
-
-    private static String getApplicationFilter() {
-        return format("(%s=*)", JAX_RS_APPLICATION_BASE);
-    }
-
-    private static String getExtensionsFilter() {
-        return format("(%s=true)", JAX_RS_EXTENSION);
-    }
-
-    private static String getResourcesFilter() {
-        return format("(%s=true)", JAX_RS_RESOURCE);
-    }
-
     private static OSGi<CachingServiceReference<Object>> 
onlySupportedInterfaces(
         OSGi<CachingServiceReference<Object>> program,
         Consumer<CachingServiceReference<?>> onInvalidAdded,
@@ -822,4 +854,35 @@ public class Whiteboard {
         }
     }
 
+    private static final Filter _extensionsFilter;
+
+    private static final Filter _resourcesFilter;
+
+    private static Filter _applicationsFilter;
+
+    private static Filter _applicationExtensionsFilter;
+
+    static {
+        try {
+            _applicationsFilter = FrameworkUtil.createFilter(
+                format(
+                    "(&(objectClass=%s)(%s=*))", Application.class.getName(),
+                    JAX_RS_APPLICATION_BASE));
+            String extensionFilterString = format(
+                "(%s=true)", JAX_RS_EXTENSION);
+            _extensionsFilter = FrameworkUtil.createFilter(
+                extensionFilterString);
+            _applicationExtensionsFilter = FrameworkUtil.createFilter(
+                format(
+                    "(&(!(objectClass=%s))(%s=%s)%s)",
+                    ApplicationExtensionRegistration.class.getName(),
+                    JAX_RS_EXTENSION, true, extensionFilterString));
+            _resourcesFilter = FrameworkUtil.createFilter(
+                format("(%s=true)", JAX_RS_RESOURCE));
+        }
+        catch (InvalidSyntaxException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
 }

Reply via email to