Fixed FELIX-2284 /Add common utility method for converting object (array) to string/
https://issues.apache.org/jira/browse/FELIX-2284

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@951318 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/webconsole/src/main/java/org/apache/felix/webconsole/WebConsoleUtil.java b/webconsole/src/main/java/org/apache/felix/webconsole/WebConsoleUtil.java
index 7ff5b55..5c609e4 100644
--- a/webconsole/src/main/java/org/apache/felix/webconsole/WebConsoleUtil.java
+++ b/webconsole/src/main/java/org/apache/felix/webconsole/WebConsoleUtil.java
@@ -20,6 +20,7 @@
 
 
 import java.io.IOException;
+import java.lang.reflect.Array;
 import java.net.URLDecoder;
 import java.util.HashMap;
 import java.util.Iterator;
@@ -367,4 +368,61 @@
             return URLDecoder.decode( value );
         }
     }
+
+    /**
+     * This method will stringify a Java object. It is mostly used to print the values
+     * of unknown properties. This method will correctly handle if the passed object
+     * is array and will property display it.
+     *
+     * If the value is byte[] the elements are shown as Hex
+     * 
+     * @param value the value to convert
+     * @return the string representation of the value
+     */
+    public static final String toString(Object value)
+    {
+        if (value == null)
+        {
+            return "n/a";
+        }
+        else if (value.getClass().isArray())
+        {
+            final StringBuffer sb = new StringBuffer();
+            final int len = Array.getLength(value);
+            synchronized (sb)
+            { // it's faster to synchronize ALL loop calls
+                sb.append('[');
+                for (int i = 0; i < len; i++)
+                {
+                    final Object element = Array.get(value, i);
+                    if (element instanceof Byte)
+                    {
+                        // convert byte[] to hex string
+                        sb.append("0x");
+                        final String x = Integer.toHexString(((Byte) element).intValue() & 0xff);
+                        if (1 == x.length())
+                        {
+                            sb.append('0');
+                        }
+                        sb.append(x);
+                    }
+                    else
+                    {
+                        sb.append(toString(element));
+                    }
+
+                    if (i < len - 1)
+                    {
+                        sb.append(", ");
+                    }
+                }
+                return sb.append(']').toString();
+            }
+        }
+        else
+        {
+            return value.toString();
+        }
+
+    }
 }
diff --git a/webconsole/src/main/java/org/apache/felix/webconsole/internal/compendium/ComponentConfigurationPrinter.java b/webconsole/src/main/java/org/apache/felix/webconsole/internal/compendium/ComponentConfigurationPrinter.java
index 9ad6332..67ef652 100644
--- a/webconsole/src/main/java/org/apache/felix/webconsole/internal/compendium/ComponentConfigurationPrinter.java
+++ b/webconsole/src/main/java/org/apache/felix/webconsole/internal/compendium/ComponentConfigurationPrinter.java
@@ -29,6 +29,7 @@
 import org.apache.felix.scr.Component;
 import org.apache.felix.scr.Reference;
 import org.apache.felix.scr.ScrService;
+import org.apache.felix.webconsole.WebConsoleUtil;
 import org.apache.felix.webconsole.internal.AbstractConfigurationPrinter;
 import org.apache.felix.webconsole.internal.Util;
 import org.osgi.framework.Constants;
@@ -218,6 +219,7 @@
             {
                 String key = ( String ) ki.next();
                 Object value = props.get( key );
+                value = WebConsoleUtil.toString( value );
                 if ( value.getClass().isArray() )
                 {
                     value = Arrays.asList( ( Object[] ) value );
diff --git a/webconsole/src/main/java/org/apache/felix/webconsole/internal/compendium/ComponentsServlet.java b/webconsole/src/main/java/org/apache/felix/webconsole/internal/compendium/ComponentsServlet.java
index 1783911..db1f244 100644
--- a/webconsole/src/main/java/org/apache/felix/webconsole/internal/compendium/ComponentsServlet.java
+++ b/webconsole/src/main/java/org/apache/felix/webconsole/internal/compendium/ComponentsServlet.java
@@ -385,10 +385,7 @@
                 b.append( key ).append( " = " );
 
                 Object prop = props.get( key );
-                if ( prop.getClass().isArray() )
-                {
-                    prop = Arrays.asList( ( Object[] ) prop );
-                }
+                prop = WebConsoleUtil.toString( prop );
                 b.append( prop );
                 buf.put(b.toString());
             }
diff --git a/webconsole/src/main/java/org/apache/felix/webconsole/internal/core/ServicesServlet.java b/webconsole/src/main/java/org/apache/felix/webconsole/internal/core/ServicesServlet.java
index 9a56bb6..47cfbd0 100644
--- a/webconsole/src/main/java/org/apache/felix/webconsole/internal/core/ServicesServlet.java
+++ b/webconsole/src/main/java/org/apache/felix/webconsole/internal/core/ServicesServlet.java
@@ -182,27 +182,8 @@
 
     static final String propertyAsString( ServiceReference ref, String name )
     {
-        Object value = ref.getProperty( name );
-        if ( value instanceof Object[] )
-        {
-            StringBuffer dest = new StringBuffer();
-            Object[] values = ( Object[] ) value;
-            for ( int j = 0; j < values.length; j++ )
-            {
-                if ( j > 0 )
-                    dest.append( ", " );
-                dest.append( values[j] );
-            }
-            return dest.toString();
-        }
-        else if ( value != null )
-        {
-            return value.toString();
-        }
-        else
-        {
-            return "n/a";
-        }
+        final Object value = ref.getProperty( name );
+        return WebConsoleUtil.toString( value );
     }
 
 
diff --git a/webconsole/src/main/java/org/apache/felix/webconsole/internal/misc/ConfigurationRender.java b/webconsole/src/main/java/org/apache/felix/webconsole/internal/misc/ConfigurationRender.java
index 05e2189..4154bea 100644
--- a/webconsole/src/main/java/org/apache/felix/webconsole/internal/misc/ConfigurationRender.java
+++ b/webconsole/src/main/java/org/apache/felix/webconsole/internal/misc/ConfigurationRender.java
@@ -378,37 +378,11 @@
             pw.print( " = " );
         }
 
-        pw.print( asString( value ) );
+        pw.print( WebConsoleUtil.toString( value ) );
 
         pw.println();
     }
 
-
-    private static final String asString( final Object value )
-    {
-        if ( value == null )
-        {
-            return "n/a";
-        }
-        else if ( value.getClass().isArray() )
-        {
-            StringBuffer dest = new StringBuffer();
-            Object[] values = ( Object[] ) value;
-            for ( int j = 0; j < values.length; j++ )
-            {
-                if ( j > 0 )
-                    dest.append( ", " );
-                dest.append( values[j] );
-            }
-            return dest.toString();
-        }
-        else
-        {
-            return value.toString();
-        }
-    }
-
-
     private final String getTitle( final String title, final Bundle provider )
     {
         if ( !title.startsWith( "%" ) )