You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@aries.apache.org by ro...@apache.org on 2017/08/04 13:56:41 UTC

aries-jax-rs-whiteboard git commit: whiteboard target filter matching

Repository: aries-jax-rs-whiteboard
Updated Branches:
  refs/heads/master 757c964c9 -> bdbabeaaf


whiteboard target filter matching

Signed-off-by: Raymond Auge <ro...@apache.org>


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

Branch: refs/heads/master
Commit: bdbabeaafe27acb3ba7b6e5045d0f7a9a16ea4ec
Parents: 757c964
Author: Raymond Auge <ro...@apache.org>
Authored: Fri Aug 4 09:44:58 2017 -0400
Committer: Raymond Auge <ro...@apache.org>
Committed: Fri Aug 4 09:52:29 2017 -0400

----------------------------------------------------------------------
 .../java/test/WhiteboardTargetFilterTest.java   | 76 ++++++++++++++++++++
 .../rs/whiteboard/internal/TargetFilter.java    | 48 +++++++++++++
 .../jax/rs/whiteboard/internal/Whiteboard.java  | 30 +++++---
 3 files changed, 143 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/bdbabeaa/jax-rs.itests/src/main/java/test/WhiteboardTargetFilterTest.java
----------------------------------------------------------------------
diff --git a/jax-rs.itests/src/main/java/test/WhiteboardTargetFilterTest.java b/jax-rs.itests/src/main/java/test/WhiteboardTargetFilterTest.java
new file mode 100644
index 0000000..8600649
--- /dev/null
+++ b/jax-rs.itests/src/main/java/test/WhiteboardTargetFilterTest.java
@@ -0,0 +1,76 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import java.util.Dictionary;
+import java.util.Hashtable;
+
+import org.junit.Ignore;
+import org.junit.Test;
+import org.osgi.framework.ServiceRegistration;
+import org.osgi.service.jaxrs.whiteboard.JaxRSWhiteboardConstants;
+
+import test.types.TestAddon;
+import test.types.TestHelper;
+
+@Ignore
+public class WhiteboardTargetFilterTest extends TestHelper {
+
+    @Test
+    public void testInvalidTargetFilter() throws Exception {
+        Dictionary<String, Object> properties = new Hashtable<>();
+        properties.put(JaxRSWhiteboardConstants.JAX_RS_RESOURCE, "true");
+        properties.put(JaxRSWhiteboardConstants.JAX_RS_WHITEBOARD_TARGET, "//");
+
+        ServiceRegistration<Object> serviceRegistration = bundleContext.registerService(
+            Object.class, new TestAddon(), properties);
+
+        try {
+
+            //System.out.println("blad");
+
+        }
+        finally {
+            serviceRegistration.unregister();
+        }
+    }
+
+    @Test
+    public void testNonMatchingFilter() throws Exception {
+        Dictionary<String, Object> properties = new Hashtable<>();
+        properties.put(JaxRSWhiteboardConstants.JAX_RS_RESOURCE, "true");
+        properties.put(JaxRSWhiteboardConstants.JAX_RS_WHITEBOARD_TARGET, "(crazy=the joker)");
+
+        ServiceRegistration<Object> serviceRegistration = bundleContext.registerService(
+            Object.class, new TestAddon(), properties);
+
+        try {
+
+            //System.out.println("blad");
+
+        }
+        finally {
+            serviceRegistration.unregister();
+        }
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/bdbabeaa/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/TargetFilter.java
----------------------------------------------------------------------
diff --git a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/TargetFilter.java b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/TargetFilter.java
new file mode 100644
index 0000000..d83caa8
--- /dev/null
+++ b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/TargetFilter.java
@@ -0,0 +1,48 @@
+package org.apache.aries.jax.rs.whiteboard.internal;
+
+import static org.osgi.service.jaxrs.whiteboard.JaxRSWhiteboardConstants.JAX_RS_WHITEBOARD_TARGET;
+
+import java.util.function.Predicate;
+
+import org.osgi.framework.Filter;
+import org.osgi.framework.FrameworkUtil;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class TargetFilter<T> implements Predicate<ServiceReference<T>>  {
+
+    public TargetFilter(ServiceReference<?> serviceRuntimeReference) {
+        _serviceRuntimeReference = serviceRuntimeReference;
+    }
+
+    @Override
+    public boolean test(ServiceReference<T> ref) {
+        String target = (String)ref.getProperty(JAX_RS_WHITEBOARD_TARGET);
+
+        if (target == null) {
+            return true;
+        }
+
+        Filter filter;
+
+        try {
+            filter = FrameworkUtil.createFilter(target);
+        }
+        catch (InvalidSyntaxException ise) {
+            if (_log.isErrorEnabled()) {
+                _log.error("Invalid '{}' syntax in {}", JAX_RS_WHITEBOARD_TARGET, ref, ise);
+            }
+
+            return false;
+        }
+
+        return filter.match(_serviceRuntimeReference);
+    }
+
+    private static final Logger _log = LoggerFactory.getLogger(TargetFilter.class);
+
+    private final ServiceReference<?> _serviceRuntimeReference;
+
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/bdbabeaa/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 a9d87d0..2845f24 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
@@ -78,12 +78,13 @@ public class Whiteboard {
             just(createDefaultJaxRsServiceRegistrator(bus)).flatMap(defaultServiceRegistrator ->
             registerJaxRSServiceRuntime(bundleContext, bus, Maps.from(configuration)).flatMap(registratorRegistration ->
             just(new ServiceRegistrationChangeCounter(registratorRegistration)).flatMap(counter ->
+            just(registratorRegistration.getReference()).flatMap(reference ->
                 all(
-                    countChanges(whiteboardApplications(bus), counter),
-                    countChanges(whiteBoardApplicationSingletons(), counter),
-                    countChanges(whiteboardExtensions(defaultServiceRegistrator), counter),
-                    countChanges(whiteboardSingletons(defaultServiceRegistrator), counter)
-            ))))));
+                    countChanges(whiteboardApplications(reference, bus), counter),
+                    countChanges(whiteBoardApplicationSingletons(reference), counter),
+                    countChanges(whiteboardExtensions(reference, defaultServiceRegistrator), counter),
+                    countChanges(whiteboardSingletons(reference, defaultServiceRegistrator), counter)
+            )))))));
     }
 
     private static OSGi<Collection<String>> bestEffortCalculationOfEnpoints(Filter filter) {
@@ -215,9 +216,10 @@ public class Whiteboard {
         return program;
     }
 
-    private static OSGi<?> whiteBoardApplicationSingletons() {
+    private static OSGi<?> whiteBoardApplicationSingletons(ServiceReference<?> jaxRsRuntimeServiceReference) {
         return
             serviceReferences(format("(%s=*)", JAX_RS_APPLICATION_SELECT)).
+                filter(new TargetFilter<>(jaxRsRuntimeServiceReference)).
                 flatMap(ref ->
             just(ref.getProperty(JAX_RS_APPLICATION_SELECT).toString()).
                 flatMap(applicationFilter ->
@@ -227,10 +229,13 @@ public class Whiteboard {
         )));
     }
 
-    private static OSGi<?> whiteboardApplications(ExtensionManagerBus bus) {
+    private static OSGi<?> whiteboardApplications(
+        ServiceReference<?> jaxRsRuntimeServiceReference, ExtensionManagerBus bus) {
+
         return
             repeatInOrder(
-                serviceReferences(Application.class, getApplicationFilter())).
+                serviceReferences(Application.class, getApplicationFilter()).
+                    filter(new TargetFilter<>(jaxRsRuntimeServiceReference))).
                 flatMap(ref ->
             just(CXFJaxRsServiceRegistrator.getProperties(ref, JAX_RS_APPLICATION_BASE)).
                 flatMap(properties ->
@@ -240,10 +245,12 @@ public class Whiteboard {
     }
 
     private static OSGi<?> whiteboardExtensions(
-        CXFJaxRsServiceRegistrator defaultServiceRegistrator) {
+        ServiceReference<?> jaxRsRuntimeServiceReference, CXFJaxRsServiceRegistrator defaultServiceRegistrator) {
 
         return
-            serviceReferences(getExtensionFilter()).flatMap(ref ->
+            serviceReferences(getExtensionFilter()).
+                filter(new TargetFilter<>(jaxRsRuntimeServiceReference)).
+                flatMap(ref ->
             waitForExtensionDependencies(ref,
                 safeRegisterExtension(ref, defaultServiceRegistrator)
             )
@@ -251,10 +258,11 @@ public class Whiteboard {
     }
 
     private static OSGi<?> whiteboardSingletons(
-        CXFJaxRsServiceRegistrator defaultServiceRegistrator) {
+        ServiceReference<?> jaxRsRuntimeServiceReference, CXFJaxRsServiceRegistrator defaultServiceRegistrator) {
 
         return
             serviceReferences(getSingletonsFilter()).
+                filter(new TargetFilter<>(jaxRsRuntimeServiceReference)).
                 flatMap(serviceReference ->
             waitForExtensionDependencies(serviceReference,
                 safeRegisterEndpoint(