aries-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From csie...@apache.org
Subject [13/14] aries-jax-rs-whiteboard git commit: Factor out gettable checks
Date Fri, 18 Aug 2017 14:02:02 GMT
Factor out gettable checks

to make them reusable for other elements as well


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

Branch: refs/heads/master
Commit: ffc117555d028f609143b659278e7efc01f6928f
Parents: 6e52bcb
Author: Carlos Sierra <csierra@apache.org>
Authored: Thu Aug 17 18:30:44 2017 +0200
Committer: Carlos Sierra <csierra@apache.org>
Committed: Fri Aug 18 15:59:21 2017 +0200

----------------------------------------------------------------------
 .../internal/AriesJaxRSServiceRuntime.java      | 71 +++++---------------
 .../aries/jax/rs/whiteboard/internal/Utils.java | 35 ++++++++++
 .../jax/rs/whiteboard/internal/Whiteboard.java  | 12 +++-
 3 files changed, 61 insertions(+), 57 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/ffc11755/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 62acc89..11e9772 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
@@ -30,7 +30,6 @@ import org.osgi.service.jaxrs.runtime.dto.RuntimeDTO;
 import org.osgi.service.jaxrs.whiteboard.JaxRSWhiteboardConstants;
 
 import javax.ws.rs.core.Application;
-import java.util.Collection;
 import java.util.Comparator;
 import java.util.Objects;
 import java.util.TreeSet;
