cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From amic...@apache.org
Subject [1/2] git commit: Enhanced SimpleServiceTracker with modified handler and access to service references
Date Tue, 18 Mar 2014 14:05:06 GMT
Repository: cxf-dosgi
Updated Branches:
  refs/heads/master 7b8225d57 -> 4c86a73f5


Enhanced SimpleServiceTracker with modified handler and access to service references


Project: http://git-wip-us.apache.org/repos/asf/cxf-dosgi/repo
Commit: http://git-wip-us.apache.org/repos/asf/cxf-dosgi/commit/231f2eea
Tree: http://git-wip-us.apache.org/repos/asf/cxf-dosgi/tree/231f2eea
Diff: http://git-wip-us.apache.org/repos/asf/cxf-dosgi/diff/231f2eea

Branch: refs/heads/master
Commit: 231f2eeaf477eb8eca861532420d6113c069ad3a
Parents: 7b8225d
Author: Amichai Rothman <amichai@apache.org>
Authored: Tue Mar 18 14:58:38 2014 +0200
Committer: Amichai Rothman <amichai@apache.org>
Committed: Tue Mar 18 15:01:55 2014 +0200

----------------------------------------------------------------------
 .../exporter/TopologyManagerExport.java         |  7 ++-
 .../importer/TopologyManagerImport.java         |  8 ++-
 .../util/SimpleServiceTracker.java              | 52 ++++++++++++++++----
 .../util/SimpleServiceTrackerListener.java      | 16 +++++-
 .../util/SimpleServiceTrackerTest.java          | 12 +++--
 5 files changed, 75 insertions(+), 20 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf-dosgi/blob/231f2eea/dsw/cxf-topology-manager/src/main/java/org/apache/cxf/dosgi/topologymanager/exporter/TopologyManagerExport.java
