Author: fmeschbe
Date: Mon Jan 14 11:23:23 2008
New Revision: 611900

URL: http://svn.apache.org/viewvc?rev=611900&view=rev
Log:
Make OosgiUtil more generic and cleanup imports

Modified:
    
incubator/sling/whiteboard/fmeschbe/resource/osgi/commons/src/main/java/org/apache/sling/osgi/commons/OsgiUtil.java
    
incubator/sling/whiteboard/fmeschbe/resource/osgi/commons/src/main/java/org/apache/sling/osgi/commons/internal/AdapterFactoryDescriptorKey.java
    
incubator/sling/whiteboard/fmeschbe/resource/osgi/commons/src/main/java/org/apache/sling/osgi/commons/internal/AdapterManagerImpl.java
    
incubator/sling/whiteboard/fmeschbe/resource/osgi/commons/src/test/java/org/apache/sling/osgi/commons/mock/MockBundle.java

Modified: 
incubator/sling/whiteboard/fmeschbe/resource/osgi/commons/src/main/java/org/apache/sling/osgi/commons/OsgiUtil.java
URL: 
http://svn.apache.org/viewvc/incubator/sling/whiteboard/fmeschbe/resource/osgi/commons/src/main/java/org/apache/sling/osgi/commons/OsgiUtil.java?rev=611900&r1=611899&r2=611900&view=diff
==============================================================================
--- 
incubator/sling/whiteboard/fmeschbe/resource/osgi/commons/src/main/java/org/apache/sling/osgi/commons/OsgiUtil.java
 (original)
+++ 
incubator/sling/whiteboard/fmeschbe/resource/osgi/commons/src/main/java/org/apache/sling/osgi/commons/OsgiUtil.java
 Mon Jan 14 11:23:23 2008
@@ -28,7 +28,6 @@
 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 @@
      * 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 @@
      * 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 @@
      * 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 @@
      * 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 @@
      * <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 @@
      * 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 @@
                 }
             }
             return values.toArray(new String[values.size()]);
-            
+
         } else if (propValue instanceof Vector) {
             // vector
             Vector<?> valueVector = (Vector<?>) propValue;
@@ -179,7 +171,8 @@
         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 @@
         // 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));
             }
         }

Modified: 
incubator/sling/whiteboard/fmeschbe/resource/osgi/commons/src/main/java/org/apache/sling/osgi/commons/internal/AdapterFactoryDescriptorKey.java
URL: 
http://svn.apache.org/viewvc/incubator/sling/whiteboard/fmeschbe/resource/osgi/commons/src/main/java/org/apache/sling/osgi/commons/internal/AdapterFactoryDescriptorKey.java?rev=611900&r1=611899&r2=611900&view=diff
==============================================================================
--- 
incubator/sling/whiteboard/fmeschbe/resource/osgi/commons/src/main/java/org/apache/sling/osgi/commons/internal/AdapterFactoryDescriptorKey.java
 (original)
+++ 
incubator/sling/whiteboard/fmeschbe/resource/osgi/commons/src/main/java/org/apache/sling/osgi/commons/internal/AdapterFactoryDescriptorKey.java
 Mon Jan 14 11:23:23 2008
@@ -41,7 +41,7 @@
 
     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) {

Modified: 
incubator/sling/whiteboard/fmeschbe/resource/osgi/commons/src/main/java/org/apache/sling/osgi/commons/internal/AdapterManagerImpl.java
URL: 
http://svn.apache.org/viewvc/incubator/sling/whiteboard/fmeschbe/resource/osgi/commons/src/main/java/org/apache/sling/osgi/commons/internal/AdapterManagerImpl.java?rev=611900&r1=611899&r2=611900&view=diff
==============================================================================
--- 
incubator/sling/whiteboard/fmeschbe/resource/osgi/commons/src/main/java/org/apache/sling/osgi/commons/internal/AdapterManagerImpl.java
 (original)
+++ 
incubator/sling/whiteboard/fmeschbe/resource/osgi/commons/src/main/java/org/apache/sling/osgi/commons/internal/AdapterManagerImpl.java
 Mon Jan 14 11:23:23 2008
@@ -21,7 +21,6 @@
 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 @@
      */
     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 @@
     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;

Modified: 
incubator/sling/whiteboard/fmeschbe/resource/osgi/commons/src/test/java/org/apache/sling/osgi/commons/mock/MockBundle.java
URL: 
http://svn.apache.org/viewvc/incubator/sling/whiteboard/fmeschbe/resource/osgi/commons/src/test/java/org/apache/sling/osgi/commons/mock/MockBundle.java?rev=611900&r1=611899&r2=611900&view=diff
==============================================================================
--- 
incubator/sling/whiteboard/fmeschbe/resource/osgi/commons/src/test/java/org/apache/sling/osgi/commons/mock/MockBundle.java
 (original)
+++ 
incubator/sling/whiteboard/fmeschbe/resource/osgi/commons/src/test/java/org/apache/sling/osgi/commons/mock/MockBundle.java
 Mon Jan 14 11:23:23 2008
@@ -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 {


Reply via email to