aries-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From csie...@apache.org
Subject [06/12] aries-jax-rs-whiteboard git commit: Implemented wait for extensions
Date Fri, 24 Feb 2017 16:41:53 GMT
Implemented wait for extensions


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

Branch: refs/heads/master
Commit: 5292346e0fcd6381c50f2945b624c24bb6fcfab9
Parents: 0e87e50
Author: Carlos Sierra <csierra@apache.org>
Authored: Wed Feb 15 15:12:31 2017 +0100
Committer: Raymond Auge <raymond.auge@liferay.com>
Committed: Fri Feb 24 10:11:03 2017 -0500

----------------------------------------------------------------------
 jax-rs.itests/src/main/java/test/JaxrsTest.java | 92 ++++++++++++++++++-
 .../activator/CXFJaxRsBundleActivator.java      | 94 ++++++++++++++------
 2 files changed, 153 insertions(+), 33 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/5292346e/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 f7c983d..00282ee 100644
--- a/jax-rs.itests/src/main/java/test/JaxrsTest.java
+++ b/jax-rs.itests/src/main/java/test/JaxrsTest.java
@@ -260,6 +260,67 @@ public class JaxrsTest {
         }
     }
 
+    @Test
+    public void testStandaloneEndpointWithExtensionsDependencies() {
+        Client client = createClient();
+
+        WebTarget webTarget = client.
+            target("http://localhost:8080").
+            path("/test-addon").
+            path("test");
+
+        ServiceRegistration<?> serviceRegistration = null;
+        ServiceRegistration<?> extensionRegistration1;
+        ServiceRegistration<?> extensionRegistration2;
+
+        try {
+            serviceRegistration = registerAddon(
+                "osgi.jaxrs.resource.base", "/test-addon",
+                "osgi.jaxrs.extension.select", new String[]{
+                    "(property one=one)",
+                    "(property two=two)",
+                });
+
+            assertEquals(404, webTarget.request().get().getStatus());
+
+            extensionRegistration1 = registerExtension(
+                "aExtension", "property one", "one");
+
+            assertEquals(404, webTarget.request().get().getStatus());
+
+            extensionRegistration2 = registerExtension(
+                "anotherExtension", "property two", "two");
+
+            Response response = webTarget.request().get();
+
+            assertEquals(
+                "This should say hello", "Hello test",
+                response.readEntity(String.class));
+
+            extensionRegistration1.unregister();
+
+            assertEquals(404, webTarget.request().get().getStatus());
+
+            extensionRegistration1 = registerExtension(
+                "aExtension", "property one", "one");
+
+            assertEquals(
+                "This should say hello", "Hello test",
+                response.readEntity(String.class));
+
+            extensionRegistration2.unregister();
+
+            assertEquals(404, webTarget.request().get().getStatus());
+
+            extensionRegistration1.unregister();
+        }
+        finally {
+            if (serviceRegistration != null) {
+                serviceRegistration.unregister();
+            }
+        }
+    }
+
     private Client createClient() {
         Thread thread = Thread.currentThread();
 
@@ -276,12 +337,15 @@ public class JaxrsTest {
         }
     }
 
