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