Reorganize Utils and Whiteboard methods
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/8a076268 Tree: http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/tree/8a076268 Diff: http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/diff/8a076268 Branch: refs/heads/master Commit: 8a076268716d7096423251bca36a319f7ce00a3c Parents: 72ea289 Author: Carlos Sierra <[email protected]> Authored: Fri Aug 25 17:56:34 2017 +0200 Committer: Carlos Sierra <[email protected]> Committed: Fri Aug 25 18:04:43 2017 +0200 ---------------------------------------------------------------------- .../aries/jax/rs/whiteboard/internal/Utils.java | 319 ++++---------- .../jax/rs/whiteboard/internal/Whiteboard.java | 424 ++++++++++++------- 2 files changed, 344 insertions(+), 399 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/8a076268/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 e013c14..29dead9 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 @@ -19,19 +19,15 @@ package org.apache.aries.jax.rs.whiteboard.internal; import org.apache.aries.osgi.functional.Event; import org.apache.aries.osgi.functional.OSGi; -import org.apache.cxf.Bus; import org.apache.cxf.jaxrs.lifecycle.ResourceProvider; import org.apache.cxf.message.Message; import org.osgi.framework.ServiceObjects; import org.osgi.framework.ServiceReference; -import org.osgi.service.jaxrs.whiteboard.JaxRSWhiteboardConstants; -import javax.ws.rs.core.Application; import java.util.Comparator; import java.util.HashMap; import java.util.Map; import java.util.NavigableSet; -import java.util.SortedSet; import java.util.TreeSet; import java.util.concurrent.ConcurrentHashMap; import java.util.function.Consumer; @@ -41,7 +37,6 @@ 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; /** * @author Carlos Sierra Andrés @@ -75,168 +70,6 @@ public class Utils { return properties; } - public static <T> OSGi<T> service(ServiceReference<T> serviceReference) { - return - bundleContext().flatMap(bundleContext -> - onClose(() -> bundleContext.ungetService(serviceReference)).then( - just(bundleContext.getService(serviceReference)) - )); - } - - public static <T> OSGi<ServiceObjects<T>> serviceObjects( - ServiceReference<T> serviceReference) { - - return - bundleContext().flatMap(bundleContext -> - just(bundleContext.getServiceObjects(serviceReference)) - ); - } - - public static OSGi<?> deployRegistrator( - Bus bus, ServiceTuple<Application> tuple, Map<String, Object> props, - AriesJaxRSServiceRuntime runtime) { - - try { - CXFJaxRsServiceRegistrator registrator = - new CXFJaxRsServiceRegistrator(bus, tuple.getService()); - - return - onClose(registrator::close).then( - register(CXFJaxRsServiceRegistrator.class, registrator, props) - ); - } - catch (Exception e) { - ServiceReference<Application> serviceReference = - tuple.getServiceReference(); - - runtime.addErroredApplication(serviceReference); - - return onClose( - () -> runtime.removeErroredApplication(serviceReference) - ).then( - nothing() - ); - } - } - - public static OSGi<?> safeRegisterExtension( - ServiceReference<?> serviceReference, String applicationName, - CXFJaxRsServiceRegistrator registrator, - AriesJaxRSServiceRuntime runtime) { - - Map<String, Object> properties = getProperties(serviceReference); - - properties.put( - JaxRSWhiteboardConstants.JAX_RS_NAME, applicationName); - properties.put( - "original.objectClass", - serviceReference.getProperty("objectClass")); - - return - onlyGettables( - just(serviceReference), - runtime::addNotGettableExtension, - runtime::removeNotGettableExtension - ).foreach( - registrator::addProvider, - registrator::removeProvider - ).foreach( - __ -> runtime.addApplicationExtension( - applicationName, serviceReference), - __ -> runtime.removeApplicationExtension( - applicationName, serviceReference) - ).then( - register( - ApplicationExtensionRegistration.class, - new ApplicationExtensionRegistration(){}, properties) - ); - } - - public static <T> OSGi<?> safeRegisterEndpoint( - ServiceReference<T> serviceReference, - String applicationName, - CXFJaxRsServiceRegistrator registrator, - AriesJaxRSServiceRuntime runtime) { - - return - 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 <T extends Comparable<? super T>> OSGi<T> repeatInOrder( - OSGi<T> program) { - - return program.route(new RepeatInOrderRouter<>()); - } - - public static <T> OSGi<ServiceTuple<T>> onlyGettables( - OSGi<ServiceReference<T>> program, - Consumer<ServiceReference<T>> whenAddedNotGettable, - Consumer<ServiceReference<T>> whenLeavingNotGettable) { - - return bundleContext().flatMap(bundleContext -> - program.flatMap(serviceReference -> { - 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 ServiceTuple<>(serviceReference, service)) - ); - } - )); - } - - public static <T> OSGi<ResourceProvider> - registerEndpoint( - CXFJaxRsServiceRegistrator registrator, - ServiceObjects<T> serviceObjects) { - - ResourceProvider resourceProvider = getResourceProvider(serviceObjects); - registrator.add(resourceProvider); - return just(resourceProvider); - } - - public static String safeToString(Object object) { - return object == null ? "" : object.toString(); - } - public static <T> ResourceProvider getResourceProvider( ServiceObjects<T> serviceObjects) { @@ -245,56 +78,6 @@ public class Utils { return new ComparableResourceProvider(serviceReference, serviceObjects); } - public static void unregisterEndpoint( - CXFJaxRsServiceRegistrator registrator, - ResourceProvider resourceProvider) { - - registrator.remove(resourceProvider); - } - - private static class RepeatInOrderRouter<T extends Comparable<? super T>> - implements Consumer<OSGi.Router<T>> { - - private final TreeSet<Event<T>> _treeSet; - - public RepeatInOrderRouter() { - Comparator<Event<T>> comparing = Comparator.comparing( - Event::getContent); - - _treeSet = new TreeSet<>(comparing.reversed()); - } - - @Override - public void accept(OSGi.Router<T> router) { - router.onIncoming(ev -> { - _treeSet.add(ev); - - SortedSet<Event<T>> events = _treeSet.tailSet(ev, false); - events.forEach(router::signalLeave); - - router.signalAdd(ev); - - events.forEach(router::signalAdd); - }); - router.onLeaving(ev -> { - _treeSet.remove(ev); - - SortedSet<Event<T>> events = _treeSet.tailSet(ev, false); - events.forEach(router::signalLeave); - - router.signalLeave(ev); - - events.forEach(router::signalAdd); - }); - router.onClose(() -> { - _treeSet.forEach(router::signalLeave); - - _treeSet.clear(); - }); - } - - } - public static <K, T extends Comparable<? super T>> OSGi<T> highestPer( Function<T, K> keySupplier, OSGi<T> program, Consumer<T> onAddingShadowed, Consumer<T> onRemovedShadowed) { @@ -362,11 +145,80 @@ public class Utils { ); } + public static <T> OSGi<ServiceTuple<T>> onlyGettables( + OSGi<ServiceReference<T>> program, + Consumer<ServiceReference<T>> whenAddedNotGettable, + Consumer<ServiceReference<T>> whenLeavingNotGettable) { + + return bundleContext().flatMap(bundleContext -> + program.flatMap(serviceReference -> { + 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 ServiceTuple<>(serviceReference, service)) + ); + } + )); + } + + public static <T> OSGi<T> service(ServiceReference<T> serviceReference) { + return + bundleContext().flatMap(bundleContext -> + onClose(() -> bundleContext.ungetService(serviceReference)).then( + just(bundleContext.getService(serviceReference)) + )); + } + + public static <T> OSGi<ServiceObjects<T>> serviceObjects( + ServiceReference<T> serviceReference) { + + return + bundleContext().flatMap(bundleContext -> + just(bundleContext.getServiceObjects(serviceReference)) + ); + } + + public static void unregisterEndpoint( + CXFJaxRsServiceRegistrator registrator, + ResourceProvider resourceProvider) { + + registrator.remove(resourceProvider); + } + + static OSGi<Void> ignore(OSGi<?> program) { + return program.map(t -> null); + } + + public interface PropertyHolder { + Object get(String propertyName); + } + + public interface ApplicationExtensionRegistration {} + public static class ComparableResourceProvider implements ResourceProvider, Comparable<ComparableResourceProvider> { - private ServiceReference<?> _serviceReference; private final ServiceObjects<?> _serviceObjects; + private ServiceReference<?> _serviceReference; public ComparableResourceProvider( ServiceReference<?> serviceReference, @@ -422,19 +274,17 @@ public class Utils { _service = service; } + public T getService() { + return _service; + } + @Override public int compareTo(ServiceTuple<T> o) { return _serviceReference.compareTo(o._serviceReference); } - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - - ServiceTuple<?> that = (ServiceTuple<?>) o; - - return _serviceReference.equals(that._serviceReference); + public ServiceReference<T> getServiceReference() { + return _serviceReference; } @Override @@ -442,19 +292,16 @@ public class Utils { return _serviceReference.hashCode(); } - public ServiceReference<T> getServiceReference() { - return _serviceReference; - } + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; - public T getService() { - return _service; + ServiceTuple<?> that = (ServiceTuple<?>) o; + + return _serviceReference.equals(that._serviceReference); } - } - public interface PropertyHolder { - Object get(String propertyName); } - public interface ApplicationExtensionRegistration {} - } http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/8a076268/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 a2f5b25..3b6abef 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 @@ -23,11 +23,13 @@ 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; +import org.apache.cxf.jaxrs.lifecycle.ResourceProvider; import org.apache.cxf.transport.servlet.CXFNonSpringServlet; import org.osgi.framework.BundleContext; import org.osgi.framework.Constants; import org.osgi.framework.Filter; import org.osgi.framework.InvalidSyntaxException; +import org.osgi.framework.ServiceObjects; import org.osgi.framework.ServiceReference; import org.osgi.framework.ServiceRegistration; import org.osgi.framework.wiring.BundleWiring; @@ -62,14 +64,14 @@ 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.canonicalize; -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.getResourceProvider; import static org.apache.aries.jax.rs.whiteboard.internal.Utils.highestPer; +import static org.apache.aries.jax.rs.whiteboard.internal.Utils.ignore; 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.service; +import static org.apache.aries.jax.rs.whiteboard.internal.Utils.serviceObjects; 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; @@ -119,6 +121,17 @@ public class Whiteboard { public static final String DEFAULT_NAME = ".default"; + public static OSGi<ApplicationReference> allApplicationReferences() { + return + serviceReferences(CXFJaxRsServiceRegistrator.class). + flatMap(registratorReference -> + just(getApplicationName(registratorReference::getProperty)). + flatMap(applicationName -> + service(registratorReference).flatMap(registrator -> + just(new ApplicationReference(applicationName, registrator)) + ))); + } + public static OSGi<?> createWhiteboard(Dictionary<String, ?> configuration) { AriesJaxRSServiceRuntime runtime = new AriesJaxRSServiceRuntime(); @@ -148,8 +161,108 @@ public class Whiteboard { )))))); } - private static OSGi<Void> ignore(OSGi<?> program) { - return program.map(t -> null); + public static OSGi<?> deployRegistrator( + Bus bus, ServiceTuple<Application> tuple, Map<String, Object> props, + AriesJaxRSServiceRuntime runtime) { + + try { + CXFJaxRsServiceRegistrator registrator = + new CXFJaxRsServiceRegistrator(bus, tuple.getService()); + + return + onClose(registrator::close).then( + register(CXFJaxRsServiceRegistrator.class, registrator, props) + ); + } + catch (Exception e) { + ServiceReference<Application> serviceReference = + tuple.getServiceReference(); + + runtime.addErroredApplication(serviceReference); + + return onClose( + () -> runtime.removeErroredApplication(serviceReference) + ).then( + nothing() + ); + } + } + + public static String getApplicationBase(PropertyHolder properties) { + return properties.get(JAX_RS_APPLICATION_BASE).toString(); + } + + public static <T> OSGi<ResourceProvider> + registerEndpoint( + CXFJaxRsServiceRegistrator registrator, + ServiceObjects<T> serviceObjects) { + + ResourceProvider resourceProvider = getResourceProvider(serviceObjects); + registrator.add(resourceProvider); + return just(resourceProvider); + } + + public static <T> OSGi<?> safeRegisterEndpoint( + ServiceReference<T> serviceReference, + String applicationName, + CXFJaxRsServiceRegistrator registrator, + AriesJaxRSServiceRuntime runtime) { + + return + onlyGettables( + just(serviceReference), + runtime::addNotGettableEndpoint, + runtime::removeNotGettableEndpoint + ).flatMap( + tuple -> serviceObjects(serviceReference).flatMap( + serviceObjects -> registerEndpoint( + registrator, serviceObjects).flatMap( + resourceProvider -> + onClose( + () -> Utils.unregisterEndpoint( + registrator, resourceProvider) + ) + ) + ) + ).foreach( + __ -> runtime.addApplicationEndpoint( + applicationName, serviceReference), + __ -> runtime.removeApplicationEndpoint( + applicationName, serviceReference) + ); + } + + public static OSGi<?> safeRegisterExtension( + ServiceReference<?> serviceReference, String applicationName, + CXFJaxRsServiceRegistrator registrator, + AriesJaxRSServiceRuntime runtime) { + + Map<String, Object> properties = getProperties(serviceReference); + + properties.put( + JAX_RS_NAME, applicationName); + properties.put( + "original.objectClass", + serviceReference.getProperty("objectClass")); + + return + onlyGettables( + just(serviceReference), + runtime::addNotGettableExtension, + runtime::removeNotGettableExtension + ).foreach( + registrator::addProvider, + registrator::removeProvider + ).foreach( + __ -> runtime.addApplicationExtension( + applicationName, serviceReference), + __ -> runtime.removeApplicationExtension( + applicationName, serviceReference) + ).then( + register( + ApplicationExtensionRegistration.class, + new ApplicationExtensionRegistration(){}, properties) + ); } private static OSGi<Collection<String>> bestEffortCalculationOfEnpoints(Filter filter) { @@ -168,6 +281,35 @@ public class Whiteboard { ); } + private static OSGi<ApplicationReference> chooseApplication( + ServiceReference<?> serviceReference, OSGi<ApplicationReference> theDefault) { + + Object applicationSelectProperty = serviceReference.getProperty( + JAX_RS_APPLICATION_SELECT); + + if (applicationSelectProperty == null) { + return theDefault; + } + + String applicationName = getApplicationName( + serviceReference::getProperty); + + return + serviceReferences( + CXFJaxRsServiceRegistrator.class, + applicationSelectProperty.toString()). + flatMap(registratorReference -> + service(registratorReference).flatMap(registrator -> + just(new ApplicationReference(applicationName, registrator)) + )); + } + + private static <T> OSGi<T> countChanges( + OSGi<T> program, ChangeCounter counter) { + + return program.map(t -> {counter.inc(); return t;}); + } + private static ExtensionManagerBus createBus( BundleContext bundleContext, Map<String, ?> configuration) { @@ -188,6 +330,12 @@ public class Whiteboard { return bus; } + private static CXFNonSpringServlet createCXFServlet(Bus bus) { + CXFNonSpringServlet cxfNonSpringServlet = new CXFNonSpringServlet(); + cxfNonSpringServlet.setBus(bus); + return cxfNonSpringServlet; + } + private static OSGi<ApplicationReference> createDefaultJaxRsServiceRegistrator( Map<String, ?> configuration, AriesJaxRSServiceRuntime runtime) { @@ -209,10 +357,49 @@ public class Whiteboard { )))); } + private static OSGi<ServiceTuple<Application>> deployApplication( + Map<String, ?> configuration, BundleContext bundleContext, + ServiceTuple<Application> tuple, AriesJaxRSServiceRuntime runtime) { + + ExtensionManagerBus bus = createBus(bundleContext, configuration); + + ServiceReference<Application> serviceReference = + tuple.getServiceReference(); + + Map<String, Object> properties = getProperties(serviceReference); + + properties.computeIfAbsent( + JAX_RS_NAME, (__) -> generateApplicationName( + serviceReference::getProperty)); + + return + deployRegistrator(bus, tuple, properties, runtime).then( + registerCXFServletService( + bus, getApplicationBase(properties::get), properties)).then( + just(tuple) + ); + } + + private static String getApplicationExtensionsFilter() { + return format( + "(&(!(objectClass=%s))(%s=%s)%s)", + ApplicationExtensionRegistration.class.getName(), + JAX_RS_EXTENSION, true, getExtensionsFilter()); + } + private static String getApplicationFilter() { return format("(%s=*)", JAX_RS_APPLICATION_BASE); } + private static OSGi<ServiceReference<Application>> + getApplicationsForWhiteboard( + ServiceReference<?> jaxRsRuntimeServiceReference) { + + return + serviceReferences(Application.class, getApplicationFilter()). + filter(new TargetFilter<>(jaxRsRuntimeServiceReference)); + } + private static String getExtensionsFilter() { return format("(%s=true)", JAX_RS_EXTENSION); } @@ -221,6 +408,45 @@ public class Whiteboard { return format("(%s=true)", JAX_RS_RESOURCE); } + private static OSGi<ServiceReference<Object>> onlySupportedInterfaces( + OSGi<ServiceReference<Object>> program, + Consumer<ServiceReference<?>> onInvalidAdded, + Consumer<ServiceReference<?>> onInvalidRemoved) { + + return program.flatMap(sr -> { + if (signalsValidInterface(sr)) { + return just(sr); + } + else { + onInvalidAdded.accept(sr); + return + onClose(() -> onInvalidRemoved.accept(sr)).then(nothing()); + } + }); + } + + private static OSGi<ServiceRegistration<Servlet>> registerCXFServletService( + Bus bus, String address, Map<String, ?> configuration) { + + Map<String, Object> properties = new HashMap<>(configuration); + + properties.putIfAbsent( + HTTP_WHITEBOARD_TARGET, "(osgi.http.endpoint=*)"); + + properties.putIfAbsent( + HTTP_WHITEBOARD_CONTEXT_SELECT, + format( + "(%s=%s)", + HTTP_WHITEBOARD_CONTEXT_NAME, + HTTP_WHITEBOARD_DEFAULT_CONTEXT_NAME)); + + properties.putIfAbsent(HTTP_WHITEBOARD_SERVLET_PATTERN, address + "/*"); + + CXFNonSpringServlet cxfNonSpringServlet = createCXFServlet(bus); + + return register(Servlet.class, cxfNonSpringServlet, properties); + } + private static OSGi<ServiceRegistration<?>> registerJaxRSServiceRuntime( JaxRSServiceRuntime runtime, @@ -257,6 +483,16 @@ public class Whiteboard { ); } + private static boolean signalsValidInterface( + ServiceReference<Object> serviceReference) { + + String[] objectClasses = canonicalize(serviceReference.getProperty( + "objectClass")); + + return Arrays.stream(objectClasses). + anyMatch(SUPPORTED_EXTENSION_INTERFACES::contains); + } + private static OSGi<?> waitForExtensionDependencies( BundleContext bundleContext, ServiceReference<?> serviceReference, @@ -293,7 +529,7 @@ public class Whiteboard { then(program); } catch (InvalidSyntaxException e) { - + } } @@ -304,28 +540,6 @@ public class Whiteboard { return program; } - private static OSGi<?> whiteBoardApplicationResources( - BundleContext bundleContext, - ServiceReference<?> jaxRsRuntimeServiceReference, - ApplicationReference defaultApplicationReference, - AriesJaxRSServiceRuntime runtime) { - return - serviceReferences(getResourcesFilter()). - filter(new TargetFilter<>(jaxRsRuntimeServiceReference)). - flatMap(resourceReference -> - chooseApplication( - resourceReference, just(defaultApplicationReference)). - flatMap(applicationReference -> - waitForExtensionDependencies( - bundleContext, resourceReference, applicationReference, - runtime, - safeRegisterEndpoint( - resourceReference, - applicationReference.getApplicationName(), - applicationReference.getRegistrator(), runtime) - ))); - } - private static OSGi<?> whiteBoardApplicationExtensions( BundleContext bundleContext, ServiceReference<?> jaxRsRuntimeServiceReference, @@ -352,74 +566,28 @@ public class Whiteboard { ))); } - private static OSGi<ServiceReference<Object>> onlySupportedInterfaces( - OSGi<ServiceReference<Object>> program, - Consumer<ServiceReference<?>> onInvalidAdded, - Consumer<ServiceReference<?>> onInvalidRemoved) { - - return program.flatMap(sr -> { - if (signalsValidInterface(sr)) { - return just(sr); - } - else { - onInvalidAdded.accept(sr); - return - onClose(() -> onInvalidRemoved.accept(sr)).then(nothing()); - } - }); - } - - private static boolean signalsValidInterface( - ServiceReference<Object> serviceReference) { - - String[] objectClasses = canonicalize(serviceReference.getProperty( - "objectClass")); - - return Arrays.stream(objectClasses). - anyMatch(SUPPORTED_EXTENSION_INTERFACES::contains); - } - - private static OSGi<ApplicationReference> chooseApplication( - ServiceReference<?> serviceReference, OSGi<ApplicationReference> theDefault) { - - Object applicationSelectProperty = serviceReference.getProperty( - JAX_RS_APPLICATION_SELECT); - - if (applicationSelectProperty == null) { - return theDefault; - } - - String applicationName = getApplicationName( - serviceReference::getProperty); - - return - serviceReferences( - CXFJaxRsServiceRegistrator.class, - applicationSelectProperty.toString()). - flatMap(registratorReference -> - service(registratorReference).flatMap(registrator -> - just(new ApplicationReference(applicationName, registrator)) - )); - } - - public static OSGi<ApplicationReference> allApplicationReferences() { + private static OSGi<?> whiteBoardApplicationResources( + BundleContext bundleContext, + ServiceReference<?> jaxRsRuntimeServiceReference, + ApplicationReference defaultApplicationReference, + AriesJaxRSServiceRuntime runtime) { return - serviceReferences(CXFJaxRsServiceRegistrator.class). - flatMap(registratorReference -> - just(getApplicationName(registratorReference::getProperty)). - flatMap(applicationName -> - service(registratorReference).flatMap(registrator -> - just(new ApplicationReference(applicationName, registrator)) + serviceReferences(getResourcesFilter()). + filter(new TargetFilter<>(jaxRsRuntimeServiceReference)). + flatMap(resourceReference -> + chooseApplication( + resourceReference, just(defaultApplicationReference)). + flatMap(applicationReference -> + waitForExtensionDependencies( + bundleContext, resourceReference, applicationReference, + runtime, + safeRegisterEndpoint( + resourceReference, + applicationReference.getApplicationName(), + applicationReference.getRegistrator(), runtime) ))); } - private static String getApplicationExtensionsFilter() { - return format( - "(&(!(objectClass=%s))(%s=%s)%s)", - ApplicationExtensionRegistration.class.getName(), - JAX_RS_EXTENSION, true, getExtensionsFilter()); - } - private static OSGi<?> whiteboardApplications( ServiceReference<?> jaxRsRuntimeServiceReference, AriesJaxRSServiceRuntime runtime, @@ -455,85 +623,19 @@ public class Whiteboard { ); } - private static OSGi<ServiceTuple<Application>> deployApplication( - Map<String, ?> configuration, BundleContext bundleContext, - ServiceTuple<Application> tuple, AriesJaxRSServiceRuntime runtime) { - - ExtensionManagerBus bus = createBus(bundleContext, configuration); - - ServiceReference<Application> serviceReference = - tuple.getServiceReference(); - - Map<String, Object> properties = getProperties(serviceReference); - - properties.computeIfAbsent( - JAX_RS_NAME, (__) -> generateApplicationName( - serviceReference::getProperty)); - - return - deployRegistrator(bus, tuple, properties, runtime).then( - registerCXFServletService( - bus, getApplicationBase(properties::get), properties)).then( - just(tuple) - ); - } - - private static OSGi<ServiceReference<Application>> - getApplicationsForWhiteboard( - ServiceReference<?> jaxRsRuntimeServiceReference) { - - return - serviceReferences(Application.class, getApplicationFilter()). - filter(new TargetFilter<>(jaxRsRuntimeServiceReference)); - } - - private static <T> OSGi<T> countChanges( - OSGi<T> program, ChangeCounter counter) { - - return program.map(t -> {counter.inc(); return t;}); - } - private static interface ChangeCounter { public void inc(); } - private static OSGi<ServiceRegistration<Servlet>> registerCXFServletService( - Bus bus, String address, Map<String, ?> configuration) { - - Map<String, Object> properties = new HashMap<>(configuration); - - properties.putIfAbsent( - HTTP_WHITEBOARD_TARGET, "(osgi.http.endpoint=*)"); - - properties.putIfAbsent( - HTTP_WHITEBOARD_CONTEXT_SELECT, - format( - "(%s=%s)", - HTTP_WHITEBOARD_CONTEXT_NAME, - HTTP_WHITEBOARD_DEFAULT_CONTEXT_NAME)); - - properties.putIfAbsent(HTTP_WHITEBOARD_SERVLET_PATTERN, address + "/*"); - - CXFNonSpringServlet cxfNonSpringServlet = createCXFServlet(bus); - - return register(Servlet.class, cxfNonSpringServlet, properties); - } - - private static CXFNonSpringServlet createCXFServlet(Bus bus) { - CXFNonSpringServlet cxfNonSpringServlet = new CXFNonSpringServlet(); - cxfNonSpringServlet.setBus(bus); - return cxfNonSpringServlet; - } - private static class ServiceRegistrationChangeCounter implements ChangeCounter{ private static final String changecount = "service.changecount"; private final AtomicLong _atomicLong = new AtomicLong(); - private ServiceRegistration<?> _serviceRegistration; private final Hashtable<String, Object> _properties; + private ServiceRegistration<?> _serviceRegistration; public ServiceRegistrationChangeCounter( ServiceRegistration<?> serviceRegistration) { @@ -565,10 +667,6 @@ public class Whiteboard { } } - public static String getApplicationBase(PropertyHolder properties) { - return properties.get(JAX_RS_APPLICATION_BASE).toString(); - } - private static class ApplicationReference { private final String _applicationName; private final CXFJaxRsServiceRegistrator _registrator;
