Advertisement
Guest User

SystemCommandExecutor

a guest
Dec 8th, 2011
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.83 KB | None | 0 0
  1. package net.i2p.hack.scripts.util;
  2.  
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.OutputStream;
  6. import java.util.List;
  7.  
  8. /**
  9.  * This class can be used to execute a system command from a Java application.
  10.  * See the documentation for the public methods of this class for more
  11.  * information.
  12.  *
  13.  * Documentation for this class is available at this URL:
  14.  *
  15.  * http://devdaily.com/java/java-processbuilder-process-system-exec
  16.  *
  17.  *
  18.  * Copyright 2010 alvin j. alexander, devdaily.com.
  19.  *
  20.  * This program is free software: you can redistribute it and/or modify
  21.  * it under the terms of the GNU Lesser Public License as published by
  22.  * the Free Software Foundation, either version 3 of the License, or
  23.  * (at your option) any later version.
  24.  
  25.  * This program is distributed in the hope that it will be useful,
  26.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  28.  * GNU Lesser Public License for more details.
  29.  
  30.  * You should have received a copy of the GNU Lesser Public License
  31.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  32.  *
  33.  * Please ee the following page for the LGPL license:
  34.  * http://www.gnu.org/licenses/lgpl.txt
  35.  *
  36.  */
  37. public class SystemCommandExecutor
  38. {
  39.   private List<String> commandInformation;
  40.   private String adminPassword;
  41.   private ThreadedStreamHandler inputStreamHandler;
  42.   private ThreadedStreamHandler errorStreamHandler;
  43.  
  44.   /**
  45.    * Pass in the system command you want to run as a List of Strings, as shown here:
  46.    *
  47.    * List<String> commands = new ArrayList<String>();
  48.    * commands.add("/sbin/ping");
  49.    * commands.add("-c");
  50.    * commands.add("5");
  51.    * commands.add("www.google.com");
  52.    * SystemCommandExecutor commandExecutor = new SystemCommandExecutor(commands);
  53.    * commandExecutor.executeCommand();
  54.    *
  55.    * Note: I've removed the other constructor that was here to support executing
  56.    *       the sudo command. I'll add that back in when I get the sudo command
  57.    *       working to the point where it won't hang when the given password is
  58.    *       wrong.
  59.    *
  60.    * @param commandInformation The command you want to run.
  61.    */
  62.   public SystemCommandExecutor(final List<String> commandInformation)
  63.   {
  64.     if (commandInformation==null) throw new NullPointerException("The commandInformation is required.");
  65.     this.commandInformation = commandInformation;
  66.     this.adminPassword = null;
  67.   }
  68.  
  69.   public int executeCommand()
  70.   throws IOException, InterruptedException
  71.   {
  72.     int exitValue = -99;
  73.  
  74.     try
  75.     {
  76.       ProcessBuilder pb = new ProcessBuilder(commandInformation);
  77.       Process process = pb.start();
  78.  
  79.       // you need this if you're going to write something to the command's input stream
  80.       // (such as when invoking the 'sudo' command, and it prompts you for a password).
  81.       OutputStream stdOutput = process.getOutputStream();
  82.      
  83.       // i'm currently doing these on a separate line here in case i need to set them to null
  84.       // to get the threads to stop.
  85.       // see http://java.sun.com/j2se/1.5.0/docs/guide/misc/threadPrimitiveDeprecation.html
  86.       InputStream inputStream = process.getInputStream();
  87.       InputStream errorStream = process.getErrorStream();
  88.  
  89.       // these need to run as java threads to get the standard output and error from the command.
  90.       // the inputstream handler gets a reference to our stdOutput in case we need to write
  91.       // something to it, such as with the sudo command
  92.       inputStreamHandler = new ThreadedStreamHandler(inputStream, stdOutput, adminPassword);
  93.       errorStreamHandler = new ThreadedStreamHandler(errorStream);
  94.  
  95.       // TODO the inputStreamHandler has a nasty side-effect of hanging if the given password is wrong; fix it
  96.       inputStreamHandler.start();
  97.       errorStreamHandler.start();
  98.  
  99.       // TODO a better way to do this?
  100.       exitValue = process.waitFor();
  101.  
  102.       // TODO a better way to do this?
  103.       inputStreamHandler.interrupt();
  104.       errorStreamHandler.interrupt();
  105.       inputStreamHandler.join();
  106.       errorStreamHandler.join();
  107.     }
  108.     catch (IOException e)
  109.     {
  110.       // TODO deal with this here, or just throw it?
  111.       throw e;
  112.     }
  113.     catch (InterruptedException e)
  114.     {
  115.       // generated by process.waitFor() call
  116.       // TODO deal with this here, or just throw it?
  117.       throw e;
  118.     }
  119.     return exitValue;
  120.   }
  121.  
  122.   /**
  123.    * Get the standard output (stdout) from the command you just exec'd.
  124.    */
  125.   public StringBuilder getStandardOutputFromCommand()
  126.   {
  127.     return inputStreamHandler.getOutputBuffer();
  128.   }
  129.  
  130.   /**
  131.    * Get the standard error (stderr) from the command you just exec'd.
  132.    */
  133.   public StringBuilder getStandardErrorFromCommand()
  134.   {
  135.     return errorStreamHandler.getOutputBuffer();
  136.   }
  137.  
  138.  
  139. }
  140.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement