1 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 32 public class QSAdminShell extends Thread { 33 private static Logger logger = Logger.getLogger( 34 QSAdminShell.class.getName()); 35 36 private String promptName = "QSAdmin"; 37 private String promptPostFix = ":> "; 38 private String prompt = promptName+promptPostFix; 39 40 private String error = "Error: "; 41 42 private BufferedReader in; 43 private String 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 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 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 name) { 90 if(name==null) return; 91 promptName = name; 92 prompt = promptName+promptPostFix; 93 } 94 95 public String 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 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 161 if(command.toLowerCase().startsWith("disconnect")) { 162 try { 163 clientSocket.close(); 164 s_bo.close(); 165 s_br.close(); 166 } catch(Exception 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 e) { 184 println("-ERR "+e); 185 } 186 } qsAdminShell = null; 188 } 189 190 private void print(String text) { 191 System.out.print(text); 192 } 193 194 private void println(String 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 host = 203 server.getQSAdminServer().getServer().getBindAddr().getHostAddress(); 204 connect(host, port); 205 return true; 206 } catch(Exception e) { 207 println(error+e.getMessage()); 208 logger.warning(MyString.getStackTrace(e)); 209 if(clientSocket!=null) { 210 try { 211 clientSocket.close(); 212 } catch(Exception ignore) {} 213 clientSocket = null; 214 } 215 return false; 216 } 217 } 218 219 private void connect(String 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 temp = null; 228 temp = s_br.readLine(); 230 temp = s_br.readLine(); 231 temp = s_br.readLine(); 232 233 if(oldMaxClient==-1) { 234 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 ignoreEx) { 252 println(error+ignoreEx.getMessage()); 253 } 254 } 255 startSocketListener(); 256 } 257 258 private synchronized void sendCommand(String command) throws IOException { 259 sendCommand(command, true); 260 } 261 262 private synchronized void sendCommand(String 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 e) { 276 logger.fine(error+e.getMessage()); 277 } 278 } 279 280 public void startSocketListener() { 281 final String pad = ""; 282 Thread t = new Thread () { 283 public void run() { 284 String 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 } try { 312 clientSocket.close(); 313 clientSocket = null; 314 } catch(Exception 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 ()); 326 String os = System.getProperty("os.name"); 327 if(os!=null && os.toLowerCase().startsWith("windows")) { 328 try { 329 java.awt.Robot robot = new java.awt.Robot (); 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 ignore) { 335 logger.warning("Could not press: Ctrl+Break"); 336 } 337 } else { 338 try { 339 java.awt.Robot robot = new java.awt.Robot (); 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 ignore) { 345 logger.warning("Could not press: Ctrl+\\"); 346 } 347 } 348 } 349 } 350 | Popular Tags |