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