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