View Javadoc

1   package org.apache.maven.surefire.booter.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  import org.apache.maven.surefire.util.NestedRuntimeException;
23  import org.codehaus.plexus.util.cli.CommandLineException;
24  import org.codehaus.plexus.util.cli.Commandline;
25  
26  import java.util.ArrayList;
27  import java.util.Arrays;
28  import java.util.List;
29  
30  /**
31   * <p/>
32   * Class with patches copied from plexus-utils with fix for PLX-161,
33   * as we can not upgrade plexus-utils until it's upgraded in core Maven
34   * </p>
35   * <p/>
36   * TODO deprecate when plexus-utils 1.2 can be used
37   *
38   * @author <a href="mailto:carlos@apache.org">Carlos Sanchez</a>
39   */
40  public class Shell
41  {
42      private String shellCommand;
43  
44      private String[] shellArgs;
45  
46      /**
47       * Set the command to execute the shell (eg. COMMAND.COM, /bin/bash,...)
48       *
49       * @param shellCommand
50       */
51      public void setShellCommand( String shellCommand )
52      {
53          this.shellCommand = shellCommand;
54      }
55  
56      /**
57       * Get the command to execute the shell
58       *
59       * @return
60       */
61      public String getShellCommand()
62      {
63          return shellCommand;
64      }
65  
66      /**
67       * Set the shell arguments when calling a command line (not the executable arguments)
68       * (eg. /X /C for CMD.EXE)
69       *
70       * @param shellArgs
71       */
72      public void setShellArgs( String[] shellArgs )
73      {
74          this.shellArgs = shellArgs;
75      }
76  
77      /**
78       * Get the shell arguments
79       *
80       * @return
81       */
82      public String[] getShellArgs()
83      {
84          return shellArgs;
85      }
86  
87      /**
88       * Get the command line for the provided executable and arguments in this shell
89       *
90       * @param executable executable that the shell has to call
91       * @param arguments  arguments for the executable, not the shell
92       * @return List with one String object with executable and arguments quoted as needed
93       */
94      public List getCommandLine( String executable, String[] arguments )
95      {
96  
97          List commandLine = new ArrayList();
98          try
99          {
100             StringBuffer sb = new StringBuffer();
101 
102             if ( executable != null )
103             {
104                 sb.append( Commandline.quoteArgument( executable ) );
105             }
106             for ( int i = 0; i < arguments.length; i++ )
107             {
108                 sb.append( " " );
109                 sb.append( Commandline.quoteArgument( arguments[i] ) );
110             }
111 
112             commandLine.add( sb.toString() );
113         }
114         catch ( CommandLineException e )
115         {
116             throw new NestedRuntimeException( e );
117         }
118 
119         return commandLine;
120     }
121 
122     /**
123      * Get the full command line to execute, including shell command, shell arguments,
124      * executable and executable arguments
125      *
126      * @param executable executable that the shell has to call
127      * @param arguments  arguments for the executable, not the shell
128      * @return List of String objects, whose array version is suitable to be used as argument
129      *         of Runtime.getRuntime().exec()
130      */
131     public List getShellCommandLine( String executable, String[] arguments )
132     {
133 
134         List commandLine = new ArrayList();
135 
136         if ( getShellCommand() != null )
137         {
138             commandLine.add( getShellCommand() );
139         }
140 
141         if ( getShellArgs() != null )
142         {
143             commandLine.addAll( Arrays.asList( getShellArgs() ) );
144         }
145 
146         commandLine.addAll( getCommandLine( executable, arguments ) );
147 
148         return commandLine;
149 
150     }
151 
152 }