Main Page   Class Hierarchy   Compound List   File List   Compound Members  

CommandHandlerImpl.java

00001 /*  Package Web Test Tools 
00002  *  Copyright (C) 2001 "Artur Hefczyc" <kobit@users.sourceforge.net>
00003  *
00004  *  This program is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU Lesser General Public License as published
00006  *  by the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  This program is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00012  *  GNU Lesser General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU Lesser General Public License
00015  *  along with this program; if not, write to the Free Software Foundation,
00016  *  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017  *
00018  * $Id: CommandHandlerImpl.java,v 1.1 2002/11/29 15:18:26 kobit Exp $
00019  * $Author: kobit $
00020  * $Date: 2002/11/29 15:18:26 $
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         } // end of if (command.trim().equals("time"))
00084         if (command.trim().equals("gc")) {
00085           System.gc();
00086           res = "OK\r\n";
00087         } // end of if (command.trim().equals("gc"))
00088         if (command.trim().startsWith("mem")) {
00089           Runtime rt = Runtime.getRuntime();
00090           res = ""+rt.totalMemory()+"/"+rt.freeMemory()+"\r\n";
00091         } // end of if (command.trim().startsWith("mem"))
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         } // end of if (command.trim().startsWith("exit"))
00097         if (command.trim().startsWith("exec")) {
00098           res = externalCommand(command.substring(5).trim());
00099         } // end of if (command.trim().startsWith("exec"))
00100     if (command.trim().equals("yes")) {
00101       if (last_command.trim().startsWith("exit")) {
00102         System.exit(10);
00103       } // end of if (last_command.trim().startsWith("exit"))
00104     } // end of if (command.trim().equals("yes"))
00105     if (command.trim().equals("no")) {
00106       if (last_command.trim().startsWith("exit")) {
00107         res = "\r\n";
00108       } // end of if (last_command.trim().startsWith("exit"))
00109     } // end of if (command.trim().equals("no"))
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         } // end of while (stt.hasMoreTokens())
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           } // end of while (line != null)
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           } // end of while (line != null)
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         } // end of try-catch
00199         return "\r\n"+res;
00200   }
00201 
00209   public CommandHandlerIfc getInstance() {
00210     return new CommandHandlerImpl();
00211   }
00212   
00213 }// CommandHandlerImpl
00214 /*
00215  * Changes in file:
00216  *
00217  * $Log: CommandHandlerImpl.java,v $
00218  * Revision 1.1  2002/11/29 15:18:26  kobit
00219  * Refactoring packages, thanks to RefactorIT
00220  *
00221  * Revision 1.1  2002/03/21 09:57:27  kobit
00222  * 1. Small but important redesign:
00223  *    - package 'remotecons.wttools' was removed and all classes
00224  *      were moved to 'remotecons' package
00225  * 2. Added 'asyncMessage' to provide sending messages to remote clients
00226  *    in asynchronous mode
00227  *
00228  * Revision 1.14  2002/01/23 15:49:24  kobit
00229  * Java doc for all classes finishedant clean dist
00230  *
00231  * Revision 1.13  2002/01/21 16:30:19  kobit
00232  * Commenting out code in progress
00233  *
00234  * Revision 1.12  2002/01/20 18:39:40  kobit
00235  * Some inprovments added in executing external commands
00236  *
00237  * Revision 1.11  2002/01/18 14:18:46  kobit
00238  * Next part of javadoc documentation
00239  *
00240  * Revision 1.10  2002/01/17 10:39:09  kobit
00241  * Bug corrected in reading stream from process run by remote console
00242  *
00243  * Revision 1.9  2002/01/15 19:20:01  kobit
00244  * Corrected bug in external command runner
00245  *
00246  * Revision 1.8  2002/01/14 17:02:01  kobit
00247  * Changed interface to CommandHandlersIfc
00248  *
00249  * Revision 1.7  2002/01/13 17:14:45  kobit
00250  * Added commands: ls, show file, echo and support for windows telnet
00251  *
00252  * Revision 1.6  2002/01/12 16:19:21  kobit
00253  * Started code for command 'show filename'
00254  *
00255  * Revision 1.5  2002/01/12 12:54:22  kobit
00256  * Bug in closing connection command fixed
00257  *
00258  * Revision 1.4  2002/01/12 12:25:34  kobit
00259  * Almost finished basic code
00260  *
00261  *
00262  */

Generated on Thu Dec 19 21:00:47 2002 for WTTools - Remote Console by doxygen1.3-rc2