Return-Path: Delivered-To: apmail-geronimo-scm-archive@www.apache.org Received: (qmail 41233 invoked from network); 29 Apr 2009 11:58:05 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 29 Apr 2009 11:58:05 -0000 Received: (qmail 14143 invoked by uid 500); 29 Apr 2009 11:58:05 -0000 Delivered-To: apmail-geronimo-scm-archive@geronimo.apache.org Received: (qmail 14082 invoked by uid 500); 29 Apr 2009 11:58:05 -0000 Mailing-List: contact scm-help@geronimo.apache.org; run by ezmlm Precedence: bulk list-help: list-unsubscribe: List-Post: Reply-To: dev@geronimo.apache.org List-Id: Delivered-To: mailing list scm@geronimo.apache.org Received: (qmail 14073 invoked by uid 99); 29 Apr 2009 11:58:05 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 29 Apr 2009 11:58:05 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 29 Apr 2009 11:58:04 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 04B4B238896D; Wed, 29 Apr 2009 11:57:43 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit 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 -0000 To: scm@geronimo.apache.org From: gnodet@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20090429115744.04B4B238896D@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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) {