You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@aries.apache.org by cs...@apache.org on 2016/12/02 10:58:13 UTC

aries-jax-rs-whiteboard git commit: Unify handling of tracked CXFJaxRsServiceRegistrator

Repository: aries-jax-rs-whiteboard
Updated Branches:
  refs/heads/master bfa66e0b2 -> 77a17072a


Unify handling of tracked CXFJaxRsServiceRegistrator


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

Branch: refs/heads/master
Commit: 77a17072a7d7c965cdbf12f2dc0ea7df09191c7b
Parents: bfa66e0
Author: Christian Schneider <ch...@die-schneider.net>
Authored: Fri Dec 2 11:57:59 2016 +0100
Committer: Christian Schneider <ch...@die-schneider.net>
Committed: Fri Dec 2 11:57:59 2016 +0100

----------------------------------------------------------------------
 .../ApplicationServiceTrackerCustomizer.java    |  88 ++-----------
 .../internal/CXFJaxRsServiceRegistrator.java    |  17 +++
 .../SingletonServiceTrackerCustomizer.java      | 129 ++++---------------
 .../internal/TrackedJaxRsRegistrator.java       |  45 +++++++
 4 files changed, 97 insertions(+), 182 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/77a17072/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 264a3fd..ccf2165 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
@@ -17,8 +17,6 @@
 
 package org.apache.aries.jax.rs.whiteboard.internal;
 
-import java.util.HashMap;
-import java.util.Hashtable;
 import java.util.Map;
 
 import javax.ws.rs.core.Application;
@@ -26,107 +24,43 @@ import javax.ws.rs.core.Application;
 import org.apache.cxf.Bus;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceReference;
-import org.osgi.framework.ServiceRegistration;
 import org.osgi.util.tracker.ServiceTrackerCustomizer;
 
 public class ApplicationServiceTrackerCustomizer
