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:23:02 UTC

[sling-org-apache-sling-commons-osgi] 02/38: Make OosgiUtil more generic and cleanup imports

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

commit 341e8d7f0b55793e26a9c33d2b718d409d76392a
Author: Felix Meschberger <fm...@apache.org>
AuthorDate: Mon Jan 14 19:23:23 2008 +0000

    Make OosgiUtil more generic and cleanup imports
    
    git-svn-id: https://svn.apache.org/repos/asf/incubator/sling/whiteboard/fmeschbe/resource/osgi/commons@611900 13f79535-47bb-0310-9956-ffa450edef68
---
 .../org/apache/sling/osgi/commons/OsgiUtil.java    | 52 ++++++++++------------
 .../internal/AdapterFactoryDescriptorKey.java      |  2 +-
 .../osgi/commons/internal/AdapterManagerImpl.java  |  7 ++-
 .../apache/sling/osgi/commons/mock/MockBundle.java |  2 -
 4 files changed, 28 insertions(+), 35 deletions(-)

diff --git a/src/main/java/org/apache/sling/osgi/commons/OsgiUtil.java b/src/main/java/org/apache/sling/osgi/commons/OsgiUtil.java
index 49d7e23..724accd 100644
--- a/src/main/java/org/apache/sling/osgi/commons/OsgiUtil.java
+++ b/src/main/java/org/apache/sling/osgi/commons/OsgiUtil.java
@@ -28,7 +28,6 @@ import java.util.Vector;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.ServiceReference;
 import org.osgi.service.event.Event;
-import org.osgi.service.event.EventAdmin;
 import org.osgi.service.event.EventConstants;
 
 /**
@@ -44,9 +43,8 @@ public class OsgiUtil {
      * by calling <code>Boolean.valueOf</code> on the string value of the
      * property.
      */