-    private ServiceRegistration<?> registerAddon(String key, String value) {
+    private ServiceRegistration<?> registerAddon(Object ... keyValues) {
+
         TestAddon testAddon = new TestAddon();
 
         Dictionary<String, Object> properties = new Hashtable<>();
 
-        properties.put(key, value);
+        for (int i = 0; i < keyValues.length; i = i + 2) {
+            properties.put(keyValues[i].toString(), keyValues[i + 1]);
+        }
 
         return bundleContext.registerService(
             Object.class, testAddon, properties);
@@ -300,12 +364,32 @@ public class JaxrsTest {
             Application.class, testApplication, properties);
     }
 
-    private ServiceRegistration<?> registerFilter(String key, String value) {
+    private ServiceRegistration<?> registerFilter(Object ... keyValues) {
+
+        TestFilter testFilter = new TestFilter();
+
+        Dictionary<String, Object> properties = new Hashtable<>();
+
+        for (int i = 0; i < keyValues.length; i = i + 2) {
+            properties.put(keyValues[i].toString(), keyValues[i + 1]);
+        }
+
+        return bundleContext.registerService(
+            Object.class, testFilter, properties);
+    }
+
+    private ServiceRegistration<?> registerExtension(
+        String name, Object ... keyValues) {
+
         TestFilter testFilter = new TestFilter();
 
         Dictionary<String, Object> properties = new Hashtable<>();
 
-        properties.put(key, value);
+        properties.put("osgi.jaxrs.extension.name", name);
+
+        for (int i = 0; i < keyValues.length; i = i + 2) {
+            properties.put(keyValues[i].toString(), keyValues[i + 1]);
+        }
 
         return bundleContext.registerService(
             Object.class, testFilter, properties);

http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/5292346e/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/activator/CXFJaxRsBundleActivator.java
----------------------------------------------------------------------
diff --git a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/activator/CXFJaxRsBundleActivator.java
b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/activator/CXFJaxRsBundleActivator.java
index 45fbf04..1275c0d 100644
--- a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/activator/CXFJaxRsBundleActivator.java
+++ b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/activator/CXFJaxRsBundleActivator.java
@@ -43,6 +43,7 @@ import java.util.Set;
 
 import static org.apache.aries.osgi.functional.OSGi.bundleContext;
 import static org.apache.aries.osgi.functional.OSGi.just;
+import static org.apache.aries.osgi.functional.OSGi.nothing;
 import static org.apache.aries.osgi.functional.OSGi.onClose;
 import static org.apache.aries.osgi.functional.OSGi.register;
 import static org.apache.aries.osgi.functional.OSGi.serviceReferences;
@@ -101,40 +102,44 @@ public class CXFJaxRsBundleActivator implements BundleActivator {
         OSGi<?> singletons =
             serviceReferences(getSingletonsFilter()).
                 flatMap(serviceReference ->
-            just(
-                CXFJaxRsServiceRegistrator.getProperties(
-                    serviceReference, "osgi.jaxrs.resource.base")).
-                flatMap(properties ->
-            service(serviceReference).flatMap(service ->
-            cxfRegistrator(bus,
-                new Application() {
-                    @Override
-                        public Set<Object> getSingletons() {
-                            return Collections.singleton(service);
-                        }
-                },
-                properties)
-        )));
+            waitForExtensionDependencies(serviceReference,
+                just(
+                    CXFJaxRsServiceRegistrator.getProperties(
+                        serviceReference, "osgi.jaxrs.resource.base")).
+                    flatMap(properties ->
+                service(serviceReference).flatMap(service ->
+                cxfRegistrator(bus,
+                    new Application() {
+                        @Override
+                            public Set<Object> getSingletons() {
+                                return Collections.singleton(service);
+                            }
+                    },
+                    properties)
+                )))
+            );
 
         _singletonsResult = singletons.run(bundleContext);
 
         OSGi<?> filters =
             serviceReferences(getFiltersFilter()).flatMap(ref ->
-            just(
-                ref.getProperty("osgi.jaxrs.filter.base").toString()).
-                flatMap(filterBase ->
-            serviceReferences(
-                CXFJaxRsServiceRegistrator.class, "(CXF_ENDPOINT_ADDRESS=*)").
-                filter(regref ->
-                    regref.
-                        getProperty("CXF_ENDPOINT_ADDRESS").
-                        toString().
-                        startsWith(filterBase)).
-                flatMap(regref ->
-            service(regref).flatMap(registrator ->
-            service(ref).flatMap(service ->
-            safeRegisterEndpoint(ref, registrator, service)
-        )))));
+            waitForExtensionDependencies(ref,
+                just(
+                    ref.getProperty("osgi.jaxrs.filter.base").toString()).
+                    flatMap(filterBase ->
+                serviceReferences(
+                    CXFJaxRsServiceRegistrator.class, "(CXF_ENDPOINT_ADDRESS=*)").
+                    filter(regref ->
+                        regref.
+                            getProperty("CXF_ENDPOINT_ADDRESS").
+                            toString().
+                            startsWith(filterBase)).
+                    flatMap(regref ->
+                service(regref).flatMap(registrator ->
+                service(ref).flatMap(service ->
+                safeRegisterEndpoint(ref, registrator, service)
+            )))))
+        );
 
         _filtersResult = filters.run(bundleContext);
     }
@@ -157,6 +162,37 @@ public class CXFJaxRsBundleActivator implements BundleActivator {
         }
     }
 
+    private String[] canonicalize(Object propertyValue) {
+        if (propertyValue == null) {
+            return new String[0];
+        }
+
+        if (propertyValue instanceof String[]) {
+            return (String[]) propertyValue;
+        }
+
+        return new String[]{propertyValue.toString()};
+    }
+
+    private String buildExtensionFilter(String filter) {
+        return "(&(osgi.jaxrs.extension.name=*)" + filter + ")";
+    }
+
+    private OSGi<?> waitForExtensionDependencies(
+        ServiceReference<?> serviceReference, OSGi<?> program) {
+
+        String[] extensionDependencies = canonicalize(
+            serviceReference.getProperty("osgi.jaxrs.extension.select"));
+
+        for (String extensionDependency : extensionDependencies) {
+            program =
+                serviceReferences(buildExtensionFilter(extensionDependency)).
+                then(program);
+        }
+
+        return program;
+    }
+
     private OSGi<?> safeRegisterEndpoint(
         ServiceReference<?> ref, CXFJaxRsServiceRegistrator registrator,
         Object service) {


Mime
View raw message