aries-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cschnei...@apache.org
Subject aries-jax-rs-whiteboard git commit: Simplify addons tracker
Date Fri, 02 Dec 2016 12:45:15 GMT
Repository: aries-jax-rs-whiteboard
Updated Branches:
  refs/heads/master 77a17072a -> b058ab793


Simplify addons tracker


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

Branch: refs/heads/master
Commit: b058ab793a1a92ce6a8f217e61f2db9c85f44aba
Parents: 77a1707
Author: Christian Schneider <chris@die-schneider.net>
Authored: Fri Dec 2 13:45:06 2016 +0100
Committer: Christian Schneider <chris@die-schneider.net>
Committed: Fri Dec 2 13:45:06 2016 +0100

----------------------------------------------------------------------
 .../AddonsServiceTrackerCustomizer.java         | 56 +++++++-------------
 .../internal/CXFJaxRsServiceRegistrator.java    | 52 +++++++-----------
 ...AndInterceptorsServiceTrackerCustomizer.java |  4 +-
 .../ServicesServiceTrackerCustomizer.java       | 43 ++++-----------
 4 files changed, 47 insertions(+), 108 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/b058ab79/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/AddonsServiceTrackerCustomizer.java
----------------------------------------------------------------------
diff --git a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/AddonsServiceTrackerCustomizer.java
b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/AddonsServiceTrackerCustomizer.java
index ddf4cb2..9db1aea 100644
--- a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/AddonsServiceTrackerCustomizer.java
+++ b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/AddonsServiceTrackerCustomizer.java
@@ -21,15 +21,12 @@ import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceReference;
 import org.osgi.util.tracker.ServiceTrackerCustomizer;
 
-import javax.ws.rs.ext.Provider;
-
 public class AddonsServiceTrackerCustomizer
     implements
         ServiceTrackerCustomizer<CXFJaxRsServiceRegistrator, CXFJaxRsServiceRegistrator>
{
 
     private final BundleContext _bundleContext;
     private final ClassLoader _classLoader;
-    private final Class<?> _serviceClass;
     private final Object _service;
 
     public AddonsServiceTrackerCustomizer(
@@ -39,42 +36,19 @@ public class AddonsServiceTrackerCustomizer
         _bundleContext = bundleContext;
         _classLoader = classLoader;
         _service = service;
-
-        _serviceClass = service.getClass();
     }
 
     @Override
-    public CXFJaxRsServiceRegistrator addingService(
-        ServiceReference<CXFJaxRsServiceRegistrator> reference) {
-
-        Thread thread = Thread.currentThread();
-
-        ClassLoader contextClassLoader =
-            thread.getContextClassLoader();
-
-        CXFJaxRsServiceRegistrator cxfJaxRsServiceRegistrator =
-            _bundleContext.getService(reference);
-
+    public CXFJaxRsServiceRegistrator addingService(ServiceReference<CXFJaxRsServiceRegistrator>
reference) {
+        CXFJaxRsServiceRegistrator registrator = _bundleContext.getService(reference);
         try {
-            thread.setContextClassLoader(_classLoader);
-
-            if (_serviceClass.isAnnotationPresent(Provider.class)) {
-                cxfJaxRsServiceRegistrator.addProvider(_service);
-            } else {
-                cxfJaxRsServiceRegistrator.addService(_service);
-            }
-
-
-            return cxfJaxRsServiceRegistrator;
+            runInClassLoader(_classLoader, () -> registrator.add(_service));
+            return registrator;
         }
         catch (Exception e) {
             _bundleContext.ungetService(reference);
-
             throw e;
         }
-        finally {
-            thread.setContextClassLoader(contextClassLoader);
-        }
     }
 
     @Override
@@ -83,7 +57,6 @@ public class AddonsServiceTrackerCustomizer
         CXFJaxRsServiceRegistrator cxfJaxRsServiceRegistrator) {
 
         removedService(reference, cxfJaxRsServiceRegistrator);
-
         addingService(reference);
     }
 
@@ -92,13 +65,20 @@ public class AddonsServiceTrackerCustomizer
         ServiceReference<CXFJaxRsServiceRegistrator> reference,
         CXFJaxRsServiceRegistrator cxfJaxRsServiceRegistrator) {
 
-        if (_serviceClass.isAnnotationPresent(Provider.class)) {
-            cxfJaxRsServiceRegistrator.removeProvider(_service);
-        } else {
-            cxfJaxRsServiceRegistrator.removeService(_service);
-        }
-
+        cxfJaxRsServiceRegistrator.remove(_service);
         _bundleContext.ungetService(reference);
     }