@@ -47,31 +46,38 @@ public class AriesJaxRSServiceRuntime implements JaxRSServiceRuntime {
     private ConcurrentHashMap<String, TreeSet<Event<MutableTuple<Application>>>>
         _applications = new ConcurrentHashMap<>();
 
-    private Collection<Event<MutableTuple<Application>>>
+    private TreeSet<ServiceReference<Application>>
         _ungettableApplications;
+
     private Comparator<Event<MutableTuple<Application>>>
         _applicationComparator;
-    private BundleContext _bundleContext;
 
     public AriesJaxRSServiceRuntime(BundleContext bundleContext) {
-        _bundleContext = bundleContext;
-
         _applicationComparator = Comparator.comparing(
             Event::getContent);
 
         _applicationComparator = _applicationComparator.reversed();
 
-        _ungettableApplications = new TreeSet<>(_applicationComparator);
+        _ungettableApplications = new TreeSet<>(Comparator.reverseOrder());
+    }
+
+    public boolean addNotGettable(
+        ServiceReference<Application> serviceReference) {
+
+        return _ungettableApplications.add(serviceReference);
+    }
+
+    public boolean removeNotGettable(
+        ServiceReference<Application> serviceReference) {
+
+        return _ungettableApplications.remove(serviceReference);
     }
 
     public OSGi<MutableTuple<Application>> processApplications(
-        OSGi<ServiceReference<Application>> source) {
+        OSGi<MutableTuple<Application>> source) {
 
         return
-            source.map(
-                sr -> new MutableTuple<>(sr, null)
-            ).
-            route(
+            source.route(
                 router -> {
                 router.onIncoming(event -> {
                     MutableTuple<Application>
@@ -80,16 +86,6 @@ public class AriesJaxRSServiceRuntime implements JaxRSServiceRuntime {
                     ServiceReference<Application> serviceReference =
                         tuple.getServiceReference();
 
-                    Application application = checkGettable(serviceReference);
-
-                    if (application == null)  {
-                        _ungettableApplications.add(event);
-
-                        return;
-                    }
-
-                    tuple.setService(application);
-
                     String path = serviceReference.getProperty(JAX_RS_APPLICATION_BASE).
                         toString();
 
@@ -116,13 +112,6 @@ public class AriesJaxRSServiceRuntime implements JaxRSServiceRuntime
{
                     ServiceReference<Application> serviceReference =
                         tuple.getServiceReference();
 
-                    boolean ungettable = _ungettableApplications.removeIf(
-                        t -> t.getContent().equals(tuple));
-
-                    if (ungettable) {
-                        return;
-                    }
-
                     String path = serviceReference.getProperty(JAX_RS_APPLICATION_BASE).
                         toString();
 
@@ -149,28 +138,6 @@ public class AriesJaxRSServiceRuntime implements JaxRSServiceRuntime
{
             });
     }
 
-    private Application checkGettable(
-        ServiceReference<Application> serviceReference) {
-
-        Application application = null;
-
-        try {
-            application = _bundleContext.getService(
-                serviceReference);
-        }
-        catch (Exception e) {
-        }
-
-        if (application == null) {
-            return null;
-        }
-        else {
-            _bundleContext.ungetService(serviceReference);
-        }
-
-        return application;
-    }
-
     @Override
     public RequestInfoDTO calculateRequestInfoDTO(String path) {
         return null;
@@ -213,10 +180,6 @@ public class AriesJaxRSServiceRuntime implements JaxRSServiceRuntime
{
     private Stream<FailedApplicationDTO> unreferenciableApplications() {
         return _ungettableApplications.stream().
             map(
-                Event::getContent
-            ).map(
-                MutableTuple::getServiceReference
-            ).map(
                 sr -> buildFailedApplicationDTO(
                     DTOConstants.FAILURE_REASON_SERVICE_NOT_GETTABLE,
                     sr)

http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/ffc11755/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 2fb2854..9846ebb 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
@@ -17,6 +17,7 @@
 
 package org.apache.aries.jax.rs.whiteboard.internal;
 
+import org.apache.aries.jax.rs.whiteboard.internal.AriesJaxRSServiceRuntime.MutableTuple;
 import org.apache.aries.osgi.functional.Event;
 import org.apache.aries.osgi.functional.OSGi;
 import org.apache.cxf.Bus;
@@ -36,6 +37,7 @@ import java.util.function.Consumer;
 
 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;
 
@@ -126,6 +128,39 @@ public class Utils {
         return program.route(new RepeatInOrderRouter<>());
     }
 
+    public static <T> OSGi<MutableTuple<T>> onlyGettables(
+        ServiceReference<T> serviceReference,
+        Consumer<ServiceReference<T>> whenAddedNotGettable,
+        Consumer<ServiceReference<T>> whenLeavingNotGettable) {
+
+        return bundleContext().flatMap(
+            bundleContext -> {
+                T service = null;
+                try {
+                    service = bundleContext.getService(serviceReference);
+                }
+                catch (Exception e){
+                }
+                if (service == null) {
+                    whenAddedNotGettable.accept(serviceReference);
+
+                    return
+                        onClose(
+                            () -> whenLeavingNotGettable.accept(serviceReference)
+                        ).then(
+                            nothing()
+                        );
+                }
+                return
+                    onClose(
+                        () -> bundleContext.ungetService(serviceReference)
+                    ).then(
+                        just(new MutableTuple<>(serviceReference, service)))
+                    ;
+            }
+        );
+    }
+
     public static <T> OSGi<ResourceProvider>
         registerEndpoint(
             ServiceReference<?> serviceReference,

http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/ffc11755/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 4628fb7..d4a4178 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.AriesJaxRSServiceRuntime.MutableTuple;
 import org.apache.aries.osgi.functional.OSGi;
 import org.apache.cxf.Bus;
 import org.apache.cxf.bus.extension.ExtensionManagerBus;
@@ -43,6 +44,7 @@ import java.util.concurrent.atomic.AtomicLong;
 
 import static java.lang.String.format;
 import static org.apache.aries.jax.rs.whiteboard.internal.Utils.deployRegistrator;
+import static org.apache.aries.jax.rs.whiteboard.internal.Utils.onlyGettables;
 import static org.apache.aries.jax.rs.whiteboard.internal.Utils.safeRegisterEndpoint;
 import static org.apache.aries.jax.rs.whiteboard.internal.Utils.safeRegisterExtension;
 import static org.apache.aries.jax.rs.whiteboard.internal.Utils.safeRegisterGeneric;
@@ -229,8 +231,12 @@ public class Whiteboard {
         AriesJaxRSServiceRuntime runtime,
         Map<String, ?> configuration) {
 
-        OSGi<ServiceReference<Application>> applicationsForWhiteboard =
-            getApplicationsForWhiteboard(jaxRsRuntimeServiceReference);
+        OSGi<MutableTuple<Application>> applicationsForWhiteboard =
+            getApplicationsForWhiteboard(jaxRsRuntimeServiceReference).
+                flatMap(
+                    sr -> onlyGettables(
+                        sr, runtime::addNotGettable, runtime::removeNotGettable)
+                );
 
         return
             bundleContext().flatMap(
@@ -242,7 +248,7 @@ public class Whiteboard {
 
     private static OSGi<Void> deployApplication(
         Map<String, ?> configuration, BundleContext bundleContext,
-        AriesJaxRSServiceRuntime.MutableTuple<Application> tuple) {
+        MutableTuple<Application> tuple) {
 
         ExtensionManagerBus bus = createBus(bundleContext, configuration);
 


Mime
View raw message