You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/11/07 09:24:27 UTC
[sling-org-apache-sling-commons-osgi] 23/29: SLING-1431 : Utility
method to get the service ranking
This is an automated email from the ASF dual-hosted git repository.
rombert pushed a commit to annotated tag org.apache.sling.commons.osgi-2.0.6
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-commons-osgi.git
commit 01b2201aaf2cc33c7c63e8295dc4e3a706ed0fa6
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Tue Mar 9 18:00:50 2010 +0000
SLING-1431 : Utility method to get the service ranking
git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/commons/osgi@921017 13f79535-47bb-0310-9956-ffa450edef68
---
.../org/apache/sling/commons/osgi/OsgiUtil.java | 72 +++++++++++++++-------
.../apache/sling/commons/osgi/OsgiUtilTest.java | 24 ++++++--
2 files changed, 68 insertions(+), 28 deletions(-)
diff --git a/src/main/java/org/apache/sling/commons/osgi/OsgiUtil.java b/src/main/java/org/apache/sling/commons/osgi/OsgiUtil.java
index d95c82f..0216457 100644
--- a/src/main/java/org/apache/sling/commons/osgi/OsgiUtil.java
+++ b/src/main/java/org/apache/sling/commons/osgi/OsgiUtil.java
@@ -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;
+ }
+ };
}
+
}
diff --git a/src/test/java/org/apache/sling/commons/osgi/OsgiUtilTest.java b/src/test/java/org/apache/sling/commons/osgi/OsgiUtilTest.java
index d1aa341..464b3ba 100644
--- a/src/test/java/org/apache/sling/commons/osgi/OsgiUtilTest.java
+++ b/src/test/java/org/apache/sling/commons/osgi/OsgiUtilTest.java
@@ -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 TestCase {
}
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));
}
}
--
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.