View Javadoc
1   package org.apache.maven.plugin.cxx.utils;
2   
3   /*
4    * Copyright (C) 2011-2016, Neticoa SAS France - Tous droits réservés.
5    * Author(s) : Franck Bonin, Neticoa SAS France
6    *
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
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   * Executor service
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            //getLog().debug( "Toolchains are ignored, 'executable' parameter is set to " + execFile.getAbsolutePath() );
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                      // now try to figure the path from PATH, PATHEXT env vars
70                      // if bat file, wrap in cmd /c
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 ); // let us decide of exit value
113         exec.setStreamHandler( new PumpStreamHandler( out, err, in ) );
114         return exec.execute( commandLine, enviro );
115     }
116     
117 }