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