felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mccu...@apache.org
Subject svn commit: r1199573 - in /felix/trunk/bundleplugin/src: main/java/org/apache/felix/bundleplugin/BundlePlugin.java test/java/org/apache/felix/bundleplugin/BundlePluginTest.java
Date Wed, 09 Nov 2011 00:45:13 GMT
Author: mcculls
Date: Wed Nov  9 00:45:12 2011
New Revision: 1199573

URL: http://svn.apache.org/viewvc?rev=1199573&view=rev
Log:
FELIX-3209: sanitize any non-String property keys/values

Modified:
    felix/trunk/bundleplugin/src/main/java/org/apache/felix/bundleplugin/BundlePlugin.java
    felix/trunk/bundleplugin/src/test/java/org/apache/felix/bundleplugin/BundlePluginTest.java

Modified: felix/trunk/bundleplugin/src/main/java/org/apache/felix/bundleplugin/BundlePlugin.java
URL: http://svn.apache.org/viewvc/felix/trunk/bundleplugin/src/main/java/org/apache/felix/bundleplugin/BundlePlugin.java?rev=1199573&r1=1199572&r2=1199573&view=diff
==============================================================================
--- felix/trunk/bundleplugin/src/main/java/org/apache/felix/bundleplugin/BundlePlugin.java
(original)
+++ felix/trunk/bundleplugin/src/main/java/org/apache/felix/bundleplugin/BundlePlugin.java
Wed Nov  9 00:45:12 2011
@@ -25,6 +25,7 @@ import java.io.File;
 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 @@ public class BundlePlugin extends Abstra
 
         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 @@ public class BundlePlugin extends Abstra
     }
 
 
+    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 )

Modified: felix/trunk/bundleplugin/src/test/java/org/apache/felix/bundleplugin/BundlePluginTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/bundleplugin/src/test/java/org/apache/felix/bundleplugin/BundlePluginTest.java?rev=1199573&r1=1199572&r2=1199573&view=diff
==============================================================================
--- felix/trunk/bundleplugin/src/test/java/org/apache/felix/bundleplugin/BundlePluginTest.java
(original)
+++ felix/trunk/bundleplugin/src/test/java/org/apache/felix/bundleplugin/BundlePluginTest.java
Wed Nov  9 00:45:12 2011
@@ -21,6 +21,8 @@ package org.apache.felix.bundleplugin;
  */
 
 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 @@ public class BundlePluginTest extends Ab
         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" );
+    }
 }



Mime
View raw message