001: /*
002: * This file is part of the QuickServer library
003: * Copyright (C) 2003-2005 QuickServer.org
004: *
005: * Use, modification, copying and distribution of this software is subject to
006: * the terms and conditions of the GNU Lesser General Public License.
007: * You should have received a copy of the GNU LGP License along with this
008: * library; if not, you can download a copy from <http://www.quickserver.org/>.
009: *
010: * For questions, suggestions, bug-reports, enhancement-requests etc.
011: * visit http://www.quickserver.org
012: *
013: */
014:
015: package cmdserver;
016:
017: import java.net.*;
018: import java.io.*;
019: import org.quickserver.net.server.ClientCommandHandler;
020: import org.quickserver.net.server.ClientHandler;
021: import java.util.logging.*;
022:
023: public class CmdCommandHandler implements ClientCommandHandler {
024: private static Logger logger = Logger
025: .getLogger(CmdCommandHandler.class.getName());
026:
027: public void gotConnected(ClientHandler handler)
028: throws SocketTimeoutException, IOException {
029: handler.sendSystemMsg("Connection opened : "
030: + handler.getSocket().getInetAddress());
031:
032: handler
033: .sendClientMsg("Welcome to CmdServer v "
034: + CmdServer.VER);
035: handler.sendClientMsg("Send 'quit server' to exit ");
036: }
037:
038: public void lostConnection(ClientHandler handler)
039: throws IOException {
040: cleanup(handler);
041: handler.sendSystemMsg("Connection lost : "
042: + handler.getSocket().getInetAddress());
043: }
044:
045: public void closingConnection(ClientHandler handler)
046: throws IOException {
047: cleanup(handler);
048: handler.sendSystemMsg("Connection closed: "
049: + handler.getSocket().getInetAddress());
050: }
051:
052: public void handleCommand(ClientHandler handler, String command)
053: throws SocketTimeoutException, IOException {
054: CmdData data = (CmdData) handler.getClientData();
055: if (data.process == null) {
056: handler.sendClientMsg("Starting shell..");
057: startProcess(handler);
058: }
059: if (command.toLowerCase().equals("quit server")) {
060: handler.sendClientMsg("Bye ;-)");
061: handler.closeConnection();
062: } else {
063: try {
064: OutputStream out = data.process.getOutputStream();
065: BufferedOutputStream b_out = new BufferedOutputStream(
066: out);
067: command = command + "\n";
068: b_out.write(command.getBytes(), 0, command.length());
069: b_out.flush();
070: } catch (Exception e) {
071: handler.sendClientMsg("Error : " + e);
072: }
073: }
074: }
075:
076: //////////////////
077: // helper methods
078: /////////////////
079: private void startProcess(ClientHandler handler) throws IOException {
080: CmdData data = (CmdData) handler.getClientData();
081: try {
082: Object[] store = handler.getServer().getStoreObjects();
083: String process = "cmd.exe";
084: if (store != null)
085: ;
086: process = (String) store[0];
087: data.process = Runtime.getRuntime().exec(process);
088: } catch (IOException e) {
089: handler.sendClientMsg("Error" + e);
090: }
091: PReader preader = new PReader(handler);
092: preader.start();
093: }
094:
095: private void cleanup(ClientHandler handler) {
096: CmdData data = (CmdData) handler.getClientData();
097: if (data == null || data.process == null)
098: return;
099: data.process.destroy();
100: data.process = null;
101: }
102: }
103:
104: /*
105: * Class that keeps reading from the process and sends any data
106: * to client
107: */
108: class PReader extends Thread {
109: ClientHandler handler;
110: private static Logger logger = Logger.getLogger(PReader.class
111: .getName());
112:
113: public PReader(ClientHandler handler) {
114: this .handler = handler;
115: }
116:
117: public void run() {
118: CmdData data = (CmdData) handler.getClientData();
119: if (data.process == null) //if no process
120: return;
121: InputStream in = data.process.getInputStream();
122: BufferedReader b_in = new BufferedReader(new InputStreamReader(
123: in));
124: String got = null;
125: while (true) {
126: try {
127: got = b_in.readLine();
128: if (got == null || handler == null)
129: break;
130: handler.sendClientMsg(got);
131: } catch (NullPointerException e) {
132: /*
133: When client closes connection,
134: then cleanup was called, which destroyes the process,
135: so b_in becames null. So we ignore the error.
136: */
137: if (data.process != null)
138: logger.logp(Level.SEVERE, "PReader", "run",
139: "Error in PReader : " + e);
140: } catch (Exception e) {
141: if (handler == null || data.process == null)
142: break;
143: try {
144: handler.isConnected();
145: handler.sendClientMsg("Error " + e);
146: break;
147: } catch (SocketException se) {
148: //socket was closed
149: } catch (IOException ie) {
150: logger.logp(Level.FINEST, "PReader", "run",
151: "IOError in PReader : " + e + "\n\t" + ie);
152: } catch (Exception ee) {
153: logger.logp(Level.SEVERE, "PReader", "run",
154: "Error in PReader : " + e + "\n\t" + ee);
155: }
156: }
157: }
158: }
159: }
|