1 package org.apache.maven.plugin.cxx.utils;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 import java.io.File;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.OutputStream;
25
26 import java.util.Locale;
27 import java.util.Properties;
28
29 import org.codehaus.plexus.util.cli.CommandLineUtils;
30
31 import org.apache.commons.exec.CommandLine;
32 import org.apache.commons.exec.ExecuteException;
33 import org.apache.commons.exec.Executor;
34 import org.apache.commons.exec.OS;
35 import org.apache.commons.exec.PumpStreamHandler;
36 import org.codehaus.plexus.util.StringUtils;
37
38
39
40
41 public class ExecutorService
42 {
43 public static Properties getSystemEnvVars() throws IOException
44 {
45 return CommandLineUtils.getSystemEnvVars();
46 }
47
48 public static CommandLine getExecutablePath( String executableName, Properties enviro, File dir )
49 {
50 File execFile = new File( executableName );
51 String exec = null;
52 if ( execFile.exists() && execFile.isFile() && execFile.canExecute() )
53 {
54
55 exec = execFile.getAbsolutePath();
56 }
57 else
58 {
59 if ( OS.isFamilyWindows() )
60 {
61 String ex = executableName.indexOf( "." ) < 0 ? executableName + ".bat" : executableName;
62 File f = new File( dir, ex );
63 if ( f.exists() )
64 {
65 exec = ex;
66 }
67 else
68 {
69
70
71 String path = (String) enviro.get( "PATH" );
72 if ( path != null )
73 {
74 String[] elems = StringUtils.split( path, File.pathSeparator );
75 for ( int i = 0; i < elems.length; i++ )
76 {
77 f = new File( new File( elems[i] ), ex );
78 if ( f.exists() )
79 {
80 exec = ex;
81 break;
82 }
83 }
84 }
85 }
86 }
87 }
88
89 if ( exec == null )
90 {
91 exec = executableName;
92 }
93
94 CommandLine toRet;
95 if ( OS.isFamilyWindows() && exec.toLowerCase( Locale.getDefault() ).endsWith( ".bat" ) )
96 {
97 toRet = new CommandLine( "cmd" );
98 toRet.addArgument( "/c" );
99 toRet.addArgument( exec );
100 }
101 else
102 {
103 toRet = new CommandLine( exec );
104 }
105
106 return toRet;
107 }
108
109 public static int executeCommandLine( Executor exec, CommandLine commandLine, Properties enviro, OutputStream out,
110 OutputStream err, InputStream in ) throws ExecuteException, IOException
111 {
112 exec.setExitValues( null );
113 exec.setStreamHandler( new PumpStreamHandler( out, err, in ) );
114 return exec.execute( commandLine, enviro );
115 }
116
117 }