Register errored applications
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/fe929ae8 Tree: http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/tree/fe929ae8 Diff: http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/diff/fe929ae8 Branch: refs/heads/master Commit: fe929ae882a46e17f0608a7baa3d0c313339612e Parents: 7c1fce8 Author: Carlos Sierra <[email protected]> Authored: Wed Aug 23 14:24:26 2017 +0200 Committer: Carlos Sierra <[email protected]> Committed: Wed Aug 23 14:24:26 2017 +0200 ---------------------------------------------------------------------- jax-rs.itests/src/main/java/test/JaxrsTest.java | 51 +++++++++++++++++++- .../internal/AriesJaxRSServiceRuntime.java | 44 ++++++++++++----- .../aries/jax/rs/whiteboard/internal/Utils.java | 27 +++++++++-- .../jax/rs/whiteboard/internal/Whiteboard.java | 37 +++++++------- 4 files changed, 121 insertions(+), 38 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/fe929ae8/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 f089eb6..e71f2b4 100644 --- a/jax-rs.itests/src/main/java/test/JaxrsTest.java +++ b/jax-rs.itests/src/main/java/test/JaxrsTest.java @@ -22,6 +22,7 @@ import static org.osgi.service.jaxrs.whiteboard.JaxRSWhiteboardConstants.*; import java.util.Dictionary; import java.util.Hashtable; +import java.util.Set; import org.junit.After; import org.junit.Test; @@ -32,6 +33,7 @@ import org.osgi.framework.ServiceRegistration; import org.osgi.service.jaxrs.runtime.JaxRSServiceRuntime; import org.osgi.service.jaxrs.runtime.dto.DTOConstants; +import org.osgi.service.jaxrs.runtime.dto.RuntimeDTO; import org.osgi.util.tracker.ServiceTracker; import test.types.TestAddon; import test.types.TestAddonConflict; @@ -69,10 +71,10 @@ public class JaxrsTest extends TestHelper { try { JaxRSServiceRuntime runtime = getJaxRSServiceRuntime(); - assertEquals(1, runtime.getRuntimeDTO().applicationDTOs.length); - assertNotNull(runtime); + assertEquals(1, runtime.getRuntimeDTO().applicationDTOs.length); + serviceRegistration = registerApplication( new TestApplication()); @@ -97,6 +99,51 @@ public class JaxrsTest extends TestHelper { } @Test + public void testApplicationWithError() throws InterruptedException { + JaxRSServiceRuntime runtime = getJaxRSServiceRuntime(); + + assertNotNull(runtime); + + RuntimeDTO runtimeDTO = runtime.getRuntimeDTO(); + + assertEquals(1, runtimeDTO.applicationDTOs.length); + assertEquals(0, runtimeDTO.failedExtensionDTOs.length); + + ServiceRegistration<?> serviceRegistration = registerApplication( + new TestApplication() { + + @Override + public Set<Object> getSingletons() { + throw new RuntimeException(); + } + + }); + + runtimeDTO = runtime.getRuntimeDTO(); + + assertEquals(1, runtimeDTO.applicationDTOs.length); + assertEquals(1, runtimeDTO.failedApplicationDTOs.length); + assertEquals( + DTOConstants.FAILURE_REASON_UNKNOWN, + runtimeDTO.failedApplicationDTOs[0].failureReason); + + Client client = createClient(); + + WebTarget webTarget = client. + target("http://localhost:8080"). + path("/test-application"); + + assertEquals(404, webTarget.request().get().getStatus()); + + serviceRegistration.unregister(); + + runtimeDTO = runtime.getRuntimeDTO(); + + assertEquals(1, runtimeDTO.applicationDTOs.length); + assertEquals(0, runtimeDTO.failedApplicationDTOs.length); + } + + @Test public void testApplicationConflict() { Client client = createClient(); http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/fe929ae8/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 da6de71..c556e4f 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 @@ -17,6 +17,7 @@ package org.apache.aries.jax.rs.whiteboard.internal; +import org.apache.aries.jax.rs.whiteboard.internal.Utils.PropertyHolder; import org.osgi.framework.ServiceReference; import org.osgi.service.jaxrs.runtime.JaxRSServiceRuntime; import org.osgi.service.jaxrs.runtime.dto.ApplicationDTO; @@ -37,7 +38,7 @@ import java.util.TreeSet; import java.util.concurrent.ConcurrentHashMap; import java.util.stream.Stream; -import static org.apache.aries.jax.rs.whiteboard.internal.Utils.getProperties; +import static org.apache.aries.jax.rs.whiteboard.internal.Utils.generateApplicationName; import static org.osgi.service.jaxrs.whiteboard.JaxRSWhiteboardConstants.JAX_RS_NAME; public class AriesJaxRSServiceRuntime implements JaxRSServiceRuntime { @@ -59,9 +60,18 @@ public class AriesJaxRSServiceRuntime implements JaxRSServiceRuntime { private Set<ServiceReference<Application>> _shadowedApplications = new TreeSet<>(); + private Set<ServiceReference<Application>> _erroredApplications = + new TreeSet<>(); + private TreeSet<ServiceReference<?>> _ungettableEndpoints = new TreeSet<>(); private TreeSet<ServiceReference<?>> _ungettableExtensions = new TreeSet<>(); + public void addErroredApplication( + ServiceReference<Application> serviceReference) { + + _erroredApplications.add(serviceReference); + } + public boolean addNotGettableApplication( ServiceReference<Application> serviceReference) { @@ -74,6 +84,12 @@ public class AriesJaxRSServiceRuntime implements JaxRSServiceRuntime { _ungettableExtensions.add(serviceReference); } + public void removeErroredApplication( + ServiceReference<Application> serviceReference) { + + _erroredApplications.remove(serviceReference); + } + public boolean removeNotGettableApplication( ServiceReference<Application> serviceReference) { @@ -191,7 +207,9 @@ public class AriesJaxRSServiceRuntime implements JaxRSServiceRuntime { runtimeDTO.failedApplicationDTOs = Stream.concat( shadowedApplicationsDTOStream(), - unreferenciableApplicationsDTOStream() + Stream.concat( + unreferenciableApplicationsDTOStream(), + erroredApplicationsDTOStream()) ).toArray( FailedApplicationDTO[]::new ); @@ -213,6 +231,13 @@ public class AriesJaxRSServiceRuntime implements JaxRSServiceRuntime { return runtimeDTO; } + private Stream<FailedApplicationDTO> erroredApplicationsDTOStream() { + return _erroredApplications.stream().map( + sr -> buildFailedApplicationDTO( + DTOConstants.FAILURE_REASON_UNKNOWN, sr) + ); + } + private FailedExtensionDTO buildFailedExtensionDTO( int reason, ServiceReference<?> serviceReference) { @@ -274,8 +299,8 @@ public class AriesJaxRSServiceRuntime implements JaxRSServiceRuntime { ApplicationDTO applicationDTO = new ApplicationDTO(){}; - applicationDTO.name = getApplicationName(properties); - applicationDTO.base = Whiteboard.getApplicationBase(properties); + applicationDTO.name = getApplicationName(properties::get); + applicationDTO.base = Whiteboard.getApplicationBase(properties::get); applicationDTO.serviceId = (Long)properties.get("service.id"); applicationDTO.resourceDTOs = getApplicationEndpointsStream( @@ -352,7 +377,7 @@ public class AriesJaxRSServiceRuntime implements JaxRSServiceRuntime { JaxRSWhiteboardConstants.JAX_RS_NAME); failedApplicationDTO.name = nameProperty == null ? - generateApplicationName(getProperties(serviceReference)) : + generateApplicationName(serviceReference::getProperty) : nameProperty.toString(); failedApplicationDTO.failureReason = reason; @@ -360,7 +385,7 @@ public class AriesJaxRSServiceRuntime implements JaxRSServiceRuntime { return failedApplicationDTO; } - public static String getApplicationName(Map<String, Object> properties) { + public static String getApplicationName(PropertyHolder properties) { Object property = properties.get(JAX_RS_NAME); @@ -371,11 +396,4 @@ public class AriesJaxRSServiceRuntime implements JaxRSServiceRuntime { return property.toString(); } - public static String generateApplicationName( - Map<String, Object> properties) { - - return ".jax-rs-application-" + - properties.get("service.id").toString(); - } - } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/fe929ae8/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 2506be3..411281f 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 @@ -48,6 +48,12 @@ import static org.apache.aries.osgi.functional.OSGi.register; */ public class Utils { + public static String generateApplicationName( + PropertyHolder propertyHolder) { + + return ".generated.for." + propertyHolder.get("service.id"); + } + public static Map<String, Object> getProperties(ServiceReference<?> sref) { String[] propertyKeys = sref.getPropertyKeys(); Map<String, Object> properties = new HashMap<>(propertyKeys.length); @@ -77,11 +83,12 @@ public class Utils { } public static OSGi<?> deployRegistrator( - Bus bus, Application application, Map<String, Object> props) { + Bus bus, ServiceTuple<Application> tuple, Map<String, Object> props, + AriesJaxRSServiceRuntime runtime) { try { CXFJaxRsServiceRegistrator registrator = - new CXFJaxRsServiceRegistrator(bus, application); + new CXFJaxRsServiceRegistrator(bus, tuple.getService()); return onClose(registrator::close).then( @@ -89,8 +96,16 @@ public class Utils { ); } catch (Exception e) { - return register( - FailedApplicationDTO.class, new FailedApplicationDTO(), props); + ServiceReference<Application> serviceReference = + tuple.getServiceReference(); + + runtime.addErroredApplication(serviceReference); + + return onClose( + () -> runtime.removeErroredApplication(serviceReference) + ).then( + nothing() + ); } } @@ -440,4 +455,8 @@ public class Utils { } } + public interface PropertyHolder { + Object get(String propertyName); + } + } http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/fe929ae8/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 aaf6220..ffb6379 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.PropertyHolder; import org.apache.aries.jax.rs.whiteboard.internal.Utils.ServiceTuple; import org.apache.aries.osgi.functional.OSGi; import org.apache.cxf.Bus; @@ -46,6 +47,7 @@ import java.util.function.Function; import static java.lang.String.format; import static org.apache.aries.jax.rs.whiteboard.internal.AriesJaxRSServiceRuntime.getApplicationName; import static org.apache.aries.jax.rs.whiteboard.internal.Utils.deployRegistrator; +import static org.apache.aries.jax.rs.whiteboard.internal.Utils.generateApplicationName; import static org.apache.aries.jax.rs.whiteboard.internal.Utils.getProperties; import static org.apache.aries.jax.rs.whiteboard.internal.Utils.highestPer; import static org.apache.aries.jax.rs.whiteboard.internal.Utils.onlyGettables; @@ -77,7 +79,7 @@ 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(Utils::getProperties).andThen(Whiteboard::getApplicationBase); + 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) { @@ -244,7 +246,7 @@ public class Whiteboard { CXFJaxRsServiceRegistrator.class, applicationFilter). flatMap(registratorReference -> just( - getApplicationName(getProperties(registratorReference))). + getApplicationName(registratorReference::getProperty)). flatMap(applicationName -> waitForExtensionDependencies(ref, service(registratorReference).flatMap(registrator -> @@ -272,22 +274,24 @@ public class Whiteboard { return bundleContext().flatMap( bundleContext -> highestRankedPerPath.flatMap( - ref -> deployApplication(configuration, bundleContext, ref) + tuple -> deployApplication( + configuration, bundleContext, tuple, runtime) ).map( ServiceTuple::getServiceReference ).map( Utils::getProperties ).foreach( p -> runtime.setApplicationForPath( - getApplicationBase(p), p), - p -> runtime.unsetApplicationForPath(getApplicationBase(p)) + getApplicationBase(p::get), p), + p -> runtime.unsetApplicationForPath( + getApplicationBase(p::get)) ) ); } private static OSGi<ServiceTuple<Application>> deployApplication( Map<String, ?> configuration, BundleContext bundleContext, - ServiceTuple<Application> tuple) { + ServiceTuple<Application> tuple, AriesJaxRSServiceRuntime runtime) { ExtensionManagerBus bus = createBus(bundleContext, configuration); @@ -297,18 +301,15 @@ public class Whiteboard { Map<String, Object> properties = getProperties(serviceReference); properties.computeIfAbsent( - JAX_RS_NAME, - (__) -> AriesJaxRSServiceRuntime.generateApplicationName( - properties)); + JAX_RS_NAME, (__) -> generateApplicationName( + serviceReference::getProperty)); return - all( - deployRegistrator(bus, tuple.getService(), properties), - registerCXFServletService( - bus, getApplicationBase(properties), properties)). - then( - just(tuple) - ); + deployRegistrator(bus, tuple, properties, runtime).then( + registerCXFServletService( + bus, getApplicationBase(properties::get), properties)).then( + just(tuple) + ); } private static OSGi<ServiceReference<Application>> @@ -438,9 +439,7 @@ public class Whiteboard { } } - public static String getApplicationBase( - Map<String, Object> properties) { - + public static String getApplicationBase(PropertyHolder properties) { return properties.get(JAX_RS_APPLICATION_BASE).toString(); }
