aries-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From n..@apache.org
Subject svn commit: r1151229 - /aries/trunk/util/src/main/java/org/apache/aries/util/tracker/SingleServiceTracker.java
Date Tue, 26 Jul 2011 20:35:43 GMT
Author: not
Date: Tue Jul 26 20:35:42 2011
New Revision: 1151229

URL: http://svn.apache.org/viewvc?rev=1151229&view=rev
Log:
ARIES-713 Add the ability to set a filter to a single service tracker.


Modified:
    aries/trunk/util/src/main/java/org/apache/aries/util/tracker/SingleServiceTracker.java

Modified: aries/trunk/util/src/main/java/org/apache/aries/util/tracker/SingleServiceTracker.java
URL: http://svn.apache.org/viewvc/aries/trunk/util/src/main/java/org/apache/aries/util/tracker/SingleServiceTracker.java?rev=1151229&r1=1151228&r2=1151229&view=diff
==============================================================================
--- aries/trunk/util/src/main/java/org/apache/aries/util/tracker/SingleServiceTracker.java
(original)
+++ aries/trunk/util/src/main/java/org/apache/aries/util/tracker/SingleServiceTracker.java
Tue Jul 26 20:35:42 2011
@@ -23,6 +23,7 @@ import java.util.concurrent.atomic.Atomi
 
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.Constants;
+import org.osgi.framework.Filter;
 import org.osgi.framework.InvalidSyntaxException;
 import org.osgi.framework.ServiceEvent;
 import org.osgi.framework.ServiceListener;
@@ -43,7 +44,9 @@ public final class SingleServiceTracker<
   private final AtomicReference<ServiceReference> ref = new AtomicReference<ServiceReference>();
   private final AtomicBoolean open = new AtomicBoolean(false);
   private final SingleServiceListener serviceListener;
-  
+  private String filterString;
+  private Filter filter;
+
   private final ServiceListener listener = new ServiceListener()
   {
     public void serviceChanged(ServiceEvent event) 
@@ -68,6 +71,13 @@ public final class SingleServiceTracker<
     serviceListener = sl;
   }
   
+  public SingleServiceTracker(BundleContext context, Class<T> clazz, String filterString,
SingleServiceListener sl) throws InvalidSyntaxException
+  {
+    this(context, clazz, sl);
+    this.filterString = filterString;
+    if (filterString != null) filter = context.createFilter(filterString);
+  }
+  
   public T getService()
   {
     return service.get();
@@ -82,7 +92,9 @@ public final class SingleServiceTracker<
   {
     if (open.compareAndSet(false, true)) {
       try {
-        ctx.addServiceListener(listener, '(' + Constants.OBJECTCLASS + '=' + className +
')');
+        String filterString = '(' + Constants.OBJECTCLASS + '=' + className + ')';
+        if (filter != null) filterString = "(&" + filterString + filter + ')';
+        ctx.addServiceListener(listener, filterString);
         findMatchingReference(null);
       } catch (InvalidSyntaxException e) {
         // this can never happen. (famous last words :)
@@ -93,7 +105,7 @@ public final class SingleServiceTracker<
   private void findMatchingReference(ServiceReference original) {
     boolean clear = true;
     ServiceReference ref = ctx.getServiceReference(className);
-    if (ref != null) {
+    if (ref != null && (filter == null || filter.match(ref))) {
       @SuppressWarnings("unchecked")
       T service = (T) ctx.getService(ref);
       if (service != null) {



Mime
View raw message