You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@aries.apache.org by cs...@apache.org on 2017/08/25 15:25:45 UTC

[4/8] aries-jax-rs-whiteboard git commit: Calculate needed extensions per application

Calculate needed extensions per application


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

Branch: refs/heads/master
Commit: 00057c9ea2891defbc7adc54bd950b8dce97a6d7
Parents: fe929ae
Author: Carlos Sierra <cs...@apache.org>
Authored: Thu Aug 24 12:58:10 2017 +0200
Committer: Carlos Sierra <cs...@apache.org>
Committed: Thu Aug 24 12:58:10 2017 +0200

----------------------------------------------------------------------
 jax-rs.itests/src/main/java/test/JaxrsTest.java |  7 ++-
 .../internal/AriesJaxRSServiceRuntime.java      |  2 +-
 .../aries/jax/rs/whiteboard/internal/Utils.java | 24 +++++++--
 .../jax/rs/whiteboard/internal/Whiteboard.java  | 53 +++++++++++++-------
 4 files changed, 61 insertions(+), 25 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/00057c9e/jax-rs.itests/src/main/java/test/JaxrsTest.java
----------------------------------------------------------------------
diff --git a/jax-rs.itests/src/main/java/test/JaxrsTest.java b/jax-rs.itests/src/main/java/test/JaxrsTest.java
index e71f2b4..6491ec3 100644
--- a/jax-rs.itests/src/main/java/test/JaxrsTest.java
+++ b/jax-rs.itests/src/main/java/test/JaxrsTest.java
@@ -25,6 +25,7 @@ import java.util.Hashtable;
 import java.util.Set;
 
 import org.junit.After;
+import org.junit.Ignore;
 import org.junit.Test;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.PrototypeServiceFactory;
