1 19 20 package org.netbeans.modules.web.monitor.server; 21 22 import java.io.BufferedReader ; 23 import java.io.InputStream ; 24 import java.io.InputStreamReader ; 25 import java.io.IOException ; 26 import java.io.PrintWriter ; 27 28 import java.net.InetAddress ; 29 import java.net.MalformedURLException ; 30 import java.net.Socket ; import java.net.UnknownHostException ; 32 import java.net.URL ; 33 import java.net.URLConnection ; 34 35 import java.util.Enumeration ; 36 import java.util.ResourceBundle ; 37 import java.util.StringTokenizer ; 38 import java.util.Vector ; 39 40 import org.netbeans.modules.web.monitor.data.*; 41 42 43 52 53 class NotifyUtil { 54 55 private String ideServer = null; 56 private Vector otherIDEs = null; 57 private final static String putServlet = 58 "/servlet/org.netbeans.modules.web.monitor.client.PutTransaction?"; 60 private final static String replayServlet = 61 "/servlet/org.netbeans.modules.web.monitor.client.ReplaySendXMLServlet"; 63 64 private static final boolean debug = false; 65 66 NotifyUtil() { 67 otherIDEs = new Vector (); 68 if(debug) log("NotifyUtil::constructor at end"); } 70 71 81 void setIDE(String name, String portS) throws MalformedURLException { 82 83 int port = 0; 84 try { 85 port = Integer.parseInt(portS); 86 } 87 catch(NumberFormatException nfe) { 88 throw new MalformedURLException ("Port number is not an integer"); } 90 91 try { 92 if(ideServer == null) 93 ideServer = new URL ("http", name, port, putServlet).toString(); else 95 otherIDEs.add(new URL ("http", name, port, putServlet).toString()); 97 } 98 catch(MalformedURLException mux) { 99 throw mux; 100 } 101 } 102 103 111 void setIDE(String server) throws MalformedURLException { 112 113 String host = server.substring(0, server.indexOf(":")); String port = server.substring(server.indexOf(":") + 1); setIDE(host, port); 116 if(debug){ 117 log("host: " + host); log("port: " + port); } 120 } 121 122 void sendRecord(MonitorData monData, String queryStr) { 123 124 if(debug) log("NotifyUtil::notifyServer"); 126 if(ideServer != null) { 127 String urlStr = ideServer.concat(queryStr); 128 if(debug) log("NotifyUtil: url is " + urlStr); sendRecord(urlStr, monData); 130 } 131 132 if(otherIDEs.isEmpty()) return; 133 134 Enumeration ides = otherIDEs.elements(); 135 while(ides.hasMoreElements()) { 136 137 String base = (String )ides.nextElement(); 138 139 if(debug) 140 log("NotifyUtil: url is " + base.concat(queryStr)); if(!sendRecord(base.concat(queryStr), monData)) 142 otherIDEs.remove(base); 143 } 144 return; 145 } 146 147 private boolean sendRecord(String urlS, MonitorData monData) { 148 149 boolean status = false; 150 151 URL url = null; 152 try { 153 url = new URL (urlS); 154 } 155 catch(MalformedURLException mux) { 156 return status; 157 } 158 RecordSender recordSender = new RecordSender(url, monData); 159 recordSender.start(); 160 try { 161 recordSender.join(3000); 162 status = recordSender.getStatus(); 163 } 164 catch(InterruptedException ix) {} 165 recordSender = null; 166 return status; 167 } 168 169 170 176 177 RequestData getRecord(String id, String status, String host, int port) { 178 179 RequestData rd = null; 180 181 183 StringBuffer uriBuf = new StringBuffer (replayServlet); uriBuf.append("?status="); uriBuf.append(status); 186 uriBuf.append("&id="); uriBuf.append(id); 188 189 URL url = null; 190 try { 191 url = new URL ("http", host, port, uriBuf.toString()); } 193 catch(MalformedURLException mux) { 194 } 196 197 if(debug) log(url.toString()); 198 199 RecordFetcher recordFetcher = new RecordFetcher(url); 200 recordFetcher.start(); 201 try { 202 recordFetcher.join(3000); 203 if(recordFetcher.getStatus()) 204 return recordFetcher.getMonitorData().getRequestData(); 205 else 206 return null; 207 } 208 catch(InterruptedException ix) {} 209 recordFetcher = null; 210 return null; 211 } 212 213 217 class RecordSender extends Thread { 218 219 URL url = null; 220 boolean gotAck = false; 221 boolean triedToRestart = false; 222 URLConnection conn = null; 223 MonitorData monData = null; 224 225 RecordSender(URL url, MonitorData monData) { 226 super("HTTP Monitor, sends data to IDE"); this.monData = monData; 228 this.url = url; 229 } 230 231 public void run() { 232 if(debug) 233 log("NotifyUtil: connecting to " + url.toString()); 235 236 PrintWriter out = null; 237 BufferedReader in = null; 238 239 try { 240 if(debug) log("\tOpening connection"); conn = url.openConnection(); 242 conn.setRequestProperty("\tContent-type","text/xml"); conn.setDoOutput(true); 244 try { 245 out = new PrintWriter (conn.getOutputStream()); 246 } catch (IOException e) { 247 String msg = ResourceBundle.getBundle("org.netbeans.modules.web.monitor.server.Bundle").getString("MON_Warning_httpserver_problem"); System.out.println(msg); 249 return; 250 } 251 if(debug) log("\tGot output stream"); monData.write(out); 253 out.flush(); 254 255 if(debug) { 256 String file = 257 monData.createTempFile("notifyutil.xml"); log("Wrote replay data to " + file); } 260 261 try { 262 in = new BufferedReader (new InputStreamReader (conn.getInputStream())); 263 } catch (IOException e) { 264 String msg = ResourceBundle.getBundle("org.netbeans.modules.web.monitor.server.Bundle").getString("MON_Warning_disabled_monitor"); System.out.println(msg); 266 return; 267 } 268 269 String inputLine = null; 270 271 while ((inputLine = in.readLine()) != null) { 272 273 if(inputLine.equals(Constants.Comm.ACK)) { 274 if(debug) log("\tGot ack"); gotAck = true; 276 break; 277 } 278 } 279 } 280 catch(IOException ioe) { 281 log(ioe); 282 283 } 284 catch(NullPointerException npe) { 285 log(npe); 286 } 287 catch(Throwable t) { 288 log(t); 289 } 290 finally { 291 try { 292 in.close(); 293 } 294 catch(Throwable t) { 295 } 296 297 try { 298 out.close(); 299 } 300 catch(Throwable t) { 301 } 302 } 303 } 304 305 boolean getStatus() { 306 return gotAck; 307 } 308 } 309 310 311 class RecordFetcher extends Thread { 312 313 URL url = null; 314 boolean gotAck = false; 315 String trace = null; 316 317 MonitorData monData = null; 318 319 RecordFetcher(URL url) { 320 super("HTTP Monitor, retrieves data from IDE"); this.url = url; 322 } 323 324 MonitorData getMonitorData() { 325 return monData; 326 } 327 328 public void run() { 329 330 InputStream in = null; 331 InputStreamReader urlIn = null; 332 333 try { 334 in = url.openStream(); 335 urlIn = new InputStreamReader (in); 336 monData = MonitorData.createGraph(urlIn); 337 gotAck = true; 338 } 339 catch(Throwable t) { 340 gotAck = false; 341 trace = Logger.getStackTrace(t); 342 } 343 finally { 344 try { 345 urlIn.close(); 346 } 347 catch (Exception ex) {} 348 try { 349 in.close(); 350 } 351 catch (Exception ex) {} 352 } 353 } 354 355 boolean getStatus() { 356 return gotAck; 357 } 358 359 } 360 361 void log(Throwable t) { 362 log(Logger.getStackTrace(t)); 363 } 364 365 void log(String msg) { 366 System.out.println("NotifyUtil::" + msg); } 368 } | Popular Tags |