1 package org.codehaus.plexus.util.cli;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60 import java.io.File;
61 import java.io.IOException;
62 import java.util.Collections;
63 import java.util.LinkedHashMap;
64 import java.util.Map;
65 import java.util.Properties;
66 import java.util.Vector;
67
68 import org.codehaus.plexus.util.Os;
69 import org.codehaus.plexus.util.StringUtils;
70 import org.codehaus.plexus.util.cli.shell.BourneShell;
71 import org.codehaus.plexus.util.cli.shell.CmdShell;
72 import org.codehaus.plexus.util.cli.shell.CommandShell;
73 import org.codehaus.plexus.util.cli.shell.Shell;
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97 public class Commandline implements Cloneable {
98
99
100
101 @Deprecated
102 protected static final String OS_NAME = "os.name";
103
104
105
106
107 @Deprecated
108 protected static final String WINDOWS = "Windows";
109
110 protected Vector<Arg> arguments = new Vector<>();
111
112
113
114 protected Map<String, String> envVars = Collections.synchronizedMap(new LinkedHashMap<String, String>());
115
116 private long pid = -1;
117
118 private Shell shell;
119
120
121
122
123 @Deprecated
124 protected String executable;
125
126
127
128
129
130 @Deprecated
131 private File workingDir;
132
133
134
135
136
137
138
139
140 public Commandline(String toProcess, Shell shell) {
141 this.shell = shell;
142
143 String[] tmp = new String[0];
144 try {
145 tmp = CommandLineUtils.translateCommandline(toProcess);
146 } catch (Exception e) {
147 System.err.println("Error translating Commandline.");
148 }
149 if ((tmp != null) && (tmp.length > 0)) {
150 setExecutable(tmp[0]);
151 for (int i = 1; i < tmp.length; i++) {
152 createArgument().setValue(tmp[i]);
153 }
154 }
155 }
156
157
158
159
160
161
162 public Commandline(Shell shell) {
163 this.shell = shell;
164 }
165
166
167
168
169
170
171 public Commandline(String toProcess) {
172 setDefaultShell();
173 String[] tmp = new String[0];
174 try {
175 tmp = CommandLineUtils.translateCommandline(toProcess);
176 } catch (Exception e) {
177 System.err.println("Error translating Commandline.");
178 }
179 if ((tmp != null) && (tmp.length > 0)) {
180 setExecutable(tmp[0]);
181 for (int i = 1; i < tmp.length; i++) {
182 createArgument().setValue(tmp[i]);
183 }
184 }
185 }
186
187
188
189
190 public Commandline() {
191 setDefaultShell();
192 }
193
194 public long getPid() {
195 if (pid == -1) {
196 pid = Long.parseLong(String.valueOf(System.currentTimeMillis()));
197 }
198
199 return pid;
200 }
201
202 public void setPid(long pid) {
203 this.pid = pid;
204 }
205
206
207
208
209
210
211
212 public class Marker {
213
214 private int position;
215
216 private int realPos = -1;
217
218 Marker(int position) {
219 this.position = position;
220 }
221
222
223
224
225
226
227 public int getPosition() {
228 if (realPos == -1) {
229 realPos = (getLiteralExecutable() == null ? 0 : 1);
230 for (int i = 0; i < position; i++) {
231 Arg arg = arguments.elementAt(i);
232 realPos += arg.getParts().length;
233 }
234 }
235 return realPos;
236 }
237 }
238
239
240
241
242
243
244 private void setDefaultShell() {
245
246 if (Os.isFamily(Os.FAMILY_WINDOWS)) {
247 if (Os.isFamily(Os.FAMILY_WIN9X)) {
248 setShell(new CommandShell());
249 } else {
250 setShell(new CmdShell());
251 }
252 } else {
253 setShell(new BourneShell());
254 }
255 }
256
257
258
259
260
261
262
263
264
265
266
267 @Deprecated
268 public Argument createArgument() {
269 return this.createArgument(false);
270 }
271
272
273
274
275
276
277
278
279
280
281
282 @Deprecated
283 public Argument createArgument(boolean insertAtStart) {
284 Argument argument = new Argument();
285 if (insertAtStart) {
286 arguments.insertElementAt(argument, 0);
287 } else {
288 arguments.addElement(argument);
289 }
290 return argument;
291 }
292
293
294
295
296
297
298
299
300
301
302 public Arg createArg() {
303 return this.createArg(false);
304 }
305
306
307
308
309
310
311
312
313
314 public Arg createArg(boolean insertAtStart) {
315 Arg argument = new Argument();
316 if (insertAtStart) {
317 arguments.insertElementAt(argument, 0);
318 } else {
319 arguments.addElement(argument);
320 }
321 return argument;
322 }
323
324
325
326
327
328 public void addArg(Arg argument) {
329 this.addArg(argument, false);
330 }
331
332
333
334
335
336
337
338 public void addArg(Arg argument, boolean insertAtStart) {
339 if (insertAtStart) {
340 arguments.insertElementAt(argument, 0);
341 } else {
342 arguments.addElement(argument);
343 }
344 }
345
346
347
348
349
350 public void setExecutable(String executable) {
351 shell.setExecutable(executable);
352 this.executable = executable;
353 }
354
355
356
357
358 public String getLiteralExecutable() {
359 return executable;
360 }
361
362
363
364
365
366
367
368 public String getExecutable() {
369 String exec = shell.getExecutable();
370
371 if (exec == null) {
372 exec = executable;
373 }
374
375 return exec;
376 }
377
378 public void addArguments(String[] line) {
379 for (String aLine : line) {
380 createArgument().setValue(aLine);
381 }
382 }
383
384
385
386
387
388
389 public void addEnvironment(String name, String value) {
390
391 envVars.put(name, value);
392 }
393
394
395
396
397
398 public void addSystemEnvironment() throws Exception {
399 Properties systemEnvVars = CommandLineUtils.getSystemEnvVars();
400
401 for (Object o : systemEnvVars.keySet()) {
402 String key = (String) o;
403 if (!envVars.containsKey(key)) {
404 addEnvironment(key, systemEnvVars.getProperty(key));
405 }
406 }
407 }
408
409
410
411
412
413 public String[] getEnvironmentVariables() throws CommandLineException {
414 try {
415 addSystemEnvironment();
416 } catch (Exception e) {
417 throw new CommandLineException("Error setting up environmental variables", e);
418 }
419 String[] environmentVars = new String[envVars.size()];
420 int i = 0;
421 for (Object o : envVars.keySet()) {
422 String name = (String) o;
423 String value = envVars.get(name);
424 environmentVars[i] = name + "=" + value;
425 i++;
426 }
427 return environmentVars;
428 }
429
430
431
432
433
434 public String[] getCommandline() {
435 if (Os.isFamily(Os.FAMILY_WINDOWS)) {
436 return getShellCommandline();
437 }
438
439 return getRawCommandline();
440 }
441
442
443
444
445
446 public String[] getRawCommandline() {
447 final String[] args = getArguments();
448 String executable = getLiteralExecutable();
449
450 if (executable == null) {
451 return args;
452 }
453 final String[] result = new String[args.length + 1];
454 result[0] = executable;
455 System.arraycopy(args, 0, result, 1, args.length);
456 return result;
457 }
458
459
460
461
462
463
464 public String[] getShellCommandline() {
465
466 verifyShellState();
467
468 return getShell().getShellCommandLine(getArguments()).toArray(new String[0]);
469 }
470
471
472
473
474 public String[] getArguments() {
475 Vector<String> result = new Vector<>(arguments.size() * 2);
476 for (int i = 0; i < arguments.size(); i++) {
477 Arg arg = arguments.elementAt(i);
478 String[] s = arg.getParts();
479 if (s != null) {
480 for (String value : s) {
481 result.addElement(value);
482 }
483 }
484 }
485
486 String[] res = new String[result.size()];
487 result.copyInto(res);
488 return res;
489 }
490
491 @Override
492 public String toString() {
493 return StringUtils.join(getShellCommandline(), " ");
494 }
495
496 public int size() {
497 return getCommandline().length;
498 }
499
500 @Override
501 public Object clone() {
502 Commandline c = new Commandline((Shell) shell.clone());
503 c.executable = executable;
504 c.workingDir = workingDir;
505 c.addArguments(getArguments());
506 return c;
507 }
508
509
510
511
512 public void clear() {
513 executable = null;
514 workingDir = null;
515 shell.setExecutable(null);
516 shell.clearArguments();
517 arguments.removeAllElements();
518 }
519
520
521
522
523 public void clearArgs() {
524 arguments.removeAllElements();
525 }
526
527
528
529
530
531
532
533
534 public Marker createMarker() {
535 return new Marker(arguments.size());
536 }
537
538
539
540
541
542 public void setWorkingDirectory(String path) {
543 shell.setWorkingDirectory(path);
544 workingDir = new File(path);
545 }
546
547
548
549
550
551 public void setWorkingDirectory(File workingDirectory) {
552 shell.setWorkingDirectory(workingDirectory);
553 workingDir = workingDirectory;
554 }
555
556 public File getWorkingDirectory() {
557 File workDir = shell.getWorkingDirectory();
558
559 if (workDir == null) {
560 workDir = workingDir;
561 }
562
563 return workDir;
564 }
565
566
567
568
569
570
571 public Process execute() throws CommandLineException {
572
573 verifyShellState();
574
575 Process process;
576
577
578
579 String[] environment = getEnvironmentVariables();
580
581 File workingDir = shell.getWorkingDirectory();
582
583 try {
584 if (workingDir == null) {
585 process = Runtime.getRuntime().exec(getCommandline(), environment, workingDir);
586 } else {
587 if (!workingDir.exists()) {
588 throw new CommandLineException(
589 "Working directory \"" + workingDir.getPath() + "\" does not exist!");
590 } else if (!workingDir.isDirectory()) {
591 throw new CommandLineException(
592 "Path \"" + workingDir.getPath() + "\" does not specify a directory.");
593 }
594
595 process = Runtime.getRuntime().exec(getCommandline(), environment, workingDir);
596 }
597 } catch (IOException ex) {
598 throw new CommandLineException("Error while executing process.", ex);
599 }
600
601 return process;
602 }
603
604
605
606
607 @Deprecated
608 private void verifyShellState() {
609 if (shell.getWorkingDirectory() == null) {
610 shell.setWorkingDirectory(workingDir);
611 }
612
613 if (shell.getOriginalExecutable() == null) {
614 shell.setExecutable(executable);
615 }
616 }
617
618 public Properties getSystemEnvVars() throws Exception {
619 return CommandLineUtils.getSystemEnvVars();
620 }
621
622
623
624
625
626
627
628
629 public void setShell(Shell shell) {
630 this.shell = shell;
631 }
632
633
634
635
636
637
638
639
640 public Shell getShell() {
641 return shell;
642 }
643
644
645
646
647
648
649
650 @Deprecated
651 public static String[] translateCommandline(String toProcess) throws Exception {
652 return CommandLineUtils.translateCommandline(toProcess);
653 }
654
655
656
657
658
659
660
661 @Deprecated
662 public static String quoteArgument(String argument) throws CommandLineException {
663 return CommandLineUtils.quote(argument);
664 }
665
666
667
668
669
670
671 @Deprecated
672 public static String toString(String[] line) {
673 return CommandLineUtils.toString(line);
674 }
675
676 public static class Argument implements Arg {
677 private String[] parts;
678
679
680
681
682
683 @Override
684 public void setValue(String value) {
685 if (value != null) {
686 parts = new String[] {value};
687 }
688 }
689
690
691
692
693
694 @Override
695 public void setLine(String line) {
696 if (line == null) {
697 return;
698 }
699 try {
700 parts = CommandLineUtils.translateCommandline(line);
701 } catch (Exception e) {
702 System.err.println("Error translating Commandline.");
703 }
704 }
705
706
707
708
709
710 @Override
711 public void setFile(File value) {
712 parts = new String[] {value.getAbsolutePath()};
713 }
714
715
716
717
718
719 @Override
720 public String[] getParts() {
721 return parts;
722 }
723 }
724 }