Deploy raw services in the same application
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/d425d5d9 Tree: http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/tree/d425d5d9 Diff: http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/diff/d425d5d9 Branch: refs/heads/new-spec-with-component-dsl Commit: d425d5d97cd0fd57f864515202a8d0e865044380 Parents: 729e58c Author: Carlos Sierra <[email protected]> Authored: Wed Feb 22 17:41:57 2017 +0100 Committer: Carlos Sierra <[email protected]> Committed: Wed Feb 22 17:49:47 2017 +0100 ---------------------------------------------------------------------- .../activator/CXFJaxRsBundleActivator.java | 57 ++++++----- .../internal/CXFJaxRsServiceRegistrator.java | 100 +++++++++++++++++-- 2 files changed, 125 insertions(+), 32 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/d425d5d9/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/activator/CXFJaxRsBundleActivator.java ---------------------------------------------------------------------- diff --git a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/activator/CXFJaxRsBundleActivator.java b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/activator/CXFJaxRsBundleActivator.java index 1275c0d..e40b991 100644 --- a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/activator/CXFJaxRsBundleActivator.java +++ b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/activator/CXFJaxRsBundleActivator.java @@ -22,6 +22,7 @@ import javax.ws.rs.core.Application; import javax.ws.rs.ext.RuntimeDelegate; import org.apache.aries.jax.rs.whiteboard.internal.CXFJaxRsServiceRegistrator; +import org.apache.aries.jax.rs.whiteboard.internal.CXFJaxRsServiceRegistrator.ServiceInformation; import org.apache.aries.osgi.functional.OSGi; import org.apache.aries.osgi.functional.OSGiResult; import org.apache.cxf.Bus; @@ -37,6 +38,7 @@ import org.osgi.framework.wiring.BundleWiring; import java.util.Collections; import java.util.Dictionary; +import java.util.HashMap; import java.util.Hashtable; import java.util.Map; import java.util.Set; @@ -67,13 +69,17 @@ public class CXFJaxRsBundleActivator implements BundleActivator { )); } - private static OSGi<?> cxfRegistrator( + private static OSGi<CXFJaxRsServiceRegistrator> cxfRegistrator( Bus bus, Application application, Map<String, Object> props) { + CXFJaxRsServiceRegistrator registrator = + new CXFJaxRsServiceRegistrator(bus, application, props); + return - just(new CXFJaxRsServiceRegistrator(bus, application, props)).flatMap(registrator -> onClose(registrator::close).then( - register(CXFJaxRsServiceRegistrator.class, registrator, props))); + register(CXFJaxRsServiceRegistrator.class, registrator, props).then( + just(registrator) + )); } @Override @@ -99,6 +105,12 @@ public class CXFJaxRsBundleActivator implements BundleActivator { _applicationsResult = applications.run(bundleContext); + Application defaultApplication = new Application() {}; + + CXFJaxRsServiceRegistrator defaultServiceRegistrator = + new CXFJaxRsServiceRegistrator( + bus, defaultApplication, new HashMap<>()); + OSGi<?> singletons = serviceReferences(getSingletonsFilter()). flatMap(serviceReference -> @@ -108,16 +120,10 @@ public class CXFJaxRsBundleActivator implements BundleActivator { serviceReference, "osgi.jaxrs.resource.base")). flatMap(properties -> service(serviceReference).flatMap(service -> - cxfRegistrator(bus, - new Application() { - @Override - public Set<Object> getSingletons() { - return Collections.singleton(service); - } - }, - properties) - ))) - ); + safeRegisterEndpoint( + serviceReference, defaultServiceRegistrator) + ))) + ); _singletonsResult = singletons.run(bundleContext); @@ -166,11 +172,9 @@ public class CXFJaxRsBundleActivator implements BundleActivator { if (propertyValue == null) { return new String[0]; } - if (propertyValue instanceof String[]) { return (String[]) propertyValue; } - return new String[]{propertyValue.toString()}; } @@ -194,12 +198,14 @@ public class CXFJaxRsBundleActivator implements BundleActivator { } private OSGi<?> safeRegisterEndpoint( - ServiceReference<?> ref, CXFJaxRsServiceRegistrator registrator, - Object service) { + ServiceReference<?> ref, CXFJaxRsServiceRegistrator registrator) { return + bundleContext().flatMap(bundleContext -> + service(ref).flatMap(service -> onClose(() -> unregisterEndpoint(registrator, service)).then( - registerEndpoint(ref, registrator, service)); + registerEndpoint(ref, registrator, service) + ))); } private OSGi<?> registerEndpoint( @@ -207,21 +213,26 @@ public class CXFJaxRsBundleActivator implements BundleActivator { CXFJaxRsServiceRegistrator registrator, Object service) { Thread thread = Thread.currentThread(); - ClassLoader contextClassLoader = thread.getContextClassLoader(); - ClassLoader classLoader = ref.getBundle().adapt(BundleWiring.class). getClassLoader(); + Object resourceBaseObject = ref.getProperty("osgi.jaxrs.resource.base"); + + String resourceBase; + if (resourceBaseObject == null) { + resourceBase = ""; + } + else { + resourceBase = resourceBaseObject.toString(); + } try { thread.setContextClassLoader(classLoader); - - registrator.add(service); + registrator.add(new ServiceInformation(resourceBase, "", service)); } finally { thread.setContextClassLoader(contextClassLoader); } - return just(service); } http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/d425d5d9/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 43f6ddb..683cb0f 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 @@ -20,6 +20,8 @@ package org.apache.aries.jax.rs.whiteboard.internal; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; +import java.util.Iterator; +import java.util.List; import java.util.Map; import javax.ws.rs.core.Application; @@ -29,7 +31,10 @@ import javax.ws.rs.ext.RuntimeDelegate; import org.apache.cxf.Bus; import org.apache.cxf.endpoint.Server; import org.apache.cxf.jaxrs.JAXRSServerFactoryBean; +import org.apache.cxf.jaxrs.JAXRSServiceFactoryBean; import org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider; +import org.apache.cxf.jaxrs.model.ClassResourceInfo; +import org.apache.cxf.jaxrs.model.URITemplate; import org.apache.cxf.jaxrs.provider.json.JSONProvider; import org.osgi.framework.ServiceReference; @@ -40,7 +45,7 @@ public class CXFJaxRsServiceRegistrator { private final Map<String, Object> _properties; private final Collection<Object> _providers = new ArrayList<>(); private Server _server; - private final Collection<Object> _services = new ArrayList<>(); + private final Collection<ServiceInformation> _services = new ArrayList<>(); public CXFJaxRsServiceRegistrator( Bus bus, Application application, Map<String, Object> properties) { @@ -79,14 +84,17 @@ public class CXFJaxRsServiceRegistrator { _closed = true; } - public void add(Object object) { + public void add(ServiceInformation serviceInformation) { if (_closed) { return; } + + Object object = serviceInformation.getService(); + if (object.getClass().isAnnotationPresent(Provider.class)) { _providers.add(object); } else { - _services.add(object); + _services.add(serviceInformation); } rewire(); } @@ -95,11 +103,21 @@ public class CXFJaxRsServiceRegistrator { if (_closed) { return; } + if (object.getClass().isAnnotationPresent(Provider.class)) { _providers.remove(object); - } else { - _services.remove(object); } + else { + Iterator<ServiceInformation> iterator = _services.iterator(); + while (iterator.hasNext()) { + ServiceInformation next = iterator.next(); + + if (next.getService() == object) { + iterator.remove(); + } + } + } + rewire(); } @@ -108,6 +126,13 @@ public class CXFJaxRsServiceRegistrator { _server.destroy(); } + if (_services.isEmpty() && + _application.getSingletons().isEmpty() && + _application.getClasses().isEmpty()) { + + return; + } + RuntimeDelegate runtimeDelegate = RuntimeDelegate.getInstance(); JAXRSServerFactoryBean jaxRsServerFactoryBean = @@ -130,12 +155,42 @@ public class CXFJaxRsServiceRegistrator { jaxRsServerFactoryBean.setProvider(provider); } - for (Object service : _services) { - jaxRsServerFactoryBean.setResourceProvider( - new SingletonResourceProvider(service, true)); + JAXRSServiceFactoryBean serviceFactory = + jaxRsServerFactoryBean.getServiceFactory(); + + for (ServiceInformation serviceInformation : _services) { + Object service = serviceInformation.getService(); + + SingletonResourceProvider rp = new SingletonResourceProvider( + service, true); + + jaxRsServerFactoryBean.setResourceProvider(rp); + + List<ClassResourceInfo> classResourceInfo = + serviceFactory.getClassResourceInfo(); + + for (ClassResourceInfo resourceInfo : classResourceInfo) { + if (resourceInfo.getServiceClass() == service.getClass()) { + URITemplate uriTemplate = resourceInfo.getURITemplate(); + resourceInfo.setURITemplate( + new URITemplate( + serviceInformation.getPrefixPath() + + uriTemplate.getValue())); + } + } } - String address = _properties.get("CXF_ENDPOINT_ADDRESS").toString(); + Object cxfEndpointAddressObject = _properties.get( + "CXF_ENDPOINT_ADDRESS"); + + String address; + + if (cxfEndpointAddressObject == null) { + address = ""; + } + else { + address = cxfEndpointAddressObject.toString(); + } if (address != null) { jaxRsServerFactoryBean.setAddress(address); @@ -146,4 +201,31 @@ public class CXFJaxRsServiceRegistrator { _server.start(); } + public static class ServiceInformation { + private final String prefixPath; + private final String scope; + private final Object service; + + public ServiceInformation( + String prefixPath, String scope, Object service) { + + this.prefixPath = prefixPath; + this.scope = scope; + this.service = service; + } + + public String getPrefixPath() { + return prefixPath; + } + + public String getScope() { + return scope; + } + + public Object getService() { + return service; + } + + } + }
