Added "gettable" checks
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/cabf65d7 Tree: http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/tree/cabf65d7 Diff: http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/diff/cabf65d7 Branch: refs/heads/master Commit: cabf65d716fc63b3db5aff1d45a3dae5ae135e01 Parents: d369a6a Author: Carlos Sierra <[email protected]> Authored: Thu Aug 17 16:13:25 2017 +0200 Committer: Carlos Sierra <[email protected]> Committed: Fri Aug 18 15:59:21 2017 +0200 ---------------------------------------------------------------------- jax-rs.itests/src/main/java/test/JaxrsTest.java | 542 ++++++++++++------- .../internal/AriesJaxRSServiceRuntime.java | 98 +++- .../aries/jax/rs/whiteboard/internal/Utils.java | 2 +- .../jax/rs/whiteboard/internal/Whiteboard.java | 64 ++- 4 files changed, 480 insertions(+), 226 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/cabf65d7/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 c36f0df..2f1439e 100644 --- a/jax-rs.itests/src/main/java/test/JaxrsTest.java +++ b/jax-rs.itests/src/main/java/test/JaxrsTest.java @@ -31,6 +31,7 @@ import org.osgi.framework.ServiceFactory; import org.osgi.framework.ServiceRegistration; import org.osgi.service.jaxrs.runtime.JaxRSServiceRuntime; +import org.osgi.service.jaxrs.runtime.dto.DTOConstants; import org.osgi.util.tracker.ServiceTracker; import test.types.TestAddon; import test.types.TestAddonConflict; @@ -54,6 +55,13 @@ public class JaxrsTest extends TestHelper { private ServiceTracker<JaxRSServiceRuntime, JaxRSServiceRuntime> _runtimeTracker; + @After + public void tearDown() { + if (_runtimeTracker != null) { + _runtimeTracker.close(); + } + } + @Test public void testApplication() throws InterruptedException { ServiceRegistration<?> serviceRegistration = null; @@ -88,15 +96,6 @@ public class JaxrsTest extends TestHelper { } } - private JaxRSServiceRuntime getJaxRSServiceRuntime() throws InterruptedException { - _runtimeTracker = new ServiceTracker<>( - bundleContext, JaxRSServiceRuntime.class, null); - - _runtimeTracker.open(); - - return _runtimeTracker.waitForService(5000); - } - @Test public void testApplicationConflict() { Client client = createClient(); @@ -144,108 +143,6 @@ public class JaxrsTest extends TestHelper { } @Test - public void testApplicationOverride() throws InterruptedException { - Client client = createClient(); - - WebTarget webTarget = client. - target("http://localhost:8080"). - path("test-application"); - - JaxRSServiceRuntime runtime = getJaxRSServiceRuntime(); - - ServiceRegistration<?> serviceRegistration = null; - ServiceRegistration<?> serviceRegistration2; - - try { - assertEquals(0, runtime.getRuntimeDTO().applicationDTOs.length); - assertEquals(0, runtime.getRuntimeDTO().failedApplicationDTOs.length); - - serviceRegistration = registerApplication(new TestApplication()); - - assertEquals(1, runtime.getRuntimeDTO().applicationDTOs.length); - assertEquals(0, runtime.getRuntimeDTO().failedApplicationDTOs.length); - - Response response = webTarget.request().get(); - - assertEquals( - "Hello application", - response.readEntity(String.class)); - - serviceRegistration2 = registerApplication( - new TestApplicationConflict(), "service.ranking", 1); - - assertEquals(1, runtime.getRuntimeDTO().applicationDTOs.length); - assertEquals(1, runtime.getRuntimeDTO().failedApplicationDTOs.length); - - response = webTarget.request().get(); - - assertEquals( - "Hello application conflict", - response.readEntity(String.class)); - - assertEquals( - "conflict", - webTarget.path("conflict").request().get(String.class)); - - serviceRegistration2.unregister(); - - assertEquals(1, runtime.getRuntimeDTO().applicationDTOs.length); - assertEquals(0, runtime.getRuntimeDTO().failedApplicationDTOs.length); - - response = webTarget.request().get(); - - assertEquals( - "Hello application", response.readEntity(String.class)); - } - finally { - if (serviceRegistration != null) { - serviceRegistration.unregister(); - } - } - } - - @Test - public void testApplicationReadd() throws InterruptedException { - Client client = createClient(); - - WebTarget webTarget = client. - target("http://localhost:8080"). - path("/test-application"); - - JaxRSServiceRuntime runtime = getJaxRSServiceRuntime(); - - Runnable testCase = () -> { - assertEquals(0, runtime.getRuntimeDTO().applicationDTOs.length); - - assertEquals(404, webTarget.request().get().getStatus()); - - ServiceRegistration<?> serviceRegistration = null; - - try { - serviceRegistration = registerApplication(new TestApplication()); - - assertEquals( - "Hello application", - webTarget. - request(). - get(). - readEntity(String.class)); - - assertEquals(1, runtime.getRuntimeDTO().applicationDTOs.length); - } - finally { - if (serviceRegistration != null) { - serviceRegistration.unregister(); - } - } - }; - - testCase.run(); - - testCase.run(); - } - - @Test public void testApplicationEndpointExtension() { Client client = createClient(); @@ -333,6 +230,67 @@ public class JaxrsTest extends TestHelper { } @Test + public void testApplicationOverride() throws InterruptedException { + Client client = createClient(); + + WebTarget webTarget = client. + target("http://localhost:8080"). + path("test-application"); + + JaxRSServiceRuntime runtime = getJaxRSServiceRuntime(); + + ServiceRegistration<?> serviceRegistration = null; + ServiceRegistration<?> serviceRegistration2; + + try { + assertEquals(0, runtime.getRuntimeDTO().applicationDTOs.length); + assertEquals(0, runtime.getRuntimeDTO().failedApplicationDTOs.length); + + serviceRegistration = registerApplication(new TestApplication()); + + assertEquals(1, runtime.getRuntimeDTO().applicationDTOs.length); + assertEquals(0, runtime.getRuntimeDTO().failedApplicationDTOs.length); + + Response response = webTarget.request().get(); + + assertEquals( + "Hello application", + response.readEntity(String.class)); + + serviceRegistration2 = registerApplication( + new TestApplicationConflict(), "service.ranking", 1); + + assertEquals(1, runtime.getRuntimeDTO().applicationDTOs.length); + assertEquals(1, runtime.getRuntimeDTO().failedApplicationDTOs.length); + + response = webTarget.request().get(); + + assertEquals( + "Hello application conflict", + response.readEntity(String.class)); + + assertEquals( + "conflict", + webTarget.path("conflict").request().get(String.class)); + + serviceRegistration2.unregister(); + + assertEquals(1, runtime.getRuntimeDTO().applicationDTOs.length); + assertEquals(0, runtime.getRuntimeDTO().failedApplicationDTOs.length); + + response = webTarget.request().get(); + + assertEquals( + "Hello application", response.readEntity(String.class)); + } + finally { + if (serviceRegistration != null) { + serviceRegistration.unregister(); + } + } + } + + @Test public void testApplicationProviderExtension() { Client client = createClient(); @@ -388,7 +346,7 @@ public class JaxrsTest extends TestHelper { "Hello application", webTarget.request().get().readEntity(String.class)); - Runnable testCase = () -> { + Runnable testCase = () -> { Response response = webTarget.request().get(); assertNull(response.getHeaders().getFirst("Filtered")); @@ -426,6 +384,47 @@ public class JaxrsTest extends TestHelper { } @Test + public void testApplicationReadd() throws InterruptedException { + Client client = createClient(); + + WebTarget webTarget = client. + target("http://localhost:8080"). + path("/test-application"); + + JaxRSServiceRuntime runtime = getJaxRSServiceRuntime(); + + Runnable testCase = () -> { + assertEquals(0, runtime.getRuntimeDTO().applicationDTOs.length); + + assertEquals(404, webTarget.request().get().getStatus()); + + ServiceRegistration<?> serviceRegistration = null; + + try { + serviceRegistration = registerApplication(new TestApplication()); + + assertEquals( + "Hello application", + webTarget. + request(). + get(). + readEntity(String.class)); + + assertEquals(1, runtime.getRuntimeDTO().applicationDTOs.length); + } + finally { + if (serviceRegistration != null) { + serviceRegistration.unregister(); + } + } + }; + + testCase.run(); + + testCase.run(); + } + + @Test public void testEndpointsOverride() { Client client = createClient(); @@ -475,6 +474,131 @@ public class JaxrsTest extends TestHelper { } @Test + public void testGettableAndNotGettableApplication() throws InterruptedException { + Client client = createClient(); + + WebTarget webTarget = client. + target("http://localhost:8080"). + path("test-application"); + + JaxRSServiceRuntime runtime = getJaxRSServiceRuntime(); + + assertEquals(0, runtime.getRuntimeDTO().applicationDTOs.length); + assertEquals(0, runtime.getRuntimeDTO().failedApplicationDTOs.length); + + ServiceRegistration<Application> serviceRegistration = + registerApplication(new TestApplication()); + + assertEquals(1, runtime.getRuntimeDTO().applicationDTOs.length); + assertEquals(0, runtime.getRuntimeDTO().failedApplicationDTOs.length); + + assertEquals( + "Hello application", + webTarget.request().get().readEntity(String.class)); + + ServiceRegistration<Application> ungettableServiceRegistration = + registerUngettableApplication("service.ranking", 1); + + assertEquals(1, runtime.getRuntimeDTO().applicationDTOs.length); + assertEquals(1, runtime.getRuntimeDTO().failedApplicationDTOs.length); + + assertEquals( + DTOConstants.FAILURE_REASON_SERVICE_NOT_GETTABLE, + runtime.getRuntimeDTO().failedApplicationDTOs[0].failureReason); + + assertEquals( + "Hello application", + webTarget.request().get().readEntity(String.class)); + + serviceRegistration.unregister(); + + assertEquals(0, runtime.getRuntimeDTO().applicationDTOs.length); + assertEquals(1, runtime.getRuntimeDTO().failedApplicationDTOs.length); + + assertEquals(404, webTarget.request().get().getStatus()); + + ungettableServiceRegistration.unregister(); + + assertEquals(0, runtime.getRuntimeDTO().applicationDTOs.length); + assertEquals(0, runtime.getRuntimeDTO().failedApplicationDTOs.length); + } + + @Test + public void testGettableAndShadowedNotGettableApplication() + throws InterruptedException { + + Client client = createClient(); + + WebTarget webTarget = client. + target("http://localhost:8080"). + path("test-application"); + + JaxRSServiceRuntime runtime = getJaxRSServiceRuntime(); + + assertEquals(0, runtime.getRuntimeDTO().applicationDTOs.length); + assertEquals(0, runtime.getRuntimeDTO().failedApplicationDTOs.length); + + ServiceRegistration<Application> serviceRegistration = + registerApplication(new TestApplication()); + + assertEquals(1, runtime.getRuntimeDTO().applicationDTOs.length); + assertEquals(0, runtime.getRuntimeDTO().failedApplicationDTOs.length); + + assertEquals( + "Hello application", + webTarget.request().get().readEntity(String.class)); + + ServiceRegistration<Application> ungettableServiceRegistration = + registerUngettableApplication("service.ranking", -1); + + assertEquals(1, runtime.getRuntimeDTO().applicationDTOs.length); + assertEquals(1, runtime.getRuntimeDTO().failedApplicationDTOs.length); + + assertEquals( + DTOConstants.FAILURE_REASON_SERVICE_NOT_GETTABLE, + runtime.getRuntimeDTO().failedApplicationDTOs[0].failureReason); + + assertEquals( + "Hello application", + webTarget.request().get().readEntity(String.class)); + + serviceRegistration.unregister(); + + assertEquals(0, runtime.getRuntimeDTO().applicationDTOs.length); + assertEquals(1, runtime.getRuntimeDTO().failedApplicationDTOs.length); + + assertEquals(404, webTarget.request().get().getStatus()); + + ungettableServiceRegistration.unregister(); + + assertEquals(0, runtime.getRuntimeDTO().applicationDTOs.length); + assertEquals(0, runtime.getRuntimeDTO().failedApplicationDTOs.length); + } + + @Test + public void testNotGettableApplication() throws InterruptedException { + JaxRSServiceRuntime runtime = getJaxRSServiceRuntime(); + + assertEquals(0, runtime.getRuntimeDTO().applicationDTOs.length); + assertEquals(0, runtime.getRuntimeDTO().failedApplicationDTOs.length); + + ServiceRegistration<Application> serviceRegistration = + registerUngettableApplication(); + + assertEquals(0, runtime.getRuntimeDTO().applicationDTOs.length); + assertEquals(1, runtime.getRuntimeDTO().failedApplicationDTOs.length); + + assertEquals( + DTOConstants.FAILURE_REASON_SERVICE_NOT_GETTABLE, + runtime.getRuntimeDTO().failedApplicationDTOs[0].failureReason); + + serviceRegistration.unregister(); + + assertEquals(0, runtime.getRuntimeDTO().applicationDTOs.length); + assertEquals(0, runtime.getRuntimeDTO().failedApplicationDTOs.length); + } + + @Test public void testStandaloneEndPoint() { Client client = createClient(); @@ -500,6 +624,32 @@ public class JaxrsTest extends TestHelper { } } + @Test + public void testStandaloneEndPointPrototypeLifecycle() { + Client client = createClient(); + + WebTarget webTarget = client. + target("http://localhost:8080"). + path("/test-addon-lifecycle"); + + ServiceRegistration<?> serviceRegistration = null; + + try { + serviceRegistration = registerAddonLifecycle( + false, JAX_RS_RESOURCE, "true"); + + String first = webTarget.request().get().readEntity(String.class); + + String second = webTarget.request().get().readEntity(String.class); + + assertNotEquals("This should be different", first, second); + } + finally { + if (serviceRegistration != null) { + serviceRegistration.unregister(); + } + } + } @Test public void testStandaloneEndPointReadd() { @@ -561,24 +711,57 @@ public class JaxrsTest extends TestHelper { } @Test - public void testStandaloneEndPointPrototypeLifecycle() { + public void testStandaloneEndpointWithExtensionsDependencies() { Client client = createClient(); WebTarget webTarget = client. target("http://localhost:8080"). - path("/test-addon-lifecycle"); + path("test"); ServiceRegistration<?> serviceRegistration = null; + ServiceRegistration<?> extensionRegistration1; + ServiceRegistration<?> extensionRegistration2; try { - serviceRegistration = registerAddonLifecycle( - false, JAX_RS_RESOURCE, "true"); + serviceRegistration = registerAddon( + new TestAddon(), + JAX_RS_EXTENSION_SELECT, new String[]{ + "(property one=one)", + "(property two=two)", + }); - String first = webTarget.request().get().readEntity(String.class); + assertEquals(404, webTarget.request().get().getStatus()); - String second = webTarget.request().get().readEntity(String.class); + extensionRegistration1 = registerExtension( + "aExtension", "property one", "one"); - assertNotEquals("This should be different", first, second); + 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) { @@ -677,67 +860,16 @@ public class JaxrsTest extends TestHelper { } } - @Test - public void testStandaloneEndpointWithExtensionsDependencies() { - Client client = createClient(); - - WebTarget webTarget = client. - target("http://localhost:8080"). - path("test"); - - ServiceRegistration<?> serviceRegistration = null; - ServiceRegistration<?> extensionRegistration1; - ServiceRegistration<?> extensionRegistration2; - - try { - serviceRegistration = registerAddon( - new TestAddon(), - JAX_RS_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(); + private JaxRSServiceRuntime getJaxRSServiceRuntime() throws InterruptedException { + _runtimeTracker = new ServiceTracker<>( + bundleContext, JaxRSServiceRuntime.class, null); - assertEquals(404, webTarget.request().get().getStatus()); + _runtimeTracker.open(); - extensionRegistration1.unregister(); - } - finally { - if (serviceRegistration != null) { - serviceRegistration.unregister(); - } - } + return _runtimeTracker.waitForService(5000); } - private ServiceRegistration<?> registerAddon(Object instance, Object ... keyValues) { + private ServiceRegistration<?> registerAddon(Object instance, Object... keyValues) { Dictionary<String, Object> properties = new Hashtable<>(); properties.put(JAX_RS_RESOURCE, "true"); @@ -751,7 +883,7 @@ public class JaxrsTest extends TestHelper { } private ServiceRegistration<?> registerAddonLifecycle( - boolean singleton, Object ... keyValues) { + boolean singleton, Object... keyValues) { Dictionary<String, Object> properties = new Hashtable<>(); @@ -762,8 +894,7 @@ public class JaxrsTest extends TestHelper { if (singleton) { return bundleContext.registerService( Object.class, new TestAddonLifecycle(), properties); - } - else { + } else { PrototypeServiceFactory<Object> prototypeServiceFactory = new PrototypeServiceFactory<Object>() { @Override @@ -782,13 +913,13 @@ public class JaxrsTest extends TestHelper { }; return bundleContext.registerService( - Object.class, (ServiceFactory<?>)prototypeServiceFactory, + Object.class, (ServiceFactory<?>) prototypeServiceFactory, properties); } } private ServiceRegistration<Application> registerApplication( - Application application, Object ... keyValues) { + Application application, Object... keyValues) { Dictionary<String, Object> properties = new Hashtable<>(); @@ -802,22 +933,23 @@ public class JaxrsTest extends TestHelper { Application.class, application, properties); } - private ServiceRegistration<?> registerFilter(Object ... keyValues) { - - TestFilter testFilter = new TestFilter(); + private ServiceRegistration<Application> registerApplication( + ServiceFactory<Application> serviceFactory, Object... keyValues) { Dictionary<String, Object> properties = new Hashtable<>(); + properties.put(JAX_RS_APPLICATION_BASE, "/test-application"); + 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); + Application.class, serviceFactory, properties); } private ServiceRegistration<?> registerExtension( - String name, Object ... keyValues) { + String name, Object... keyValues) { TestFilter testFilter = new TestFilter(); @@ -833,11 +965,41 @@ public class JaxrsTest extends TestHelper { Object.class, testFilter, properties); } - @After - public void tearDown() { - if (_runtimeTracker != null) { - _runtimeTracker.close(); + 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<Application> registerUngettableApplication( + Object... keyValues) { + + return registerApplication( + new ServiceFactory<Application>() { + @Override + public Application getService( + Bundle bundle, + ServiceRegistration<Application> serviceRegistration) { + + return null; + } + + @Override + public void ungetService( + Bundle bundle, + ServiceRegistration<Application> serviceRegistration, + Application application) { + + } + }, keyValues); } } http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/cabf65d7/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 5317b66..e312432 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 @@ -19,6 +19,7 @@ package org.apache.aries.jax.rs.whiteboard.internal; import org.apache.aries.osgi.functional.Event; import org.apache.aries.osgi.functional.OSGi; +import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceReference; import org.osgi.service.jaxrs.runtime.JaxRSServiceRuntime; import org.osgi.service.jaxrs.runtime.dto.ApplicationDTO; @@ -29,6 +30,7 @@ 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; @@ -45,12 +47,20 @@ public class AriesJaxRSServiceRuntime implements JaxRSServiceRuntime { private ConcurrentHashMap<String, TreeSet<Event<ServiceReference<Application>>>> _applications = new ConcurrentHashMap<>(); + private Collection<Event<ServiceReference<Application>>> + _ungettableApplications; + private Comparator<Event<ServiceReference<Application>>> _applicationComparator; + private BundleContext _bundleContext; + + public AriesJaxRSServiceRuntime(BundleContext bundleContext) { + _bundleContext = bundleContext; - public AriesJaxRSServiceRuntime() { _applicationComparator = Comparator.comparing(Event::getContent); _applicationComparator = _applicationComparator.reversed(); + + _ungettableApplications = new TreeSet<>(_applicationComparator); } public OSGi<ServiceReference<Application>> processApplications( @@ -61,6 +71,12 @@ public class AriesJaxRSServiceRuntime implements JaxRSServiceRuntime { ServiceReference<Application> serviceReference = event.getContent(); + if (!checkGettable(serviceReference)) { + _ungettableApplications.add(event); + + return; + } + String path = serviceReference.getProperty(JAX_RS_APPLICATION_BASE). toString(); @@ -86,6 +102,13 @@ public class AriesJaxRSServiceRuntime implements JaxRSServiceRuntime { ServiceReference<Application> serviceReference = event.getContent(); + boolean ungettable = _ungettableApplications.removeIf( + t -> t.getContent().equals(serviceReference)); + + if (ungettable) { + return; + } + String path = serviceReference.getProperty(JAX_RS_APPLICATION_BASE). toString(); @@ -112,6 +135,28 @@ public class AriesJaxRSServiceRuntime implements JaxRSServiceRuntime { }); } + private boolean checkGettable( + ServiceReference<Application> serviceReference) { + + Application application = null; + + try { + application = _bundleContext.getService( + serviceReference); + } + catch (Exception e) { + } + + if (application == null) { + return false; + } + else { + _bundleContext.ungetService(serviceReference); + } + + return true; + } + @Override public RequestInfoDTO calculateRequestInfoDTO(String path) { return null; @@ -121,7 +166,23 @@ public class AriesJaxRSServiceRuntime implements JaxRSServiceRuntime { public RuntimeDTO getRuntimeDTO() { RuntimeDTO runtimeDTO = new RuntimeDTO(); - runtimeDTO.applicationDTOs = _applications.values().stream(). + runtimeDTO.applicationDTOs = applicationDTOStream(). + toArray( + ApplicationDTO[]::new + ); + + runtimeDTO.failedApplicationDTOs = Stream.concat( + shadowedApplications(), + unreferenciableApplications() + ).toArray( + FailedApplicationDTO[]::new + ); + + return runtimeDTO; + } + + private Stream<ApplicationDTO> applicationDTOStream() { + return _applications.values().stream(). flatMap( tree -> tree.size() > 0 ? Stream.of(tree.first()) : Stream.empty() ).filter( @@ -130,25 +191,32 @@ public class AriesJaxRSServiceRuntime implements JaxRSServiceRuntime { Event::getContent ).map( AriesJaxRSServiceRuntime::buildApplicationDTO - ).toArray( - ApplicationDTO[]::new ); + } - runtimeDTO.failedApplicationDTOs = _applications.values().stream(). - flatMap( - tree -> tree.size() > 0 ? tree.tailSet(tree.first(), false).stream() : Stream.empty() - ).filter( - Objects::nonNull - ).map( + private Stream<FailedApplicationDTO> unreferenciableApplications() { + return _ungettableApplications.stream(). + map( Event::getContent ).map( sr -> buildFailedApplicationDTO( - DTOConstants.FAILURE_REASON_SHADOWED_BY_OTHER_SERVICE, sr) - ).toArray( - FailedApplicationDTO[]::new - ); + DTOConstants.FAILURE_REASON_SERVICE_NOT_GETTABLE, + sr) + ); + } - return runtimeDTO; + private Stream<FailedApplicationDTO> shadowedApplications() { + return _applications.values().stream().flatMap( + tree -> tree.size() > 0 ? tree.tailSet(tree.first(), false).stream() : Stream.empty() + ).filter( + Objects::nonNull + ).map( + Event::getContent + ).map( + sr -> buildFailedApplicationDTO( + DTOConstants.FAILURE_REASON_SHADOWED_BY_OTHER_SERVICE, + sr) + ); } private static ApplicationDTO buildApplicationDTO( http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/cabf65d7/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 58f7421..2fb2854 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 @@ -61,7 +61,7 @@ public class Utils { ); } - public static OSGi<?> cxfRegistrator( + public static OSGi<?> deployRegistrator( Bus bus, Application application, Map<String, Object> props) { try { http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/cabf65d7/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 33284a5..aaee9ac 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 @@ -42,7 +42,7 @@ import java.util.Map; import java.util.concurrent.atomic.AtomicLong; import static java.lang.String.format; -import static org.apache.aries.jax.rs.whiteboard.internal.Utils.cxfRegistrator; +import static org.apache.aries.jax.rs.whiteboard.internal.Utils.deployRegistrator; 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; @@ -72,10 +72,11 @@ import static org.osgi.service.jaxrs.whiteboard.JaxRSWhiteboardConstants.JAX_RS_ */ public class Whiteboard { public static OSGi<Void> createWhiteboard(Dictionary<String, ?> configuration) { - AriesJaxRSServiceRuntime runtime = new AriesJaxRSServiceRuntime(); + return bundleContext().flatMap(bundleContext -> + just(new AriesJaxRSServiceRuntime(bundleContext)).flatMap(runtime -> registerJaxRSServiceRuntime(runtime, bundleContext, Maps.from(configuration)).flatMap(runtimeResgistration -> createDefaultJaxRsServiceRegistrator(Maps.from(configuration)).flatMap(defaultServiceRegistrator -> just(new ServiceRegistrationChangeCounter(runtimeResgistration)).flatMap(counter -> @@ -85,7 +86,7 @@ public class Whiteboard { countChanges(whiteBoardApplicationSingletons(reference), counter), countChanges(whiteboardExtensions(reference, defaultServiceRegistrator), counter), countChanges(whiteboardSingletons(reference, defaultServiceRegistrator), counter) - )))))); + ))))))); } private static OSGi<Collection<String>> bestEffortCalculationOfEnpoints(Filter filter) { @@ -229,28 +230,51 @@ public class Whiteboard { AriesJaxRSServiceRuntime runtime, Map<String, ?> configuration) { + OSGi<ServiceReference<Application>> applicationsForWhiteboard = + getApplicationsForWhiteboard(jaxRsRuntimeServiceReference); + return bundleContext().flatMap( - bundleContext -> runtime.processApplications( - serviceReferences(Application.class, getApplicationFilter()). - filter(new TargetFilter<>(jaxRsRuntimeServiceReference))).flatMap( - ref -> - just(createBus(bundleContext, configuration)). - flatMap(bus -> - just(CXFJaxRsServiceRegistrator.getProperties(ref, JAX_RS_APPLICATION_BASE)). - flatMap(properties -> - service(ref).flatMap(application -> - all( - cxfRegistrator(bus, application, properties), - registerCXFServletService( - bus, ref.getProperty(JAX_RS_APPLICATION_BASE).toString(), - properties) - ) - ))))); + bundleContext -> + runtime.processApplications(applicationsForWhiteboard).flatMap( + ref -> deployApplication( + configuration, bundleContext, ref))); + } + + private static OSGi<Void> deployApplication( + Map<String, ?> configuration, BundleContext bundleContext, + ServiceReference<Application> ref) { + + ExtensionManagerBus bus = createBus(bundleContext, configuration); + Map<String, Object> properties = + CXFJaxRsServiceRegistrator.getProperties( + ref, JAX_RS_APPLICATION_BASE); + + return service(ref).flatMap( + application -> + all( + deployRegistrator(bus, application, properties), + registerCXFServletService( + bus, ref.getProperty(JAX_RS_APPLICATION_BASE).toString(), + properties) + ) + ); + } + + private static OSGi<ServiceReference<Application>> + getApplicationsForWhiteboard( + ServiceReference<?> jaxRsRuntimeServiceReference) { + + return + serviceReferences( + Application.class, getApplicationFilter()). + filter( + new TargetFilter<>(jaxRsRuntimeServiceReference)); } private static OSGi<?> whiteboardExtensions( - ServiceReference<?> jaxRsRuntimeServiceReference, CXFJaxRsServiceRegistrator defaultServiceRegistrator) { + ServiceReference<?> jaxRsRuntimeServiceReference, + CXFJaxRsServiceRegistrator defaultServiceRegistrator) { return serviceReferences(getExtensionFilter()).
