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 D4D18200C49 for ; Fri, 24 Feb 2017 17:41:50 +0100 (CET) Received: by cust-asf.ponee.io (Postfix) id D3C8F160B7D; Fri, 24 Feb 2017 16:41:50 +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 E38AE160B84 for ; Fri, 24 Feb 2017 17:41:49 +0100 (CET) Received: (qmail 77973 invoked by uid 500); 24 Feb 2017 16:41:49 -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 77340 invoked by uid 99); 24 Feb 2017 16:41:48 -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, 24 Feb 2017 16:41:48 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 78559DFD9E; Fri, 24 Feb 2017 16:41:48 +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, 24 Feb 2017 16:41:57 -0000 Message-Id: <612df43ab8b34749a78d515c46482150@git.apache.org> In-Reply-To: <8e0abbe534cb411ab53f0b541633ccf6@git.apache.org> References: <8e0abbe534cb411ab53f0b541633ccf6@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [10/12] aries-jax-rs-whiteboard git commit: Deploy raw services in the same application archived-at: Fri, 24 Feb 2017 16:41:51 -0000 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/e7caec34 Tree: http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/tree/e7caec34 Diff: http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/diff/e7caec34 Branch: refs/heads/master Commit: e7caec349a925e31d0734a7ff065c4da6bb724c9 Parents: d337acd Author: Carlos Sierra Authored: Wed Feb 22 17:41:57 2017 +0100 Committer: Raymond Auge Committed: Fri Feb 24 10:11:03 2017 -0500 ---------------------------------------------------------------------- .../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/e7caec34/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 cxfRegistrator( Bus bus, Application application, Map 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 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/e7caec34/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 _properties; private final Collection _providers = new ArrayList<>(); private Server _server; - private final Collection _services = new ArrayList<>(); + private final Collection _services = new ArrayList<>(); public CXFJaxRsServiceRegistrator( Bus bus, Application application, Map 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 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 = + 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; + } + + } + }