----------------------------------------------------------------------
diff --git a/dsw/cxf-topology-manager/src/main/java/org/apache/cxf/dosgi/topologymanager/exporter/TopologyManagerExport.java
b/dsw/cxf-topology-manager/src/main/java/org/apache/cxf/dosgi/topologymanager/exporter/TopologyManagerExport.java
index 767376e..4199d51 100644
--- a/dsw/cxf-topology-manager/src/main/java/org/apache/cxf/dosgi/topologymanager/exporter/TopologyManagerExport.java
+++ b/dsw/cxf-topology-manager/src/main/java/org/apache/cxf/dosgi/topologymanager/exporter/TopologyManagerExport.java
@@ -81,7 +81,7 @@ public class TopologyManagerExport {
         // track RemoteServiceAdmins through which we can export services
         remoteServiceAdminTracker.addListener(new SimpleServiceTrackerListener<RemoteServiceAdmin>()
{
 
-            public void added(RemoteServiceAdmin rsa) {
+            public void added(ServiceReference<RemoteServiceAdmin> reference, RemoteServiceAdmin
rsa) {
                 LOG.debug("RemoteServiceAdmin added: {}, total {}",
                         rsa, remoteServiceAdminTracker.getAllServices().size());
                 for (ServiceReference serviceRef : endpointRepo.getServicesToBeExportedFor(rsa))
{
@@ -89,7 +89,10 @@ public class TopologyManagerExport {
                 }
             }
 
-            public void removed(RemoteServiceAdmin rsa) {
+            public void modified(ServiceReference<RemoteServiceAdmin> reference, RemoteServiceAdmin
rsa) {
+            }
+
+            public void removed(ServiceReference<RemoteServiceAdmin> reference, RemoteServiceAdmin
rsa) {
                 LOG.debug("RemoteServiceAdmin removed: {}, total {}", rsa,
                         remoteServiceAdminTracker.getAllServices().size());
                 List<EndpointDescription> endpoints = endpointRepo.removeRemoteServiceAdmin(rsa);

http://git-wip-us.apache.org/repos/asf/cxf-dosgi/blob/231f2eea/dsw/cxf-topology-manager/src/main/java/org/apache/cxf/dosgi/topologymanager/importer/TopologyManagerImport.java
----------------------------------------------------------------------
diff --git a/dsw/cxf-topology-manager/src/main/java/org/apache/cxf/dosgi/topologymanager/importer/TopologyManagerImport.java
b/dsw/cxf-topology-manager/src/main/java/org/apache/cxf/dosgi/topologymanager/importer/TopologyManagerImport.java
index 47f7840..0ab7ab6 100644
--- a/dsw/cxf-topology-manager/src/main/java/org/apache/cxf/dosgi/topologymanager/importer/TopologyManagerImport.java
+++ b/dsw/cxf-topology-manager/src/main/java/org/apache/cxf/dosgi/topologymanager/importer/TopologyManagerImport.java
@@ -34,6 +34,7 @@ import org.apache.cxf.dosgi.topologymanager.util.ReferenceCounter;
 import org.apache.cxf.dosgi.topologymanager.util.SimpleServiceTracker;
 import org.apache.cxf.dosgi.topologymanager.util.SimpleServiceTrackerListener;
 import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
 import org.osgi.framework.hooks.service.ListenerHook;
 import org.osgi.service.remoteserviceadmin.EndpointDescription;
 import org.osgi.service.remoteserviceadmin.EndpointListener;
@@ -94,11 +95,14 @@ public class TopologyManagerImport implements EndpointListener, RemoteServiceAdm
         remoteServiceAdminTracker = rsaTracker;
         remoteServiceAdminTracker.addListener(new SimpleServiceTrackerListener<RemoteServiceAdmin>()
{
 
-            public void added(RemoteServiceAdmin rsa) {
+            public void added(ServiceReference<RemoteServiceAdmin> reference, RemoteServiceAdmin
rsa) {
                 triggerImportsForRemoteServiceAdmin(rsa);
             }
 
-            public void removed(RemoteServiceAdmin rsa) {
+            public void modified(ServiceReference<RemoteServiceAdmin> reference, RemoteServiceAdmin
rsa) {
+            }
+
+            public void removed(ServiceReference<RemoteServiceAdmin> reference, RemoteServiceAdmin
rsa) {
                 // the RSA's imports will be closed by its shutdown, so nothing to do here
             }
         });

http://git-wip-us.apache.org/repos/asf/cxf-dosgi/blob/231f2eea/dsw/cxf-topology-manager/src/main/java/org/apache/cxf/dosgi/topologymanager/util/SimpleServiceTracker.java
----------------------------------------------------------------------
diff --git a/dsw/cxf-topology-manager/src/main/java/org/apache/cxf/dosgi/topologymanager/util/SimpleServiceTracker.java
b/dsw/cxf-topology-manager/src/main/java/org/apache/cxf/dosgi/topologymanager/util/SimpleServiceTracker.java
index a5339f8..29238aa 100644
--- a/dsw/cxf-topology-manager/src/main/java/org/apache/cxf/dosgi/topologymanager/util/SimpleServiceTracker.java
+++ b/dsw/cxf-topology-manager/src/main/java/org/apache/cxf/dosgi/topologymanager/util/SimpleServiceTracker.java
@@ -25,6 +25,7 @@ import java.util.concurrent.ConcurrentMap;
 import java.util.concurrent.CopyOnWriteArrayList;
 
 import org.osgi.framework.BundleContext;
+import org.osgi.framework.Filter;
 import org.osgi.framework.ServiceReference;
 import org.osgi.util.tracker.ServiceTracker;
 
@@ -50,22 +51,34 @@ import org.osgi.util.tracker.ServiceTracker;
 public class SimpleServiceTracker<T> extends ServiceTracker<T, T> {
 
     // we must use a map with references as keys, so as not to invoke equals remotely on
service objects
-    private ConcurrentMap<ServiceReference<T>, T> services;
-    private List<SimpleServiceTrackerListener<T>> listeners;
+    private final ConcurrentMap<ServiceReference<T>, T> services = new ConcurrentHashMap<ServiceReference<T>,
T>();
+    private final List<SimpleServiceTrackerListener<T>> listeners =
+        new CopyOnWriteArrayList<SimpleServiceTrackerListener<T>>();
 
     /**
-     * Create a <code>SimpleServiceTracker</code> on the specified class' name.
+     * Create a {@code SimpleServiceTracker} on the specified class name.
      * <p>
-     * Services registered under the specified class' name will be tracked by
-     * this <code>SimpleServiceTracker</code>.
+     * Services registered under the specified class name will be tracked by
+     * this {@code SimpleServiceTracker}.
      *
      * @param context the {@code BundleContext} against which the tracking is done
      * @param clazz the class of the services to be tracked
      */
     public SimpleServiceTracker(BundleContext context, Class<T> clazz) {
         super(context, clazz.getName(), null);
-        this.listeners = new CopyOnWriteArrayList<SimpleServiceTrackerListener<T>>();
-        this.services = new ConcurrentHashMap<ServiceReference<T>, T>();
+    }
+
+    /**
+     * Create a {@code SimpleServiceTracker} on the specified {@code Filter} object.
+     * <p>
+     * Services which match the specified {@code Filter} object will be tracked by
+     * this {@code SimpleServiceTracker}.
+     *
+     * @param context the {@code BundleContext} against which the tracking is done
+     * @param filter The {@code Filter} to select the services to be tracked
+     */
+    public SimpleServiceTracker(BundleContext context, Filter filter) {
+        super(context, filter, null);
     }
 
     /**
@@ -82,16 +95,24 @@ public class SimpleServiceTracker<T> extends ServiceTracker<T,
T> {
         T service = (T) super.addingService(reference);
         services.put(reference, service);
         for (SimpleServiceTrackerListener<T> listener : listeners) {
-            listener.added(service);
+            listener.added(reference, service);
         }
         return service;
     }
 
     @Override
+    public void modifiedService(ServiceReference<T> reference, T service) {
+        for (SimpleServiceTrackerListener<T> listener : listeners) {
+            listener.modified(reference, service);
+        }
+        super.modifiedService(reference, service);
+    }
+
+    @Override
     public void removedService(ServiceReference<T> reference, T service) {
         services.remove(reference, service);
         for (SimpleServiceTrackerListener<T> listener : listeners) {
-            listener.removed(service);
+            listener.removed(reference, service);
         }
         super.removedService(reference, service);
     }
@@ -114,4 +135,17 @@ public class SimpleServiceTracker<T> extends ServiceTracker<T,
T> {
     public List<T> getAllServices() {
         return new ArrayList<T>(services.values());
     }
+
+    /**
+     * Returns all currently tracked service references.
+     * <p>
+     * Unlike {@link ServiceTracker#getServiceReferences()}, if it is called from within
a service
+     * {@link SimpleServiceTrackerListener#added added} event handler, the returned list
+     * will include the newly added service reference.
+     *
+     * @return all currently tracked service references
+     */
+    public List<ServiceReference<T>> getAllServiceReferences() {
+        return new ArrayList<ServiceReference<T>>(services.keySet());
+    }
 }

http://git-wip-us.apache.org/repos/asf/cxf-dosgi/blob/231f2eea/dsw/cxf-topology-manager/src/main/java/org/apache/cxf/dosgi/topologymanager/util/SimpleServiceTrackerListener.java
----------------------------------------------------------------------
diff --git a/dsw/cxf-topology-manager/src/main/java/org/apache/cxf/dosgi/topologymanager/util/SimpleServiceTrackerListener.java
b/dsw/cxf-topology-manager/src/main/java/org/apache/cxf/dosgi/topologymanager/util/SimpleServiceTrackerListener.java
index 22375c4..ef67b61 100644
--- a/dsw/cxf-topology-manager/src/main/java/org/apache/cxf/dosgi/topologymanager/util/SimpleServiceTrackerListener.java
+++ b/dsw/cxf-topology-manager/src/main/java/org/apache/cxf/dosgi/topologymanager/util/SimpleServiceTrackerListener.java
@@ -18,6 +18,8 @@
  */
 package org.apache.cxf.dosgi.topologymanager.util;
 
+import org.osgi.framework.ServiceReference;
+
 /**
  * Callback interface for notifications of services that are
  * added to or removed from tracking by a {@link SimpleServiceTracker}.
@@ -29,14 +31,24 @@ public interface SimpleServiceTrackerListener<T> {
     /**
      * Called when a new service is added to the tracked services.
      *
+     * @param reference the newly added service reference
      * @param service the newly added service
      */
-    void added(T service);
+    void added(ServiceReference<T> reference, T service);
+
+    /**
+     * Called when a tracked service is modified.
+     *
+     * @param reference the modified service reference
+     * @param service the modified service
+     */
+    void modified(ServiceReference<T> reference, T service);
 
     /**
      * Called when a service is removed from the tracked services.
      *
+     * @param reference the removed service reference
      * @param service the removed service
      */
-    void removed(T service);
+    void removed(ServiceReference<T> reference, T service);
 }

http://git-wip-us.apache.org/repos/asf/cxf-dosgi/blob/231f2eea/dsw/cxf-topology-manager/src/test/java/org/apache/cxf/dosgi/topologymanager/util/SimpleServiceTrackerTest.java
----------------------------------------------------------------------
diff --git a/dsw/cxf-topology-manager/src/test/java/org/apache/cxf/dosgi/topologymanager/util/SimpleServiceTrackerTest.java
b/dsw/cxf-topology-manager/src/test/java/org/apache/cxf/dosgi/topologymanager/util/SimpleServiceTrackerTest.java
index f73cdf0..0785e28 100644
--- a/dsw/cxf-topology-manager/src/test/java/org/apache/cxf/dosgi/topologymanager/util/SimpleServiceTrackerTest.java
+++ b/dsw/cxf-topology-manager/src/test/java/org/apache/cxf/dosgi/topologymanager/util/SimpleServiceTrackerTest.java
@@ -108,11 +108,9 @@ public class SimpleServiceTrackerTest {
         // add our listener
         SimpleServiceTrackerListener<RemoteServiceAdmin> listener =
                 new SimpleServiceTrackerListener<RemoteServiceAdmin>() {
-                @SuppressWarnings({
-                    "unchecked", "rawtypes"
-                })
+                @SuppressWarnings({ "unchecked", "rawtypes" })
                 @Override
-                public void added(RemoteServiceAdmin service) {
+                public void added(ServiceReference<RemoteServiceAdmin> reference, RemoteServiceAdmin
service) {
                     // prove that original ServiceTracker fails here
                     Object[] trackerServices = (Object[])
                         (tracker.getServices() != null ? tracker.getServices() : new Object[0]);
@@ -123,7 +121,11 @@ public class SimpleServiceTrackerTest {
                 }
 
                 @Override
-                public void removed(RemoteServiceAdmin service) {
+                public void modified(ServiceReference<RemoteServiceAdmin> reference,
RemoteServiceAdmin service) {
+                }
+
+                @Override
+                public void removed(ServiceReference<RemoteServiceAdmin> reference,
RemoteServiceAdmin service) {
                     assertEqualsUnordered(services, tracker.getAllServices());
                 }
             };


Mime
View raw message