Coverage Report - org.apache.maven.shared.utils.cli.shell.Shell
 
Classes in this File Line Coverage Branch Coverage Complexity
Shell
89%
82/92
61%
21/34
1.625
 
 1  
 package org.apache.maven.shared.utils.cli.shell;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  *   http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 
 23  
 import java.io.File;
 24  
 import java.util.ArrayList;
 25  
 import java.util.Arrays;
 26  
 import java.util.List;
 27  
 import org.apache.maven.shared.utils.StringUtils;
 28  
 
 29  
 /**
 30  
  * <p>
 31  
  * Class that abstracts the Shell functionality,
 32  
  * with subclases for shells that behave particularly, like
 33  
  * <ul>
 34  
  * <li><code>command.com</code></li>
 35  
  * <li><code>cmd.exe</code></li>
 36  
  * </ul>
 37  
  * </p>
 38  
  *
 39  
  * @author <a href="mailto:carlos@apache.org">Carlos Sanchez</a>
 40  
  * 
 41  
  */
 42  10
 public class Shell
 43  
     implements Cloneable
 44  
 {
 45  1
     private static final char[] DEFAULT_QUOTING_TRIGGER_CHARS = { ' ' };
 46  
 
 47  
     private String shellCommand;
 48  
 
 49  10
     private final List<String> shellArgs = new ArrayList<String>();
 50  
 
 51  10
     private boolean quotedArgumentsEnabled = true;
 52  
 
 53  
     private String executable;
 54  
 
 55  
     private String workingDir;
 56  
 
 57  10
     private boolean quotedExecutableEnabled = true;
 58  
 
 59  10
     private boolean singleQuotedArgumentEscaped = false;
 60  
 
 61  10
     private boolean singleQuotedExecutableEscaped = false;
 62  
 
 63  10
     private char argQuoteDelimiter = '\"';
 64  
 
 65  10
     private char exeQuoteDelimiter = '\"';
 66  
 
 67  
     /**
 68  
      * Set the command to execute the shell (eg. COMMAND.COM, /bin/bash,...)
 69  
      *
 70  
      * @param shellCommand The command
 71  
      */
 72  
     void setShellCommand( String shellCommand )
 73  
     {
 74  10
         this.shellCommand = shellCommand;
 75  10
     }
 76  
 
 77  
     /**
 78  
      * Get the command to execute the shell
 79  
      *
 80  
      * @return  The command
 81  
      */
 82  
     String getShellCommand()
 83  
     {
 84  20
         return shellCommand;
 85  
     }
 86  
 
 87  
     /**
 88  
      * Set the shell arguments when calling a command line (not the executable arguments)
 89  
      * (eg. /X /C for CMD.EXE)
 90  
      *
 91  
      * @param shellArgs the arguments to the shell
 92  
      */
 93  
     void setShellArgs( String[] shellArgs )
 94  
     {
 95  1
         this.shellArgs.clear();
 96  1
         this.shellArgs.addAll( Arrays.asList( shellArgs ) );
 97  1
     }
 98  
 
 99  
     /**
 100  
      * Get the shell arguments
 101  
      *
 102  
      * @return  The arguments
 103  
      */
 104  
     String[] getShellArgs()
 105  
     {
 106  10
         if ( ( shellArgs == null ) || shellArgs.isEmpty() )
 107  
         {
 108  9
             return null;
 109  
         }
 110  
         else
 111  
         {
 112  1
             return shellArgs.toArray( new String[shellArgs.size()] );
 113  
         }
 114  
     }
 115  
 
 116  
     /**
 117  
      * Get the command line for the provided executable and arguments in this shell
 118  
      *
 119  
      * @param executable executable that the shell has to call
 120  
      * @param arguments  arguments for the executable, not the shell
 121  
      * @return List with one String object with executable and arguments quoted as needed
 122  
      */
 123  
     List<String> getCommandLine( String executable, String... arguments )
 124  
     {
 125  10
         return getRawCommandLine( executable, arguments );
 126  
     }
 127  
 
 128  
     List<String> getRawCommandLine( String executable, String... arguments )
 129  
     {
 130  10
         List<String> commandLine = new ArrayList<String>();
 131  10
         StringBuilder sb = new StringBuilder();
 132  
 
 133  10
         if ( executable != null )
 134  
         {
 135  9
             String preamble = getExecutionPreamble();
 136  9
             if ( preamble != null )
 137  
             {
 138  6
                 sb.append( preamble );
 139  
             }
 140  
 
 141  9
             if ( isQuotedExecutableEnabled() )
 142  
             {
 143  9
                 char[] escapeChars =
 144  
                     getEscapeChars( isSingleQuotedExecutableEscaped(), isDoubleQuotedExecutableEscaped() );
 145  
 
 146  9
                 sb.append( StringUtils.quoteAndEscape( getExecutable(), getExecutableQuoteDelimiter(), escapeChars,
 147  
                                                        getQuotingTriggerChars(), '\\', false ) );
 148  9
             }
 149  
             else
 150  
             {
 151  0
                 sb.append( getExecutable() );
 152  
             }
 153  
         }
 154  38
         for ( String argument : arguments )
 155  
         {
 156  28
             if ( sb.length() > 0 )
 157  
             {
 158  27
                 sb.append( ' ' );
 159  
             }
 160  
 
 161  28
             if ( isQuotedArgumentsEnabled() )
 162  
             {
 163  28
                 char[] escapeChars = getEscapeChars( isSingleQuotedArgumentEscaped(), isDoubleQuotedArgumentEscaped() );
 164  
 
 165  28
                 sb.append( StringUtils.quoteAndEscape( argument, getArgumentQuoteDelimiter(), escapeChars,
 166  
                                                        getQuotingTriggerChars(), '\\', false ) );
 167  28
             }
 168  
             else
 169  
             {
 170  0
                 sb.append( argument );
 171  
             }
 172  
         }
 173  
 
 174  10
         commandLine.add( sb.toString() );
 175  
 
 176  10
         return commandLine;
 177  
     }
 178  
 
 179  
     char[] getQuotingTriggerChars()
 180  
     {
 181  2
         return DEFAULT_QUOTING_TRIGGER_CHARS;
 182  
     }
 183  
 
 184  
     String getExecutionPreamble()
 185  
     {
 186  0
         return null;
 187  
     }
 188  
 
 189  
     char[] getEscapeChars( boolean includeSingleQuote, boolean includeDoubleQuote )
 190  
     {
 191  37
         StringBuilder buf = new StringBuilder( 2 );
 192  37
         if ( includeSingleQuote )
 193  
         {
 194  26
             buf.append( '\'' );
 195  
         }
 196  
 
 197  37
         if ( includeDoubleQuote )
 198  
         {
 199  0
             buf.append( '\"' );
 200  
         }
 201  
 
 202  37
         char[] result = new char[buf.length()];
 203  37
         buf.getChars( 0, buf.length(), result, 0 );
 204  
 
 205  37
         return result;
 206  
     }
 207  
 
 208  
     protected boolean isDoubleQuotedArgumentEscaped()
 209  
     {
 210  28
         return false;
 211  
     }
 212  
 
 213  
     protected boolean isSingleQuotedArgumentEscaped()
 214  
     {
 215  28
         return singleQuotedArgumentEscaped;
 216  
     }
 217  
 
 218  
     boolean isDoubleQuotedExecutableEscaped()
 219  
     {
 220  9
         return false;
 221  
     }
 222  
 
 223  
     boolean isSingleQuotedExecutableEscaped()
 224  
     {
 225  9
         return singleQuotedExecutableEscaped;
 226  
     }
 227  
 
 228  
     void setArgumentQuoteDelimiter( char argQuoteDelimiter )
 229  
     {
 230  9
         this.argQuoteDelimiter = argQuoteDelimiter;
 231  9
     }
 232  
 
 233  
     char getArgumentQuoteDelimiter()
 234  
     {
 235  28
         return argQuoteDelimiter;
 236  
     }
 237  
 
 238  
     void setExecutableQuoteDelimiter( char exeQuoteDelimiter )
 239  
     {
 240  9
         this.exeQuoteDelimiter = exeQuoteDelimiter;
 241  9
     }
 242  
 
 243  
     char getExecutableQuoteDelimiter()
 244  
     {
 245  9
         return exeQuoteDelimiter;
 246  
     }
 247  
 
 248  
     /**
 249  
      * Get the full command line to execute, including shell command, shell arguments,
 250  
      * executable and executable arguments
 251  
      *
 252  
      * @param arguments arguments for the executable, not the shell
 253  
      * @return List of String objects, whose array version is suitable to be used as argument
 254  
      *         of Runtime.getRuntime().exec()
 255  
      */
 256  
     public List<String> getShellCommandLine( String... arguments )
 257  
     {
 258  
 
 259  10
         List<String> commandLine = new ArrayList<String>();
 260  
 
 261  10
         if ( getShellCommand() != null )
 262  
         {
 263  10
             commandLine.add( getShellCommand() );
 264  
         }
 265  
 
 266  10
         if ( getShellArgs() != null )
 267  
         {
 268  10
             commandLine.addAll( getShellArgsList() );
 269  
         }
 270  
 
 271  10
         commandLine.addAll( getCommandLine( getExecutable(), arguments ) );
 272  
 
 273  10
         return commandLine;
 274  
 
 275  
     }
 276  
 
 277  
     List<String> getShellArgsList()
 278  
     {
 279  10
         return shellArgs;
 280  
     }
 281  
 
 282  
     public void setQuotedArgumentsEnabled( boolean quotedArgumentsEnabled )
 283  
     {
 284  4
         this.quotedArgumentsEnabled = quotedArgumentsEnabled;
 285  4
     }
 286  
 
 287  
     boolean isQuotedArgumentsEnabled()
 288  
     {
 289  28
         return quotedArgumentsEnabled;
 290  
     }
 291  
 
 292  
     void setQuotedExecutableEnabled( boolean quotedExecutableEnabled )
 293  
     {
 294  10
         this.quotedExecutableEnabled = quotedExecutableEnabled;
 295  10
     }
 296  
 
 297  
     boolean isQuotedExecutableEnabled()
 298  
     {
 299  9
         return quotedExecutableEnabled;
 300  
     }
 301  
 
 302  
     /**
 303  
      * Sets the executable to run.
 304  
      */
 305  
     public void setExecutable( String executable )
 306  
     {
 307  9
         if ( ( executable == null ) || ( executable.length() == 0 ) )
 308  
         {
 309  0
             return;
 310  
         }
 311  9
         this.executable = executable.replace( '/', File.separatorChar ).replace( '\\', File.separatorChar );
 312  9
     }
 313  
 
 314  
     public String getExecutable()
 315  
     {
 316  19
         return executable;
 317  
     }
 318  
 
 319  
     /**
 320  
      * Sets execution directory.
 321  
      */
 322  
     public void setWorkingDirectory( String path )
 323  
     {
 324  6
         if ( path != null )
 325  
         {
 326  6
             workingDir = path;
 327  
         }
 328  6
     }
 329  
 
 330  
     /**
 331  
      * Sets execution directory.
 332  
      */
 333  
     public void setWorkingDirectory( File workingDir )
 334  
     {
 335  0
         if ( workingDir != null )
 336  
         {
 337  0
             this.workingDir = workingDir.getAbsolutePath();
 338  
         }
 339  0
     }
 340  
 
 341  
     public File getWorkingDirectory()
 342  
     {
 343  0
         return workingDir == null ? null : new File( workingDir );
 344  
     }
 345  
 
 346  
     String getWorkingDirectoryAsString()
 347  
     {
 348  15
         return workingDir;
 349  
     }
 350  
 
 351  
     public Object clone()
 352  
     {
 353  0
         throw new RuntimeException( "Do we ever clone this?" );
 354  
 /*        Shell shell = new Shell();
 355  
         shell.setExecutable( getExecutable() );
 356  
         shell.setWorkingDirectory( getWorkingDirectory() );
 357  
         shell.setShellArgs( getShellArgs() );
 358  
         return shell;*/
 359  
     }
 360  
 
 361  
     void setSingleQuotedArgumentEscaped( boolean singleQuotedArgumentEscaped )
 362  
     {
 363  9
         this.singleQuotedArgumentEscaped = singleQuotedArgumentEscaped;
 364  9
     }
 365  
 
 366  
     void setSingleQuotedExecutableEscaped( boolean singleQuotedExecutableEscaped )
 367  
     {
 368  9
         this.singleQuotedExecutableEscaped = singleQuotedExecutableEscaped;
 369  9
     }
 370  
 
 371  
 }