-    implements ServiceTrackerCustomizer
-        <Application, ApplicationServiceTrackerCustomizer.Tracked> {
+    implements ServiceTrackerCustomizer<Application, TrackedJaxRsRegistrator> {
 
     private BundleContext _bundleContext;
     private Bus _bus;
 
-    public ApplicationServiceTrackerCustomizer(
-        BundleContext bundleContext, Bus bus) {
-
+    public ApplicationServiceTrackerCustomizer(BundleContext bundleContext, Bus bus) {
         _bundleContext = bundleContext;
         _bus = bus;
     }
 
     @Override
-    public Tracked addingService(
-        ServiceReference<Application> serviceReference) {
-
-        Application application = _bundleContext.getService(
-            serviceReference);
-
+    public TrackedJaxRsRegistrator addingService(ServiceReference<Application> serviceReference) {
+        Application application = _bundleContext.getService(serviceReference);
         try {
-            String[] propertyKeys = serviceReference.getPropertyKeys();
-
-            Map<String, Object> properties = new HashMap<>(
-                propertyKeys.length);
-
-            for (String propertyKey : propertyKeys) {
-                properties.put(
-                    propertyKey, serviceReference.getProperty(propertyKey));
-            }
-
-            properties.put(
-                "CXF_ENDPOINT_ADDRESS",
-                serviceReference.getProperty("osgi.jaxrs.application.base").
-                    toString());
-
-            CXFJaxRsServiceRegistrator cxfJaxRsServiceRegistrator =
-                new CXFJaxRsServiceRegistrator(_bus, application, properties);
-
-            ServiceRegistration<CXFJaxRsServiceRegistrator>
-                cxfJaxRsServiceRegistratorRegistration =
-                    _bundleContext.registerService(
-                        CXFJaxRsServiceRegistrator.class,
-                        cxfJaxRsServiceRegistrator,
-                        new Hashtable<>(properties));
-
-            return new Tracked(
-                cxfJaxRsServiceRegistrator, application,
-                cxfJaxRsServiceRegistratorRegistration);
+            Map<String, Object> props = CXFJaxRsServiceRegistrator
+                .getProperties(serviceReference, "osgi.jaxrs.application.base");
+            CXFJaxRsServiceRegistrator registrator = new CXFJaxRsServiceRegistrator(_bus, application, props);
+            return new TrackedJaxRsRegistrator(registrator, _bundleContext, props);
         }
         catch (Throwable e) {
             _bundleContext.ungetService(serviceReference);
-
             throw e;
         }
     }
 
     @Override
-    public void modifiedService(
-        ServiceReference<Application> serviceReference, Tracked tracked) {
-
+    public void modifiedService(ServiceReference<Application> serviceReference, TrackedJaxRsRegistrator tracked) {
         removedService(serviceReference, tracked);
-
         addingService(serviceReference);
     }
 
     @Override
-    public void removedService(
-        ServiceReference<Application> reference, Tracked tracked) {
-
+    public void removedService(ServiceReference<Application> reference, TrackedJaxRsRegistrator tracked) {
         _bundleContext.ungetService(reference);
-
-        tracked._cxfJaxRsServiceRegistrator.close();
-
-        tracked._cxfJaxRsServiceRegistratorServiceRegistration.unregister();
-    }
-
-    public static class Tracked {
-
-        private final CXFJaxRsServiceRegistrator _cxfJaxRsServiceRegistrator;
-        private final Application _application;
-        private final ServiceRegistration<CXFJaxRsServiceRegistrator>
-            _cxfJaxRsServiceRegistratorServiceRegistration;
-
-        public Tracked(
-            CXFJaxRsServiceRegistrator cxfJaxRsServiceRegistrator,
-            Application application,
-            ServiceRegistration<CXFJaxRsServiceRegistrator>
-                cxfJaxRsServiceRegistratorServiceRegistration) {
-
-            _cxfJaxRsServiceRegistrator = cxfJaxRsServiceRegistrator;
-            _application = application;
-            _cxfJaxRsServiceRegistratorServiceRegistration =
-                cxfJaxRsServiceRegistratorServiceRegistration;
-        }
-
+        tracked.close();
     }
 }
-
-

http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/77a17072/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 e6ab988..e3e8583 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
@@ -19,6 +19,7 @@ package org.apache.aries.jax.rs.whiteboard.internal;
 
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.HashMap;
 import java.util.Map;
 
 import javax.ws.rs.core.Application;
@@ -29,6 +30,7 @@ import org.apache.cxf.endpoint.Server;
 import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
 import org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider;
 import org.apache.cxf.jaxrs.provider.json.JSONProvider;
+import org.osgi.framework.ServiceReference;
 
 public class CXFJaxRsServiceRegistrator {
 
@@ -41,6 +43,21 @@ public class CXFJaxRsServiceRegistrator {
 
         rewire();
     }
+    
+    public static Map<String, Object> getProperties(ServiceReference<?> sref, String addressKey) {
+        String[] propertyKeys = sref.getPropertyKeys();
+        Map<String, Object> properties = new HashMap<String, Object>(propertyKeys.length);
+
+        for (String key : propertyKeys) {
+            if (key.equals("osgi.jaxrs.resource.base")) {
+                continue;
+            }
+            properties.put(key, sref.getProperty(key));
+        }
+
+        properties.put("CXF_ENDPOINT_ADDRESS", sref.getProperty(addressKey).toString());
+        return properties;
+    }
 
     public void close() {
         if (_closed) {

http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/77a17072/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 e6c2787..2d1264d 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
@@ -17,143 +17,62 @@
 
 package org.apache.aries.jax.rs.whiteboard.internal;
 
+import java.util.Collections;
+import java.util.Map;
+import java.util.Set;
+
+import javax.ws.rs.core.Application;
+
 import org.apache.cxf.Bus;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceReference;
-import org.osgi.framework.ServiceRegistration;
 import org.osgi.util.tracker.ServiceTrackerCustomizer;
 
-import javax.ws.rs.core.Application;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Hashtable;
-import java.util.Map;
-import java.util.Set;
-
 public class SingletonServiceTrackerCustomizer
-    implements ServiceTrackerCustomizer
-        <Object, SingletonServiceTrackerCustomizer.Tracked> {
+    implements ServiceTrackerCustomizer<Object, TrackedJaxRsRegistrator> {
 
     private BundleContext _bundleContext;
     private Bus _bus;
 
-    public SingletonServiceTrackerCustomizer(
-        BundleContext bundleContext, Bus bus) {
-
+    public SingletonServiceTrackerCustomizer(BundleContext bundleContext, Bus bus) {
         _bundleContext = bundleContext;
         _bus = bus;
     }
 
     @Override
-    public Tracked addingService(
+    public TrackedJaxRsRegistrator addingService(
         ServiceReference<Object> serviceReference) {
 
-        final Object service = _bundleContext.getService(
-            serviceReference);
-
-        try {
-            String[] propertyKeys = serviceReference.getPropertyKeys();
-
-            Map<String, Object> properties = new HashMap<>(
-                propertyKeys.length);
-
-            for (String propertyKey : propertyKeys) {
-                if (propertyKey.equals("osgi.jaxrs.resource.base")) {
-                    continue;
-                }
-                properties.put(
-                    propertyKey, serviceReference.getProperty(propertyKey));
+        final Object service = _bundleContext.getService(serviceReference);
+        Application application = new Application() {
+            @Override
+            public Set<Object> getSingletons() {
+                return Collections.singleton(service);
             }
-
-            properties.put(
-                "CXF_ENDPOINT_ADDRESS",
-                serviceReference.getProperty("osgi.jaxrs.resource.base").
-                    toString());
-
-            CXFJaxRsServiceRegistrator cxfJaxRsServiceRegistrator =
-                new CXFJaxRsServiceRegistrator(
-                    _bus,
-                    new Application() {
-                        @Override
-                        public Set<Object> getSingletons() {
-                            return Collections.singleton(service);
-                        }
-                    },
-                    properties);
-
-            return new Tracked(
-                cxfJaxRsServiceRegistrator, service,
-                _bundleContext.registerService(
-                    CXFJaxRsServiceRegistrator.class,
-                    cxfJaxRsServiceRegistrator, new Hashtable<>(properties)));
+        };
+        try {
+            Map<String, Object> properties = CXFJaxRsServiceRegistrator
+                .getProperties(serviceReference, "osgi.jaxrs.resource.base");
+            CXFJaxRsServiceRegistrator cxfJaxRsServiceRegistrator = 
+                new CXFJaxRsServiceRegistrator(_bus, application, properties);
+            return new TrackedJaxRsRegistrator(cxfJaxRsServiceRegistrator, _bundleContext, properties);
         }
         catch (Exception e) {
             _bundleContext.ungetService(serviceReference);
-
             throw e;
         }
     }
 
     @Override
-    public void modifiedService(
-        ServiceReference<Object> serviceReference, Tracked tracked) {
-
+    public void modifiedService(ServiceReference<Object> serviceReference, TrackedJaxRsRegistrator tracked) {
         removedService(serviceReference, tracked);
-
         addingService(serviceReference);
     }
 
     @Override
-    public void removedService(
-        ServiceReference<Object> reference, Tracked tracked) {
-
+    public void removedService(ServiceReference<Object> reference, TrackedJaxRsRegistrator tracked) {
         _bundleContext.ungetService(reference);
-
-        Object service = tracked.getService();
-
-        CXFJaxRsServiceRegistrator cxfJaxRsServiceRegistrator =
-            tracked.getCxfJaxRsServiceRegistrator();
-
-        cxfJaxRsServiceRegistrator.close();
-
-        tracked.getCxfJaxRsServiceRegistratorServiceRegistration().unregister();
-    }
-
-    public static class Tracked {
-
-        private final CXFJaxRsServiceRegistrator _cxfJaxRsServiceRegistrator;
-        private final Object _service;
-        private final ServiceRegistration<CXFJaxRsServiceRegistrator>
-            _cxfJaxRsServiceRegistratorServiceRegistration;
-
-        public Object getService() {
-            return _service;
-        }
-
-        public CXFJaxRsServiceRegistrator getCxfJaxRsServiceRegistrator() {
-            return _cxfJaxRsServiceRegistrator;
-        }
-
-        public ServiceRegistration<CXFJaxRsServiceRegistrator>
-            getCxfJaxRsServiceRegistratorServiceRegistration() {
-
-            return _cxfJaxRsServiceRegistratorServiceRegistration;
-        }
-
-        public Tracked(
-            CXFJaxRsServiceRegistrator cxfJaxRsServiceRegistrator,
-            Object service,
-            ServiceRegistration<CXFJaxRsServiceRegistrator>
-                cxfJaxRsServiceRegistratorServiceRegistration) {
-
-            _cxfJaxRsServiceRegistrator = cxfJaxRsServiceRegistrator;
-            _service = service;
-            _cxfJaxRsServiceRegistratorServiceRegistration =
-                cxfJaxRsServiceRegistratorServiceRegistration;
-        }
-
+        tracked.close();
     }
 
 }
-
-

http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/77a17072/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/TrackedJaxRsRegistrator.java
----------------------------------------------------------------------
diff --git a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/TrackedJaxRsRegistrator.java b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/TrackedJaxRsRegistrator.java
new file mode 100644
index 0000000..90ed68e
--- /dev/null
+++ b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/TrackedJaxRsRegistrator.java
@@ -0,0 +1,45 @@
+/*
+ * 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.Hashtable;
+import java.util.Map;
+
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceRegistration;
+
+public class TrackedJaxRsRegistrator {
+    private final CXFJaxRsServiceRegistrator _registrator;
+    private final ServiceRegistration<?> _sreg;
+
+    public TrackedJaxRsRegistrator(CXFJaxRsServiceRegistrator cxfJaxRsServiceRegistrator, 
+                                   BundleContext bundleContext, 
+                                   Map<String, Object> properties) {
+        _registrator = cxfJaxRsServiceRegistrator;
+        _sreg = bundleContext.
+            registerService(CXFJaxRsServiceRegistrator.class, 
+                            cxfJaxRsServiceRegistrator,
+                            new Hashtable<>(properties));
+    }
+
+
+
+    public void close() {
+        _registrator.close();
+        _sreg.unregister();
+    }
+}
\ No newline at end of file