1 package Jt; 2 import java.util.*; 3 import java.lang.reflect.*; 4 import java.beans.*; 5 import java.io.*; 6 7 8 11 12 13 public class JtOSCommand extends JtObject { 14 15 String command = null; 16 Process process = null; 17 int status = 0; 18 String stdout; 19 20 21 25 26 public void setCommand (String newCommand) { 27 command = newCommand; 28 } 29 30 33 34 public String getCommand () { 35 return (command); 36 } 37 38 39 42 43 public String getStdout () { 44 BufferedReader d; 45 StringBuffer output = null; 46 String 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 (); 63 output.append (line); 64 } else 65 output.append ("\n" + line); 67 } 68 } catch (Exception e) { 69 handleException (e); 70 } 71 if (output != null) 72 stdout = output.toString (); 73 return (stdout); 74 } 75 76 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 e) { 88 handleException (e); 89 } 90 91 return (process.exitValue ()); 92 } 93 94 95 98 99 public void setStatus (int status) { 100 101 } 102 103 105 void execute () { 106 107 if (command == null) { 108 process = null; return; 110 } 111 112 try { 113 process = Runtime.getRuntime().exec (command); 114 } catch (Exception e) { 115 handleException (e); 116 } 117 } 118 119 120 127 128 public Object processMessage (Object message) { 129 130 String 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 ) e.getMsgId (); 139 140 if (msgid == null) 141 return null; 142 143 144 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 160 161 public static void main(String [] args) { 162 163 JtObject main = new JtObject (); 164 JtMessage msg; 165 File tmp; 166 Integer status; 167 String output; 168 169 170 if (args.length < 1) { 171 System.err.println ("Usage: java Jt.JtOSCommand command"); 172 return; 173 } 174 175 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 ) main.getValue ("command", "status"); 186 System.err.println ("Exit status:" + status); 187 output = (String ) main.getValue ("command", "stdout"); 188 System.err.println ("Command output:" + output); 189 190 main.removeObject ("command"); 191 192 } 193 } 194 195 196 | Popular Tags |