-    public static boolean getProperty(ServiceReference reference, String name,
-            boolean defaultValue) {
-        Object propValue = getPropertyObject(reference, name);
+    public static boolean toBoolean(Object propValue, boolean defaultValue) {
+        propValue = toObject(propValue);
         if (propValue instanceof Boolean) {
             return (Boolean) propValue;
         } else if (propValue != null) {
@@ -60,9 +58,8 @@ public class OsgiUtil {
      * Returns the named service reference property as a string or the
      * <code>defaultValue</code> if no such reference property exists.
      */
-    public static String getProperty(ServiceReference reference, String name,
-            String defaultValue) {
-        Object propValue = getPropertyObject(reference, name);
+    public static String toString(Object propValue, String defaultValue) {
+        propValue = toObject(propValue);
         return (propValue != null) ? propValue.toString() : defaultValue;
     }
 
@@ -72,14 +69,13 @@ public class OsgiUtil {
      * the property is not an <code>Integer</code> and cannot be converted to
      * an <code>Integer</code> from the property's string value.
      */
-    public static int getProperty(ServiceReference reference, String name,
-            int defaultValue) {
-        Object propValue = getPropertyObject(reference, name);
-        if (propValue instanceof Integer) {
-            return (Integer) propValue;
+    public static long toLong(Object propValue, long defaultValue) {
+        propValue = toObject(propValue);
+        if (propValue instanceof Long) {
+            return (Long) propValue;
         } else if (propValue != null) {
             try {
-                return Integer.valueOf(String.valueOf(propValue));
+                return Long.valueOf(String.valueOf(propValue));
             } catch (NumberFormatException nfe) {
                 // don't care, fall through to default value
             }
@@ -94,9 +90,8 @@ public class OsgiUtil {
      * the property is not an <code>Double</code> and cannot be converted to
      * an <code>Double</code> from the property's string value.
      */
-    public static double getProperty(ServiceReference reference, String name,
-            double defaultValue) {
-        Object propValue = getPropertyObject(reference, name);
+    public static double getProperty(Object propValue, double defaultValue) {
+        propValue = toObject(propValue);
         if (propValue instanceof Double) {
             return (Double) propValue;
         } else if (propValue != null) {
@@ -118,9 +113,7 @@ public class OsgiUtil {
      * <code>java.util.Vector</code>, the first vector element is returned.
      * Otherwise <code>null</code> is returned.
      */
-    public static Object getPropertyObject(ServiceReference reference,
-            String name) {
-        Object propValue = reference.getProperty(name);
+    public static Object toObject(Object propValue) {
         if (propValue == null) {
             return null;
         } else if (propValue.getClass().isArray()) {
@@ -143,16 +136,15 @@ public class OsgiUtil {
      * Otherwise (if the property does not exist) <code>null</code> is
      * returned.
      */
-    public static String[] getProperty(ServiceReference reference, String name) {
-        Object propValue = reference.getProperty(name);
+    public static String[] toStringArray(Object propValue) {
         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;
@@ -163,7 +155,7 @@ public class OsgiUtil {
                 }
             }
             return values.toArray(new String[values.size()]);
-            
+
         } else if (propValue instanceof Vector) {
             // vector
             Vector<?> valueVector = (Vector<?>) propValue;
@@ -179,7 +171,8 @@ public class OsgiUtil {
         return null;
     }
 
-    public static Event createEvent(Bundle sourceBundle, ServiceReference sourceService, String eventName,
+    public static Event createEvent(Bundle sourceBundle,
+            ServiceReference sourceService, String eventName,
             Map<String, Object> props) {
 
         // get a private copy of the properties
@@ -188,12 +181,15 @@ public class OsgiUtil {
         // service information of this JcrResourceResolverFactoryImpl service
         if (sourceService != null) {
             table.put(EventConstants.SERVICE, sourceService);
-            table.put(EventConstants.SERVICE_ID,
+            table.put(
+                EventConstants.SERVICE_ID,
                 sourceService.getProperty(org.osgi.framework.Constants.SERVICE_ID));
-            table.put(EventConstants.SERVICE_OBJECTCLASS,
+            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,
+                table.put(
+                    EventConstants.SERVICE_PID,
                     sourceService.getProperty(org.osgi.framework.Constants.SERVICE_PID));
             }
         }
diff --git a/src/main/java/org/apache/sling/osgi/commons/internal/AdapterFactoryDescriptorKey.java b/src/main/java/org/apache/sling/osgi/commons/internal/AdapterFactoryDescriptorKey.java
index d12c9e8..c9f5c64 100644
--- a/src/main/java/org/apache/sling/osgi/commons/internal/AdapterFactoryDescriptorKey.java
+++ b/src/main/java/org/apache/sling/osgi/commons/internal/AdapterFactoryDescriptorKey.java
@@ -41,7 +41,7 @@ public class AdapterFactoryDescriptorKey implements
 
     public AdapterFactoryDescriptorKey(ServiceReference ref) {
         bundleId = ref.getBundle().getBundleId();
-        serviceId = OsgiUtil.getProperty(ref, Constants.SERVICE_ID, -1);
+        serviceId = OsgiUtil.toLong(ref.getProperty(Constants.SERVICE_ID), -1);
     }
 
     public int compareTo(AdapterFactoryDescriptorKey o) {
diff --git a/src/main/java/org/apache/sling/osgi/commons/internal/AdapterManagerImpl.java b/src/main/java/org/apache/sling/osgi/commons/internal/AdapterManagerImpl.java
index cf0f0e9..8cd3a08 100644
--- a/src/main/java/org/apache/sling/osgi/commons/internal/AdapterManagerImpl.java
+++ b/src/main/java/org/apache/sling/osgi/commons/internal/AdapterManagerImpl.java
@@ -21,7 +21,6 @@ package org.apache.sling.osgi.commons.internal;
 import static org.apache.sling.osgi.commons.AdapterFactory.ADAPTABLE_CLASSES;
 import static org.apache.sling.osgi.commons.AdapterFactory.ADAPTER_CLASSES;
 
-import java.io.PrintStream;
 import java.util.HashMap;
 import java.util.LinkedList;
 import java.util.List;
@@ -232,8 +231,8 @@ public class AdapterManagerImpl implements AdapterManager {
      */
     private void registerAdapterFactory(ComponentContext context,
             ServiceReference reference) {
-        String[] adaptables = OsgiUtil.getProperty(reference, ADAPTABLE_CLASSES);
-        String[] adapters = OsgiUtil.getProperty(reference, ADAPTER_CLASSES);
+        String[] adaptables = OsgiUtil.toStringArray(reference.getProperty(ADAPTABLE_CLASSES));
+        String[] adapters = OsgiUtil.toStringArray(reference.getProperty(ADAPTER_CLASSES));
 
         if (adaptables == null || adaptables.length == 0 || adapters == null
             || adapters.length == 0) {
@@ -270,7 +269,7 @@ public class AdapterManagerImpl implements AdapterManager {
     private void unregisterAdapterFactory(ServiceReference reference) {
         boundAdapterFactories.remove(reference);
 
-        String[] adaptables = OsgiUtil.getProperty(reference, ADAPTABLE_CLASSES);
+        String[] adaptables = OsgiUtil.toStringArray(reference.getProperty(ADAPTABLE_CLASSES));
 
         if (adaptables == null || adaptables.length == 0) {
             return;
diff --git a/src/test/java/org/apache/sling/osgi/commons/mock/MockBundle.java b/src/test/java/org/apache/sling/osgi/commons/mock/MockBundle.java
index 13fda4f..f6a3523 100644
--- a/src/test/java/org/apache/sling/osgi/commons/mock/MockBundle.java
+++ b/src/test/java/org/apache/sling/osgi/commons/mock/MockBundle.java
@@ -18,14 +18,12 @@
  */
 package org.apache.sling.osgi.commons.mock;
 
-import java.io.IOException;
 import java.io.InputStream;
 import java.net.URL;
 import java.util.Dictionary;
 import java.util.Enumeration;
 
 import org.osgi.framework.Bundle;
-import org.osgi.framework.BundleException;
 import org.osgi.framework.ServiceReference;
 
 public class MockBundle implements Bundle {

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