Return-Path: X-Original-To: apmail-cxf-commits-archive@www.apache.org Delivered-To: apmail-cxf-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id D8DCC10D6D for ; Mon, 1 Dec 2014 14:36:14 +0000 (UTC) Received: (qmail 96577 invoked by uid 500); 1 Dec 2014 14:36:14 -0000 Delivered-To: apmail-cxf-commits-archive@cxf.apache.org Received: (qmail 96514 invoked by uid 500); 1 Dec 2014 14:36:14 -0000 Mailing-List: contact commits-help@cxf.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@cxf.apache.org Delivered-To: mailing list commits@cxf.apache.org Received: (qmail 96505 invoked by uid 99); 1 Dec 2014 14:36:14 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 01 Dec 2014 14:36:14 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id 7DD23988BE5; Mon, 1 Dec 2014 14:36:14 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: cschneider@apache.org To: commits@cxf.apache.org Message-Id: X-Mailer: ASF-Git Admin Mailer Subject: cxf git commit: CXF-6124 Switch http transport to use HttpService for OSGi Date: Mon, 1 Dec 2014 14:36:14 +0000 (UTC) Repository: cxf Updated Branches: refs/heads/master 25b660324 -> 1f02b3d4f CXF-6124 Switch http transport to use HttpService for OSGi Project: http://git-wip-us.apache.org/repos/asf/cxf/repo Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/1f02b3d4 Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/1f02b3d4 Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/1f02b3d4 Branch: refs/heads/master Commit: 1f02b3d4f2c9d341046ce35222c8f4d7674b10b2 Parents: 25b6603 Author: Christian Schneider Authored: Mon Dec 1 15:36:05 2014 +0100 Committer: Christian Schneider Committed: Mon Dec 1 15:36:05 2014 +0100 ---------------------------------------------------------------------- rt/transports/http/pom.xml | 43 ++++++- .../http/osgi/HTTPTransportActivator.java | 114 +++++++------------ .../transport/http/osgi/ServletExporter.java | 102 +++++++++++++++++ 3 files changed, 184 insertions(+), 75 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cxf/blob/1f02b3d4/rt/transports/http/pom.xml ---------------------------------------------------------------------- diff --git a/rt/transports/http/pom.xml b/rt/transports/http/pom.xml index a0de832..d40d9e6 100644 --- a/rt/transports/http/pom.xml +++ b/rt/transports/http/pom.xml @@ -35,8 +35,14 @@ javax.servlet*;version="${cxf.osgi.javax.servlet.version}", org.apache.aries*;version="${cxf.aries.version.range}";resolution:=optional, - org.springframework*;resolution:="optional";version="${cxf.osgi.spring.version}" + org.springframework*;resolution:="optional";version="${cxf.osgi.spring.version}", + org.osgi.service.http + + org.osgi.service.http, + org.apache.cxf.transports.http.*, + org.apache.cxf.transport.http.*, + org.apache.aries.blueprint.NamespaceHandler;osgi.service.blueprint.namespace="http://cxf.apache.org/transports/http/configuration" @@ -151,6 +157,41 @@ + + org.apache.felix + maven-bundle-plugin + true + + + versions + validate + + cleanVersions + + + + ${project.version} + + + + + + + ${cxf.osgi.symbolic.name} + + ${cxf.osgi.import}, + * + + + ${cxf.osgi.export} + + + ${cxf.export.service} + + ${cxf.bundle.activator} + + + http://git-wip-us.apache.org/repos/asf/cxf/blob/1f02b3d4/rt/transports/http/src/main/java/org/apache/cxf/transport/http/osgi/HTTPTransportActivator.java ---------------------------------------------------------------------- diff --git a/rt/transports/http/src/main/java/org/apache/cxf/transport/http/osgi/HTTPTransportActivator.java b/rt/transports/http/src/main/java/org/apache/cxf/transport/http/osgi/HTTPTransportActivator.java index c01afb4..7116759 100644 --- a/rt/transports/http/src/main/java/org/apache/cxf/transport/http/osgi/HTTPTransportActivator.java +++ b/rt/transports/http/src/main/java/org/apache/cxf/transport/http/osgi/HTTPTransportActivator.java @@ -19,7 +19,6 @@ package org.apache.cxf.transport.http.osgi; -import java.util.Dictionary; import java.util.Properties; import javax.servlet.Servlet; @@ -36,17 +35,22 @@ import org.apache.cxf.transport.servlet.CXFNonSpringServlet; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; import org.osgi.framework.Constants; +import org.osgi.framework.ServiceReference; import org.osgi.framework.ServiceRegistration; import org.osgi.service.cm.ConfigurationException; import org.osgi.service.cm.ManagedService; import org.osgi.service.cm.ManagedServiceFactory; +import org.osgi.service.http.HttpService; +import org.osgi.util.tracker.ServiceTracker; +import org.osgi.util.tracker.ServiceTrackerCustomizer; public class HTTPTransportActivator implements BundleActivator { - private static final String CXF_CONFIG_SCOPE = "org.apache.cxf.osgi"; - private static final String DISABLE_DEFAULT_HTTP_TRANSPORT = CXF_CONFIG_SCOPE + ".http.transport.disable"; + private static final String CXF_CONFIG_PID = "org.apache.cxf.osgi"; + private static final String DISABLE_DEFAULT_HTTP_TRANSPORT = CXF_CONFIG_PID + ".http.transport.disable"; + private ServiceTracker httpServiceTracker; - public void start(BundleContext context) throws Exception { + public void start(final BundleContext context) throws Exception { ConfigAdminHttpConduitConfigurer conduitConfigurer = new ConfigAdminHttpConduitConfigurer(); @@ -63,13 +67,39 @@ public class HTTPTransportActivator DestinationRegistry destinationRegistry = new DestinationRegistryImpl(); HTTPTransportFactory transportFactory = new HTTPTransportFactory(destinationRegistry); - Servlet servlet = new CXFNonSpringServlet(destinationRegistry , false); - ServletConfigurer servletConfig = new ServletConfigurer(context, servlet); + final Servlet servlet = new CXFNonSpringServlet(destinationRegistry , false); + httpServiceTracker = new ServiceTracker(context, HttpService.class.getName(), new ServiceTrackerCustomizer() { + + private ServiceRegistration servletPublisherReg; + private ServletExporter servletExporter; + + @Override + public void removedService(ServiceReference reference, Object service) { + servletPublisherReg.unregister(); + try { + servletExporter.updated(null); + } catch (ConfigurationException e) { + // Ignore + } + } + + @Override + public void modifiedService(ServiceReference reference, Object service) { + } + + @Override + public Object addingService(ServiceReference reference) { + HttpService httpService = (HttpService)context.getService(reference); + servletExporter = new ServletExporter(servlet, httpService); + servletPublisherReg = registerService(context, ManagedService.class, servletExporter, CXF_CONFIG_PID); + return null; + } + }); + httpServiceTracker.open(); context.registerService(DestinationRegistry.class.getName(), destinationRegistry, null); context.registerService(HTTPTransportFactory.class.getName(), transportFactory, null); - registerService(context, ManagedService.class, servletConfig, CXF_CONFIG_SCOPE); - + BlueprintNameSpaceHandlerFactory factory = new BlueprintNameSpaceHandlerFactory() { @Override @@ -81,77 +111,13 @@ public class HTTPTransportActivator "http://cxf.apache.org/transports/http/configuration"); } - private void registerService(BundleContext context, Class serviceInterface, + private ServiceRegistration registerService(BundleContext context, Class serviceInterface, Object serviceObject, String servicePid) { Properties servProps = new Properties(); servProps.put(Constants.SERVICE_PID, servicePid); - context.registerService(serviceInterface.getName(), serviceObject, servProps); + return context.registerService(serviceInterface.getName(), serviceObject, servProps); } public void stop(BundleContext context) throws Exception { } - - - class ServletConfigurer implements ManagedService { - private ServiceRegistration reg; - private BundleContext context; - private Servlet servlet; - private ServiceRegistration serviceRegistration; - - public ServletConfigurer(BundleContext context, Servlet servlet) { - this.servlet = servlet; - this.context = context; - } - - @SuppressWarnings("rawtypes") - @Override - public void updated(Dictionary properties) throws ConfigurationException { - if (reg != null) { - reg.unregister(); - } - if (properties == null) { - properties = new Properties(); - } - Properties sprops = new Properties(); - sprops.put("alias", - getProp(properties, "org.apache.cxf.servlet.context", "/cxf")); - sprops.put("servlet-name", - getProp(properties, "org.apache.cxf.servlet.name", "cxf-osgi-transport-servlet")); - sprops.put("hide-service-list-page", - getProp(properties, "org.apache.cxf.servlet.hide-service-list-page", "false")); - sprops.put("disable-address-updates", - getProp(properties, "org.apache.cxf.servlet.disable-address-updates", "true")); - sprops.put("base-address", - getProp(properties, "org.apache.cxf.servlet.base-address", "")); - sprops.put("service-list-path", - getProp(properties, "org.apache.cxf.servlet.service-list-path", "")); - sprops.put("static-resources-list", - getProp(properties, "org.apache.cxf.servlet.static-resources-list", "")); - sprops.put("redirects-list", - getProp(properties, "org.apache.cxf.servlet.redirects-list", "")); - sprops.put("redirect-servlet-name", - getProp(properties, "org.apache.cxf.servlet.redirect-servlet-name", "")); - sprops.put("redirect-servlet-path", - getProp(properties, "org.apache.cxf.servlet.redirect-servlet-path", "")); - sprops.put("service-list-all-contexts", - getProp(properties, "org.apache.cxf.servlet.service-list-all-contexts", "")); - sprops.put("service-list-page-authenticate", - getProp(properties, "org.apache.cxf.servlet.service-list-page-authenticate", "false")); - sprops.put("service-list-page-authenticate-realm", - getProp(properties, "org.apache.cxf.servlet.service-list-page-authenticate-realm", "karaf")); - sprops.put("use-x-forwarded-headers", - getProp(properties, "org.apache.cxf.servlet.use-x-forwarded-headers", "false")); - if (serviceRegistration != null) { - serviceRegistration.unregister(); - } - serviceRegistration = context.registerService(Servlet.class.getName(), servlet, sprops); - } - - @SuppressWarnings("rawtypes") - private Object getProp(Dictionary properties, String key, Object defaultValue) { - Object value = properties.get(key); - return value == null ? defaultValue : value; - } - - } } http://git-wip-us.apache.org/repos/asf/cxf/blob/1f02b3d4/rt/transports/http/src/main/java/org/apache/cxf/transport/http/osgi/ServletExporter.java ---------------------------------------------------------------------- diff --git a/rt/transports/http/src/main/java/org/apache/cxf/transport/http/osgi/ServletExporter.java b/rt/transports/http/src/main/java/org/apache/cxf/transport/http/osgi/ServletExporter.java new file mode 100644 index 0000000..3274922 --- /dev/null +++ b/rt/transports/http/src/main/java/org/apache/cxf/transport/http/osgi/ServletExporter.java @@ -0,0 +1,102 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.cxf.transport.http.osgi; + +import java.util.Dictionary; +import java.util.Properties; +import java.util.logging.Level; +import java.util.logging.Logger; + +import javax.servlet.Servlet; + +import org.apache.cxf.common.logging.LogUtils; +import org.osgi.framework.ServiceRegistration; +import org.osgi.service.cm.ConfigurationException; +import org.osgi.service.cm.ManagedService; +import org.osgi.service.http.HttpContext; +import org.osgi.service.http.HttpService; + +class ServletExporter implements ManagedService { + protected static final Logger LOG = LogUtils.getL7dLogger(ServletExporter.class); + private String alias; + private Servlet servlet; + private ServiceRegistration serviceRegistration; + private HttpService httpService; + + public ServletExporter(Servlet servlet, HttpService httpService) { + this.servlet = servlet; + this.httpService = httpService; + } + + @SuppressWarnings("rawtypes") + @Override + public void updated(Dictionary properties) throws ConfigurationException { + if (alias != null) { + httpService.unregister(alias); + alias = null; + } + if (properties == null) { + properties = new Properties(); + } + Properties sprops = new Properties(); + sprops.put("servlet-name", + getProp(properties, "org.apache.cxf.servlet.name", "cxf-osgi-transport-servlet")); + sprops.put("hide-service-list-page", + getProp(properties, "org.apache.cxf.servlet.hide-service-list-page", "false")); + sprops.put("disable-address-updates", + getProp(properties, "org.apache.cxf.servlet.disable-address-updates", "true")); + sprops.put("base-address", + getProp(properties, "org.apache.cxf.servlet.base-address", "")); + sprops.put("service-list-path", + getProp(properties, "org.apache.cxf.servlet.service-list-path", "")); + sprops.put("static-resources-list", + getProp(properties, "org.apache.cxf.servlet.static-resources-list", "")); + sprops.put("redirects-list", + getProp(properties, "org.apache.cxf.servlet.redirects-list", "")); + sprops.put("redirect-servlet-name", + getProp(properties, "org.apache.cxf.servlet.redirect-servlet-name", "")); + sprops.put("redirect-servlet-path", + getProp(properties, "org.apache.cxf.servlet.redirect-servlet-path", "")); + sprops.put("service-list-all-contexts", + getProp(properties, "org.apache.cxf.servlet.service-list-all-contexts", "")); + sprops.put("service-list-page-authenticate", + getProp(properties, "org.apache.cxf.servlet.service-list-page-authenticate", "false")); + sprops.put("service-list-page-authenticate-realm", + getProp(properties, "org.apache.cxf.servlet.service-list-page-authenticate-realm", "karaf")); + sprops.put("use-x-forwarded-headers", + getProp(properties, "org.apache.cxf.servlet.use-x-forwarded-headers", "false")); + if (serviceRegistration != null) { + serviceRegistration.unregister(); + } + alias = (String)getProp(properties, "org.apache.cxf.servlet.context", "/cxf"); + HttpContext context = httpService.createDefaultHttpContext(); + try { + httpService.registerServlet(alias, servlet, sprops, context); + } catch (Exception e) { + LOG.log(Level.WARNING, "Error registering CXF OSGi servlet " + e.getMessage(), e); + } + } + + @SuppressWarnings("rawtypes") + private Object getProp(Dictionary properties, String key, Object defaultValue) { + Object value = properties.get(key); + return value == null ? defaultValue : value; + } + +} \ No newline at end of file