-
+    
+    private void runInClassLoader(ClassLoader cl, Runnable runable) {
+        Thread thread = Thread.currentThread();
+        ClassLoader contextClassLoader = thread.getContextClassLoader();
+        try {
+            thread.setContextClassLoader(_classLoader);
+            runable.run();
+        }
+        finally {
+            thread.setContextClassLoader(contextClassLoader);
+        }
+    }
+    
 }

http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/b058ab79/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 e3e8583..43f6ddb 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
@@ -23,6 +23,7 @@ import java.util.HashMap;
 import java.util.Map;
 
 import javax.ws.rs.core.Application;
+import javax.ws.rs.ext.Provider;
 import javax.ws.rs.ext.RuntimeDelegate;
 
 import org.apache.cxf.Bus;
@@ -33,6 +34,13 @@ import org.apache.cxf.jaxrs.provider.json.JSONProvider;
 import org.osgi.framework.ServiceReference;
 
 public class CXFJaxRsServiceRegistrator {
+    private volatile boolean _closed = false;
+    private final Application _application;
+    private final Bus _bus;
+    private final Map<String, Object> _properties;
+    private final Collection<Object> _providers = new ArrayList<>();
+    private Server _server;
+    private final Collection<Object> _services = new ArrayList<>();
 
     public CXFJaxRsServiceRegistrator(
         Bus bus, Application application, Map<String, Object> properties) {
@@ -71,43 +79,27 @@ public class CXFJaxRsServiceRegistrator {
         _closed = true;
     }
 
-    public void addProvider(Object provider) {
+    public void add(Object object) {
         if (_closed) {
             return;
         }
-
-        _providers.add(provider);
-
-        rewire();
-    }
-
-    public void addService(Object service) {
-        if (_closed) {
-            return;
+        if (object.getClass().isAnnotationPresent(Provider.class)) {
+            _providers.add(object);
+        } else {
+            _services.add(object);
         }
-
-        _services.add(service);
-
         rewire();
     }
 
-    public void removeProvider(Object provider) {
+    public void remove(Object object) {
         if (_closed) {
             return;
         }
-
-        _providers.remove(provider);
-
-        rewire();
-    }
-
-    public void removeService(Object service) {
-        if (_closed) {
-            return;
+        if (object.getClass().isAnnotationPresent(Provider.class)) {
+            _providers.remove(object);
+        } else {
+            _services.remove(object);
         }
-
-        _services.remove(service);
-
         rewire();
     }
 
@@ -154,12 +146,4 @@ public class CXFJaxRsServiceRegistrator {
         _server.start();
     }
 
-    private volatile boolean _closed = false;
-    private final Application _application;
-    private final Bus _bus;
-    private final Map<String, Object> _properties;
-    private final Collection<Object> _providers = new ArrayList<>();
-    private Server _server;
-    private final Collection<Object> _services = new ArrayList<>();
-
 }

http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/b058ab79/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/FiltersAndInterceptorsServiceTrackerCustomizer.java
----------------------------------------------------------------------
diff --git a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/FiltersAndInterceptorsServiceTrackerCustomizer.java
b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/FiltersAndInterceptorsServiceTrackerCustomizer.java
index 71c8725..9e516b8 100644
--- a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/FiltersAndInterceptorsServiceTrackerCustomizer.java
+++ b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/FiltersAndInterceptorsServiceTrackerCustomizer.java
@@ -62,7 +62,7 @@ public class FiltersAndInterceptorsServiceTrackerCustomizer
                         CXFJaxRsServiceRegistrator serviceRegistrator =
                             _bundleContext.getService(cxfReference);
                         try {
-                            serviceRegistrator.addProvider(service);
+                            serviceRegistrator.add(service);
 
                             return serviceRegistrator;
                         }
@@ -89,7 +89,7 @@ public class FiltersAndInterceptorsServiceTrackerCustomizer
                     CXFJaxRsServiceRegistrator serviceRegistrator) {
 
                     try {
-                        serviceRegistrator.removeProvider(service);
+                        serviceRegistrator.remove(service);
                     }
                     finally {
                         _bundleContext.ungetService(reference);

http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/b058ab79/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/ServicesServiceTrackerCustomizer.java
----------------------------------------------------------------------
diff --git a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/ServicesServiceTrackerCustomizer.java
b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/ServicesServiceTrackerCustomizer.java
index fa5410b..758199a 100644
--- a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/ServicesServiceTrackerCustomizer.java
+++ b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/ServicesServiceTrackerCustomizer.java
@@ -26,10 +26,7 @@ import org.osgi.framework.wiring.BundleWiring;
 import org.osgi.util.tracker.ServiceTracker;
 import org.osgi.util.tracker.ServiceTrackerCustomizer;
 
-public class ServicesServiceTrackerCustomizer
-    implements ServiceTrackerCustomizer
-        <Object, ServiceTracker
-            <CXFJaxRsServiceRegistrator, ?>> {
+public class ServicesServiceTrackerCustomizer implements ServiceTrackerCustomizer<Object,
ServiceTracker<?, ?>> {
 
     private final BundleContext _bundleContext;
 
@@ -38,61 +35,39 @@ public class ServicesServiceTrackerCustomizer
     }
 
     @Override
-    public ServiceTracker
-        <CXFJaxRsServiceRegistrator, ?>
-    addingService(ServiceReference<Object> reference) {
-
-        String applicationSelector =
-            reference.getProperty("jaxrs.application.select").toString();
-
+    public ServiceTracker<?, ?> addingService(ServiceReference<Object> reference)
{
+        String applicationSelector = reference.getProperty("jaxrs.application.select").toString();
         Bundle bundle = reference.getBundle();
-
         BundleWiring bundleWiring = bundle.adapt(BundleWiring.class);
-
         ClassLoader classLoader = bundleWiring.getClassLoader();
-
         Object service = _bundleContext.getService(reference);
-
         try {
             Filter filter = _bundleContext.createFilter(
                 "(&(objectClass=" + CXFJaxRsServiceRegistrator.class.getName() +
-                    ")" + applicationSelector + ")");
-
-            ServiceTracker
-                <CXFJaxRsServiceRegistrator, ?>
-                serviceTracker = new ServiceTracker<>(
-                    _bundleContext, filter,
+                ")" + applicationSelector + ")");
+            ServiceTracker<?, ?> serviceTracker = new ServiceTracker<>(
+                    _bundleContext,
+                    filter,
                     new AddonsServiceTrackerCustomizer(
                         _bundleContext, classLoader, service));
-
             serviceTracker.open();
-
             return serviceTracker;
         }
         catch (InvalidSyntaxException ise) {
             _bundleContext.ungetService(reference);
-
             throw new RuntimeException(ise);
         }
     }
 
     @Override
-    public void modifiedService(
-        ServiceReference<Object> reference,
-        ServiceTracker<CXFJaxRsServiceRegistrator, ?> serviceTracker) {
-
+    public void modifiedService(ServiceReference<Object> reference, ServiceTracker<?,
?> serviceTracker) {
         removedService(reference, serviceTracker);
-
         addingService(reference);
     }
 
     @Override
-    public void removedService(
-        ServiceReference<Object> reference,
-        ServiceTracker<CXFJaxRsServiceRegistrator, ?> serviceTracker) {
-
+    public void removedService(ServiceReference<Object> reference, ServiceTracker<?,
?> serviceTracker) {
         serviceTracker.close();
-
         _bundleContext.ungetService(reference);
     }
 


Mime
View raw message