felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From vvalc...@apache.org
Subject svn commit: r951318 - in /felix/trunk/webconsole/src/main/java/org/apache/felix/webconsole: ./ internal/compendium/ internal/core/ internal/misc/
Date Fri, 04 Jun 2010 08:27:38 GMT
Author: vvalchev
Date: Fri Jun  4 08:27:37 2010
New Revision: 951318

URL: http://svn.apache.org/viewvc?rev=951318&view=rev
Log:
Fixed FELIX-2284 /Add common utility method for converting object (array) to string/
https://issues.apache.org/jira/browse/FELIX-2284

Modified:
    felix/trunk/webconsole/src/main/java/org/apache/felix/webconsole/WebConsoleUtil.java
    felix/trunk/webconsole/src/main/java/org/apache/felix/webconsole/internal/compendium/ComponentConfigurationPrinter.java
    felix/trunk/webconsole/src/main/java/org/apache/felix/webconsole/internal/compendium/ComponentsServlet.java
    felix/trunk/webconsole/src/main/java/org/apache/felix/webconsole/internal/core/ServicesServlet.java
    felix/trunk/webconsole/src/main/java/org/apache/felix/webconsole/internal/misc/ConfigurationRender.java

Modified: felix/trunk/webconsole/src/main/java/org/apache/felix/webconsole/WebConsoleUtil.java
URL: http://svn.apache.org/viewvc/felix/trunk/webconsole/src/main/java/org/apache/felix/webconsole/WebConsoleUtil.java?rev=951318&r1=951317&r2=951318&view=diff
==============================================================================
--- felix/trunk/webconsole/src/main/java/org/apache/felix/webconsole/WebConsoleUtil.java (original)
+++ felix/trunk/webconsole/src/main/java/org/apache/felix/webconsole/WebConsoleUtil.java Fri
Jun  4 08:27:37 2010
@@ -20,6 +20,7 @@ package org.apache.felix.webconsole;
 
 
 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 @@ public final class WebConsoleUtil
             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();
+        }
+
+    }
 }

Modified: felix/trunk/webconsole/src/main/java/org/apache/felix/webconsole/internal/compendium/ComponentConfigurationPrinter.java
URL: http://svn.apache.org/viewvc/felix/trunk/webconsole/src/main/java/org/apache/felix/webconsole/internal/compendium/ComponentConfigurationPrinter.java?rev=951318&r1=951317&r2=951318&view=diff
==============================================================================
--- felix/trunk/webconsole/src/main/java/org/apache/felix/webconsole/internal/compendium/ComponentConfigurationPrinter.java
(original)
+++ felix/trunk/webconsole/src/main/java/org/apache/felix/webconsole/internal/compendium/ComponentConfigurationPrinter.java
Fri Jun  4 08:27:37 2010
@@ -29,6 +29,7 @@ import java.util.TreeSet;
 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 @@ public class ComponentConfigurationPrint
             {
                 String key = ( String ) ki.next();
                 Object value = props.get( key );
+                value = WebConsoleUtil.toString( value );
                 if ( value.getClass().isArray() )
                 {
                     value = Arrays.asList( ( Object[] ) value );

Modified: felix/trunk/webconsole/src/main/java/org/apache/felix/webconsole/internal/compendium/ComponentsServlet.java
URL: http://svn.apache.org/viewvc/felix/trunk/webconsole/src/main/java/org/apache/felix/webconsole/internal/compendium/ComponentsServlet.java?rev=951318&r1=951317&r2=951318&view=diff
==============================================================================
--- felix/trunk/webconsole/src/main/java/org/apache/felix/webconsole/internal/compendium/ComponentsServlet.java
(original)
+++ felix/trunk/webconsole/src/main/java/org/apache/felix/webconsole/internal/compendium/ComponentsServlet.java
Fri Jun  4 08:27:37 2010
@@ -385,10 +385,7 @@ public class ComponentsServlet extends S
                 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());
             }

Modified: felix/trunk/webconsole/src/main/java/org/apache/felix/webconsole/internal/core/ServicesServlet.java
URL: http://svn.apache.org/viewvc/felix/trunk/webconsole/src/main/java/org/apache/felix/webconsole/internal/core/ServicesServlet.java?rev=951318&r1=951317&r2=951318&view=diff
==============================================================================
--- felix/trunk/webconsole/src/main/java/org/apache/felix/webconsole/internal/core/ServicesServlet.java
(original)
+++ felix/trunk/webconsole/src/main/java/org/apache/felix/webconsole/internal/core/ServicesServlet.java
Fri Jun  4 08:27:37 2010
@@ -182,27 +182,8 @@ public class ServicesServlet extends Sim
 
     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 );
     }
 
 

Modified: felix/trunk/webconsole/src/main/java/org/apache/felix/webconsole/internal/misc/ConfigurationRender.java
URL: http://svn.apache.org/viewvc/felix/trunk/webconsole/src/main/java/org/apache/felix/webconsole/internal/misc/ConfigurationRender.java?rev=951318&r1=951317&r2=951318&view=diff
==============================================================================
--- felix/trunk/webconsole/src/main/java/org/apache/felix/webconsole/internal/misc/ConfigurationRender.java
(original)
+++ felix/trunk/webconsole/src/main/java/org/apache/felix/webconsole/internal/misc/ConfigurationRender.java
Fri Jun  4 08:27:37 2010
@@ -378,37 +378,11 @@ public class ConfigurationRender extends
             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( "%" ) )



Mime
View raw message