KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Jt > JtOSCommand


1 package Jt;
2 import java.util.*;
3 import java.lang.reflect.*;
4 import java.beans.*;
5 import java.io.*;
6
7
8 /**
9  * Class used to invoke OS commands.
10  */

11
12
13 public class JtOSCommand extends JtObject {
14
15   String JavaDoc command = null;
16   Process JavaDoc process = null;
17   int status = 0;
18   String JavaDoc stdout;
19
20
21  /**
22   * Specifies the OS command to be executed.
23   * @param newCommand OS command
24   */

25
26   public void setCommand (String JavaDoc newCommand) {
27      command = newCommand;
28   }
29
30  /**
31    * Returns the OS command to be executed.
32    */

33
34   public String JavaDoc getCommand () {
35      return (command);
36   }
37
38
39  /**
40    * Returns the output of the command.
41    */

42
43   public String JavaDoc getStdout () {
44      BufferedReader d;
45      StringBuffer JavaDoc output = null;
46      String JavaDoc line;
47      InputStream stream;
48
49      if (process == null)
50     return (null);
51
52      stream = process.getInputStream ();
53
54      if (stream == null)
55     return (null);
56
57      d = new BufferedReader (new InputStreamReader (stream));
58      try {
59         while ((line = d.readLine ()) != null) {
60        handleTrace (line);
61        if (output == null) {
62         output = new StringBuffer JavaDoc ();
63         output.append (line);
64        } else
65         output.append ("\n" + line); // check
66

67     }
68      } catch (Exception JavaDoc e) {
69     handleException (e);
70      }
71      if (output != null)
72         stdout = output.toString ();
73      return (stdout);
74   }
75
76  /**
77    * Returns the exit status of the command.
78    */

79   public int getStatus () {
80      if (process == null) {
81     handleWarning ("JtCommand: invalid process: null");
82     return (-1);
83      }
84
85      try {
86     process.waitFor ();
87      } catch (Exception JavaDoc e) {
88     handleException (e);
89      }
90
91      return (process.exitValue ());
92   }
93   
94
95  /**
96    * Void operation.
97    */

98
99   public void setStatus (int status) {
100
101   }
102
103   // Executes the OS command
104

105   void execute () {
106
107     if (command == null) {
108     process = null; // just in case
109
return;
110     }
111
112     try {
113         process = Runtime.getRuntime().exec (command);
114     } catch (Exception JavaDoc e) {
115     handleException (e);
116     }
117   }
118
119
120   /**
121    * Process object messages.
122    * <ul>
123    * <li> JtEXECUTE - Executes the OS command specified by the command attribute.
124    * </ul>
125    * @param message Jt Message
126    */

127
128   public Object JavaDoc processMessage (Object JavaDoc message) {
129
130    String JavaDoc msgid = null;
131    byte buffer[];
132    File file;
133    JtMessage e = (JtMessage) message;
134
135      if (e == null)
136     return null;
137
138      msgid = (String JavaDoc) e.getMsgId ();
139
140      if (msgid == null)
141     return null;
142
143
144      // Destroy this object
145
if (msgid.equals ("JtREMOVE")) {
146        return (null);
147      }
148
149      if (msgid.equals ("JtEXECUTE")) {
150     execute ();
151     return null;
152      }
153      handleError ("JtCommand.processMessage: invalid message id:" + msgid);
154      return null;
155   }
156
157  /**
158    * Unit tests all the message processed by JtCommand. Usage: java Jt.JtCommand command
159    */

160
161   public static void main(String JavaDoc[] args) {
162
163     JtObject main = new JtObject ();
164     JtMessage msg;
165     File tmp;
166     Integer JavaDoc status;
167     String JavaDoc output;
168  
169
170     if (args.length < 1) {
171     System.err.println ("Usage: java Jt.JtOSCommand command");
172     return;
173     }
174     
175     // main.setObjTrace (1);
176

177     main.createObject ("Jt.JtOSCommand", "command");
178     main.setValue ("command", "command", args[0]);
179
180
181     msg = new JtMessage ("JtEXECUTE");
182
183     main.sendMessage ("command", msg);
184
185     status = (Integer JavaDoc) main.getValue ("command", "status");
186     System.err.println ("Exit status:" + status);
187     output = (String JavaDoc) main.getValue ("command", "stdout");
188     System.err.println ("Command output:" + output);
189
190     main.removeObject ("command");
191  
192   }
193 }
194
195
196
Popular Tags