it might be worth considering switching to commons-exec for the executor 
instead of Runtime.exec - I think it would handle the cross-platform escaping 
better and has had several ears of dealing with edge cases on obscure platforms 
:)

On 17/11/2010, at 7:28 PM, apadi...@apache.org wrote:

> Author: apadilla
> Date: Wed Nov 17 08:28:12 2010
> New Revision: 1035948
> 
> URL: http://svn.apache.org/viewvc?rev=1035948&view=rev
> Log:
> [NPANDAY-337]
> [NPANDAY-341]
> * NPanday support for linux
> * applied patches submitted by Luca and Vladimir 
>    - modified some things and merged the patches they both submitted
> * added unit test
> 
> Added:
>    incubator/npanday/trunk/components/dotnet-executable/src/test/
>    incubator/npanday/trunk/components/dotnet-executable/src/test/java/
>    incubator/npanday/trunk/components/dotnet-executable/src/test/java/npanday/
>    
> incubator/npanday/trunk/components/dotnet-executable/src/test/java/npanday/executable/
>    
> incubator/npanday/trunk/components/dotnet-executable/src/test/java/npanday/executable/CommandExecutorTest.java
> Modified:
>    incubator/npanday/trunk/components/dotnet-executable/pom.xml
>    
> incubator/npanday/trunk/components/dotnet-executable/src/main/java/npanday/executable/CommandExecutor.java
>    
> incubator/npanday/trunk/components/dotnet-executable/src/main/java/npanday/executable/compiler/impl/DefaultCompiler.java
> 
> Modified: incubator/npanday/trunk/components/dotnet-executable/pom.xml
> URL: 
> http://svn.apache.org/viewvc/incubator/npanday/trunk/components/dotnet-executable/pom.xml?rev=1035948&r1=1035947&r2=1035948&view=diff
> ==============================================================================
> --- incubator/npanday/trunk/components/dotnet-executable/pom.xml (original)
> +++ incubator/npanday/trunk/components/dotnet-executable/pom.xml Wed Nov 17 
> 08:28:12 2010
> @@ -48,5 +48,11 @@ under the License.
>       <groupId>npanday</groupId>  
>       <artifactId>dotnet-vendor</artifactId>  
>     </dependency> 
> +    <dependency> 
> +      <groupId>junit</groupId>  
> +      <artifactId>junit</artifactId>  
> +      <version>4.8.1</version>  
> +      <scope>test</scope> 
> +    </dependency> 
>   </dependencies> 
> </project>
> 
> Modified: 
> incubator/npanday/trunk/components/dotnet-executable/src/main/java/npanday/executable/CommandExecutor.java
> URL: 
> http://svn.apache.org/viewvc/incubator/npanday/trunk/components/dotnet-executable/src/main/java/npanday/executable/CommandExecutor.java?rev=1035948&r1=1035947&r2=1035948&view=diff
> ==============================================================================
> --- 
> incubator/npanday/trunk/components/dotnet-executable/src/main/java/npanday/executable/CommandExecutor.java
>  (original)
> +++ 
> incubator/npanday/trunk/components/dotnet-executable/src/main/java/npanday/executable/CommandExecutor.java
>  Wed Nov 17 08:28:12 2010
> @@ -29,7 +29,7 @@ import org.codehaus.plexus.util.StringUt
> import org.codehaus.plexus.util.cli.shell.Shell;
> import java.io.IOException;
> 
> -  
> +
> 
> import java.util.List;
> import java.util.ArrayList;
> @@ -196,28 +196,28 @@ public interface CommandExecutor
>                     Commandline commandline = new Commandline()
>                     {
>                          protected Map envVars = Collections.synchronizedMap( 
> new LinkedHashMap() );
> -                         
> -                         
> +
> +
>                          public Process execute()
>                              throws CommandLineException
>                          {
>                              // TODO: Provided only for backward compat. with 
> <= 1.4
>                              //verifyShellState();
> -                     
> +
>                              Process process;
> -                     
> +
>                              //addEnvironment( "MAVEN_TEST_ENVAR", 
> "MAVEN_TEST_ENVAR_VALUE" );
> -                     
> +
>                              String[] environment = getEnvironmentVariables();
> -                             
> +
>                              File workingDir = getWorkingDirectory();
> -                     
> +
>                              try
>                              {
> -                                 String cmd = this.toString();
> +                                 String[] cmd = getEscapedShellCommandline();
> +
>                                  if ( workingDir == null )
>                                  {
> -                                     //process = Runtime.getRuntime().exec( 
> getShellCommandline(), environment );
>                                      process = Runtime.getRuntime().exec( 
> cmd, environment );
>                                  }
>                                  else
> @@ -232,8 +232,7 @@ public interface CommandExecutor
>                                          throw new CommandLineException( 
> "Path \"" + workingDir.getPath()
>                                              + "\" does not specify a 
> directory." );
>                                      }
> -                     
> -                                     //process = Runtime.getRuntime().exec( 
> getShellCommandline(), environment, workingDir );
> +
>                                      process = Runtime.getRuntime().exec( 
> cmd, environment, workingDir );
>                                  }
>                              }
> @@ -241,10 +240,10 @@ public interface CommandExecutor
>                              {
>                                  throw new CommandLineException( "Error while 
> executing process.", ex );
>                              }
> -                     
> +
>                              return process;
>                          }
> -                         
> +
>                          public String[] getEnvironmentVariables()
>                              throws CommandLineException
>                          {
> @@ -267,23 +266,21 @@ public interface CommandExecutor
>                              }
>                              return environmentVars;
>                          }
> -                         
> -                         
> -                         
> +
>                          public void addEnvironment( String name, String 
> value )
>                          {
>                              //envVars.add( name + "=" + value );
>                              envVars.put( name, value );
>                          }
> -                     
> +
>                          /**
> -                                                                    * Add 
> system environment variables
> -                                                                    */
> +                                                              * Add system 
> environment variables
> +                                                             */
>                          public void addSystemEnvironment()
>                              throws Exception
>                         {
>                              Properties systemEnvVars = 
> CommandLineUtils.getSystemEnvVars();
> -                     
> +
>                              for ( Iterator i = 
> systemEnvVars.keySet().iterator(); i.hasNext(); )
>                              {
>                                  String key = (String) i.next();
> @@ -293,7 +290,7 @@ public interface CommandExecutor
>                                  }
>                              }
>                         }
> -                        
> +
>                         public String toString()
>                         {
>                             StringBuffer strBuff = new StringBuffer("");
> @@ -304,51 +301,53 @@ public interface CommandExecutor
>                             }
>                             return strBuff.toString().trim();
>                         }
> -                        
> +
> +                        public String[] getEscapedShellCommandline()
> +                        {
> +                            String[] scl = getShellCommandline();
> +                            for ( int i = 0; i < scl.length; i++ ) {
> +                                scl[i] = escapeCmdParams( scl[i] );
> +                            }
> +                            return scl;
> +                        }
> +
>                         // escaped to make use of dotnet style of command 
> escapes .
>                         // Eg. 
> /define:"CONFIG=\"Debug\",DEBUG=-1,TRACE=-1,_MyType=\"Windows\",PLATFORM=\"AnyCPU\""
>                         private String escapeCmdParams(String param)
>                         {
>                             if(param == null)
>                                 return null;
> -                            
> +
>                             String str = param;
>                             if(param.startsWith("/") && param.indexOf(":") > 
> 0)
>                             {
>                                 int delem = param.indexOf(":") + 1;
>                                 String command = param.substring(0, delem);
>                                 String value = param.substring(delem);
> -                                
> +
>                                 if(value.indexOf(" ") > 0 || 
> value.indexOf("\"") > 0)
>                                 {
>                                     value = "\"" + value.replaceAll("\"", 
> "\\\\\"")  + "\"";
>                                 }
> -                                
> +
>                                 str = command + value;
>                             }
>                             else if(param.startsWith("@"))
>                             {
> -                             str = param;
> +                                str = param;
>                             }
> -                            else if(param.indexOf(" ") > 0)
> +                            else if(param.indexOf(" ") > 0 && !( 
> param.startsWith( "\"" ) &&  param.endsWith( "\"" ) ) )
>                             {
>                                 str = "\"" + param  + "\"";
>                             }
> -                            
> +
>                             return str;
>                         }
> -
> -
> -
> -
> -                    
> -                    
>                     };
> -                    
> -                    
> -                    
> +
>                     commandline.setExecutable( executable );
>                     commandline.addArguments( commands.toArray( new 
> String[commands.size()]));
> +
>                     if ( workingDirectory != null && 
> workingDirectory.exists() )
>                     {
>                         commandline.setWorkingDirectory( 
> workingDirectory.getAbsolutePath() );
> 
> Modified: 
> incubator/npanday/trunk/components/dotnet-executable/src/main/java/npanday/executable/compiler/impl/DefaultCompiler.java
> URL: 
> http://svn.apache.org/viewvc/incubator/npanday/trunk/components/dotnet-executable/src/main/java/npanday/executable/compiler/impl/DefaultCompiler.java?rev=1035948&r1=1035947&r2=1035948&view=diff
> ==============================================================================
> --- 
> incubator/npanday/trunk/components/dotnet-executable/src/main/java/npanday/executable/compiler/impl/DefaultCompiler.java
>  (original)
> +++ 
> incubator/npanday/trunk/components/dotnet-executable/src/main/java/npanday/executable/compiler/impl/DefaultCompiler.java
>  Wed Nov 17 08:28:12 2010
> @@ -291,7 +291,13 @@ public final class DefaultCompiler
>             throw new ExecutionException( "Error while creating response file 
> for the commands.", e );
>         }
>         filteredCommands.clear();
> -        filteredCommands.add("@" + escapeCmdParams(responseFilePath) );
> +        responseFilePath = "@" + responseFilePath;
> +        if ( responseFilePath.indexOf( " " ) > 0)
> +        {
> +            responseFilePath = "\"" + responseFilePath + "\"";
> +        }
> +
> +        filteredCommands.add( responseFilePath );
> 
>         return filteredCommands;
>     }
> 
> Added: 
> incubator/npanday/trunk/components/dotnet-executable/src/test/java/npanday/executable/CommandExecutorTest.java
> URL: 
> http://svn.apache.org/viewvc/incubator/npanday/trunk/components/dotnet-executable/src/test/java/npanday/executable/CommandExecutorTest.java?rev=1035948&view=auto
> ==============================================================================
> --- 
> incubator/npanday/trunk/components/dotnet-executable/src/test/java/npanday/executable/CommandExecutorTest.java
>  (added)
> +++ 
> incubator/npanday/trunk/components/dotnet-executable/src/test/java/npanday/executable/CommandExecutorTest.java
>  Wed Nov 17 08:28:12 2010
> @@ -0,0 +1,60 @@
> +package npanday.executable;
> +
> +import org.junit.Test;
> +import static org.junit.Assert.*;
> +import java.io.File;
> +import java.io.FileInputStream;
> +import java.io.FileNotFoundException;
> +import java.io.IOException;
> +import java.util.List;
> +import java.util.ArrayList;
> +import java.util.Arrays;
> +
> +import npanday.executable.CommandExecutor;
> +
> +
> +public class CommandExecutorTest
> +    {
> +        private static final String MKDIR = "mkdir";
> +        private String parentPath;
> +        private List<String> params = new ArrayList<String>();
> +        private CommandExecutor cmdExecutor;
> +
> +        public CommandExecutorTest()
> +        {
> +            File f = new File( "test" );
> +            parentPath = System.getProperty( "user.dir" ) + File.separator + 
> "target" + File.separator + "test-resources";
> +            cmdExecutor = 
> CommandExecutor.Factory.createDefaultCommmandExecutor();
> +        }
> +
> +        @Test
> +        public void testParamWithNoSpaces()
> +            throws ExecutionException
> +        {
> +            String path = parentPath + File.separator + "sampledirectory";
> +
> +            params.clear();
> +            params.add(path);
> +
> +            cmdExecutor.executeCommand( MKDIR, params );
> +            File dir = new File( path );
> +
> +            assertTrue( dir.exists() );
> +        }
> +
> +        @Test
> +        public void testParamWithSpaces()
> +            throws ExecutionException
> +        { 
> +            String path = parentPath + File.separator + "sample directory";
> +
> +            params.clear(); 
> +            params.add(path);
> +
> +            cmdExecutor.executeCommand( MKDIR, params );
> +            File dir = new File( path );
> +
> +            assertTrue( dir.exists() );
> +         }
> +
> +    }
> \ No newline at end of file
> 
> 

--
Brett Porter
br...@apache.org
http://brettporter.wordpress.com/

Reply via email to