View Javadoc
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  import java.util.Arrays;
23  import java.util.List;
24  
25  /**
26   * <p>
27   * Implementation to call the CMD Shell present on Windows NT, 2000 and XP
28   * </p>
29   *
30   * @author <a href="mailto:carlos@apache.org">Carlos Sanchez</a>
31   * 
32   */
33  public class CmdShell
34      extends Shell
35  {
36      /**
37       * Create an instance of CmdShell.
38       */
39      public CmdShell()
40      {
41          setShellCommand( "cmd.exe" );
42          setQuotedExecutableEnabled( true );
43          setShellArgs( new String[]{ "/X", "/C" } );
44      }
45  
46      /**
47       * <p>
48       * Specific implementation that quotes all the command line.
49       * </p>
50       * <p>
51       * Workaround for http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6468220
52       * </p>
53       * <p>
54       * From cmd.exe /? output:
55       * </p>
56       * <p/>
57       * <pre>
58       *      If /C or /K is specified, then the remainder of the command line after
59       *      the switch is processed as a command line, where the following logic is
60       *      used to process quote (&quot;) characters:
61       *
62       *      1.  If all of the following conditions are met, then quote characters
63       *      on the command line are preserved:
64       *
65       *      - no /S switch
66       *      - exactly two quote characters
67       *      - no special characters between the two quote characters,
68       *      where special is one of: &amp;&lt;&gt;()@&circ;|
69       *      - there are one or more whitespace characters between the
70       *      the two quote characters
71       *      - the string between the two quote characters is the name
72       *      of an executable file.
73       *
74       *      2.  Otherwise, old behavior is to see if the first character is
75       *      a quote character and if so, strip the leading character and
76       *      remove the last quote character on the command line, preserving
77       *      any text after the last quote character.
78       * </pre>
79       * <p/>
80       * <p>
81       * Always quoting the entire command line, regardless of these conditions
82       * appears to make Windows processes invoke successfully.
83       * </p>
84       * 
85       * @param executable The executable.
86       * @param arguments The arguments for the executable.
87       * @return The resulting command line.
88       */
89      public List<String> getCommandLine( String executable, String... arguments )
90      {
91          StringBuilder sb = new StringBuilder();
92          sb.append( '"' );
93          sb.append( super.getCommandLine( executable, arguments ).get( 0 ) );
94          sb.append( '"' );
95  
96          return Arrays.asList( sb.toString() );
97      }
98  
99  }