Check earlier if application throws error

If an application produces an error it is not eligible to shadow other
applications either by name or by path.

The current approach creates the application twice, first to check if
the application is healthy and, if the application is eligible for
registration, it is finally registered.

This approach is needed because it is not possible to create a bus for
the application, register the bus in a servlet and later unregister the
servlet. When the servlet is destroyed the bus is also destroyed.

Also it is not trivial for me to perform the shadowing checks and, if the
application errors, redo the checks and keep the program reusable and
untangled.


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

Branch: refs/heads/master
Commit: 57c5985a65d93e8d66285fc4d05d52d2cfc58c19
Parents: d3d92a7
Author: Carlos Sierra <[email protected]>
Authored: Mon Nov 6 14:41:49 2017 +0100
Committer: Carlos Sierra <[email protected]>
Committed: Mon Nov 6 14:41:49 2017 +0100

----------------------------------------------------------------------
 .../jax/rs/whiteboard/internal/Whiteboard.java  | 64 ++++++++++++--------
 1 file changed, 39 insertions(+), 25 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/57c5985a/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 c107941..411d60e 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
@@ -179,6 +179,38 @@ public class Whiteboard {
         return new Whiteboard(bundleContext, configuration);
     }
 
+    private OSGi<ServiceTuple<Application>> filterErroringApplications(
+        OSGi<ServiceTuple<Application>> program) {
+
+        return program.flatMap(tuple -> {
+            ExtensionManagerBus bus = createBus();
+
+            try {
+                CXFJaxRsServiceRegistrator registrator =
+                    new CXFJaxRsServiceRegistrator(bus, tuple.getService());
+
+                registrator.close();
+                bus.shutdown();
+
+                return just(tuple);
+            }
+            catch (Exception e) {
+                CachingServiceReference<Application> serviceReference =
+                    tuple.getCachingServiceReference();
+
+                _runtime.addErroredApplication(serviceReference);
+
+                return onClose(
+                    () ->
+                        _runtime.removeErroredApplication(serviceReference)
+                ).then(
+                    nothing()
+                );
+            }
+        });
+
+    }
+
     public void start() {
         _osgiResult = _program.run(_bundleContext);
     }
@@ -239,6 +271,9 @@ public class Whiteboard {
                 _runtime::addNotGettableApplication,
                 _runtime::removeNotGettableApplication);
 
+        gettableAplicationForWhiteboard = filterErroringApplications(
+            gettableAplicationForWhiteboard);
+
         OSGi<ServiceTuple<Application>> highestRankedPerName = highestPer(
             APPLICATION_NAME, gettableAplicationForWhiteboard,
             t -> 
_runtime.addClashingApplication(t.getCachingServiceReference()),
@@ -359,32 +394,11 @@ public class Whiteboard {
         Bus bus, ServiceTuple<Application> tuple, Map<String, Object> props) {
 
         return
-            join(just(() -> {
-                try {
-                    CXFJaxRsServiceRegistrator registrator =
-                        new CXFJaxRsServiceRegistrator(bus, 
tuple.getService());
+            just(() -> new CXFJaxRsServiceRegistrator(bus, 
tuple.getService())).
+                flatMap(registrator ->
 
-                    return
-                        onClose(registrator::close).then(
-                            register(
-                                CXFJaxRsServiceRegistrator.class, registrator,
-                                props)
-                        );
-                }
-                catch (RuntimeException e) {
-                    CachingServiceReference<Application> serviceReference =
-                        tuple.getCachingServiceReference();
-
-                    _runtime.addErroredApplication(serviceReference);
-
-                    return onClose(
-                        () ->
-                            _runtime.removeErroredApplication(serviceReference)
-                    ).then(
-                        nothing()
-                    );
-                }
-            }));
+            onClose(registrator::close).then(
+            register(CXFJaxRsServiceRegistrator.class, registrator, props)));
     }
 
     private OSGi<CachingServiceReference<Object>>

Reply via email to