KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quickserver > net > qsadmin > QSAdminShell


1 /*
2  * This file is part of the QuickServer library
3  * Copyright (C) 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 org.quickserver.net.qsadmin;
16
17 import java.io.*;
18 import java.util.*;
19 import java.net.*;
20 import org.quickserver.net.server.QuickServer;
21 import org.quickserver.util.xmlreader.*;
22 import org.quickserver.util.MyString;
23 import java.util.logging.*;
24
25 /**
26  * QSAdmin Command Shell.
27  * This class will auto increase the maxClient by 1 if for QSAdminServer
28  * maxClient is not equals to -1 and 0 (no other client except shell is allowed).
29  * @author Akshathkumar Shetty
30  * @since 1.3.2
31  */

32 public class QSAdminShell extends Thread JavaDoc {
33     private static Logger logger = Logger.getLogger(
34             QSAdminShell.class.getName());
35     
36     private String JavaDoc promptName = "QSAdmin";
37     private String JavaDoc promptPostFix = ":> ";
38     private String JavaDoc prompt = promptName+promptPostFix;
39
40     private String JavaDoc error = "Error: ";
41
42     private BufferedReader in;
43     private String JavaDoc command = "";
44
45     private Socket clientSocket = null;
46     private QuickServer server;
47
48     private InputStream s_in;
49     private OutputStream s_out;
50     private BufferedReader s_br;
51     private BufferedOutputStream s_bo;
52
53     private boolean gotResponse;
54     private boolean multilineResponse;
55     private boolean stop = false;
56
57     private static QSAdminShell qsAdminShell;
58     private static long oldMaxClient = -1;
59     
60
61     public static QSAdminShell getInstance(QuickServer server, String JavaDoc promptName) {
62         if(qsAdminShell==null) {
63             if(server!=null) {
64                 qsAdminShell = new QSAdminShell(server, promptName);
65                 qsAdminShell.start();
66             } else {
67                 return null;
68             }
69         } else {
70             if(server!=null)
71                 qsAdminShell.server = server;
72             if(promptName!=null)
73                 qsAdminShell.promptName = promptName;
74             qsAdminShell.stop = false;
75         }
76         return qsAdminShell;
77     }
78
79     private QSAdminShell(QuickServer server, String JavaDoc promptName) {
80         super("GUIAdminShell");
81         setDaemon(true);
82         this.server = server;
83         if(promptName!=null)
84             setPromptName(promptName);
85         in = new BufferedReader(new InputStreamReader(System.in));
86         logger.fine("Starting QSAdmin Shell");
87     }
88
89     public void setPromptName(String JavaDoc name) {
90         if(name==null) return;
91         promptName = name;
92         prompt = promptName+promptPostFix;
93     }
94
95     public String JavaDoc getPromptName() {
96         return promptName;
97     }
98
99     public void stopShell() throws IOException {
100         stop = true;
101         clientSocket.close();
102     }
103
104     public void run() {
105         try {
106             for(int i=0;i<3;i++) {
107                 sleep(500);
108                 if(server.getQSAdminServer().getServer().isClosed()==false) {
109                     connect();
110                     break;
111                 }
112                 if(i==3) {
113                     logger.warning(error+"QSAdminServer is not running!! So stopping QSAdminShell.");
114                     return;
115                 }
116             }
117         } catch(Exception JavaDoc e) {
118             logger.fine(error+e.getMessage());
119         }
120         while(stop==false) {
121             try {
122                 print(prompt);
123                 command = in.readLine();
124                 
125                 if(stop) {
126                     if(command.trim().equals("")==false)
127                         System.out.println("Command ignored since shell was closed.");
128                     break;
129                 }
130
131                 if(command==null) {
132                     System.out.println("");
133                     logger.severe("User must have forced an exit at shell!");
134                     continue;
135                 }
136
137                 if(command.equals("FullThreadDump")) {
138                     tryFullThreadDump();
139                     continue;
140                 }
141
142                 /*
143                 if(command.toLowerCase().startsWith("shellto")) {
144                     try {
145                         StringTokenizer st =
146                             new StringTokenizer(command, " ");
147                         if(st.countTokens()!=3) {
148                             throw new Exception("Bad param sent to shellto command!");
149                         }
150
151                         clientSocket.close();
152                         clientSocket = null;
153                         st.nextToken();
154                         connect(st.nextToken(),
155                             Integer.parseInt(st.nextToken()));
156                         continue;
157                     } catch(Exception e) {
158                         logger.fine(error+e.getMessage());
159                     }
160                 } else */

161                 if(command.toLowerCase().startsWith("disconnect")) {
162                     try {
163                         clientSocket.close();
164                         s_bo.close();
165                         s_br.close();
166                     } catch(Exception JavaDoc er) {
167                         println("-ERR "+er);
168                     }
169                     break;
170                 }
171
172                 if(command.trim().equals("")==false) {
173                     if(clientSocket==null) {
174                         if(connect()==false)
175                             continue;
176                     }
177                     sendCommand(command);
178                 } else {
179                     if(clientSocket==null) {
180                         connect();
181                     }
182                 }
183             } catch(Exception JavaDoc e) {
184                 println("-ERR "+e);
185             }
186         }//end while
187
qsAdminShell = null;
188     }
189
190     private void print(String JavaDoc text) {
191         System.out.print(text);
192     }
193
194     private void println(String JavaDoc text) {
195         System.out.println(text);
196     }
197
198     private boolean connect() throws IOException {
199         try {
200             server.getQSAdminServer().getServer().nextClientIsTrusted();
201             int port = server.getQSAdminServer().getServer().getPort();
202             String JavaDoc host =
203                 server.getQSAdminServer().getServer().getBindAddr().getHostAddress();
204             connect(host, port);
205             return true;
206         } catch(Exception JavaDoc e) {
207             println(error+e.getMessage());
208             logger.warning(MyString.getStackTrace(e));
209             if(clientSocket!=null) {
210                 try {
211                     clientSocket.close();
212                 } catch(Exception JavaDoc ignore) {/*ignore*/}
213                 clientSocket = null;
214             }
215             return false;
216         }
217     }
218
219     private void connect(String JavaDoc host, int port) throws IOException {
220         clientSocket = new Socket(host, port);
221         clientSocket.setSoTimeout(0);
222         s_in = clientSocket.getInputStream();
223         s_out = clientSocket.getOutputStream();
224         s_br = new BufferedReader(new InputStreamReader(s_in));
225         s_bo = new BufferedOutputStream(s_out);
226
227         String JavaDoc temp = null;
228         //skip header
229
temp = s_br.readLine();
230         temp = s_br.readLine();
231         temp = s_br.readLine();
232
233         if(oldMaxClient==-1) {
234             //increase maxClient for self by 1
235
try {
236                 long maxC = -1;
237                 sendCommand("get self maxClient", false);
238                 temp = s_br.readLine();
239                 maxC = Long.parseLong(temp.substring(4));
240                 if(maxC!=-1 && maxC!=0) {
241                     oldMaxClient = maxC;
242                     maxC++;
243                     sendCommand("set self maxClient "+maxC, false);
244                     temp = s_br.readLine();
245                     if(temp.startsWith("+OK")==false) {
246                         println(error+"Could not increase max client from QSAdmin : "+s_br);
247                     }
248                 }
249             } catch(IOException ioe) {
250                 throw ioe;
251             } catch(Exception JavaDoc ignoreEx) {
252                 println(error+ignoreEx.getMessage());
253             }
254         }
255         startSocketListener();
256     }
257
258     private synchronized void sendCommand(String JavaDoc command) throws IOException {
259         sendCommand(command, true);
260     }
261
262     private synchronized void sendCommand(String JavaDoc command, boolean wait)
263             throws IOException {
264         if(clientSocket==null)
265             println(error+"Not connected yet");
266         command += QuickServer.getNewLine();
267         gotResponse = false;
268         byte d[] = command.getBytes();
269         s_bo.write(d, 0, d.length);
270         s_bo.flush();
271         
272         try {
273             while(wait==true && gotResponse==false)
274                 sleep(100);
275         } catch(InterruptedException JavaDoc e) {
276             logger.fine(error+e.getMessage());
277         }
278     }
279
280     public void startSocketListener() {
281         final String JavaDoc pad = "";
282         Thread JavaDoc t = new Thread JavaDoc() {
283             public void run() {
284                 String JavaDoc rec = "";
285                 while(true) {
286                     try {
287                         rec = s_br.readLine();
288                     } catch(IOException e) {
289                         logger.info("Shell Closed! "+e.getMessage());
290                         break;
291                     }
292                     if(rec==null) {
293                         clientSocket=null;
294                         break;
295                     }
296
297                     if(rec.equals("+OK info follows"))
298                         multilineResponse = true;
299                     
300                     println(pad+rec);
301
302                     if(multilineResponse==false)
303                         gotResponse=true;
304                     else {
305                         if(rec.equals(".")) {
306                             gotResponse=true;
307                             multilineResponse=false;
308                         }
309                     }
310                 } //end of while
311
try {
312                     clientSocket.close();
313                     clientSocket = null;
314                 } catch(Exception JavaDoc e) {
315                     logger.fine(error+e.getMessage());
316                 }
317             }
318         };
319         t.setDaemon(true);
320         t.setName("GUIAdminShell-SocketListener");
321         t.start();
322     }
323
324     public static void tryFullThreadDump() {
325         System.out.println("Trying to get Full Thread Dump @ "+new java.util.Date JavaDoc());
326         String JavaDoc os = System.getProperty("os.name");
327         if(os!=null && os.toLowerCase().startsWith("windows")) {
328             try {
329                 java.awt.Robot JavaDoc robot = new java.awt.Robot JavaDoc();
330                 robot.keyPress(java.awt.event.KeyEvent.VK_CONTROL);
331                 robot.keyPress(java.awt.event.KeyEvent.VK_CANCEL);
332                 robot.keyRelease(java.awt.event.KeyEvent.VK_CANCEL);
333                 robot.keyRelease(java.awt.event.KeyEvent.VK_CONTROL);
334             } catch(Exception JavaDoc ignore) {
335                 logger.warning("Could not press: Ctrl+Break");
336             }
337         } else {
338             try {
339                 java.awt.Robot JavaDoc robot = new java.awt.Robot JavaDoc();
340                 robot.keyPress(java.awt.event.KeyEvent.VK_CONTROL);
341                 robot.keyPress(java.awt.event.KeyEvent.VK_BACK_SLASH);
342                 robot.keyRelease(java.awt.event.KeyEvent.VK_BACK_SLASH);
343                 robot.keyRelease(java.awt.event.KeyEvent.VK_CONTROL);
344             } catch(Exception JavaDoc ignore) {
345                 logger.warning("Could not press: Ctrl+\\");
346             }
347         }
348     }
349 }
350
Popular Tags