@@ -836,8 +837,7 @@ public class JaxrsTest extends TestHelper {
         try {
             serviceRegistration = registerAddon(new TestAddon());
 
-            filterRegistration = registerExtension(
-                "Filter", JAX_RS_EXTENSION, "test-filter");
+            filterRegistration = registerExtension("Filter");
 
             Response response = webTarget.request().get();
 
@@ -881,8 +881,7 @@ public class JaxrsTest extends TestHelper {
 
                     assertNull(response.getHeaders().getFirst("Filtered"));
 
-                    filterRegistration = registerExtension(
-                        "Filter", JAX_RS_EXTENSION, "test-filter");
+                    filterRegistration = registerExtension("Filter");
 
                     response = webTarget.request().get();
 

http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/00057c9e/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 c556e4f..c680f3c 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
@@ -351,7 +351,7 @@ public class AriesJaxRSServiceRuntime implements JaxRSServiceRuntime {
     private static ResourceDTO populateResourceDTO(
         ResourceDTO resourceDTO, ServiceReference<?> serviceReference) {
 
-        resourceDTO.name = serviceReference.getProperty(JAX_RS_NAME).toString();
+        resourceDTO.name = getApplicationName(serviceReference::getProperty);
         resourceDTO.serviceId = (Long)serviceReference.getProperty(
             "service.id");
 

http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/00057c9e/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 411281f..dd7530b 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
@@ -24,7 +24,7 @@ import org.apache.cxf.jaxrs.lifecycle.ResourceProvider;
 import org.apache.cxf.message.Message;
 import org.osgi.framework.ServiceObjects;
 import org.osgi.framework.ServiceReference;
-import org.osgi.service.jaxrs.runtime.dto.FailedApplicationDTO;
+import org.osgi.service.jaxrs.whiteboard.JaxRSWhiteboardConstants;
 
 import javax.ws.rs.core.Application;
 import java.util.Comparator;
@@ -36,6 +36,7 @@ import java.util.TreeSet;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.function.Consumer;
 import java.util.function.Function;
+import java.util.function.Supplier;
 
 import static org.apache.aries.osgi.functional.OSGi.bundleContext;
 import static org.apache.aries.osgi.functional.OSGi.just;
@@ -136,10 +137,21 @@ public class Utils {
     }
 
     public static OSGi<?> safeRegisterExtension(
-        ServiceReference<?> serviceReference,
-        String applicationName, CXFJaxRsServiceRegistrator registrator,
+        ServiceReference<?> serviceReference, String applicationName,
+        CXFJaxRsServiceRegistrator registrator,
         AriesJaxRSServiceRuntime runtime) {
 
+        Map<String, Object> properties = getProperties(serviceReference);
+
+        properties.put(
+            JaxRSWhiteboardConstants.JAX_RS_NAME, applicationName);
+        properties.put(
+            "original.objectClass",
+            serviceReference.getProperty("objectClass"));
+
+        properties.remove(JaxRSWhiteboardConstants.JAX_RS_EXTENSION);
+        properties.remove(JaxRSWhiteboardConstants.JAX_RS_RESOURCE);
+
         return
             onlyGettables(
                 just(serviceReference),
@@ -153,6 +165,10 @@ public class Utils {
                     applicationName, serviceReference),
                 __ -> runtime.removeApplicationExtension(
                     applicationName, serviceReference)
+            ).then(
+                register(
+                    ApplicationExtensionRegistration.class,
+                    new ApplicationExtensionRegistration(){}, properties)
             );
     }
 
@@ -459,4 +475,6 @@ public class Utils {
         Object get(String propertyName);
     }
 
+    public interface ApplicationExtensionRegistration {}
+
 }

http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/00057c9e/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 ffb6379..d53985e 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
@@ -17,6 +17,7 @@
 
 package org.apache.aries.jax.rs.whiteboard.internal;
 
+import org.apache.aries.jax.rs.whiteboard.internal.Utils.ApplicationExtensionRegistration;
 import org.apache.aries.jax.rs.whiteboard.internal.Utils.PropertyHolder;
 import org.apache.aries.jax.rs.whiteboard.internal.Utils.ServiceTuple;
 import org.apache.aries.osgi.functional.OSGi;
@@ -79,7 +80,12 @@ import static org.osgi.service.jaxrs.whiteboard.JaxRSWhiteboardConstants.JAX_RS_
  */
 public class Whiteboard {
 
-    public static final Function<ServiceTuple<Application>, String> APPLICATION_BASE = ((Function<ServiceTuple<Application>, ServiceReference<Application>>) ServiceTuple::getServiceReference).andThen(sr -> getApplicationBase(sr::getProperty));
+    public static final Function<ServiceTuple<Application>, String>
+        APPLICATION_BASE =
+        ((Function<ServiceTuple<Application>, ServiceReference<Application>>)
+            ServiceTuple::getServiceReference).andThen(
+                sr -> getApplicationBase(sr::getProperty));
+
     public static final String DEFAULT_NAME = ".default";
 
     public static OSGi<Void> createWhiteboard(Dictionary<String, ?> configuration) {
@@ -118,7 +124,11 @@ public class Whiteboard {
     }
 
     private static String buildExtensionFilter(String filter) {
-        return String.format("(&%s%s)", getExtensionFilter(), filter);
+        filter = filter.replace("(objectClass=", "(original.objectClass=");
+
+        return String.format(
+            "(&(objectClass=%s)%s)",
+            ApplicationExtensionRegistration.class.getName(), filter);
     }
 
     private static String[] canonicalize(Object propertyValue) {
@@ -174,11 +184,11 @@ public class Whiteboard {
         return format("(%s=*)", JAX_RS_APPLICATION_BASE);
     }
 
-    private static String getExtensionFilter() {
-        return format("(%s=*)", JAX_RS_EXTENSION);
+    private static String getExtensionsFilter() {
+        return format("(%s=true)", JAX_RS_EXTENSION);
     }
 
-    private static String getSingletonsFilter() {
+    private static String getResourcesFilter() {
         return format("(%s=true)", JAX_RS_RESOURCE);
     }
 
@@ -219,7 +229,8 @@ public class Whiteboard {
     }
 
     private static OSGi<?> waitForExtensionDependencies(
-        ServiceReference<?> serviceReference, OSGi<?> program) {
+        ServiceReference<?> serviceReference, AriesJaxRSServiceRuntime runtime,
+        OSGi<?> program) {
 
         String[] extensionDependencies = canonicalize(
             serviceReference.getProperty(JAX_RS_EXTENSION_SELECT));
@@ -237,10 +248,12 @@ public class Whiteboard {
         ServiceReference<?> jaxRsRuntimeServiceReference,
         AriesJaxRSServiceRuntime runtime) {
         return
-            serviceReferences(format("(%s=*)", JAX_RS_APPLICATION_SELECT)).
+            serviceReferences(getApplicationSingletonsFilter()).
                 filter(new TargetFilter<>(jaxRsRuntimeServiceReference)).
-                flatMap(ref ->
-            just(ref.getProperty(JAX_RS_APPLICATION_SELECT).toString()).
+                flatMap(endpointReference ->
+            just(endpointReference.
+                    getProperty(JAX_RS_APPLICATION_SELECT).
+                    toString()).
                 flatMap(applicationFilter ->
             serviceReferences(
                 CXFJaxRsServiceRegistrator.class, applicationFilter).
@@ -248,12 +261,19 @@ public class Whiteboard {
             just(
                 getApplicationName(registratorReference::getProperty)).
                 flatMap(applicationName ->
-            waitForExtensionDependencies(ref,
+            waitForExtensionDependencies(endpointReference, runtime,
                 service(registratorReference).flatMap(registrator ->
-                safeRegisterGeneric(ref, applicationName, registrator, runtime)
+                safeRegisterGeneric(
+                    endpointReference, applicationName, registrator, runtime)
         ))))));
     }
 
+    private static String getApplicationSingletonsFilter() {
+        return format(
+            "(&(|%s%s)(%s=*))", getExtensionsFilter(), getResourcesFilter(),
+            JAX_RS_APPLICATION_SELECT);
+    }
+
     private static OSGi<?> whiteboardApplications(
         ServiceReference<?> jaxRsRuntimeServiceReference,
         AriesJaxRSServiceRuntime runtime,
@@ -327,10 +347,10 @@ public class Whiteboard {
         AriesJaxRSServiceRuntime runtime) {
 
         return
-            serviceReferences(getExtensionFilter()).
+            serviceReferences(getExtensionsFilter()).
                 filter(new TargetFilter<>(jaxRsRuntimeServiceReference)).
                 flatMap(ref ->
-            waitForExtensionDependencies(ref,
+            waitForExtensionDependencies(ref, runtime,
                 safeRegisterExtension(
                     ref, DEFAULT_NAME, defaultServiceRegistrator, runtime)
             ).foreach(
@@ -346,14 +366,13 @@ public class Whiteboard {
         AriesJaxRSServiceRuntime runtime) {
 
         return
-            serviceReferences(getSingletonsFilter()).
+            serviceReferences(getResourcesFilter()).
                 filter(new TargetFilter<>(jaxRsRuntimeServiceReference)).
                 flatMap(ref ->
             waitForExtensionDependencies(
-                ref,
+                ref, runtime,
                 safeRegisterEndpoint(
-                    ref, DEFAULT_NAME, defaultServiceRegistrator,
-                    runtime)
+                    ref, DEFAULT_NAME, defaultServiceRegistrator, runtime)
             ).foreach(
                 __ -> runtime.addApplicationEndpoint(DEFAULT_NAME, ref),
                 __ -> runtime.removeApplicationExtension(DEFAULT_NAME, ref)