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