aries-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From csie...@apache.org
Subject aries-jax-rs-whiteboard git commit: simplify by not publishing the Bus service
Date Fri, 02 Dec 2016 10:16:56 GMT
Repository: aries-jax-rs-whiteboard
Updated Branches:
  refs/heads/master 1cb665bd0 -> bfa66e0b2


simplify by not publishing the Bus service


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/bfa66e0b
Tree: http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/tree/bfa66e0b
Diff: http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/diff/bfa66e0b

Branch: refs/heads/master
Commit: bfa66e0b23cc228b0c98ec4e967cdf8ae8d34f2d
Parents: 1cb665b
Author: Carlos Sierra <csierra@apache.org>
Authored: Fri Dec 2 11:16:23 2016 +0100
Committer: Carlos Sierra <csierra@apache.org>
Committed: Fri Dec 2 11:16:23 2016 +0100

----------------------------------------------------------------------
 .../activator/CXFJaxRsBundleActivator.java      |  93 ++++++++++++--
 .../activator/ServicesRegistrator.java          | 121 -------------------
 .../ApplicationServiceTrackerCustomizer.java    |   2 +-
 .../internal/BusServiceTrackerCustomizer.java   | 118 ------------------
 .../SingletonServiceTrackerCustomizer.java      |   2 +-
 5 files changed, 82 insertions(+), 254 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/bfa66e0b/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 918b619..97555bd 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
@@ -17,41 +17,74 @@
 
 package org.apache.aries.jax.rs.whiteboard.activator;
 
+import javax.servlet.Servlet;
+import javax.ws.rs.core.Application;
 import javax.ws.rs.ext.RuntimeDelegate;
 
+import org.apache.aries.jax.rs.whiteboard.internal.ApplicationServiceTrackerCustomizer;
+import org.apache.aries.jax.rs.whiteboard.internal.FiltersAndInterceptorsServiceTrackerCustomizer;
+import org.apache.aries.jax.rs.whiteboard.internal.SingletonServiceTrackerCustomizer;
 import org.apache.cxf.Bus;
+import org.apache.cxf.BusFactory;
+import org.apache.cxf.bus.CXFBusFactory;
+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.Filter;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceRegistration;
 import org.osgi.framework.wiring.BundleWiring;
 import org.osgi.util.tracker.ServiceTracker;
 
-import org.apache.aries.jax.rs.whiteboard.internal.BusServiceTrackerCustomizer;
 import org.apache.aries.jax.rs.whiteboard.internal.ServicesServiceTrackerCustomizer;
 
