git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@270327 13f79535-47bb-0310-9956-ffa450edef68master
| @@ -9,7 +9,6 @@ package org.apache.tools.ant.taskdefs.exec; | |||||
| import java.io.File; | import java.io.File; | ||||
| import java.io.IOException; | import java.io.IOException; | ||||
| import java.util.ArrayList; | |||||
| import java.util.Locale; | import java.util.Locale; | ||||
| import java.util.Properties; | import java.util.Properties; | ||||
| import org.apache.avalon.excalibur.io.FileUtil; | import org.apache.avalon.excalibur.io.FileUtil; | ||||
| @@ -165,7 +164,8 @@ public class Execute | |||||
| * @param watchdog a watchdog for the subprocess or <code>null</code> to to | * @param watchdog a watchdog for the subprocess or <code>null</code> to to | ||||
| * disable a timeout for the subprocess. | * disable a timeout for the subprocess. | ||||
| */ | */ | ||||
| public Execute( ExecuteStreamHandler streamHandler, ExecuteWatchdog watchdog ) | |||||
| public Execute( final ExecuteStreamHandler streamHandler, | |||||
| final ExecuteWatchdog watchdog ) | |||||
| { | { | ||||
| m_streamHandler = streamHandler; | m_streamHandler = streamHandler; | ||||
| m_watchdog = watchdog; | m_watchdog = watchdog; | ||||
| @@ -179,15 +179,16 @@ public class Execute | |||||
| * @param cmdline The command to execute. | * @param cmdline The command to execute. | ||||
| * @throws TaskException if the command does not return 0. | * @throws TaskException if the command does not return 0. | ||||
| */ | */ | ||||
| public static void runCommand( Task task, String[] cmdline ) | |||||
| public static void runCommand( final Task task, final String[] cmdline ) | |||||
| throws TaskException | throws TaskException | ||||
| { | { | ||||
| try | try | ||||
| { | { | ||||
| task.log( Commandline.toString( cmdline ), Project.MSG_VERBOSE ); | task.log( Commandline.toString( cmdline ), Project.MSG_VERBOSE ); | ||||
| Execute exe = new Execute( new LogStreamHandler( task, | |||||
| Project.MSG_INFO, | |||||
| Project.MSG_ERR ) ); | |||||
| final Execute exe = | |||||
| new Execute( new LogStreamHandler( task, | |||||
| Project.MSG_INFO, | |||||
| Project.MSG_ERR ) ); | |||||
| exe.setCommandline( cmdline ); | exe.setCommandline( cmdline ); | ||||
| int retval = exe.execute(); | int retval = exe.execute(); | ||||
| if( retval != 0 ) | if( retval != 0 ) | ||||
| @@ -195,9 +196,9 @@ public class Execute | |||||
| throw new TaskException( cmdline[ 0 ] + " failed with return code " + retval ); | throw new TaskException( cmdline[ 0 ] + " failed with return code " + retval ); | ||||
| } | } | ||||
| } | } | ||||
| catch( IOException exc ) | |||||
| catch( final IOException ioe ) | |||||
| { | { | ||||
| throw new TaskException( "Could not launch " + cmdline[ 0 ] + ": " + exc ); | |||||
| throw new TaskException( "Could not launch " + cmdline[ 0 ] + ": " + ioe ); | |||||
| } | } | ||||
| } | } | ||||
| @@ -254,10 +255,10 @@ public class Execute | |||||
| * | * | ||||
| * @return the environment used to create a subprocess | * @return the environment used to create a subprocess | ||||
| */ | */ | ||||
| public String[] getEnvironment() | |||||
| private String[] getEnvironment() | |||||
| throws TaskException | throws TaskException | ||||
| { | { | ||||
| if( m_environment == null || m_newEnvironment ) | |||||
| if( m_newEnvironment ) | |||||
| { | { | ||||
| return Environment.toNativeFormat( m_environment ); | return Environment.toNativeFormat( m_environment ); | ||||
| } | } | ||||
| @@ -284,27 +285,26 @@ public class Execute | |||||
| public int execute() | public int execute() | ||||
| throws IOException, TaskException | throws IOException, TaskException | ||||
| { | { | ||||
| CommandLauncher launcher = c_launcher != null ? c_launcher : c_shellLauncher; | |||||
| if( !m_useVMLauncher ) | |||||
| { | |||||
| launcher = c_shellLauncher; | |||||
| } | |||||
| final ExecMetaData metaData = | final ExecMetaData metaData = | ||||
| new ExecMetaData( m_command, getEnvironment(), | new ExecMetaData( m_command, getEnvironment(), | ||||
| m_workingDirectory, false ); | m_workingDirectory, false ); | ||||
| final CommandLauncher launcher = getLauncher(); | |||||
| final Process process = launcher.exec( metaData ); | final Process process = launcher.exec( metaData ); | ||||
| try | try | ||||
| { | { | ||||
| m_streamHandler.setProcessInputStream( process.getOutputStream() ); | m_streamHandler.setProcessInputStream( process.getOutputStream() ); | ||||
| m_streamHandler.setProcessOutputStream( process.getInputStream() ); | m_streamHandler.setProcessOutputStream( process.getInputStream() ); | ||||
| m_streamHandler.setProcessErrorStream( process.getErrorStream() ); | m_streamHandler.setProcessErrorStream( process.getErrorStream() ); | ||||
| } | } | ||||
| catch( IOException e ) | |||||
| catch( final IOException ioe ) | |||||
| { | { | ||||
| process.destroy(); | process.destroy(); | ||||
| throw e; | |||||
| throw ioe; | |||||
| } | } | ||||
| m_streamHandler.start(); | m_streamHandler.start(); | ||||
| // add the process to the list of those to destroy if the VM exits | // add the process to the list of those to destroy if the VM exits | ||||
| @@ -312,19 +312,42 @@ public class Execute | |||||
| c_processDestroyer.add( process ); | c_processDestroyer.add( process ); | ||||
| if( m_watchdog != null ) | if( m_watchdog != null ) | ||||
| { | |||||
| m_watchdog.start( process ); | m_watchdog.start( process ); | ||||
| waitFor( process ); | |||||
| } | |||||
| try | |||||
| { | |||||
| process.waitFor(); | |||||
| } | |||||
| catch( final InterruptedException ie ) | |||||
| { | |||||
| //shu\ould never happen | |||||
| } | |||||
| // remove the process to the list of those to destroy if the VM exits | // remove the process to the list of those to destroy if the VM exits | ||||
| // | // | ||||
| c_processDestroyer.remove( process ); | c_processDestroyer.remove( process ); | ||||
| if( m_watchdog != null ) | if( m_watchdog != null ) | ||||
| { | |||||
| m_watchdog.stop(); | m_watchdog.stop(); | ||||
| } | |||||
| m_streamHandler.stop(); | m_streamHandler.stop(); | ||||
| if( m_watchdog != null ) | if( m_watchdog != null ) | ||||
| { | |||||
| m_watchdog.checkException(); | m_watchdog.checkException(); | ||||
| return m_exitValue; | |||||
| } | |||||
| return process.exitValue();; | |||||
| } | |||||
| private CommandLauncher getLauncher() | |||||
| { | |||||
| CommandLauncher launcher = c_launcher; | |||||
| if( !m_useVMLauncher ) | |||||
| { | |||||
| launcher = c_shellLauncher; | |||||
| } | |||||
| return launcher; | |||||
| } | } | ||||
| /** | /** | ||||
| @@ -337,21 +360,4 @@ public class Execute | |||||
| { | { | ||||
| return m_watchdog != null && m_watchdog.killedProcess(); | return m_watchdog != null && m_watchdog.killedProcess(); | ||||
| } | } | ||||
| private void setExitValue( final int value ) | |||||
| { | |||||
| m_exitValue = value; | |||||
| } | |||||
| protected void waitFor( Process process ) | |||||
| { | |||||
| try | |||||
| { | |||||
| process.waitFor(); | |||||
| setExitValue( process.exitValue() ); | |||||
| } | |||||
| catch( InterruptedException e ) | |||||
| { | |||||
| } | |||||
| } | |||||
| } | } | ||||
| @@ -9,7 +9,6 @@ package org.apache.tools.ant.taskdefs.exec; | |||||
| import java.io.File; | import java.io.File; | ||||
| import java.io.IOException; | import java.io.IOException; | ||||
| import java.util.ArrayList; | |||||
| import java.util.Locale; | import java.util.Locale; | ||||
| import java.util.Properties; | import java.util.Properties; | ||||
| import org.apache.avalon.excalibur.io.FileUtil; | import org.apache.avalon.excalibur.io.FileUtil; | ||||
| @@ -165,7 +164,8 @@ public class Execute | |||||
| * @param watchdog a watchdog for the subprocess or <code>null</code> to to | * @param watchdog a watchdog for the subprocess or <code>null</code> to to | ||||
| * disable a timeout for the subprocess. | * disable a timeout for the subprocess. | ||||
| */ | */ | ||||
| public Execute( ExecuteStreamHandler streamHandler, ExecuteWatchdog watchdog ) | |||||
| public Execute( final ExecuteStreamHandler streamHandler, | |||||
| final ExecuteWatchdog watchdog ) | |||||
| { | { | ||||
| m_streamHandler = streamHandler; | m_streamHandler = streamHandler; | ||||
| m_watchdog = watchdog; | m_watchdog = watchdog; | ||||
| @@ -179,15 +179,16 @@ public class Execute | |||||
| * @param cmdline The command to execute. | * @param cmdline The command to execute. | ||||
| * @throws TaskException if the command does not return 0. | * @throws TaskException if the command does not return 0. | ||||
| */ | */ | ||||
| public static void runCommand( Task task, String[] cmdline ) | |||||
| public static void runCommand( final Task task, final String[] cmdline ) | |||||
| throws TaskException | throws TaskException | ||||
| { | { | ||||
| try | try | ||||
| { | { | ||||
| task.log( Commandline.toString( cmdline ), Project.MSG_VERBOSE ); | task.log( Commandline.toString( cmdline ), Project.MSG_VERBOSE ); | ||||
| Execute exe = new Execute( new LogStreamHandler( task, | |||||
| Project.MSG_INFO, | |||||
| Project.MSG_ERR ) ); | |||||
| final Execute exe = | |||||
| new Execute( new LogStreamHandler( task, | |||||
| Project.MSG_INFO, | |||||
| Project.MSG_ERR ) ); | |||||
| exe.setCommandline( cmdline ); | exe.setCommandline( cmdline ); | ||||
| int retval = exe.execute(); | int retval = exe.execute(); | ||||
| if( retval != 0 ) | if( retval != 0 ) | ||||
| @@ -195,9 +196,9 @@ public class Execute | |||||
| throw new TaskException( cmdline[ 0 ] + " failed with return code " + retval ); | throw new TaskException( cmdline[ 0 ] + " failed with return code " + retval ); | ||||
| } | } | ||||
| } | } | ||||
| catch( IOException exc ) | |||||
| catch( final IOException ioe ) | |||||
| { | { | ||||
| throw new TaskException( "Could not launch " + cmdline[ 0 ] + ": " + exc ); | |||||
| throw new TaskException( "Could not launch " + cmdline[ 0 ] + ": " + ioe ); | |||||
| } | } | ||||
| } | } | ||||
| @@ -254,10 +255,10 @@ public class Execute | |||||
| * | * | ||||
| * @return the environment used to create a subprocess | * @return the environment used to create a subprocess | ||||
| */ | */ | ||||
| public String[] getEnvironment() | |||||
| private String[] getEnvironment() | |||||
| throws TaskException | throws TaskException | ||||
| { | { | ||||
| if( m_environment == null || m_newEnvironment ) | |||||
| if( m_newEnvironment ) | |||||
| { | { | ||||
| return Environment.toNativeFormat( m_environment ); | return Environment.toNativeFormat( m_environment ); | ||||
| } | } | ||||
| @@ -284,27 +285,26 @@ public class Execute | |||||
| public int execute() | public int execute() | ||||
| throws IOException, TaskException | throws IOException, TaskException | ||||
| { | { | ||||
| CommandLauncher launcher = c_launcher != null ? c_launcher : c_shellLauncher; | |||||
| if( !m_useVMLauncher ) | |||||
| { | |||||
| launcher = c_shellLauncher; | |||||
| } | |||||
| final ExecMetaData metaData = | final ExecMetaData metaData = | ||||
| new ExecMetaData( m_command, getEnvironment(), | new ExecMetaData( m_command, getEnvironment(), | ||||
| m_workingDirectory, false ); | m_workingDirectory, false ); | ||||
| final CommandLauncher launcher = getLauncher(); | |||||
| final Process process = launcher.exec( metaData ); | final Process process = launcher.exec( metaData ); | ||||
| try | try | ||||
| { | { | ||||
| m_streamHandler.setProcessInputStream( process.getOutputStream() ); | m_streamHandler.setProcessInputStream( process.getOutputStream() ); | ||||
| m_streamHandler.setProcessOutputStream( process.getInputStream() ); | m_streamHandler.setProcessOutputStream( process.getInputStream() ); | ||||
| m_streamHandler.setProcessErrorStream( process.getErrorStream() ); | m_streamHandler.setProcessErrorStream( process.getErrorStream() ); | ||||
| } | } | ||||
| catch( IOException e ) | |||||
| catch( final IOException ioe ) | |||||
| { | { | ||||
| process.destroy(); | process.destroy(); | ||||
| throw e; | |||||
| throw ioe; | |||||
| } | } | ||||
| m_streamHandler.start(); | m_streamHandler.start(); | ||||
| // add the process to the list of those to destroy if the VM exits | // add the process to the list of those to destroy if the VM exits | ||||
| @@ -312,19 +312,42 @@ public class Execute | |||||
| c_processDestroyer.add( process ); | c_processDestroyer.add( process ); | ||||
| if( m_watchdog != null ) | if( m_watchdog != null ) | ||||
| { | |||||
| m_watchdog.start( process ); | m_watchdog.start( process ); | ||||
| waitFor( process ); | |||||
| } | |||||
| try | |||||
| { | |||||
| process.waitFor(); | |||||
| } | |||||
| catch( final InterruptedException ie ) | |||||
| { | |||||
| //shu\ould never happen | |||||
| } | |||||
| // remove the process to the list of those to destroy if the VM exits | // remove the process to the list of those to destroy if the VM exits | ||||
| // | // | ||||
| c_processDestroyer.remove( process ); | c_processDestroyer.remove( process ); | ||||
| if( m_watchdog != null ) | if( m_watchdog != null ) | ||||
| { | |||||
| m_watchdog.stop(); | m_watchdog.stop(); | ||||
| } | |||||
| m_streamHandler.stop(); | m_streamHandler.stop(); | ||||
| if( m_watchdog != null ) | if( m_watchdog != null ) | ||||
| { | |||||
| m_watchdog.checkException(); | m_watchdog.checkException(); | ||||
| return m_exitValue; | |||||
| } | |||||
| return process.exitValue();; | |||||
| } | |||||
| private CommandLauncher getLauncher() | |||||
| { | |||||
| CommandLauncher launcher = c_launcher; | |||||
| if( !m_useVMLauncher ) | |||||
| { | |||||
| launcher = c_shellLauncher; | |||||
| } | |||||
| return launcher; | |||||
| } | } | ||||
| /** | /** | ||||
| @@ -337,21 +360,4 @@ public class Execute | |||||
| { | { | ||||
| return m_watchdog != null && m_watchdog.killedProcess(); | return m_watchdog != null && m_watchdog.killedProcess(); | ||||
| } | } | ||||
| private void setExitValue( final int value ) | |||||
| { | |||||
| m_exitValue = value; | |||||
| } | |||||
| protected void waitFor( Process process ) | |||||
| { | |||||
| try | |||||
| { | |||||
| process.waitFor(); | |||||
| setExitValue( process.exitValue() ); | |||||
| } | |||||
| catch( InterruptedException e ) | |||||
| { | |||||
| } | |||||
| } | |||||
| } | } | ||||