00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 package wttools.remotecons;
00024
00025 import wttools.remotecons.ifc.CommandHandlerIfc;
00026 import java.io.*;
00027 import java.util.StringTokenizer;
00028
00045 public class CommandHandlerImpl implements CommandHandlerIfc {
00046
00058 protected String last_command = null;
00059
00064 public CommandHandlerImpl()
00065 {}
00066
00079 public String handleCommand(String command) {
00080 String res = null;
00081 if (command.trim().equals("time")) {
00082 res = ""+new java.util.Date()+"\r\n";
00083 }
00084 if (command.trim().equals("gc")) {
00085 System.gc();
00086 res = "OK\r\n";
00087 }
00088 if (command.trim().startsWith("mem")) {
00089 Runtime rt = Runtime.getRuntime();
00090 res = ""+rt.totalMemory()+"/"+rt.freeMemory()+"\r\n";
00091 }
00092 if (command.trim().startsWith("exit")) {
00093 res =
00094 "\r\nYou are going to shutdown remote JVM!\r\n"+
00095 "Are you sure to do this and lost all data? (yes/no): ";
00096 }
00097 if (command.trim().startsWith("exec")) {
00098 res = externalCommand(command.substring(5).trim());
00099 }
00100 if (command.trim().equals("yes")) {
00101 if (last_command.trim().startsWith("exit")) {
00102 System.exit(10);
00103 }
00104 }
00105 if (command.trim().equals("no")) {
00106 if (last_command.trim().startsWith("exit")) {
00107 res = "\r\n";
00108 }
00109 }
00110
00111 last_command = command;
00112 return res;
00113 }
00114
00122 public String help() {
00123 String help_text =
00124 "\r\n"+
00125 " time - display current time on server\r\n"+
00126 " gc - run System.gc() command on remote system\r\n"+
00127 " mem [total|free] - display amount of total/free memory on remote system\r\n"+
00128 " exit [n] - call System.exit(n) on remote system\r\n"+
00129 " get [params]+ - complex command require additional parameters,\r\n"+
00130 " call 'help get' for more info. (Not implemented yet.)\r\n"+
00131 " set [params]+ - complex command require additional parameters,\r\n"+
00132 " call 'help set' for more info. (Not implemented yet.) \r\n"+
00133 " exec [params]+ - run Runtime.exec(String[] params) command on remote system\r\n";
00134 return help_text;
00135 }
00136
00146 public String help(String command) {
00147 return "";
00148 }
00149
00162 protected String externalCommand(String comm) {
00163 String res = "";
00164 StringTokenizer stt = new StringTokenizer(comm);
00165 String[] cmd = new String[stt.countTokens()];
00166 int i = 0;
00167 while (stt.hasMoreTokens()) {
00168 cmd[i++] = stt.nextToken();
00169 }
00170 try {
00171 jtools.Process proc = new jtools.Process(cmd);
00172 proc.exec();
00173 ByteArrayOutputStream buff_out = proc.getProcessOutputStream();
00174 ByteArrayOutputStream buff_err = proc.getProcessErrorStream();
00175 BufferedReader brout = new BufferedReader(new StringReader(buff_out.toString()));
00176 String line = brout.readLine();
00177 while (line != null) {
00178 res += line+"\r\n";
00179 line = brout.readLine();
00180 }
00181 brout.close();
00182 buff_out.close();
00183 BufferedReader brerr = new BufferedReader(new StringReader(buff_err.toString()));
00184 line = brerr.readLine();
00185 while (line != null) {
00186 res += line+"\r\n";
00187 line = brerr.readLine();
00188 }
00189 brerr.close();
00190 buff_err.close();
00191 } catch (Exception e) {
00192 ByteArrayOutputStream baos = new ByteArrayOutputStream();
00193 PrintWriter pw = new PrintWriter(baos, false);
00194 e.printStackTrace(pw);
00195 pw.flush();
00196 res = baos.toString();
00197 pw.close();
00198 }
00199 return "\r\n"+res;
00200 }
00201
00209 public CommandHandlerIfc getInstance() {
00210 return new CommandHandlerImpl();
00211 }
00212
00213 }
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262