X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=forester%2Fjava%2Fsrc%2Forg%2Fforester%2Futil%2FSystemCommandExecutor.java;h=a4cdec544986bc949c927b4c7aeac6866f7b9ad0;hb=03c2309734ed910f833cc2e5281c654879d7a12a;hp=5b32dc44bc1a65a4c9dc84ad57a41a444b0686c5;hpb=48f7a89be9d34f1930a1f863e608235cc27184c5;p=jalview.git
diff --git a/forester/java/src/org/forester/util/SystemCommandExecutor.java b/forester/java/src/org/forester/util/SystemCommandExecutor.java
index 5b32dc4..a4cdec5 100644
--- a/forester/java/src/org/forester/util/SystemCommandExecutor.java
+++ b/forester/java/src/org/forester/util/SystemCommandExecutor.java
@@ -3,29 +3,28 @@
* This class can be used to execute a system command from a Java application.
* See the documentation for the public methods of this class for more
* information.
- *
+ *
* Documentation for this class is available at this URL:
- *
+ *
* http://devdaily.com/java/java-processbuilder-process-system-exec
- *
- *
+ *
* Copyright 2010 alvin j. alexander, devdaily.com.
- *
+ *
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
- *
+ *
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser Public License for more details.
- *
+ *
* You should have received a copy of the GNU Lesser Public License along with
* this program. If not, see .
- *
- * Please ee the following page for the LGPL license:
+ *
+ * Please see the following page for the LGPL license:
* http://www.gnu.org/licenses/lgpl.txt
- *
+ *
*/
package org.forester.util;
@@ -38,14 +37,14 @@ import java.util.List;
public class SystemCommandExecutor {
- private final List commandInformation;
- private final String adminPassword;
- private ThreadedStreamHandler inputStreamHandler;
- private ThreadedStreamHandler errorStreamHandler;
+ private final List _command_information;
+ private ThreadedStreamHandler _input_stream_handler;
+ private ThreadedStreamHandler _error_stream_handler;
+ private final static boolean DEBUG = false;
/**
* Pass in the system command you want to run as a List of Strings, as shown here:
- *
+ *
* List commands = new ArrayList();
* commands.add("/sbin/ping");
* commands.add("-c");
@@ -53,21 +52,20 @@ public class SystemCommandExecutor {
* commands.add("www.google.com");
* SystemCommandExecutor commandExecutor = new SystemCommandExecutor(commands);
* commandExecutor.executeCommand();
- *
+ *
* Note: I've removed the other constructor that was here to support executing
* the sudo command. I'll add that back in when I get the sudo command
* working to the point where it won't hang when the given password is
* wrong.
*
- * @param commandInformation The command you want to run.
+ * @param command_information The command you want to run.
*/
- public SystemCommandExecutor( final List commandInformation ) {
- if ( ( commandInformation == null ) || commandInformation.isEmpty() ) {
- throw new IllegalArgumentException( "The commandInformation is required." );
+ public SystemCommandExecutor( final List command_information ) {
+ if ( ( command_information == null ) || command_information.isEmpty() ) {
+ throw new IllegalArgumentException( "command information is required" );
}
- checkCmdFile( new File( commandInformation.get( 0 ) ) );
- this.commandInformation = commandInformation;
- adminPassword = null;
+ checkCmdFile( new File( command_information.get( 0 ) ) );
+ _command_information = command_information;
}
public static boolean isExecuteableFile( final File path_to_cmd_f ) {
@@ -83,7 +81,7 @@ public class SystemCommandExecutor {
return true;
}
- private void checkCmdFile( final File path_to_cmd_f ) {
+ private static void checkCmdFile( final File path_to_cmd_f ) {
if ( !path_to_cmd_f.exists() ) {
throw new IllegalArgumentException( "[" + path_to_cmd_f.getAbsolutePath() + "] does not exist" );
}
@@ -96,9 +94,12 @@ public class SystemCommandExecutor {
}
public int executeCommand() throws IOException, InterruptedException {
- int exitValue = -99;
+ int exit_value = -99;
try {
- final ProcessBuilder pb = new ProcessBuilder( commandInformation );
+ final ProcessBuilder pb = new ProcessBuilder( _command_information );
+ if ( DEBUG ) {
+ System.out.println( "command_information=" + _command_information );
+ }
final Process process = pb.start();
// you need this if you're going to write something to the command's input stream
// (such as when invoking the 'sudo' command, and it prompts you for a password).
@@ -111,44 +112,41 @@ public class SystemCommandExecutor {
// these need to run as java threads to get the standard output and error from the command.
// the inputstream handler gets a reference to our stdOutput in case we need to write
// something to it, such as with the sudo command
- inputStreamHandler = new ThreadedStreamHandler( inputStream, stdOutput, adminPassword );
- errorStreamHandler = new ThreadedStreamHandler( errorStream );
- // TODO the inputStreamHandler has a nasty side-effect of hanging if the given password is wrong; fix it
- inputStreamHandler.start();
- errorStreamHandler.start();
+ _input_stream_handler = new ThreadedStreamHandler( inputStream, stdOutput );
+ _error_stream_handler = new ThreadedStreamHandler( errorStream );
+ _input_stream_handler.start();
+ _error_stream_handler.start();
// TODO a better way to do this?
- exitValue = process.waitFor();
+ exit_value = process.waitFor();
// TODO a better way to do this?
- inputStreamHandler.interrupt();
- errorStreamHandler.interrupt();
- inputStreamHandler.join();
- errorStreamHandler.join();
+ _input_stream_handler.interrupt();
+ _error_stream_handler.interrupt();
+ _input_stream_handler.join();
+ _error_stream_handler.join();
}
catch ( final IOException e ) {
- // TODO deal with this here, or just throw it?
throw e;
}
catch ( final InterruptedException e ) {
// generated by process.waitFor() call
- // TODO deal with this here, or just throw it?
throw e;
}
- finally {
- return exitValue;
- }
+ // finally {
+ return exit_value;
+ // }
}
/**
* Get the standard error (stderr) from the command you just exec'd.
*/
public StringBuilder getStandardErrorFromCommand() {
- return errorStreamHandler.getOutputBuffer();
+ return _error_stream_handler.getOutputBuffer();
}
/**
* Get the standard output (stdout) from the command you just exec'd.
*/
public StringBuilder getStandardOutputFromCommand() {
- return inputStreamHandler.getOutputBuffer();
+ return _input_stream_handler.getOutputBuffer();
}
}