1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.shared.utils.cli;
20
21 import java.io.File;
22 import java.io.IOException;
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.Collections;
26 import java.util.LinkedHashMap;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Properties;
30 import java.util.Vector;
31
32 import org.apache.maven.shared.utils.Os;
33 import org.apache.maven.shared.utils.StringUtils;
34 import org.apache.maven.shared.utils.cli.shell.BourneShell;
35 import org.apache.maven.shared.utils.cli.shell.CmdShell;
36 import org.apache.maven.shared.utils.cli.shell.Shell;
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64 public class Commandline implements Cloneable {
65 private final List<Arg> arguments = new Vector<>();
66
67 private final Map<String, String> envVars = Collections.synchronizedMap(new LinkedHashMap<String, String>());
68
69 private Shell shell;
70
71 private boolean shellEnvironmentInherited = true;
72
73
74
75
76
77
78
79 public Commandline(Shell shell) {
80 this.shell = shell;
81 }
82
83
84
85
86
87
88
89
90 public Commandline(String toProcess) throws CommandLineException {
91 setDefaultShell();
92 String[] tmp = CommandLineUtils.translateCommandline(toProcess);
93 if ((tmp.length > 0)) {
94 setExecutable(tmp[0]);
95 for (int i = 1; i < tmp.length; i++) {
96 createArg().setValue(tmp[i]);
97 }
98 }
99 }
100
101
102
103
104
105 public Commandline() {
106 setDefaultShell();
107 }
108
109
110
111
112
113 private void setDefaultShell() {
114
115 if (Os.isFamily(Os.FAMILY_WINDOWS)) {
116 setShell(new CmdShell());
117 } else {
118 setShell(new BourneShell());
119 }
120 }
121
122
123
124
125
126
127 public Arg createArg() {
128 return this.createArg(false);
129 }
130
131
132
133
134
135
136
137
138 public Arg createArg(boolean insertAtStart) {
139 Arg argument = new Argument();
140 if (insertAtStart) {
141 arguments.add(0, argument);
142 } else {
143 arguments.add(argument);
144 }
145 return argument;
146 }
147
148
149
150
151
152
153 public void setExecutable(String executable) {
154 shell.setExecutable(executable);
155 }
156
157
158
159
160 public String getExecutable() {
161
162 return shell.getExecutable();
163 }
164
165
166
167
168 public void addArguments(String... line) {
169 for (String aLine : line) {
170 createArg().setValue(aLine);
171 }
172 }
173
174
175
176
177
178
179
180 public void addEnvironment(String name, String value) {
181 envVars.put(name, value);
182 }
183
184
185
186
187
188
189 @Deprecated
190 public void addSystemEnvironment() {}
191
192 private void copySystemEnvironment() {
193 Properties systemEnvVars = CommandLineUtils.getSystemEnvVars();
194
195 for (Object o : systemEnvVars.keySet()) {
196 String key = (String) o;
197 if (!envVars.containsKey(key)) {
198 addEnvironment(key, systemEnvVars.getProperty(key));
199 }
200 }
201 }
202
203
204
205
206
207
208 public String[] getEnvironmentVariables() {
209 if (isShellEnvironmentInherited()) {
210 copySystemEnvironment();
211 }
212
213 List<String> environmentVars = new ArrayList<>();
214 for (String name : envVars.keySet()) {
215 String value = envVars.get(name);
216 if (value != null) {
217 environmentVars.add(name + "=" + value);
218 }
219 }
220 return environmentVars.toArray(new String[0]);
221 }
222
223
224
225
226
227
228 public String[] getCommandline() {
229 final String[] args = getArguments();
230 String executable = getExecutable();
231
232 if (executable == null) {
233 return args;
234 }
235 final String[] result = new String[args.length + 1];
236 result[0] = executable;
237 System.arraycopy(args, 0, result, 1, args.length);
238 return result;
239 }
240
241
242
243
244 private String[] getShellCommandline() {
245 return getShellCommandline(false);
246 }
247
248
249
250
251
252
253 private String[] getShellCommandline(boolean mask) {
254 List<String> shellCommandLine = getShell().getShellCommandLine(getArguments(mask));
255 return shellCommandLine.toArray(new String[shellCommandLine.size()]);
256 }
257
258
259
260
261
262
263 public String[] getArguments() {
264 return getArguments(false);
265 }
266
267
268
269
270
271
272
273
274 public String[] getArguments(boolean mask) {
275 List<String> result = new ArrayList<>(arguments.size() * 2);
276 for (Arg argument : arguments) {
277 Argument arg = (Argument) argument;
278 String[] s = arg.getParts();
279 if (s != null) {
280 if (mask && (arg.isMask())) {
281
282 if (s.length > 0) {
283
284
285 String[] copy = new String[s.length];
286 Arrays.fill(copy, "*****");
287 s = copy;
288 }
289 }
290 Collections.addAll(result, s);
291 }
292 }
293
294 return result.toArray(new String[result.size()]);
295 }
296
297
298
299 public String toString() {
300 return StringUtils.join(getShellCommandline(true), " ");
301 }
302
303
304
305 public Object clone() {
306 throw new RuntimeException("Do we ever clone a commandline?");
307
308
309
310 }
311
312
313
314
315
316
317 public void setWorkingDirectory(String path) {
318 shell.setWorkingDirectory(path);
319 }
320
321
322
323
324
325
326 public void setWorkingDirectory(File workingDirectory) {
327 shell.setWorkingDirectory(workingDirectory);
328 }
329
330
331
332
333 public File getWorkingDirectory() {
334 return shell.getWorkingDirectory();
335 }
336
337
338
339
340 public void clearArgs() {
341 arguments.clear();
342 }
343
344
345
346
347
348
349
350
351 public boolean isShellEnvironmentInherited() {
352 return shellEnvironmentInherited;
353 }
354
355
356
357
358
359
360
361 public void setShellEnvironmentInherited(boolean shellEnvironmentInherited) {
362 this.shellEnvironmentInherited = shellEnvironmentInherited;
363 }
364
365
366
367
368
369
370
371 public Process execute() throws CommandLineException {
372 Process process;
373
374 String[] environment = getEnvironmentVariables();
375
376 File workingDir = shell.getWorkingDirectory();
377
378 try {
379 if (workingDir == null) {
380 process = Runtime.getRuntime().exec(getShellCommandline(), environment);
381 } else {
382 if (!workingDir.exists()) {
383 throw new CommandLineException(
384 "Working directory \"" + workingDir.getPath() + "\" does not exist!");
385 } else if (!workingDir.isDirectory()) {
386 throw new CommandLineException(
387 "Path \"" + workingDir.getPath() + "\" does not specify a directory.");
388 }
389
390 process = Runtime.getRuntime().exec(getShellCommandline(), environment, workingDir);
391 }
392 } catch (IOException ex) {
393 throw new CommandLineException("Error while executing process.", ex);
394 }
395
396 return process;
397 }
398
399
400
401
402
403
404 void setShell(Shell shell) {
405 this.shell = shell;
406 }
407
408
409
410
411
412
413 public Shell getShell() {
414 return shell;
415 }
416
417
418
419
420 public static class Argument implements Arg {
421 private String[] parts;
422
423 private boolean mask;
424
425
426
427
428 public void setValue(String value) {
429 if (value != null) {
430 parts = new String[] {value};
431 }
432 }
433
434
435
436
437 public void setLine(String line) throws CommandLineException {
438 if (line == null) {
439 return;
440 }
441 parts = CommandLineUtils.translateCommandline(line);
442 }
443
444
445
446
447 public void setFile(File value) {
448 parts = new String[] {value.getAbsolutePath()};
449 }
450
451
452
453
454 public void setMask(boolean mask) {
455 this.mask = mask;
456 }
457
458
459
460
461 private String[] getParts() {
462 return parts;
463 }
464
465
466
467
468 public boolean isMask() {
469 return mask;
470 }
471 }
472 }