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:41 UTC

[sling-org-apache-sling-commons-osgi] 07/14: SLING-2008 : Move properties support to own class

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.1.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-commons-osgi.git

commit a4d182b2eec61c5c61aa397a668620d35f5e9f33
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Thu Mar 3 10:03:56 2011 +0000

    SLING-2008 : Move properties support to own class
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/commons/osgi@1076573 13f79535-47bb-0310-9956-ffa450edef68
---
 pom.xml                                            |   2 +-
 .../org/apache/sling/commons/osgi/OsgiUtil.java    | 203 +++------------------
 .../osgi/{OsgiUtil.java => PropertiesUtil.java}    | 163 +----------------
 .../org/apache/sling/commons/osgi/ServiceUtil.java | 110 +++++++++++
 4 files changed, 141 insertions(+), 337 deletions(-)

diff --git a/pom.xml b/pom.xml
index 061ce41..46d95e6 100644
--- a/pom.xml
+++ b/pom.xml
@@ -53,7 +53,7 @@
                 <configuration>
                     <instructions>
                         <Export-Package>
-                            org.apache.sling.commons.osgi;version=${pom.version}
+                            org.apache.sling.commons.osgi;version=2.1.0
                         </Export-Package>
                     </instructions>
                 </configuration>
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 d571529..7b5d6af 100644
--- a/src/main/java/org/apache/sling/commons/osgi/OsgiUtil.java
+++ b/src/main/java/org/apache/sling/commons/osgi/OsgiUtil.java
@@ -18,15 +18,11 @@
  */
 package org.apache.sling.commons.osgi;
 
-import java.util.ArrayList;
-import java.util.Collection;
 import java.util.Dictionary;
 import java.util.Hashtable;
-import java.util.List;
 import java.util.Map;
 
 import org.osgi.framework.Bundle;
-import org.osgi.framework.Constants;
 import org.osgi.framework.ServiceReference;
 import org.osgi.service.event.Event;
 import org.osgi.service.event.EventConstants;
@@ -34,7 +30,9 @@ import org.osgi.service.event.EventConstants;
 /**
  * The <code>OsgiUtil</code> is a utility class providing some usefull utility
  * methods.
+ * @deprecated
  */
