home *** CD-ROM | disk | FTP | other *** search
/ Australian Personal Computer 2000 November / APCD25112k.iso / feature / webserv / SAMBAR43.ZIP / _SETUP.1 / javaeng.jar / com / sambar / util / Exec.java < prev   
Encoding:
Java Source  |  2000-04-03  |  2.9 KB  |  125 lines

  1. /*
  2. **         Sambar Server Exec Utility
  3. **
  4. **        Execute a command either synchronously or async
  5. **        much like the UNIX system or fork/exec functionality.
  6. **
  7. **        This appears on Core Web Programming from Prentice Hall Publishers,
  8. **        and may be freely used or adapted. 1997 Marty Hall, hall@apl.jhu.edu
  9. */
  10. package com.sambar.util;
  11.  
  12. import java.io.*;
  13.  
  14. public class Exec 
  15. {
  16.     private static boolean debug = true;
  17.  
  18.     public static void setDebug(boolean debugFlag) 
  19.     {
  20.         debug = debugFlag;
  21.     }
  22.  
  23.     /*
  24.     ** Execute a command and return immediately.
  25.     ** Return false if a problem is occurs due to an
  26.     ** exception.
  27.     */
  28.     public static boolean exec(String command) 
  29.     {
  30.         return (exec(command, false, false));
  31.     }
  32.   
  33.     /*
  34.     ** Execute a command and wait until it completes.
  35.     ** Return false if a problem is occurs due to an
  36.     ** exception or the command returned a non-zero value.
  37.     */
  38.     public static boolean execWait(String command) 
  39.     {
  40.         return(exec(command, false, true));
  41.     }
  42.   
  43.     /*
  44.     ** Execute a command and print the output of the command.
  45.     ** Return false if a problem is occurs due to an
  46.     ** exception or the command returned a non-zero value.
  47.     */
  48.     public static boolean execPrint(String command) 
  49.     {
  50.         return(exec(command, true, false));
  51.     }
  52.   
  53.     private static boolean exec(String command, boolean printResults, 
  54.         boolean wait) 
  55.     {
  56.         if (debug) 
  57.         {
  58.             System.out.println("============================================");
  59.             System.out.println("Executing '" + command + "'.");
  60.         }
  61.  
  62.         try 
  63.         {
  64.             Process p  = Runtime.getRuntime().exec(command);
  65.       
  66.             if (printResults) 
  67.             {
  68.                 BufferedInputStream buffer; 
  69.                 DataInputStream commandResult;
  70.                 buffer = new BufferedInputStream(p.getInputStream());
  71.                 commandResult = new DataInputStream(buffer);
  72.                 String s = null;
  73.                 try 
  74.                 {
  75.                     while ((s = commandResult.readLine()) != null)
  76.                     System.out.println("Output: " + s);
  77.                     commandResult.close();
  78.                     if (p.exitValue() != 0) 
  79.                     {
  80.                         if (debug)
  81.                             printError(command + " -- p.exitValue() != 0");
  82.                         return (false);
  83.                     }
  84.                 } catch (Exception e) {}
  85.         
  86.             } 
  87.             else if (wait) 
  88.             {
  89.                 try
  90.                 {
  91.                     int returnVal = p.waitFor();
  92.                     if (returnVal != 0) 
  93.                     {
  94.                         if (debug)
  95.                             printError(command);
  96.                         return (false);
  97.                     }
  98.                 } catch (Exception e) {
  99.                     if (debug)
  100.                         printError(command, e);
  101.                     return (false);
  102.                 }
  103.             }
  104.         } catch (Exception e) {
  105.             if (debug)
  106.                 printError(command, e);
  107.             return (false);
  108.         }
  109.  
  110.         return(true);
  111.     }
  112.   
  113.     private static void printError(String command, Exception e) 
  114.     {
  115.         System.out.println("Error doing exec(" + command + "): " + 
  116.             e.getMessage());
  117.         System.out.println("Did you specify the full " + "pathname?");
  118.     } 
  119.  
  120.     private static void printError(String command) 
  121.     {
  122.         System.out.println("Error executing '" + command + "'.");
  123.     }
  124. }
  125.