KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > magnet > domain > system > ProcessTask


1 package org.sapia.magnet.domain.system;
2
3 // Import of Apache's log4j
4
// ------------------------
5
import org.apache.log4j.Logger;
6 import org.sapia.magnet.Log;
7
8 // Import of Sun's JDK classes
9
// ---------------------------
10
import java.io.File JavaDoc;
11 import java.io.IOException JavaDoc;
12
13
14 /**
15  *
16  *
17  * @author Jean-Cedric Desrochers
18  *
19  * <dl>
20  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
21  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
22  * <a HREF="http://www.sapia-oss.org/license.html" target="sapia-license">license page</a> at the Sapia OSS web site</dd></dt>
23  * </dl>
24  */

25 public class ProcessTask implements Runnable JavaDoc {
26
27   /////////////////////////////////////////////////////////////////////////////////////////
28
////////////////////////////////// CLASS ATTRIBUTES ///////////////////////////////////
29
/////////////////////////////////////////////////////////////////////////////////////////
30

31   /** Defines the logger instance for this class. */
32   private static final Logger _theLogger = Logger.getLogger(ProcessTask.class);
33
34   /////////////////////////////////////////////////////////////////////////////////////////
35
///////////////////////////////// INSTANCE ATTRIBUTES /////////////////////////////////
36
/////////////////////////////////////////////////////////////////////////////////////////
37

38   /** The commands of this process to execute. */
39   private String JavaDoc[] _theCommands;
40
41   /** The environments variables of this process. */
42   private String JavaDoc[] _theEnvironmentVariables;
43
44   /** The working directory of this process. */
45   private File JavaDoc _theWorkingDirectory;
46
47   /////////////////////////////////////////////////////////////////////////////////////////
48
//////////////////////////////////// CONSTRUCTORS /////////////////////////////////////
49
/////////////////////////////////////////////////////////////////////////////////////////
50

51   /**
52    * Creates a new ProcessTask instance.
53    *
54    * @param someCommands The array of commands to execute.
55    * @param someVariables The array of environment variables.
56    * @param aDirectory The working directory of the process.
57    */

58   public ProcessTask(String JavaDoc[] someCommands, String JavaDoc[] someVariables, File JavaDoc aDirectory) {
59     _theCommands = someCommands;
60     _theEnvironmentVariables = someVariables;
61     _theWorkingDirectory = aDirectory;
62   }
63
64   /////////////////////////////////////////////////////////////////////////////////////////
65
/////////////////////////////// INTERACE IMPLEMENTATION ///////////////////////////////
66
/////////////////////////////////////////////////////////////////////////////////////////
67

68   /**
69    * Runs this process task.
70    */

71   public void run() {
72     Process JavaDoc aProcess = null;
73
74     // Execute this process
75
try {
76       aProcess = Runtime.getRuntime().exec(
77               _theCommands, _theEnvironmentVariables, _theWorkingDirectory);
78     } catch (IOException JavaDoc ioe) {
79       String JavaDoc aMessage = "Unable to execute the process";
80       _theLogger.error(aMessage, ioe);
81       Log.error(aMessage + " - " + Log.extactMessage(ioe));
82     }
83
84     // Monitor the streams untill the end of the process
85
boolean isTerminated = false;
86     while (!isTerminated) {
87       try {
88         // Sleep for a second
89
Thread.sleep(1000);
90
91         // Get the output stream
92
StringBuffer JavaDoc aBuffer = new StringBuffer JavaDoc();
93         while (aProcess.getInputStream().available() > 0) {
94           aBuffer.append((char) aProcess.getInputStream().read());
95         }
96         if (aBuffer.length() > 0) {
97           _theLogger.info("OUTPUT >>> " + aBuffer.toString());
98         }
99
100         // Get the error stream
101
aBuffer = new StringBuffer JavaDoc();
102         while (aProcess.getErrorStream().available() > 0) {
103           aBuffer.append((char) aProcess.getErrorStream().read());
104         }
105         if (aBuffer.length() > 0) {
106           _theLogger.error("ERROR >>> " + aBuffer.toString());
107         }
108
109         // Check the status of the running process (throws exception if not terminated)
110
int anExitValue = aProcess.exitValue();
111         isTerminated = true;
112         _theLogger.info("Process terminated with exit value: " + anExitValue);
113
114       } catch (InterruptedException JavaDoc ie) {
115         isTerminated = true;
116         _theLogger.warn("INTERRUPTED: Force termination of the process");
117         aProcess.destroy();
118
119       } catch (IOException JavaDoc ioe) {
120         String JavaDoc aMessage = "Caugh an I/O error while monitoring the running process";
121         _theLogger.warn(aMessage, ioe);
122
123       } catch (IllegalThreadStateException JavaDoc itse) {
124         // Called exitValue() on a running process... continue
125

126       } catch (RuntimeException JavaDoc re) {
127         String JavaDoc aMessage = "Caugh a system error while monitoring the running process";
128         _theLogger.warn(aMessage, re);
129       }
130     }
131   }
132 }
133
Popular Tags