+@Deprecated
 public class OsgiUtil {
 
     /**
@@ -46,15 +44,8 @@ public class OsgiUtil {
      * @param propValue the property value or <code>null</code>
      * @param defaultValue the default boolean value
      */
-    public static boolean toBoolean(Object propValue, boolean defaultValue) {
-        propValue = toObject(propValue);
-        if (propValue instanceof Boolean) {
-            return (Boolean) propValue;
-        } else if (propValue != null) {
-            return Boolean.valueOf(String.valueOf(propValue));
-        }
-
-        return defaultValue;
+    public static boolean toBoolean(final Object propValue, final boolean defaultValue) {
+        return PropertiesUtil.toBoolean(propValue, defaultValue);
     }
 
     /**
@@ -63,9 +54,8 @@ public class OsgiUtil {
      * @param propValue the property value or <code>null</code>
      * @param defaultValue the default string value
      */
-    public static String toString(Object propValue, String defaultValue) {
-        propValue = toObject(propValue);
-        return (propValue != null) ? propValue.toString() : defaultValue;
+    public static String toString(final Object propValue, final String defaultValue) {
+        return PropertiesUtil.toString(propValue, defaultValue);
     }
 
     /**
@@ -76,19 +66,8 @@ public class OsgiUtil {
      * @param propValue the property value or <code>null</code>
      * @param defaultValue the default long value
      */
-    public static long toLong(Object propValue, long defaultValue) {
-        propValue = toObject(propValue);
-        if (propValue instanceof Long) {
-            return (Long) propValue;
-        } else if (propValue != null) {
-            try {
-                return Long.valueOf(String.valueOf(propValue));
-            } catch (NumberFormatException nfe) {
-                // don't care, fall through to default value
-            }
-        }
-
-        return defaultValue;
+    public static long toLong(final Object propValue, final long defaultValue) {
+        return PropertiesUtil.toLong(propValue, defaultValue);
     }
 
     /**
@@ -99,19 +78,8 @@ public class OsgiUtil {
      * @param propValue the property value or <code>null</code>
      * @param defaultValue the default integer value
      */
-    public static int toInteger(Object propValue, int defaultValue) {
-        propValue = toObject(propValue);
-        if (propValue instanceof Integer) {
-            return (Integer) propValue;
-        } else if (propValue != null) {
-            try {
-                return Integer.valueOf(String.valueOf(propValue));
-            } catch (NumberFormatException nfe) {
-                // don't care, fall through to default value
-            }
-        }
-
-        return defaultValue;
+    public static int toInteger(final Object propValue, final int defaultValue) {
+        return PropertiesUtil.toInteger(propValue, defaultValue);
     }
 
     /**
@@ -125,8 +93,8 @@ public class OsgiUtil {
      * @deprecated since 2.0.4, use {@link #toDouble(Object, double)} instead
      */
     @Deprecated
-    public static double getProperty(Object propValue, double defaultValue) {
-        return toDouble(propValue, defaultValue);
+    public static double getProperty(final Object propValue, final double defaultValue) {
+        return PropertiesUtil.toDouble(propValue, defaultValue);
     }
 
     /**
@@ -139,19 +107,8 @@ public class OsgiUtil {
      *
      * @since 2.0.4
      */
-    public static double toDouble(Object propValue, double defaultValue) {
-        propValue = toObject(propValue);
-        if (propValue instanceof Double) {
-            return (Double) propValue;
-        } else if (propValue != null) {
-            try {
-                return Double.valueOf(String.valueOf(propValue));
-            } catch (NumberFormatException nfe) {
-                // don't care, fall through to default value
-            }
-        }
-
-        return defaultValue;
+    public static double toDouble(final Object propValue, final double defaultValue) {
+        return PropertiesUtil.toDouble(propValue, defaultValue);
     }
 
     /**
@@ -163,18 +120,8 @@ public class OsgiUtil {
      * Otherwise <code>null</code> is returned.
      * @param propValue the parameter to convert.
      */
-    public static Object toObject(Object propValue) {
-        if (propValue == null) {
-            return null;
-        } else if (propValue.getClass().isArray()) {
-            Object[] prop = (Object[]) propValue;
-            return prop.length > 0 ? prop[0] : null;
-        } else if (propValue instanceof Collection<?>) {
-            Collection<?> prop = (Collection<?>) propValue;
-            return prop.isEmpty() ? null : prop.iterator().next();
-        } else {
-            return propValue;
-        }
+    public static Object toObject(final Object propValue) {
+        return PropertiesUtil.toObject(propValue);
     }
 
     /**
@@ -187,8 +134,8 @@ public class OsgiUtil {
      * returned.
      * @param propValue The object to convert.
      */
-    public static String[] toStringArray(Object propValue) {
-        return toStringArray(propValue, null);
+    public static String[] toStringArray(final Object propValue) {
+        return PropertiesUtil.toStringArray(propValue);
     }
 
     /**
@@ -203,43 +150,9 @@ public class OsgiUtil {
      * @param propValue The object to convert.
      * @param defaultArray The default array to return.
      */
-    public static String[] toStringArray(Object propValue, String[] defaultArray) {
-        if (propValue == null) {
-            // no value at all
-            return defaultArray;
-
-        } else if (propValue instanceof String) {
-            // single string
-            return new String[] { (String) propValue };
-
-        } else if (propValue instanceof String[]) {
-            // String[]
-            return (String[]) propValue;
-
-        } else if (propValue.getClass().isArray()) {
-            // other array
-            Object[] valueArray = (Object[]) propValue;
-            List<String> values = new ArrayList<String>(valueArray.length);
-            for (Object value : valueArray) {
-                if (value != null) {
-                    values.add(value.toString());
-                }
-            }
-            return values.toArray(new String[values.size()]);
+    public static String[] toStringArray(final Object propValue, final String[] defaultArray) {
+        return PropertiesUtil.toStringArray(propValue, defaultArray);
 
-        } else if (propValue instanceof Collection<?>) {
-            // collection
-            Collection<?> valueCollection = (Collection<?>) propValue;
-            List<String> valueList = new ArrayList<String>(valueCollection.size());
-            for (Object value : valueCollection) {
-                if (value != null) {
-                    valueList.add(value.toString());
-                }
-            }
-            return valueList.toArray(new String[valueList.size()]);
-        }
-
-        return defaultArray;
     }
 
     /**
@@ -254,9 +167,10 @@ public class OsgiUtil {
      * @param props A non-null map of properties for the event.
      * @return The OSGi event.
      */
-    public static Event createEvent(Bundle sourceBundle,
-            ServiceReference sourceService, String topic,
-            Map<String, Object> props) {
+    public static Event createEvent(final Bundle sourceBundle,
+            final ServiceReference sourceService,
+            final String topic,
+            final Map<String, Object> props) {
 
         // get a private copy of the properties
         Dictionary<String, Object> table = new Hashtable<String, Object>(props);
@@ -300,73 +214,6 @@ public class OsgiUtil {
      * @since 2.0.6
      */
     public static Comparable<Object> getComparableForServiceRanking(final Map<String, Object> props) {
-        return new ComparableImplementation(props);
-    }
-
-    private static final class ComparableImplementation implements Comparable<Object> {
-
-        private final Map<String, Object> props;
-
-        private ComparableImplementation(Map<String, Object> props) {
-            this.props = props;
-        }
-
-        @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 if (reference instanceof Map){
-                final Map<String, Object> otherProps = (Map<String, Object>)reference;
-                otherId = (Long) otherProps.get(Constants.SERVICE_ID);
-                otherRankObj = otherProps.get(Constants.SERVICE_RANKING);
-            } else {
-                final ComparableImplementation other = (ComparableImplementation)reference;
-                otherId = (Long) other.props.get(Constants.SERVICE_ID);
-                otherRankObj = other.props.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;
-        }
-
-        @Override
-        public boolean equals(Object obj) {
-            if ( obj instanceof ComparableImplementation ) {
-                return this.props.equals(((ComparableImplementation)obj).props);
-            }
-            return false;
-        }
-
-        @Override
-        public int hashCode() {
-            return this.props.hashCode();
-        }
+        return ServiceUtil.getComparableForServiceRanking(props);
     }
 }
diff --git a/src/main/java/org/apache/sling/commons/osgi/OsgiUtil.java b/src/main/java/org/apache/sling/commons/osgi/PropertiesUtil.java
similarity index 57%
copy from src/main/java/org/apache/sling/commons/osgi/OsgiUtil.java
copy to src/main/java/org/apache/sling/commons/osgi/PropertiesUtil.java
index d571529..9a69a68 100644
--- a/src/main/java/org/apache/sling/commons/osgi/OsgiUtil.java
+++ b/src/main/java/org/apache/sling/commons/osgi/PropertiesUtil.java
@@ -20,22 +20,15 @@ package org.apache.sling.commons.osgi;
 
 import java.util.ArrayList;
 import java.util.Collection;
-import java.util.Dictionary;
-import java.util.Hashtable;
 import java.util.List;
-import java.util.Map;
-
-import org.osgi.framework.Bundle;
-import org.osgi.framework.Constants;
-import org.osgi.framework.ServiceReference;
-import org.osgi.service.event.Event;
-import org.osgi.service.event.EventConstants;
 
 /**
- * The <code>OsgiUtil</code> is a utility class providing some usefull utility
- * methods.
+ * The <code>PropertiesUtil</code> is a utility class providing some
+ * usefull utility methods for converting property types.
+ *
+ * @since 2.1
  */
-public class OsgiUtil {
+public class PropertiesUtil {
 
     /**
      * Returns the boolean value of the parameter or the
@@ -121,23 +114,6 @@ public class OsgiUtil {
      * a <code>Double</code> from the parameter's string value.
      * @param propValue the property value or <code>null</code>
      * @param defaultValue the default double value
-     *
-     * @deprecated since 2.0.4, use {@link #toDouble(Object, double)} instead
-     */
-    @Deprecated
-    public static double getProperty(Object propValue, double defaultValue) {
-        return toDouble(propValue, defaultValue);
-    }
-
-    /**
-     * Returns the parameter as a double or the
-     * <code>defaultValue</code> if the parameter is <code>null</code> or if
-     * the parameter is not a <code>Double</code> and cannot be converted to
-     * a <code>Double</code> from the parameter's string value.
-     * @param propValue the property value or <code>null</code>
-     * @param defaultValue the default double value
-     *
-     * @since 2.0.4
      */
     public static double toDouble(Object propValue, double defaultValue) {
         propValue = toObject(propValue);
@@ -199,7 +175,6 @@ public class OsgiUtil {
      * collection elements are converted to String objects and returned as an array.
      * Otherwise (if the property is <code>null</code>) a provided default value is
      * returned.
-     * @since 2.0.4
      * @param propValue The object to convert.
      * @param defaultArray The default array to return.
      */
@@ -241,132 +216,4 @@ public class OsgiUtil {
 
         return defaultArray;
     }
-
-    /**
-     * Create an osgi event with the given topic and properties.
-     * If a bundle parameter is provided the symbolic name is added
-     * as a property.
-     * If a service parameter is provided, information about the service
-     * is added to the properties.
-     * @param sourceBundle Optional source bundle
-     * @param sourceService Optional source service
-     * @param topic The event topic.
-     * @param props A non-null map of properties for the event.
-     * @return The OSGi event.
-     */
-    public static Event createEvent(Bundle sourceBundle,
-            ServiceReference sourceService, String topic,
-            Map<String, Object> props) {
-
-        // get a private copy of the properties
-        Dictionary<String, Object> table = new Hashtable<String, Object>(props);
-
-        // service information of the provide service reference
-        if (sourceService != null) {
-            table.put(EventConstants.SERVICE, sourceService);
-            table.put(
-                EventConstants.SERVICE_ID,
-                sourceService.getProperty(org.osgi.framework.Constants.SERVICE_ID));
-            table.put(
-                EventConstants.SERVICE_OBJECTCLASS,
-                sourceService.getProperty(org.osgi.framework.Constants.OBJECTCLASS));
-            if (sourceService.getProperty(org.osgi.framework.Constants.SERVICE_PID) != null) {
-                table.put(
-                    EventConstants.SERVICE_PID,
-                    sourceService.getProperty(org.osgi.framework.Constants.SERVICE_PID));
-            }
-        }
-
-        // source bundle information (if available)
-        if (sourceBundle != null) {
-            table.put(EventConstants.BUNDLE_SYMBOLICNAME,
-                sourceBundle.getSymbolicName());
-        }
-
-        // timestamp the event
-        table.put(EventConstants.TIMESTAMP,
-            new Long(System.currentTimeMillis()));
-
-        // create the event
-        return new Event(topic, table);
-    }
-
-    /**
-     * 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 Comparable<Object> getComparableForServiceRanking(final Map<String, Object> props) {
-        return new ComparableImplementation(props);
-    }
-
-    private static final class ComparableImplementation implements Comparable<Object> {
-
-        private final Map<String, Object> props;
-
-        private ComparableImplementation(Map<String, Object> props) {
-            this.props = props;
-        }
-
-        @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 if (reference instanceof Map){
-                final Map<String, Object> otherProps = (Map<String, Object>)reference;
-                otherId = (Long) otherProps.get(Constants.SERVICE_ID);
-                otherRankObj = otherProps.get(Constants.SERVICE_RANKING);
-            } else {
-                final ComparableImplementation other = (ComparableImplementation)reference;
-                otherId = (Long) other.props.get(Constants.SERVICE_ID);
-                otherRankObj = other.props.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;
-        }
-
-        @Override
-        public boolean equals(Object obj) {
-            if ( obj instanceof ComparableImplementation ) {
-                return this.props.equals(((ComparableImplementation)obj).props);
-            }
-            return false;
-        }
-
-        @Override
-        public int hashCode() {
-            return this.props.hashCode();
-        }
-    }
 }
diff --git a/src/main/java/org/apache/sling/commons/osgi/ServiceUtil.java b/src/main/java/org/apache/sling/commons/osgi/ServiceUtil.java
new file mode 100644
index 0000000..fa5219d
--- /dev/null
+++ b/src/main/java/org/apache/sling/commons/osgi/ServiceUtil.java
@@ -0,0 +1,110 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.commons.osgi;
+
+import java.util.Map;
+
+import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceReference;
+
+/**
+ * The <code>ServiceUtil</code> is a utility class providing some
+ * usefull utility methods for service handling.
+ * @since 2.1
+ */
+public class ServiceUtil {
+
+    /**
+     * 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
+     */
+    public static Comparable<Object> getComparableForServiceRanking(final Map<String, Object> props) {
+        return new ComparableImplementation(props);
+    }
+
+    private static final class ComparableImplementation implements Comparable<Object> {
+
+        private final Map<String, Object> props;
+
+        private ComparableImplementation(Map<String, Object> props) {
+            this.props = props;
+        }
+
+        @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 if (reference instanceof Map){
+                final Map<String, Object> otherProps = (Map<String, Object>)reference;
+                otherId = (Long) otherProps.get(Constants.SERVICE_ID);
+                otherRankObj = otherProps.get(Constants.SERVICE_RANKING);
+            } else {
+                final ComparableImplementation other = (ComparableImplementation)reference;
+                otherId = (Long) other.props.get(Constants.SERVICE_ID);
+                otherRankObj = other.props.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;
+        }
+
+        @Override
+        public boolean equals(Object obj) {
+            if ( obj instanceof ComparableImplementation ) {
+                return this.props.equals(((ComparableImplementation)obj).props);
+            }
+            return false;
+        }
+
+        @Override
+        public int hashCode() {
+            return this.props.hashCode();
+        }
+    }
+}

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.