+import java.util.Dictionary;
+import java.util.Hashtable;
+
+import static org.osgi.service.http.whiteboard.HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME;
+import static org.osgi.service.http.whiteboard.HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT;
+import static org.osgi.service.http.whiteboard.HttpWhiteboardConstants.HTTP_WHITEBOARD_DEFAULT_CONTEXT_NAME;
+import static org.osgi.service.http.whiteboard.HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN;
+
 public class CXFJaxRsBundleActivator implements BundleActivator {
 
-    private ServiceTracker<?, ?> _busServiceTracker;
     private ServiceTracker<?, ?> _singletonsTracker;
-    private ServicesRegistrator _servicesRegistrator;
+    private BundleContext _bundleContext;
+    private ServiceTracker<Application, ?> _applicationTracker;
+    private ServiceTracker<Object, ?> _singletonsServiceTracker;
+    private ServiceTracker<Object, ?> _filtersAndInterceptorsServiceTracker;
 
     @Override
     public void start(BundleContext bundleContext) throws Exception {
+        _bundleContext = bundleContext;
         initRuntimeDelegate(bundleContext);
 
         // TODO make the context path of the JAX-RS Whiteboard configurable.
-        _servicesRegistrator = new ServicesRegistrator(bundleContext);
-        _servicesRegistrator.start();
+        Bus bus = BusFactory.newInstance(
+            CXFBusFactory.class.getName()).createBus();
+        registerCXFServletService(bus);
+
+        _applicationTracker = new ServiceTracker<>(
+            bundleContext, getApplicationFilter(),
+            new ApplicationServiceTrackerCustomizer(bundleContext, bus));
+        _applicationTracker.open();
 
-        _busServiceTracker = new ServiceTracker<>(
-            bundleContext, Bus.class,
-            new BusServiceTrackerCustomizer(bundleContext));
-        _busServiceTracker.open();
+        _singletonsServiceTracker = new ServiceTracker<>(
+            bundleContext, getSingletonsFilter(),
+            new SingletonServiceTrackerCustomizer(bundleContext, bus));
+        _singletonsServiceTracker.open();
 
+        _filtersAndInterceptorsServiceTracker = new ServiceTracker<>(
+            bundleContext, getFiltersFilter(),
+            new FiltersAndInterceptorsServiceTrackerCustomizer(bundleContext));
+        _filtersAndInterceptorsServiceTracker.open();
+        
         Filter filter = bundleContext.createFilter(
             "(jaxrs.application.select=*)");
+        
         _singletonsTracker = new ServiceTracker<>(
-            bundleContext, filter,
+            bundleContext, filter, 
             new ServicesServiceTrackerCustomizer(bundleContext));
         _singletonsTracker.open();
     }
@@ -63,7 +96,8 @@ public class CXFJaxRsBundleActivator implements BundleActivator {
     private void initRuntimeDelegate(BundleContext bundleContext) {
         Thread thread = Thread.currentThread();
         ClassLoader oldClassLoader = thread.getContextClassLoader();
-        BundleWiring bundleWiring = bundleContext.getBundle().adapt(BundleWiring.class);
+        BundleWiring bundleWiring = bundleContext.getBundle().adapt(
+            BundleWiring.class);
         thread.setContextClassLoader(bundleWiring.getClassLoader());
         try {
             RuntimeDelegate.getInstance();
@@ -73,11 +107,44 @@ public class CXFJaxRsBundleActivator implements BundleActivator {
         }
     }
 
+    private ServiceRegistration<Servlet> registerCXFServletService(Bus bus) {
+        Dictionary<String, Object> properties = new Hashtable<>();
+        properties.put(HTTP_WHITEBOARD_CONTEXT_SELECT,
+            "(" + HTTP_WHITEBOARD_CONTEXT_NAME + "=" + 
+                HTTP_WHITEBOARD_DEFAULT_CONTEXT_NAME + ")");
+        properties.put(HTTP_WHITEBOARD_SERVLET_PATTERN, "/*");
+        properties.put(Constants.SERVICE_RANKING, -1);
+        CXFNonSpringServlet cxfNonSpringServlet = createCXFServlet(bus);
+        return _bundleContext.registerService(
+            Servlet.class, cxfNonSpringServlet, properties);
+    }
+
+    private CXFNonSpringServlet createCXFServlet(Bus bus) {
+        CXFNonSpringServlet cxfNonSpringServlet = new CXFNonSpringServlet();
+        cxfNonSpringServlet.setBus(bus);
+        return cxfNonSpringServlet;
+    }
+
+    private Filter getFiltersFilter() throws InvalidSyntaxException {
+        return _bundleContext.createFilter("(osgi.jaxrs.filter.base=*)");
+    }
+
+    private Filter getApplicationFilter() throws InvalidSyntaxException {
+        return _bundleContext.createFilter(
+            "(&(objectClass=" + Application.class.getName() + ")" +
+                "(osgi.jaxrs.application.base=*))");
+    }
+
+    private Filter getSingletonsFilter() throws InvalidSyntaxException {
+        return _bundleContext.createFilter("(osgi.jaxrs.resource.base=*)");
+    }
+    
     @Override
     public void stop(BundleContext context) throws Exception {
-        _busServiceTracker.close();
+        _applicationTracker.close();
+        _filtersAndInterceptorsServiceTracker.close();
+        _singletonsServiceTracker.close();
         _singletonsTracker.close();
-        _servicesRegistrator.stop();
     }
 
 }

http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/bfa66e0b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/activator/ServicesRegistrator.java
----------------------------------------------------------------------
diff --git a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/activator/ServicesRegistrator.java
b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/activator/ServicesRegistrator.java
deleted file mode 100644
index 7905c4a..0000000
--- a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/activator/ServicesRegistrator.java
+++ /dev/null
@@ -1,121 +0,0 @@
-/*
- * 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.aries.jax.rs.whiteboard.activator;
-
-import org.apache.cxf.Bus;
-import org.apache.cxf.BusFactory;
-import org.apache.cxf.bus.CXFBusFactory;
-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.ServiceReference;
-import org.osgi.framework.ServiceRegistration;
-import org.osgi.service.http.context.ServletContextHelper;
-import org.osgi.util.tracker.ServiceTracker;
-import org.osgi.util.tracker.ServiceTrackerCustomizer;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import javax.servlet.Servlet;
-
-import static org.osgi.service.http.whiteboard.HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME;
-import static org.osgi.service.http.whiteboard.HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_PATH;
-import static org.osgi.service.http.whiteboard.HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT;
-import static org.osgi.service.http.whiteboard.HttpWhiteboardConstants.HTTP_WHITEBOARD_DEFAULT_CONTEXT_NAME;
-import static org.osgi.service.http.whiteboard.HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN;
-
-import java.util.Dictionary;
-import java.util.Hashtable;
-
-public class ServicesRegistrator
-    implements ServiceTrackerCustomizer<ServletContextHelper, Object> {
-
-    private static final Logger _logger = LoggerFactory.getLogger(ServicesRegistrator.class);
-
-    private final BundleContext _bundleContext;
-    private ServiceRegistration<Bus> _busServiceRegistration;
-    private ServiceTracker<ServletContextHelper, Object> _serviceTracker;
-    private ServiceRegistration<Servlet> _servletServiceRegistration;
-
-    public ServicesRegistrator(BundleContext bundleContext) {
-        _bundleContext = bundleContext;
-    }
-
-    @Override
-    public Object addingService(ServiceReference<ServletContextHelper> reference) {
-        Bus bus = BusFactory.newInstance(CXFBusFactory.class.getName()).createBus();
-        _servletServiceRegistration = createCXFServletService(bus);
-        _busServiceRegistration = createBusService(reference, bus);
-        return new Object();
-    }
-
-    private ServiceRegistration<Servlet> createCXFServletService(Bus bus) {
-        Dictionary<String, Object> properties = new Hashtable<>();
-        properties.put(HTTP_WHITEBOARD_CONTEXT_SELECT,
-            "(" + HTTP_WHITEBOARD_CONTEXT_NAME + "=" + HTTP_WHITEBOARD_DEFAULT_CONTEXT_NAME
+ ")");
-        properties.put(HTTP_WHITEBOARD_SERVLET_PATTERN, "/*");
-        properties.put(Constants.SERVICE_RANKING, -1);
-        
-        CXFNonSpringServlet cxfNonSpringServlet = new CXFNonSpringServlet();
-        cxfNonSpringServlet.setBus(bus);
-        return _bundleContext.registerService(Servlet.class, cxfNonSpringServlet, properties);
-    }
-
-    private ServiceRegistration<Bus> createBusService(ServiceReference<ServletContextHelper>
reference, Bus bus) {
-        Dictionary<String, Object> properties = new Hashtable<>();
-        String contextPath = (String)reference.getProperty(HTTP_WHITEBOARD_CONTEXT_PATH);
-        properties.put(HTTP_WHITEBOARD_CONTEXT_PATH, contextPath);
-        return _bundleContext.registerService(Bus.class, bus, properties);
-    }
-
-    @Override
-    public void modifiedService(ServiceReference<ServletContextHelper> reference, Object
object) {        
-    }
-
-    @Override
-    public void removedService(ServiceReference<ServletContextHelper> reference, Object
object) {
-        unregister(_busServiceRegistration);
-        unregister(_servletServiceRegistration);
-    }
-
-    private void unregister(ServiceRegistration<?> reg) {
-        try {
-            reg.unregister();
-        }
-        catch (Exception e) {
-            _logger.warn("Unable to unregister CXF bus service registration " + reg);
-        }
-    }
-
-    public void start() throws InvalidSyntaxException {
-        String filterS = String.format("(&(objectClass=%s)(%s=%s))",
-                                       ServletContextHelper.class.getName(),
-                                       HTTP_WHITEBOARD_CONTEXT_NAME, 
-                                       HTTP_WHITEBOARD_DEFAULT_CONTEXT_NAME);
-        Filter filter = _bundleContext.createFilter(filterS);
-        _serviceTracker = new ServiceTracker<>(_bundleContext, filter, this);
-        _serviceTracker.open();
-    }
-
-    public void stop() {
-        _serviceTracker.close();
-    }
-
-}

http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/bfa66e0b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/ApplicationServiceTrackerCustomizer.java
----------------------------------------------------------------------
diff --git a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/ApplicationServiceTrackerCustomizer.java
b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/ApplicationServiceTrackerCustomizer.java
index 01edbbc..264a3fd 100644
--- a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/ApplicationServiceTrackerCustomizer.java
+++ b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/ApplicationServiceTrackerCustomizer.java
@@ -29,7 +29,7 @@ import org.osgi.framework.ServiceReference;
 import org.osgi.framework.ServiceRegistration;
 import org.osgi.util.tracker.ServiceTrackerCustomizer;
 
-class ApplicationServiceTrackerCustomizer
+public class ApplicationServiceTrackerCustomizer
     implements ServiceTrackerCustomizer
         <Application, ApplicationServiceTrackerCustomizer.Tracked> {
 

http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/bfa66e0b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/BusServiceTrackerCustomizer.java
----------------------------------------------------------------------
diff --git a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/BusServiceTrackerCustomizer.java
b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/BusServiceTrackerCustomizer.java
deleted file mode 100644
index 2801146..0000000
--- a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/BusServiceTrackerCustomizer.java
+++ /dev/null
@@ -1,118 +0,0 @@
-/*
- * 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.aries.jax.rs.whiteboard.internal;
-
-import java.util.Arrays;
-import java.util.Collection;
-
-import javax.ws.rs.core.Application;
-
-import org.apache.cxf.Bus;
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.Filter;
-import org.osgi.framework.InvalidSyntaxException;
-import org.osgi.framework.ServiceReference;
-import org.osgi.util.tracker.ServiceTracker;
-import org.osgi.util.tracker.ServiceTrackerCustomizer;
-
-public class BusServiceTrackerCustomizer
-    implements ServiceTrackerCustomizer<Bus, Collection<ServiceTracker<?, ?>>>
{
-
-    private BundleContext _bundleContext;
-
-    public BusServiceTrackerCustomizer(BundleContext bundleContext) {
-        _bundleContext = bundleContext;
-    }
-
-    @Override
-    public Collection<ServiceTracker<?, ?>>
-    addingService(ServiceReference<Bus> serviceReference) {
-
-        Bus bus = _bundleContext.getService(serviceReference);
-
-        try {
-            ServiceTracker<Application,?> applicationTracker =
-                new ServiceTracker<>(_bundleContext, getApplicationFilter(),
-                    new ApplicationServiceTrackerCustomizer(
-                        _bundleContext, bus));
-
-            applicationTracker.open();
-
-            ServiceTracker<Object, ?> singletonsServiceTracker =
-                new ServiceTracker<>(_bundleContext, getSingletonsFilter(),
-                    new SingletonServiceTrackerCustomizer(_bundleContext, bus));
-
-            singletonsServiceTracker.open();
-
-            ServiceTracker<Object, ?> filtersAndInterceptorsServiceTracker =
-                new ServiceTracker<>(_bundleContext, getFiltersFilter(),
-                    new FiltersAndInterceptorsServiceTrackerCustomizer(
-                        _bundleContext));
-
-            filtersAndInterceptorsServiceTracker.open();
-
-            return Arrays.asList(applicationTracker, singletonsServiceTracker,
-                filtersAndInterceptorsServiceTracker);
-        }
-        catch (InvalidSyntaxException ise) {
-            throw new RuntimeException(ise);
-        }
-        catch (Exception e) {
-            _bundleContext.ungetService(serviceReference);
-
-            throw e;
-        }
-    }
-
-    private Filter getFiltersFilter() throws InvalidSyntaxException {
-        return _bundleContext.createFilter("(osgi.jaxrs.filter.base=*)");
-    }
-
-    private Filter getApplicationFilter() throws InvalidSyntaxException {
-        return _bundleContext.createFilter(
-            "(&(objectClass=" + Application.class.getName() + ")" +
-                "(osgi.jaxrs.application.base=*))");
-    }
-
-    private Filter getSingletonsFilter() throws InvalidSyntaxException {
-        return _bundleContext.createFilter("(osgi.jaxrs.resource.base=*)");
-    }
-
-    @Override
-    public void modifiedService(
-        ServiceReference<Bus> reference,
-        Collection<ServiceTracker<?, ?>> serviceTrackers) {
-
-        removedService(reference, serviceTrackers);
-
-        addingService(reference);
-    }
-
-    @Override
-    public void removedService(
-        ServiceReference<Bus> serviceReference,
-        Collection<ServiceTracker<?, ?>> serviceTrackers) {
-
-        _bundleContext.ungetService(serviceReference);
-
-        for (ServiceTracker<?, ?> serviceTracker : serviceTrackers) {
-            serviceTracker.close();
-        }
-    }
-
-}

http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/bfa66e0b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/SingletonServiceTrackerCustomizer.java
----------------------------------------------------------------------
diff --git a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/SingletonServiceTrackerCustomizer.java
b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/SingletonServiceTrackerCustomizer.java
index 64bab69..e6c2787 100644
--- a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/SingletonServiceTrackerCustomizer.java
+++ b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/SingletonServiceTrackerCustomizer.java
@@ -30,7 +30,7 @@ import java.util.Hashtable;
 import java.util.Map;
 import java.util.Set;
 
-class SingletonServiceTrackerCustomizer
+public class SingletonServiceTrackerCustomizer
     implements ServiceTrackerCustomizer
         <Object, SingletonServiceTrackerCustomizer.Tracked> {
 


Mime
View raw message