incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r921017 - in /sling/trunk/bundles/commons/osgi/src: main/java/org/apache/sling/commons/osgi/OsgiUtil.java test/java/org/apache/sling/commons/osgi/OsgiUtilTest.java
Date Tue, 09 Mar 2010 18:00:50 GMT
Author: cziegeler
Date: Tue Mar  9 18:00:50 2010
New Revision: 921017

URL: http://svn.apache.org/viewvc?rev=921017&view=rev
Log:
SLING-1431 : Utility method to get the service ranking

Modified:
    sling/trunk/bundles/commons/osgi/src/main/java/org/apache/sling/commons/osgi/OsgiUtil.java
    sling/trunk/bundles/commons/osgi/src/test/java/org/apache/sling/commons/osgi/OsgiUtilTest.java

Modified: sling/trunk/bundles/commons/osgi/src/main/java/org/apache/sling/commons/osgi/OsgiUtil.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/osgi/src/main/java/org/apache/sling/commons/osgi/OsgiUtil.java?rev=921017&r1=921016&r2=921017&view=diff
==============================================================================
--- sling/trunk/bundles/commons/osgi/src/main/java/org/apache/sling/commons/osgi/OsgiUtil.java
(original)
+++ sling/trunk/bundles/commons/osgi/src/main/java/org/apache/sling/commons/osgi/OsgiUtil.java
Tue Mar  9 18:00:50 2010
@@ -292,30 +292,58 @@ public class OsgiUtil {
     }
 
     /**
-     * Return the service ranking
-     * @param props A property map
-     * @return The service ranking.
+     * Create a comparable object out of the service properties. With the result
+     * it is possible to compare service properties based on the service ranking
+     * of a service. Therefore this object acts like {@link ServiceReference#compareTo(Object)}.
+     * @param props The service properties.
+     * @return A comparable for the ranking of the service
      * @since 2.0.6
      */
-    public static int getServiceRanking(final Map<String, Object> props) {
-        int ranking = 0;
-        if ( props != null && props.get(Constants.SERVICE_RANKING) instanceof Integer)
{
-            ranking = (Integer)props.get(Constants.SERVICE_RANKING);
-        }
-        return ranking;
-    }
+    public static Comparable<Object> getComparableForServiceRanking(final Map<String,
Object> props) {
 
-    /**
-     * Return the service ranking
-     * @param ref The service reference.
-     * @return The service ranking.
-     * @since 2.0.6
-     */
-    public static int getServiceRanking(final ServiceReference ref) {
-        int ranking = 0;
-        if ( ref.getProperty(Constants.SERVICE_RANKING) instanceof Integer) {
-            ranking = (Integer)ref.getProperty(Constants.SERVICE_RANKING);
-        }
-        return ranking;
+        return new Comparable<Object>() {
+
+            @SuppressWarnings("unchecked")
+            public int compareTo(Object reference) {
+                final Long otherId;
+                Object otherRankObj;
+                if ( reference instanceof ServiceReference ) {
+                    final ServiceReference other = (ServiceReference) reference;
+                    otherId = (Long) other.getProperty(Constants.SERVICE_ID);
+                    otherRankObj = other.getProperty(Constants.SERVICE_RANKING);
+                } else {
+                    final Map<String, Object> otherProps = (Map<String, Object>)reference;
+                    otherId = (Long) otherProps.get(Constants.SERVICE_ID);
+                    otherRankObj = otherProps.get(Constants.SERVICE_RANKING);
+                }
+                final Long id = (Long) props.get(Constants.SERVICE_ID);
+                if (id.equals(otherId)) {
+                    return 0; // same service
+                }
+
+                Object rankObj = props.get(Constants.SERVICE_RANKING);
+
+                // If no rank, then spec says it defaults to zero.
+                rankObj = (rankObj == null) ? new Integer(0) : rankObj;
+                otherRankObj = (otherRankObj == null) ? new Integer(0) : otherRankObj;
+
+                // If rank is not Integer, then spec says it defaults to zero.
+                Integer rank = (rankObj instanceof Integer)
+                    ? (Integer) rankObj : new Integer(0);
+                Integer otherRank = (otherRankObj instanceof Integer)
+                    ? (Integer) otherRankObj : new Integer(0);
+
+                // Sort by rank in ascending order.
+                if (rank.compareTo(otherRank) < 0) {
+                    return -1; // lower rank
+                } else if (rank.compareTo(otherRank) > 0) {
+                    return 1; // higher rank
+                }
+
+                // If ranks are equal, then sort by service id in descending order.
+                return (id.compareTo(otherId) < 0) ? 1 : -1;
+            }
+        };
     }
+
 }

Modified: sling/trunk/bundles/commons/osgi/src/test/java/org/apache/sling/commons/osgi/OsgiUtilTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/osgi/src/test/java/org/apache/sling/commons/osgi/OsgiUtilTest.java?rev=921017&r1=921016&r2=921017&view=diff
==============================================================================
--- sling/trunk/bundles/commons/osgi/src/test/java/org/apache/sling/commons/osgi/OsgiUtilTest.java
(original)
+++ sling/trunk/bundles/commons/osgi/src/test/java/org/apache/sling/commons/osgi/OsgiUtilTest.java
Tue Mar  9 18:00:50 2010
@@ -19,7 +19,6 @@
 package org.apache.sling.commons.osgi;
 
 import java.util.ArrayList;
-import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -134,10 +133,23 @@ public class OsgiUtilTest extends TestCa
     }
 
     public void testRanking() {
-        assertEquals(0, OsgiUtil.getServiceRanking((Map<String, Object>)null));
-        final Map<String, Object> stringMap = Collections.singletonMap(Constants.SERVICE_RANKING,
(Object)"1");
-        assertEquals(0, OsgiUtil.getServiceRanking(stringMap));
-        final Map<String, Object> intMap = Collections.singletonMap(Constants.SERVICE_RANKING,
(Object)1);
-        assertEquals(1, OsgiUtil.getServiceRanking(intMap));
+        final Map<String, Object> map1 = new HashMap<String, Object>();
+        map1.put(Constants.SERVICE_ID, 1L);
+        map1.put(Constants.SERVICE_RANKING, 7);
+
+        final Map<String, Object> map2 = new HashMap<String, Object>();
+        map2.put(Constants.SERVICE_ID, 1L);
+        map2.put(Constants.SERVICE_RANKING, 5);
+
+        assertEquals(0, OsgiUtil.getComparableForServiceRanking(map1).compareTo(map2));
+        assertEquals(0, OsgiUtil.getComparableForServiceRanking(map1).compareTo(map1));
+        assertEquals(0, OsgiUtil.getComparableForServiceRanking(map2).compareTo(map2));
+        map2.put(Constants.SERVICE_ID, 2L);
+        assertEquals(1, OsgiUtil.getComparableForServiceRanking(map1).compareTo(map2));
+        assertEquals(-1, OsgiUtil.getComparableForServiceRanking(map2).compareTo(map1));
+
+        map1.put(Constants.SERVICE_RANKING, "hello");
+        assertEquals(-1, OsgiUtil.getComparableForServiceRanking(map1).compareTo(map2));
+        assertEquals(1, OsgiUtil.getComparableForServiceRanking(map2).compareTo(map1));
     }
 }



Mime
View raw message