KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > cmdserver > CmdCommandHandler


1 /*
2  * This file is part of the QuickServer library
3  * Copyright (C) 2003-2005 QuickServer.org
4  *
5  * Use, modification, copying and distribution of this software is subject to
6  * the terms and conditions of the GNU Lesser General Public License.
7  * You should have received a copy of the GNU LGP License along with this
8  * library; if not, you can download a copy from <http://www.quickserver.org/>.
9  *
10  * For questions, suggestions, bug-reports, enhancement-requests etc.
11  * visit http://www.quickserver.org
12  *
13  */

14
15 package cmdserver;
16
17 import java.net.*;
18 import java.io.*;
19 import org.quickserver.net.server.ClientCommandHandler;
20 import org.quickserver.net.server.ClientHandler;
21 import java.util.logging.*;
22
23 public class CmdCommandHandler implements ClientCommandHandler {
24     private static Logger logger =
25         Logger.getLogger(CmdCommandHandler.class.getName());
26
27     public void gotConnected(ClientHandler handler)
28         throws SocketTimeoutException, IOException {
29         handler.sendSystemMsg("Connection opened : "+
30             handler.getSocket().getInetAddress());
31
32         handler.sendClientMsg("Welcome to CmdServer v "+CmdServer.VER);
33         handler.sendClientMsg("Send 'quit server' to exit ");
34     }
35
36     public void lostConnection(ClientHandler handler) throws IOException {
37         cleanup(handler);
38         handler.sendSystemMsg("Connection lost : "+handler.getSocket().getInetAddress());
39     }
40     public void closingConnection(ClientHandler handler) throws IOException {
41         cleanup(handler);
42         handler.sendSystemMsg("Connection closed: "+handler.getSocket().getInetAddress());
43     }
44
45     public void handleCommand(ClientHandler handler, String JavaDoc command)
46             throws SocketTimeoutException, IOException {
47         CmdData data = (CmdData)handler.getClientData();
48         if(data.process==null) {
49             handler.sendClientMsg("Starting shell..");
50             startProcess(handler);
51         }
52         if(command.toLowerCase().equals("quit server")) {
53             handler.sendClientMsg("Bye ;-)");
54             handler.closeConnection();
55         } else {
56             try {
57                 OutputStream out = data.process.getOutputStream();
58                 BufferedOutputStream b_out = new BufferedOutputStream(out);
59                 command = command+"\n";
60                 b_out.write(command.getBytes(),0,command.length());
61                 b_out.flush();
62             } catch(Exception JavaDoc e) {
63                 handler.sendClientMsg("Error : "+e);
64             }
65         }
66     }
67
68     //////////////////
69
// helper methods
70
/////////////////
71
private void startProcess(ClientHandler handler)
72             throws IOException {
73         CmdData data = (CmdData)handler.getClientData();
74         try {
75             Object JavaDoc[] store = handler.getServer().getStoreObjects();
76             String JavaDoc process = "cmd.exe";
77             if(store!=null);
78                 process = (String JavaDoc)store[0];
79             data.process = Runtime.getRuntime().exec(process);
80         } catch (IOException e) {
81             handler.sendClientMsg("Error"+e);
82         }
83         PReader preader = new PReader(handler);
84         preader.start();
85     }
86
87     private void cleanup(ClientHandler handler) {
88         CmdData data = (CmdData)handler.getClientData();
89         if(data==null || data.process==null)
90             return;
91         data.process.destroy();
92         data.process = null;
93     }
94 }
95
96 /*
97  * Class that keeps reading from the process and sends any data
98  * to client
99  */

100 class PReader extends Thread JavaDoc {
101     ClientHandler handler;
102     private static Logger logger =
103         Logger.getLogger(PReader.class.getName());
104
105     public PReader(ClientHandler handler){
106         this.handler = handler;
107     }
108
109     public void run() {
110         CmdData data = (CmdData)handler.getClientData();
111         if(data.process==null) //if no process
112
return;
113         InputStream in = data.process.getInputStream();
114         BufferedReader b_in = new BufferedReader(
115             new InputStreamReader(in));
116         String JavaDoc got=null;
117         while(true){
118             try {
119                 got=b_in.readLine();
120                 if(got==null || handler==null)
121                     break;
122                 handler.sendClientMsg(got);
123             } catch(NullPointerException JavaDoc e) {
124                 /*
125                 When client closes connection,
126                 then cleanup was called, which destroyes the process,
127                 so b_in becames null. So we ignore the error.
128                 */

129                 if(data.process!=null)
130                     logger.logp(Level.SEVERE, "PReader", "run",
131                         "Error in PReader : " + e);
132             } catch(Exception JavaDoc e) {
133                 if(handler==null || data.process==null)
134                     break;
135                 try {
136                     handler.isConnected();
137                     handler.sendClientMsg("Error "+e);
138                     break;
139                 } catch(SocketException se) {
140                     //socket was closed
141
} catch(IOException ie) {
142                     logger.logp(Level.FINEST, "PReader", "run",
143                         "IOError in PReader : "+e+"\n\t"+ie);
144                 } catch(Exception JavaDoc ee) {
145                     logger.logp(Level.SEVERE, "PReader", "run",
146                         "Error in PReader : "+e+"\n\t"+ee);
147                 }
148             }
149         }
150     }
151 }
152
Popular Tags