FELIX-3209: sanitize any non-String property keys/values

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1199573 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/bundleplugin/src/main/java/org/apache/felix/bundleplugin/BundlePlugin.java b/bundleplugin/src/main/java/org/apache/felix/bundleplugin/BundlePlugin.java
index 67f9163..258b7c0 100644
--- a/bundleplugin/src/main/java/org/apache/felix/bundleplugin/BundlePlugin.java
+++ b/bundleplugin/src/main/java/org/apache/felix/bundleplugin/BundlePlugin.java
@@ -25,6 +25,7 @@
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.lang.reflect.Array;
 import java.lang.reflect.Method;
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -419,7 +420,7 @@
 
         Builder builder = new Builder();
         builder.setBase( getBase( currentProject ) );
-        builder.setProperties( properties );
+        builder.setProperties( sanitize( properties ) );
         if ( classpath != null )
         {
             builder.setClasspath( classpath );
@@ -429,6 +430,67 @@
     }
 
 
+    protected static Properties sanitize( Properties properties )
+    {
+        // convert any non-String keys/values to Strings
+        Properties sanitizedEntries = new Properties();
+        for ( Iterator itr = properties.entrySet().iterator(); itr.hasNext(); )
+        {
+            Map.Entry entry = ( Map.Entry ) itr.next();
+            if ( entry.getKey() instanceof String == false )
+            {
+                String key = sanitize( entry.getKey() );
+                if ( !properties.containsKey( key ) )
+                {
+                    sanitizedEntries.setProperty( key, sanitize( entry.getValue() ) );
+                }
+                itr.remove();
+            }
+            else if ( entry.getValue() instanceof String == false )
+            {
+                entry.setValue( sanitize( entry.getValue() ) );
+            }
+        }
+        properties.putAll( sanitizedEntries );
+        return properties;
+    }
+
+
+    protected static String sanitize( Object value )
+    {
+        if ( value instanceof String )
+        {
+            return ( String ) value;
+        }
+        else if ( value instanceof Iterable )
+        {
+            String delim = "";
+            StringBuilder buf = new StringBuilder();
+            for ( Object i : ( Iterable<?> ) value )
+            {
+                buf.append( delim ).append( i );
+                delim = ", ";
+            }
+            return buf.toString();
+        }
+        else if ( value.getClass().isArray() )
+        {
+            String delim = "";
+            StringBuilder buf = new StringBuilder();
+            for ( int i = 0, len = Array.getLength( value ); i < len; i++ )
+            {
+                buf.append( delim ).append( Array.get( value, i ) );
+                delim = ", ";
+            }
+            return buf.toString();
+        }
+        else
+        {
+            return String.valueOf( value );
+        }
+    }
+
+
     protected void addMavenInstructions( MavenProject currentProject, Builder builder ) throws Exception
     {
         if ( currentProject.getBasedir() != null )
diff --git a/bundleplugin/src/test/java/org/apache/felix/bundleplugin/BundlePluginTest.java b/bundleplugin/src/test/java/org/apache/felix/bundleplugin/BundlePluginTest.java
index e224c96..44c7f97 100644
--- a/bundleplugin/src/test/java/org/apache/felix/bundleplugin/BundlePluginTest.java
+++ b/bundleplugin/src/test/java/org/apache/felix/bundleplugin/BundlePluginTest.java
@@ -21,6 +21,8 @@
  */
 
 import java.io.File;
+import java.io.FileOutputStream;
+import java.util.Arrays;
 import java.util.HashMap;
 import java.util.LinkedHashSet;
 import java.util.Map;
@@ -371,4 +373,25 @@
         String eas2 = manifest2.getMainAttributes().getValue( "Embedded-Artifacts" );
         assertEquals( eas1, eas2 );
     }
+
+
+    public void testPropertySanitizing() throws Exception
+    {
+        MavenProject project = getMavenProjectStub();
+
+        Properties props = new Properties();
+
+        props.put( new File( "A" ), new File( "B" ) );
+        props.put( new int[4], new HashMap( 2 ) );
+        props.put( Arrays.asList( 1, "two", 3.0 ), new float[5] );
+
+        props.put( "A", new File( "B" ) );
+        props.put( "4", new HashMap( 2 ) );
+        props.put( "1, two, 3.0", new char[5] );
+
+        Builder builder = plugin.getOSGiBuilder( project, new HashMap(), props, plugin.getClasspath( project ) );
+
+        File file = new File( getBasedir(), "target" + File.separatorChar + "test.props" );
+        builder.getProperties().store( new FileOutputStream( file ), "TEST" );
+    }
 }