Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
Shell |
|
| 2.0;2 |
1 | package org.apache.maven.it.util.cli.shell; | |
2 | ||
3 | /* | |
4 | * Copyright 2001-2006 The Apache Software Foundation. | |
5 | * | |
6 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
7 | * you may not use this file except in compliance with the License. | |
8 | * You may obtain a copy of the License at | |
9 | * | |
10 | * http://www.apache.org/licenses/LICENSE-2.0 | |
11 | * | |
12 | * Unless required by applicable law or agreed to in writing, software | |
13 | * distributed under the License is distributed on an "AS IS" BASIS, | |
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
15 | * See the License for the specific language governing permissions and | |
16 | * limitations under the License. | |
17 | */ | |
18 | ||
19 | import org.apache.maven.it.util.cli.CommandLineException; | |
20 | import org.apache.maven.it.util.cli.Commandline; | |
21 | ||
22 | import java.util.ArrayList; | |
23 | import java.util.Arrays; | |
24 | import java.util.List; | |
25 | ||
26 | /** | |
27 | * <p> | |
28 | * Class that abstracts the Shell functionality, | |
29 | * with subclases for shells that behave particularly, like | |
30 | * <ul> | |
31 | * <li><code>command.com</code></li> | |
32 | * <li><code>cmd.exe</code></li> | |
33 | * </ul> | |
34 | * </p> | |
35 | * | |
36 | * @author <a href="mailto:carlos@apache.org">Carlos Sanchez</a> | |
37 | * @since 1.2 | |
38 | */ | |
39 | 0 | public class Shell |
40 | { | |
41 | private String shellCommand; | |
42 | ||
43 | private String[] shellArgs; | |
44 | ||
45 | /** | |
46 | * Set the command to execute the shell (eg. COMMAND.COM, /bin/bash,...) | |
47 | * | |
48 | * @param shellCommand | |
49 | */ | |
50 | public void setShellCommand( String shellCommand ) | |
51 | { | |
52 | 0 | this.shellCommand = shellCommand; |
53 | 0 | } |
54 | ||
55 | /** | |
56 | * Get the command to execute the shell | |
57 | * | |
58 | * @return | |
59 | */ | |
60 | public String getShellCommand() | |
61 | { | |
62 | 0 | return shellCommand; |
63 | } | |
64 | ||
65 | /** | |
66 | * Set the shell arguments when calling a command line (not the executable arguments) | |
67 | * (eg. /X /C for CMD.EXE) | |
68 | * | |
69 | * @param shellArgs | |
70 | */ | |
71 | public void setShellArgs( String[] shellArgs ) | |
72 | { | |
73 | 0 | this.shellArgs = shellArgs; |
74 | 0 | } |
75 | ||
76 | /** | |
77 | * Get the shell arguments | |
78 | * | |
79 | * @return | |
80 | */ | |
81 | public String[] getShellArgs() | |
82 | { | |
83 | 0 | return shellArgs; |
84 | } | |
85 | ||
86 | /** | |
87 | * Get the command line for the provided executable and arguments in this shell | |
88 | * | |
89 | * @param executable executable that the shell has to call | |
90 | * @param arguments arguments for the executable, not the shell | |
91 | * @return List with one String object with executable and arguments quoted as needed | |
92 | */ | |
93 | public List getCommandLine( String executable, String[] arguments ) | |
94 | { | |
95 | ||
96 | 0 | List commandLine = new ArrayList(); |
97 | try | |
98 | { | |
99 | 0 | StringBuffer sb = new StringBuffer(); |
100 | ||
101 | 0 | if ( executable != null ) |
102 | { | |
103 | 0 | sb.append( Commandline.quoteArgument( executable ) ); |
104 | } | |
105 | 0 | for ( int i = 0; i < arguments.length; i++ ) |
106 | { | |
107 | 0 | sb.append( " " ); |
108 | 0 | sb.append( Commandline.quoteArgument( arguments[i] ) ); |
109 | } | |
110 | ||
111 | 0 | commandLine.add( sb.toString() ); |
112 | } | |
113 | 0 | catch ( CommandLineException e ) |
114 | { | |
115 | 0 | throw new RuntimeException( e ); |
116 | 0 | } |
117 | ||
118 | 0 | return commandLine; |
119 | } | |
120 | ||
121 | /** | |
122 | * Get the full command line to execute, including shell command, shell arguments, | |
123 | * executable and executable arguments | |
124 | * | |
125 | * @param executable executable that the shell has to call | |
126 | * @param arguments arguments for the executable, not the shell | |
127 | * @return List of String objects, whose array version is suitable to be used as argument | |
128 | * of Runtime.getRuntime().exec() | |
129 | */ | |
130 | public List getShellCommandLine( String executable, String[] arguments ) | |
131 | { | |
132 | ||
133 | 0 | List commandLine = new ArrayList(); |
134 | ||
135 | 0 | if ( getShellCommand() != null ) |
136 | { | |
137 | 0 | commandLine.add( getShellCommand() ); |
138 | } | |
139 | ||
140 | 0 | if ( getShellArgs() != null ) |
141 | { | |
142 | 0 | commandLine.addAll( Arrays.asList( getShellArgs() ) ); |
143 | } | |
144 | ||
145 | 0 | commandLine.addAll( getCommandLine( executable, arguments ) ); |
146 | ||
147 | 0 | return commandLine; |
148 | ||
149 | } | |
150 | ||
151 | } |