Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 0906E200D0D for ; Fri, 25 Aug 2017 17:25:46 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 0793E16CC45; Fri, 25 Aug 2017 15:25:46 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 7786B16CC40 for ; Fri, 25 Aug 2017 17:25:44 +0200 (CEST) Received: (qmail 30942 invoked by uid 500); 25 Aug 2017 15:25:42 -0000 Mailing-List: contact commits-help@aries.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@aries.apache.org Delivered-To: mailing list commits@aries.apache.org Received: (qmail 30930 invoked by uid 99); 25 Aug 2017 15:25:42 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 25 Aug 2017 15:25:42 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id A5C3DE088D; Fri, 25 Aug 2017 15:25:42 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: csierra@apache.org To: commits@aries.apache.org Date: Fri, 25 Aug 2017 15:25:42 -0000 Message-Id: <5e4b9abcd352425f9e4f51923bd0c89d@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [1/8] aries-jax-rs-whiteboard git commit: Refactor to reuse logic and generate DTOs archived-at: Fri, 25 Aug 2017 15:25:46 -0000 Repository: aries-jax-rs-whiteboard Updated Branches: refs/heads/master 31bf73834 -> 432486ac7 Refactor to reuse logic and generate DTOs 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/216070bb Tree: http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/tree/216070bb Diff: http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/diff/216070bb Branch: refs/heads/master Commit: 216070bb24dc179e406af6c6043218f2259a8265 Parents: 31bf738 Author: Carlos Sierra Authored: Tue Aug 22 14:02:40 2017 +0200 Committer: Carlos Sierra Committed: Tue Aug 22 14:22:23 2017 +0200 ---------------------------------------------------------------------- jax-rs.itests/itest.bndrun | 2 +- jax-rs.itests/src/main/java/test/JaxrsTest.java | 25 +- .../main/java/test/WhiteboardFactoryTest.java | 2 - .../internal/AriesJaxRSServiceRuntime.java | 235 +++++++++++++++++-- .../internal/CXFJaxRsServiceRegistrator.java | 8 +- .../aries/jax/rs/whiteboard/internal/Utils.java | 126 ++++++---- .../jax/rs/whiteboard/internal/Whiteboard.java | 102 ++++---- 7 files changed, 374 insertions(+), 126 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/216070bb/jax-rs.itests/itest.bndrun ---------------------------------------------------------------------- diff --git a/jax-rs.itests/itest.bndrun b/jax-rs.itests/itest.bndrun index c74b820..203616a 100644 --- a/jax-rs.itests/itest.bndrun +++ b/jax-rs.itests/itest.bndrun @@ -43,4 +43,4 @@ org.osgi.service.http;version='[1.2.1,1.2.2)',\ org.osgi.service.http.whiteboard;version='[1.0.0,1.0.1)' -#-runvm: "-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005" +-runvm: "-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005" http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/216070bb/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 2f1439e..ecf817a 100644 --- a/jax-rs.itests/src/main/java/test/JaxrsTest.java +++ b/jax-rs.itests/src/main/java/test/JaxrsTest.java @@ -188,11 +188,7 @@ public class JaxrsTest extends TestHelper { ServiceRegistration applicationRegistration = null; - JaxRSServiceRuntime jaxRSServiceRuntime = getJaxRSServiceRuntime(); - try { - - applicationRegistration = registerApplication( new TestApplication()); @@ -306,7 +302,8 @@ public class JaxrsTest extends TestHelper { applicationRegistration = registerApplication( new TestApplication()); - filterRegistration = registerFilter( + filterRegistration = registerExtension( + "filter", JAX_RS_APPLICATION_SELECT, "(" + JAX_RS_APPLICATION_BASE + "=/test-application)"); @@ -354,7 +351,8 @@ public class JaxrsTest extends TestHelper { ServiceRegistration filterRegistration = null; try { - filterRegistration = registerFilter( + filterRegistration = registerExtension( + "Filter", JAX_RS_APPLICATION_SELECT, "(" + JAX_RS_APPLICATION_BASE + "=/test-application)"); @@ -785,8 +783,8 @@ public class JaxrsTest extends TestHelper { try { serviceRegistration = registerAddon(new TestAddon()); - filterRegistration = registerFilter( - JAX_RS_EXTENSION, "test-filter"); + filterRegistration = registerExtension( + "Filter", JAX_RS_EXTENSION, "test-filter"); Response response = webTarget.request().get(); @@ -830,8 +828,8 @@ public class JaxrsTest extends TestHelper { assertNull(response.getHeaders().getFirst("Filtered")); - filterRegistration = registerFilter( - JAX_RS_EXTENSION, "test-filter"); + filterRegistration = registerExtension( + "Filter", JAX_RS_EXTENSION, "test-filter"); response = webTarget.request().get(); @@ -955,7 +953,8 @@ public class JaxrsTest extends TestHelper { Dictionary properties = new Hashtable<>(); - properties.put(JAX_RS_EXTENSION, name); + properties.put(JAX_RS_EXTENSION, true); + properties.put(JAX_RS_NAME, name); for (int i = 0; i < keyValues.length; i = i + 2) { properties.put(keyValues[i].toString(), keyValues[i + 1]); @@ -965,7 +964,7 @@ public class JaxrsTest extends TestHelper { Object.class, testFilter, properties); } - private ServiceRegistration registerFilter(Object... keyValues) { + /*private ServiceRegistration registerFilter(Object... keyValues) { TestFilter testFilter = new TestFilter(); @@ -977,7 +976,7 @@ public class JaxrsTest extends TestHelper { return bundleContext.registerService( Object.class, testFilter, properties); - } + }*/ private ServiceRegistration registerUngettableApplication( Object... keyValues) { http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/216070bb/jax-rs.itests/src/main/java/test/WhiteboardFactoryTest.java ---------------------------------------------------------------------- diff --git a/jax-rs.itests/src/main/java/test/WhiteboardFactoryTest.java b/jax-rs.itests/src/main/java/test/WhiteboardFactoryTest.java index 1534fb9..2c74292 100644 --- a/jax-rs.itests/src/main/java/test/WhiteboardFactoryTest.java +++ b/jax-rs.itests/src/main/java/test/WhiteboardFactoryTest.java @@ -22,9 +22,7 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import static org.osgi.service.http.whiteboard.HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN; import static org.osgi.service.jaxrs.whiteboard.JaxRSWhiteboardConstants.JAX_RS_APPLICATION_BASE; -import static org.osgi.service.jaxrs.whiteboard.JaxRSWhiteboardConstants.JAX_RS_RESOURCE; -import java.util.Arrays; import java.util.Dictionary; import java.util.Hashtable; http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/216070bb/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 67166b0..140e82e 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,18 +17,20 @@ package org.apache.aries.jax.rs.whiteboard.internal; -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; import org.osgi.service.jaxrs.runtime.dto.DTOConstants; +import org.osgi.service.jaxrs.runtime.dto.ExtensionDTO; import org.osgi.service.jaxrs.runtime.dto.FailedApplicationDTO; +import org.osgi.service.jaxrs.runtime.dto.FailedExtensionDTO; +import org.osgi.service.jaxrs.runtime.dto.FailedResourceDTO; import org.osgi.service.jaxrs.runtime.dto.RequestInfoDTO; +import org.osgi.service.jaxrs.runtime.dto.ResourceDTO; import org.osgi.service.jaxrs.runtime.dto.RuntimeDTO; import org.osgi.service.jaxrs.whiteboard.JaxRSWhiteboardConstants; import javax.ws.rs.core.Application; -import java.util.Comparator; import java.util.Set; import java.util.TreeSet; import java.util.concurrent.ConcurrentHashMap; @@ -43,28 +45,57 @@ public class AriesJaxRSServiceRuntime implements JaxRSServiceRuntime { private ConcurrentHashMap> _applications = new ConcurrentHashMap<>(); + private ConcurrentHashMap>> + _applicationEndpoints = new ConcurrentHashMap<>(); + + private ConcurrentHashMap>> + _applicationExtensions = new ConcurrentHashMap<>(); + private TreeSet> - _ungettableApplications; + _ungettableApplications = new TreeSet<>(); private Set> _shadowedApplications = new TreeSet<>(); - public AriesJaxRSServiceRuntime(BundleContext bundleContext) { - _ungettableApplications = new TreeSet<>(Comparator.reverseOrder()); - } + private TreeSet> _ungettableEndpoints = new TreeSet<>(); + private TreeSet> _ungettableExtensions = new TreeSet<>(); - public boolean addNotGettable( + public boolean addNotGettableApplication( ServiceReference serviceReference) { return _ungettableApplications.add(serviceReference); } - public boolean removeNotGettable( + public void addNotGettableExtension( + ServiceReference serviceReference) { + + _ungettableExtensions.add(serviceReference); + } + + public boolean removeNotGettableApplication( ServiceReference serviceReference) { return _ungettableApplications.remove(serviceReference); } + public boolean removeNotGettableEndpoint( + ServiceReference serviceReference) { + + return _ungettableEndpoints.remove(serviceReference); + } + + public boolean addNotGettableEndpoint( + ServiceReference serviceReference) { + + return _ungettableEndpoints.add(serviceReference); + } + + public void removeNotGettableExtension( + ServiceReference serviceReference) { + + _ungettableExtensions.remove(serviceReference); + } + public ServiceReference setApplicationForPath( String path, ServiceReference serviceReference) { @@ -87,6 +118,61 @@ public class AriesJaxRSServiceRuntime implements JaxRSServiceRuntime { return _shadowedApplications.remove(serviceReference); } + public void addApplicationEndpoint( + String applicationName, ServiceReference endpointServiceReference) { + + _applicationEndpoints.computeIfAbsent( + applicationName, __ -> new TreeSet<>()); + + _applicationEndpoints.computeIfPresent( + applicationName, + (__, set) -> { + set.add(endpointServiceReference); + + return set; + }); + } + + public void removeApplicationEndpoint( + String applicationName, ServiceReference endpointServiceReference) { + + _applicationEndpoints.computeIfPresent( + applicationName, + (__, set) -> { + set.remove(endpointServiceReference); + + return set; + }); + } + + public void addApplicationExtension( + String applicationName, + ServiceReference extensionServiceReference) { + + _applicationExtensions.computeIfAbsent( + applicationName, __ -> new TreeSet<>()); + + _applicationExtensions.computeIfPresent( + applicationName, + (__, set) -> { + set.add(extensionServiceReference); + + return set; + }); + } + + public void removeApplicationExtension( + String applicationName, ServiceReference extensionServiceReference) { + + _applicationExtensions.computeIfPresent( + applicationName, + (__, set) -> { + set.remove(extensionServiceReference); + + return set; + }); + } + @Override public RequestInfoDTO calculateRequestInfoDTO(String path) { return null; @@ -102,23 +188,69 @@ public class AriesJaxRSServiceRuntime implements JaxRSServiceRuntime { ); runtimeDTO.failedApplicationDTOs = Stream.concat( - shadowedApplications(), - unreferenciableApplications() + shadowedApplicationsDTOStream(), + unreferenciableApplicationsDTOStream() ).toArray( FailedApplicationDTO[]::new ); + runtimeDTO.failedResourceDTOs = unreferenciableEndpointsDTOStream().map( + sr -> buildFailedResourceDTO( + DTOConstants.FAILURE_REASON_SERVICE_NOT_GETTABLE, sr) + ).toArray( + FailedResourceDTO[]::new + ); + + runtimeDTO.failedExtensionDTOs = unreferenciableExtensionsDTOStream().map( + sr -> buildFailedExtensionDTO( + DTOConstants.FAILURE_REASON_SERVICE_NOT_GETTABLE, sr) + ).toArray( + FailedExtensionDTO[]::new + ); + return runtimeDTO; } + private FailedExtensionDTO buildFailedExtensionDTO( + int reason, ServiceReference serviceReference) { + + FailedExtensionDTO failedExtensionDTO = new FailedExtensionDTO(); + + populateExtensionDTO(failedExtensionDTO, serviceReference); + + failedExtensionDTO.failureReason = reason; + + return failedExtensionDTO; + } + + private Stream> unreferenciableExtensionsDTOStream() { + return _ungettableExtensions.stream(); + } + + private FailedResourceDTO buildFailedResourceDTO( + int reason, ServiceReference serviceReference) { + + FailedResourceDTO failedResourceDTO = new FailedResourceDTO(); + + populateResourceDTO(failedResourceDTO, serviceReference); + + failedResourceDTO.failureReason = reason; + + return failedResourceDTO; + } + + private Stream> unreferenciableEndpointsDTOStream() { + return _ungettableEndpoints.stream(); + } + private Stream applicationDTOStream() { return _applications.values().stream(). map( - AriesJaxRSServiceRuntime::buildApplicationDTO + this::buildApplicationDTO ); } - private Stream unreferenciableApplications() { + private Stream unreferenciableApplicationsDTOStream() { return _ungettableApplications.stream(). map( sr -> buildFailedApplicationDTO( @@ -127,7 +259,7 @@ public class AriesJaxRSServiceRuntime implements JaxRSServiceRuntime { ); } - private Stream shadowedApplications() { + private Stream shadowedApplicationsDTOStream() { return _shadowedApplications.stream(). map(sr -> buildFailedApplicationDTO( DTOConstants.FAILURE_REASON_SHADOWED_BY_OTHER_SERVICE, @@ -135,16 +267,81 @@ public class AriesJaxRSServiceRuntime implements JaxRSServiceRuntime { ); } - private static ApplicationDTO buildApplicationDTO( + private ApplicationDTO buildApplicationDTO( ServiceReference serviceReference) { ApplicationDTO applicationDTO = new ApplicationDTO(){}; applicationDTO.name = getApplicationName(serviceReference); + applicationDTO.base = Whiteboard.getApplicationBase(serviceReference); + applicationDTO.serviceId = (Long)serviceReference.getProperty( + "service.id"); + + applicationDTO.resourceDTOs = getApplicationEndpointsStream( + applicationDTO.name).toArray( + ResourceDTO[]::new + ); + + applicationDTO.extensionDTOs = getApplicationExtensionsStream( + applicationDTO.name).toArray( + ExtensionDTO[]::new + ); return applicationDTO; } + private Stream getApplicationEndpointsStream(String name) { + + Set> applicationEndpoints = + _applicationEndpoints.get(name); + + Stream> applicationEndpointStream = + applicationEndpoints != null ? + applicationEndpoints.stream() : + Stream.empty(); + + return + applicationEndpointStream.map( + sr -> populateResourceDTO(new ResourceDTO(){}, sr) + ); + } + + private Stream getApplicationExtensionsStream(String name) { + + Set> applicationExtensions = + _applicationEndpoints.get(name); + + Stream> applicationExtensionStream = + applicationExtensions != null ? + applicationExtensions.stream() : + Stream.empty(); + + return + applicationExtensionStream.map( + sr -> populateExtensionDTO(new ExtensionDTO(){}, sr) + ); + } + + private static ResourceDTO populateResourceDTO( + ResourceDTO resourceDTO, ServiceReference serviceReference) { + + resourceDTO.name = serviceReference.getProperty(JAX_RS_NAME).toString(); + resourceDTO.serviceId = (Long)serviceReference.getProperty( + "service.id"); + + return resourceDTO; + } + + private static ExtensionDTO populateExtensionDTO( + ExtensionDTO extensionDTO, ServiceReference serviceReference) { + + extensionDTO.name = serviceReference.getProperty(JAX_RS_NAME).toString(); + extensionDTO.serviceId = (Long)serviceReference.getProperty( + "service.id"); + + return extensionDTO; + } + private static FailedApplicationDTO buildFailedApplicationDTO( int reason, ServiceReference serviceReference) { @@ -162,8 +359,8 @@ public class AriesJaxRSServiceRuntime implements JaxRSServiceRuntime { return failedApplicationDTO; } - private static String getApplicationName( - ServiceReference serviceReference) { + public static String getApplicationName( + ServiceReference serviceReference) { Object property = serviceReference.getProperty(JAX_RS_NAME); @@ -174,10 +371,10 @@ public class AriesJaxRSServiceRuntime implements JaxRSServiceRuntime { return property.toString(); } - private static String generateApplicationName( - ServiceReference serviceReference) { + public static String generateApplicationName( + ServiceReference serviceReference) { - return "jax-rs-application-" + + return ".jax-rs-application-" + serviceReference.getProperty("service.id").toString(); } http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/216070bb/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/CXFJaxRsServiceRegistrator.java ---------------------------------------------------------------------- diff --git a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/CXFJaxRsServiceRegistrator.java b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/CXFJaxRsServiceRegistrator.java index 91c70f8..a43abba 100644 --- a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/CXFJaxRsServiceRegistrator.java +++ b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/CXFJaxRsServiceRegistrator.java @@ -80,12 +80,12 @@ public class CXFJaxRsServiceRegistrator { rewire(); } - public void addProvider(Object provider) { + public void addProvider(Utils.ServiceTuple tuple) { if (_closed) { return; } - _providers.add(provider); + _providers.add(tuple.getService()); rewire(); } @@ -112,12 +112,12 @@ public class CXFJaxRsServiceRegistrator { rewire(); } - public void removeProvider(Object provider) { + public void removeProvider(Utils.ServiceTuple tuple) { if (_closed) { return; } - _providers.remove(provider); + _providers.remove(tuple.getService()); rewire(); } http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/216070bb/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 69310a4..0a343bf 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 @@ -27,7 +27,6 @@ import org.osgi.framework.ServiceReference; import org.osgi.service.jaxrs.runtime.dto.FailedApplicationDTO; import javax.ws.rs.core.Application; -import javax.ws.rs.ext.Provider; import java.util.Comparator; import java.util.Map; import java.util.NavigableSet; @@ -85,43 +84,79 @@ public class Utils { public static OSGi safeRegisterGeneric( ServiceReference serviceReference, - CXFJaxRsServiceRegistrator registrator) { - - return bundleContext().flatMap(bundleContext -> { - Object service = bundleContext.getService(serviceReference); - Class serviceClass = service.getClass(); - bundleContext.ungetService(serviceReference); - if (serviceClass.isAnnotationPresent(Provider.class)) { - return safeRegisterExtension(serviceReference, registrator); - } - else { - return safeRegisterEndpoint(serviceReference, registrator); - } - }); + String applicationName, + CXFJaxRsServiceRegistrator registrator, + AriesJaxRSServiceRuntime runtime) { + + if (isExtension(serviceReference)) { + return safeRegisterExtension( + serviceReference, applicationName, registrator, runtime); + } + else { + return safeRegisterEndpoint( + serviceReference, applicationName, registrator, runtime); + } + } + + private static boolean isExtension(ServiceReference serviceReference) { + Object extensionProperty = serviceReference.getProperty( + "osgi.jaxrs.extension"); + + return + (extensionProperty != null) && + (extensionProperty instanceof Boolean) && + ((boolean) extensionProperty); } public static OSGi safeRegisterExtension( ServiceReference serviceReference, - CXFJaxRsServiceRegistrator registrator) { + String applicationName, CXFJaxRsServiceRegistrator registrator, + AriesJaxRSServiceRuntime runtime) { return - service(serviceReference).flatMap(extension -> - onClose(() -> registrator.removeProvider(extension)). - foreach(ign -> - registrator.addProvider(extension) - )); + onlyGettables( + just(serviceReference), + runtime::addNotGettableExtension, + runtime::removeNotGettableExtension + ).foreach( + registrator::addProvider, + registrator::removeProvider + ).foreach( + __ -> runtime.addApplicationExtension( + applicationName, serviceReference), + __ -> runtime.removeApplicationExtension( + applicationName, serviceReference) + ); } public static OSGi safeRegisterEndpoint( - ServiceReference ref, CXFJaxRsServiceRegistrator registrator) { + ServiceReference serviceReference, + String applicationName, + CXFJaxRsServiceRegistrator registrator, + AriesJaxRSServiceRuntime runtime) { return - bundleContext().flatMap(bundleContext -> - serviceObjects(ref).flatMap(service -> - registerEndpoint(ref, registrator, service). - flatMap(serviceInformation -> - onClose(() -> - unregisterEndpoint(registrator, serviceInformation))))); + onlyGettables( + just(serviceReference), + runtime::addNotGettableEndpoint, + runtime::removeNotGettableEndpoint + ).flatMap( + tuple -> serviceObjects(serviceReference).flatMap( + serviceObjects -> registerEndpoint( + registrator, serviceObjects).flatMap( + resourceProvider -> + onClose( + () -> unregisterEndpoint( + registrator, resourceProvider) + ) + ) + ) + ).foreach( + __ -> runtime.addApplicationEndpoint( + applicationName, serviceReference), + __ -> runtime.removeApplicationEndpoint( + applicationName, serviceReference) + ); } public static > OSGi repeatInOrder( @@ -130,14 +165,15 @@ public class Utils { return program.route(new RepeatInOrderRouter<>()); } - public static OSGi> onlyGettables( - ServiceReference serviceReference, + public static OSGi> onlyGettables( + OSGi> program, Consumer> whenAddedNotGettable, Consumer> whenLeavingNotGettable) { - return bundleContext().flatMap( - bundleContext -> { + return bundleContext().flatMap(bundleContext -> + program.flatMap(serviceReference -> { T service = null; + try { service = bundleContext.getService(serviceReference); } @@ -148,7 +184,8 @@ public class Utils { return onClose( - () -> whenLeavingNotGettable.accept(serviceReference) + () -> whenLeavingNotGettable.accept( + serviceReference) ).then( nothing() ); @@ -157,15 +194,14 @@ public class Utils { onClose( () -> bundleContext.ungetService(serviceReference) ).then( - just(new MutableTuple<>(serviceReference, service))) - ; + just(new ServiceTuple<>(serviceReference, service)) + ); } - ); + )); } public static OSGi registerEndpoint( - ServiceReference serviceReference, CXFJaxRsServiceRegistrator registrator, ServiceObjects serviceObjects) { @@ -353,18 +389,18 @@ public class Utils { } - public static class MutableTuple implements Comparable> { + public static class ServiceTuple implements Comparable> { private final ServiceReference _serviceReference; - private T _service; + private final T _service; - public MutableTuple(ServiceReference a, T service) { + public ServiceTuple(ServiceReference a, T service) { _serviceReference = a; _service = service; } @Override - public int compareTo(MutableTuple o) { + public int compareTo(ServiceTuple o) { return _serviceReference.compareTo(o._serviceReference); } @@ -373,7 +409,7 @@ public class Utils { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; - MutableTuple that = (MutableTuple) o; + ServiceTuple that = (ServiceTuple) o; return _serviceReference.equals(that._serviceReference); } @@ -387,13 +423,9 @@ public class Utils { return _serviceReference; } - public T getSecond() { + public T getService() { return _service; } - - public void setService(T service) { - _service = service; - } - } + } http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/216070bb/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 99980bb..cb2c078 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,7 +17,7 @@ package org.apache.aries.jax.rs.whiteboard.internal; -import org.apache.aries.jax.rs.whiteboard.internal.Utils.MutableTuple; +import org.apache.aries.jax.rs.whiteboard.internal.Utils.ServiceTuple; import org.apache.aries.osgi.functional.OSGi; import org.apache.cxf.Bus; import org.apache.cxf.bus.extension.ExtensionManagerBus; @@ -50,12 +50,12 @@ 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; +import static org.apache.aries.jax.rs.whiteboard.internal.Utils.service; import static org.apache.aries.osgi.functional.OSGi.all; 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.register; import static org.apache.aries.osgi.functional.OSGi.serviceReferences; -import static org.apache.aries.osgi.functional.OSGi.services; import static org.osgi.service.http.runtime.HttpServiceRuntimeConstants.HTTP_SERVICE_ENDPOINT; import static org.osgi.service.http.whiteboard.HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME; import static org.osgi.service.http.whiteboard.HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT; @@ -75,24 +75,25 @@ import static org.osgi.service.jaxrs.whiteboard.JaxRSWhiteboardConstants.JAX_RS_ */ public class Whiteboard { - public static final Function, String> APPLICATION_BASE = ((Function, ServiceReference>) Utils.MutableTuple::getServiceReference).andThen(Whiteboard::getApplicationBase); + public static final Function, String> APPLICATION_BASE = ((Function, ServiceReference>) ServiceTuple::getServiceReference).andThen(Whiteboard::getApplicationBase); public static OSGi createWhiteboard(Dictionary configuration) { + AriesJaxRSServiceRuntime runtime = new AriesJaxRSServiceRuntime(); + Map configurationMap = Maps.from(configuration); return bundleContext().flatMap(bundleContext -> - just(new AriesJaxRSServiceRuntime(bundleContext)).flatMap(runtime -> - registerJaxRSServiceRuntime(runtime, bundleContext, Maps.from(configuration)).flatMap(runtimeResgistration -> - createDefaultJaxRsServiceRegistrator(Maps.from(configuration)).flatMap(defaultServiceRegistrator -> + registerJaxRSServiceRuntime(runtime, bundleContext, configurationMap).flatMap(runtimeResgistration -> + createDefaultJaxRsServiceRegistrator(configurationMap).flatMap(defaultServiceRegistrator -> just(new ServiceRegistrationChangeCounter(runtimeResgistration)).flatMap(counter -> just(runtimeResgistration.getReference()).flatMap(runtimeRegistration -> all( countChanges(whiteboardApplications(runtimeRegistration, runtime, Maps.from(configuration)), counter), - countChanges(whiteBoardApplicationSingletons(runtimeRegistration), counter), - countChanges(whiteboardExtensions(runtimeRegistration, defaultServiceRegistrator), counter), - countChanges(whiteboardSingletons(runtimeRegistration, defaultServiceRegistrator), counter) - ))))))); + countChanges(whiteBoardApplicationSingletons(runtimeRegistration, runtime), counter), + countChanges(whiteboardExtensions(runtimeRegistration, defaultServiceRegistrator, runtime), counter), + countChanges(whiteboardSingletons(runtimeRegistration, defaultServiceRegistrator, runtime), counter) + )))))); } private static OSGi> bestEffortCalculationOfEnpoints(Filter filter) { @@ -125,23 +126,28 @@ public class Whiteboard { return new String[]{propertyValue.toString()}; } - private static ExtensionManagerBus createBus(BundleContext bundleContext, Map configuration) { - BundleWiring wiring = bundleContext.getBundle().adapt(BundleWiring.class); + private static ExtensionManagerBus createBus( + BundleContext bundleContext, Map configuration) { + + BundleWiring wiring = bundleContext.getBundle().adapt( + BundleWiring.class); @SuppressWarnings("unchecked") Map properties = (Map)configuration; - properties.put("org.apache.cxf.bus.id", configuration.get(Constants.SERVICE_PID)); + properties.put("org.apache.cxf.bus.id", configuration.get( + Constants.SERVICE_PID)); - ExtensionManagerBus bus = new ExtensionManagerBus(null, properties, wiring.getClassLoader()); + ExtensionManagerBus bus = new ExtensionManagerBus( + null, properties, wiring.getClassLoader()); bus.initialize(); return bus; } - private static OSGi createDefaultJaxRsServiceRegistrator( - Map configuration) { + private static OSGi + createDefaultJaxRsServiceRegistrator(Map configuration) { Map properties = new HashMap<>(configuration); properties.put(JAX_RS_NAME, ".default"); @@ -177,7 +183,7 @@ public class Whiteboard { properties.putIfAbsent( HTTP_WHITEBOARD_TARGET, "(osgi.http.endpoint=*)"); - properties.put(Constants.SERVICE_RANKING, -1); + properties.putIfAbsent(Constants.SERVICE_RANKING, -1); String targetFilter = (String)properties.get(HTTP_WHITEBOARD_TARGET); @@ -218,17 +224,25 @@ public class Whiteboard { return program; } - private static OSGi whiteBoardApplicationSingletons(ServiceReference jaxRsRuntimeServiceReference) { + private static OSGi whiteBoardApplicationSingletons( + ServiceReference jaxRsRuntimeServiceReference, + AriesJaxRSServiceRuntime runtime) { return serviceReferences(format("(%s=*)", JAX_RS_APPLICATION_SELECT)). filter(new TargetFilter<>(jaxRsRuntimeServiceReference)). flatMap(ref -> just(ref.getProperty(JAX_RS_APPLICATION_SELECT).toString()). flatMap(applicationFilter -> - services(CXFJaxRsServiceRegistrator.class, applicationFilter). - flatMap(registrator -> - safeRegisterGeneric(ref, registrator) - ))); + serviceReferences( + CXFJaxRsServiceRegistrator.class, applicationFilter). + flatMap(registratorReference -> + just( + AriesJaxRSServiceRuntime.getApplicationName(registratorReference)). + flatMap(applicationName -> + waitForExtensionDependencies(ref, + service(registratorReference).flatMap(registrator -> + safeRegisterGeneric(ref, applicationName, registrator, runtime) + )))))); } private static OSGi whiteboardApplications( @@ -236,15 +250,14 @@ public class Whiteboard { AriesJaxRSServiceRuntime runtime, Map configuration) { - OSGi> gettableAplicationForWhiteboard = - getApplicationsForWhiteboard(jaxRsRuntimeServiceReference).flatMap( - sr -> onlyGettables( - sr, runtime::addNotGettable, runtime::removeNotGettable) - ); + OSGi> gettableAplicationForWhiteboard = + onlyGettables( + getApplicationsForWhiteboard(jaxRsRuntimeServiceReference), + runtime::addNotGettableApplication, + runtime::removeNotGettableApplication); - OSGi> highestRankedPerPath = highestPer( - APPLICATION_BASE, - gettableAplicationForWhiteboard, + OSGi> highestRankedPerPath = highestPer( + APPLICATION_BASE, gettableAplicationForWhiteboard, t -> runtime.addShadowedApplication(t.getServiceReference()), t -> runtime.removeShadowedApplication(t.getServiceReference()) ); @@ -254,7 +267,7 @@ public class Whiteboard { bundleContext -> highestRankedPerPath.flatMap( ref -> deployApplication(configuration, bundleContext, ref) ).map( - MutableTuple::getServiceReference). + ServiceTuple::getServiceReference). foreach( sr -> runtime.setApplicationForPath( getApplicationBase(sr), sr), @@ -264,9 +277,9 @@ public class Whiteboard { ); } - private static OSGi> deployApplication( + private static OSGi> deployApplication( Map configuration, BundleContext bundleContext, - MutableTuple tuple) { + ServiceTuple tuple) { ExtensionManagerBus bus = createBus(bundleContext, configuration); @@ -277,12 +290,16 @@ public class Whiteboard { CXFJaxRsServiceRegistrator.getProperties( serviceReference, JAX_RS_APPLICATION_BASE); + properties.computeIfAbsent( + JAX_RS_NAME, + (__) -> AriesJaxRSServiceRuntime.generateApplicationName( + tuple.getServiceReference())); + return all( - deployRegistrator(bus, tuple.getSecond(), properties), + deployRegistrator(bus, tuple.getService(), properties), registerCXFServletService( - bus, getApplicationBase(serviceReference), - properties)). + bus, getApplicationBase(serviceReference), properties)). then( just(tuple) ); @@ -301,20 +318,24 @@ public class Whiteboard { private static OSGi whiteboardExtensions( ServiceReference jaxRsRuntimeServiceReference, - CXFJaxRsServiceRegistrator defaultServiceRegistrator) { + CXFJaxRsServiceRegistrator defaultServiceRegistrator, + AriesJaxRSServiceRuntime runtime) { return serviceReferences(getExtensionFilter()). filter(new TargetFilter<>(jaxRsRuntimeServiceReference)). flatMap(ref -> waitForExtensionDependencies(ref, - safeRegisterExtension(ref, defaultServiceRegistrator) + safeRegisterExtension( + ref, ".default", defaultServiceRegistrator, runtime) ) ); } private static OSGi whiteboardSingletons( - ServiceReference jaxRsRuntimeServiceReference, CXFJaxRsServiceRegistrator defaultServiceRegistrator) { + ServiceReference jaxRsRuntimeServiceReference, + CXFJaxRsServiceRegistrator defaultServiceRegistrator, + AriesJaxRSServiceRuntime runtime) { return serviceReferences(getSingletonsFilter()). @@ -322,7 +343,8 @@ public class Whiteboard { flatMap(serviceReference -> waitForExtensionDependencies(serviceReference, safeRegisterEndpoint( - serviceReference, defaultServiceRegistrator) + serviceReference, ".default", defaultServiceRegistrator, + runtime) ) ); }