Guest User

ThreadedStreamHandler

a guest
Dec 8th, 2011
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.87 KB | None | 0 0
  1. package net.i2p.hack.scripts.util;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.InputStreamReader;
  7. import java.io.OutputStream;
  8. import java.io.PrintWriter;
  9.  
  10. /**
  11.  * This class is intended to be used with the SystemCommandExecutor
  12.  * class to let users execute system commands from Java applications.
  13.  *
  14.  * This class is based on work that was shared in a JavaWorld article
  15.  * named "When System.exec() won't". That article is available at this
  16.  * url:
  17.  *
  18.  * http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
  19.  *
  20.  * Documentation for this class is available at this URL:
  21.  *
  22.  * http://devdaily.com/java/java-processbuilder-process-system-exec
  23.  *
  24.  *
  25.  * Copyright 2010 alvin j. alexander, devdaily.com.
  26.  *
  27.  * This program is free software: you can redistribute it and/or modify
  28.  * it under the terms of the GNU Lesser Public License as published by
  29.  * the Free Software Foundation, either version 3 of the License, or
  30.  * (at your option) any later version.
  31.  
  32.  * This program is distributed in the hope that it will be useful,
  33.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  34.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  35.  * GNU Lesser Public License for more details.
  36.  
  37.  * You should have received a copy of the GNU Lesser Public License
  38.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  39.  *
  40.  * Please ee the following page for the LGPL license:
  41.  * http://www.gnu.org/licenses/lgpl.txt
  42.  *
  43.  */
  44. class ThreadedStreamHandler extends Thread
  45. {
  46.   InputStream inputStream;
  47.   String adminPassword;
  48.   OutputStream outputStream;
  49.   PrintWriter printWriter;
  50.   StringBuilder outputBuffer = new StringBuilder();
  51.   private boolean sudoIsRequested = false;
  52.  
  53.   /**
  54.    * A simple constructor for when the sudo command is not necessary.
  55.    * This constructor will just run the command you provide, without
  56.    * running sudo before the command, and without expecting a password.
  57.    *
  58.    * @param inputStream
  59.    * @param streamType
  60.    */
  61.   ThreadedStreamHandler(InputStream inputStream)
  62.   {
  63.     this.inputStream = inputStream;
  64.   }
  65.  
  66.   /**
  67.    * Use this constructor when you want to invoke the 'sudo' command.
  68.    * The outputStream must not be null. If it is, you'll regret it. :)
  69.    *
  70.    * TODO this currently hangs if the admin password given for the sudo command is wrong.
  71.    *
  72.    * @param inputStream
  73.    * @param streamType
  74.    * @param outputStream
  75.    * @param adminPassword
  76.    */
  77.   ThreadedStreamHandler(InputStream inputStream, OutputStream outputStream, String adminPassword)
  78.   {
  79.     this.inputStream = inputStream;
  80.     this.outputStream = outputStream;
  81.     this.printWriter = new PrintWriter(outputStream);
  82.     this.adminPassword = adminPassword;
  83.     this.sudoIsRequested = true;
  84.   }
  85.  
  86.   public void run()
  87.   {
  88.     // on mac os x 10.5.x, when i run a 'sudo' command, i need to write
  89.     // the admin password out immediately; that's why this code is
  90.     // here.
  91.     if (sudoIsRequested)
  92.     {
  93.       //doSleep(500);
  94.       printWriter.println(adminPassword);
  95.       printWriter.flush();
  96.     }
  97.  
  98.     BufferedReader bufferedReader = null;
  99.     try
  100.     {
  101.       bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
  102.       String line = null;
  103.       while ((line = bufferedReader.readLine()) != null)
  104.       {
  105.         outputBuffer.append(line + "\n");
  106.       }
  107.     }
  108.     catch (IOException ioe)
  109.     {
  110.       // TODO handle this better
  111.       ioe.printStackTrace();
  112.     }
  113.     catch (Throwable t)
  114.     {
  115.       // TODO handle this better
  116.       t.printStackTrace();
  117.     }
  118.     finally
  119.     {
  120.       try
  121.       {
  122.         bufferedReader.close();
  123.       }
  124.       catch (IOException e)
  125.       {
  126.         // ignore this one
  127.       }
  128.     }
  129.   }
  130.  
  131.   public StringBuilder getOutputBuffer()
  132.   {
  133.     return outputBuffer;
  134.   }
  135.  
  136. }
  137.  
Add Comment
Please, Sign In to add comment