geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gno...@apache.org
Subject svn commit: r769763 - /geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/UnaryServiceReferenceRecipe.java
Date Wed, 29 Apr 2009 11:57:43 GMT
Author: gnodet
Date: Wed Apr 29 11:57:42 2009
New Revision: 769763

URL: http://svn.apache.org/viewvc?rev=769763&view=rev
Log:
Fix reference tracking to be able to run correctly on equinox

Modified:
    geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/UnaryServiceReferenceRecipe.java

Modified: geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/UnaryServiceReferenceRecipe.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/UnaryServiceReferenceRecipe.java?rev=769763&r1=769762&r2=769763&view=diff
==============================================================================
--- geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/UnaryServiceReferenceRecipe.java
(original)
+++ geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/UnaryServiceReferenceRecipe.java
Wed Apr 29 11:57:42 2009
@@ -31,6 +31,7 @@
 import org.apache.xbean.recipe.Recipe;
 import org.osgi.framework.InvalidSyntaxException;
 import org.osgi.framework.ServiceReference;
+import org.osgi.framework.Constants;
 import org.osgi.service.blueprint.context.BlueprintContext;
 import org.osgi.service.blueprint.context.ServiceUnavailableException;
 import org.osgi.service.blueprint.reflect.ReferenceMetadata;
@@ -102,35 +103,63 @@
     }
 
     private ServiceReference getBestServiceReference(ServiceReference[] references) {
-        if (references == null || references.length == 0) {
+        int length = (references == null) ? 0 : references.length;
+        if (length == 0) { /* if no service is being tracked */
             return null;
         }
-        return Collections.max(Arrays.asList(references));
+        int index = 0;
+        if (length > 1) { /* if more than one service, select highest ranking */
+            int rankings[] = new int[length];
+            int count = 0;
+            int maxRanking = Integer.MIN_VALUE;
+            for (int i = 0; i < length; i++) {
+                Object property = references[i].getProperty(Constants.SERVICE_RANKING);
+                int ranking = (property instanceof Integer) ? ((Integer) property).intValue()
: 0;
+                rankings[i] = ranking;
+                if (ranking > maxRanking) {
+                    index = i;
+                    maxRanking = ranking;
+                    count = 1;
+                } else {
+                    if (ranking == maxRanking) {
+                        count++;
+                    }
+                }
+            }
+            if (count > 1) { /* if still more than one service, select lowest id */
+                long minId = Long.MAX_VALUE;
+                for (int i = 0; i < length; i++) {
+                    if (rankings[i] == maxRanking) {
+                        long id = ((Long) (references[i].getProperty(Constants.SERVICE_ID))).longValue();
+                        if (id < minId) {
+                            index = i;
+                            minId = id;
+                        }
+                    }
+                }
+            }
+        }
+        return references[index];
     }
 
     private void retrack() {
-        try {
-            ServiceReference[] refs = blueprintContext.getBundleContext().getServiceReferences(null,
getOsgiFilter());
-            ServiceReference ref = getBestServiceReference(refs);
-            if (ref != null) {
-                bind(ref);
-            } else {
-                unbind();
+        synchronized (monitor) {
+            try {
+                ServiceReference[] refs = blueprintContext.getBundleContext().getServiceReferences(null,
getOsgiFilter());
+                ServiceReference ref = getBestServiceReference(refs);
+                if (ref != null) {
+                    bind(ref);
+                } else {
+                    unbind();
+                }
+            } catch (InvalidSyntaxException e) {
+                // Ignore, should never happen
             }
-        } catch (InvalidSyntaxException e) {
-            // Ignore, should never happen
         }
     }
 
     protected void track(ServiceReference ref) {
-        if (trackedServiceReference == null) {
-            bind(ref);
-        } else {
-            if (trackedServiceReference.compareTo(ref) > 0) {
-                return;
-            }
-            bind(ref);
-        }
+        retrack();
     }
 
     protected void untrack(ServiceReference ref) {



Mime
View raw message