KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > monitor > server > NotifyUtil


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.web.monitor.server;
21
22 import java.io.BufferedReader JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.io.InputStreamReader JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.PrintWriter JavaDoc;
27
28 import java.net.InetAddress JavaDoc;
29 import java.net.MalformedURLException JavaDoc;
30 import java.net.Socket JavaDoc; // Only needed for discovery
31
import java.net.UnknownHostException JavaDoc;
32 import java.net.URL JavaDoc;
33 import java.net.URLConnection JavaDoc;
34
35 import java.util.Enumeration JavaDoc;
36 import java.util.ResourceBundle JavaDoc;
37 import java.util.StringTokenizer JavaDoc;
38 import java.util.Vector JavaDoc;
39
40 import org.netbeans.modules.web.monitor.data.*;
41
42
43 /**
44  * NotifyUtil.java
45  *
46  *
47  * Created: Mon Aug 27 17:21:03 2001
48  *
49  * @author Ana von Klopp
50  * @version
51  */

52
53 class NotifyUtil {
54
55     private String JavaDoc ideServer = null;
56     private Vector JavaDoc otherIDEs = null;
57     private final static String JavaDoc putServlet =
58         "/servlet/org.netbeans.modules.web.monitor.client.PutTransaction?"; //NOI18N
59

60     private final static String JavaDoc replayServlet =
61     "/servlet/org.netbeans.modules.web.monitor.client.ReplaySendXMLServlet"; //NOI18N
62

63
64     private static final boolean debug = false;
65      
66     NotifyUtil() {
67     otherIDEs = new Vector JavaDoc();
68     if(debug) log("NotifyUtil::constructor at end"); //NOI18N
69
}
70
71     /**
72      * Sets the URL of any IDEs that should receive monitor data.
73      * We assume that the first IDE set in this way is the primary
74      * one.
75      *
76      * @param name The host name or IP address of the host on which
77      * the IDE is running.
78      * @param portS The port on which the IDE's internal HTTP server
79      * is running.
80      */

81     void setIDE(String JavaDoc name, String JavaDoc portS) throws MalformedURLException JavaDoc {
82
83     int port = 0;
84     try {
85         port = Integer.parseInt(portS);
86     }
87     catch(NumberFormatException JavaDoc nfe) {
88         throw new MalformedURLException JavaDoc("Port number is not an integer"); //NOI18N
89
}
90
91     try {
92         if(ideServer == null)
93         ideServer = new URL JavaDoc("http", name, port, putServlet).toString(); //NOI18N
94
else
95         otherIDEs.add(new URL JavaDoc("http", name, port, putServlet).toString()); //NOI18N
96

97     }
98     catch(MalformedURLException JavaDoc mux) {
99         throw mux;
100     }
101     }
102         
103     /**
104      * Sets the URL of any IDEs that should receive monitor data.
105      * We assume that the first IDE set in this way is the primary
106      * one.
107      *
108      * @param server A reference to the server on which the IDE is
109      * running in host:port format.
110      */

111     void setIDE(String JavaDoc server) throws MalformedURLException JavaDoc {
112     
113     String JavaDoc host = server.substring(0, server.indexOf(":")); //NOI18N
114
String JavaDoc port = server.substring(server.indexOf(":") + 1); //NOI18N
115
setIDE(host, port);
116     if(debug){
117         log("host: " + host); //NOI18N
118
log("port: " + port); //NOI18N
119
}
120     }
121         
122     void sendRecord(MonitorData monData, String JavaDoc queryStr) {
123     
124     if(debug) log("NotifyUtil::notifyServer"); //NOI18N
125

126     if(ideServer != null) {
127         String JavaDoc urlStr = ideServer.concat(queryStr);
128         if(debug) log("NotifyUtil: url is " + urlStr); //NOI18N
129
sendRecord(urlStr, monData);
130     }
131
132     if(otherIDEs.isEmpty()) return;
133
134     Enumeration JavaDoc ides = otherIDEs.elements();
135     while(ides.hasMoreElements()) {
136
137         String JavaDoc base = (String JavaDoc)ides.nextElement();
138
139         if(debug)
140         log("NotifyUtil: url is " + base.concat(queryStr)); //NOI18N
141
if(!sendRecord(base.concat(queryStr), monData))
142         otherIDEs.remove(base);
143     }
144     return;
145     }
146     
147     private boolean sendRecord(String JavaDoc urlS, MonitorData monData) {
148
149     boolean status = false;
150
151     URL JavaDoc url = null;
152     try {
153         url = new URL JavaDoc(urlS);
154     }
155     catch(MalformedURLException JavaDoc 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 JavaDoc ix) {}
165     recordSender = null;
166     return status;
167     }
168
169
170     /**
171      *
172      * @param id
173      * @param status
174      * @param host
175      */

176     
177     RequestData getRecord(String JavaDoc id, String JavaDoc status, String JavaDoc host, int port) {
178
179     RequestData rd = null;
180
181     // This is the default case - the server port was already known
182

183     StringBuffer JavaDoc uriBuf = new StringBuffer JavaDoc(replayServlet); //NOI18N
184
uriBuf.append("?status="); //NOI18N
185
uriBuf.append(status);
186     uriBuf.append("&id="); //NOI18N
187
uriBuf.append(id);
188
189     URL JavaDoc url = null;
190     try {
191         url = new URL JavaDoc("http", host, port, uriBuf.toString()); //NOI18N
192
}
193     catch(MalformedURLException JavaDoc mux) {
194         // This should not happen
195
}
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 JavaDoc ix) {}
209     recordFetcher = null;
210     return null;
211     }
212     
213     /**
214      * This thread is used to send records to the IDE's internal
215      * server.
216      */

217     class RecordSender extends Thread JavaDoc {
218
219     URL JavaDoc url = null;
220     boolean gotAck = false;
221     boolean triedToRestart = false;
222     URLConnection JavaDoc conn = null;
223     MonitorData monData = null;
224
225     RecordSender(URL JavaDoc url, MonitorData monData) {
226         super("HTTP Monitor, sends data to IDE"); //NOI18N
227
this.monData = monData;
228         this.url = url;
229     }
230
231     public void run() {
232         if(debug)
233         log("NotifyUtil: connecting to " + //NOI18N
234
url.toString());
235
236         PrintWriter JavaDoc out = null;
237         BufferedReader JavaDoc in = null;
238         
239         try {
240         if(debug) log("\tOpening connection"); //NOI18N
241
conn = url.openConnection();
242         conn.setRequestProperty("\tContent-type","text/xml"); //NOI18N
243
conn.setDoOutput(true);
244                 try {
245                     out = new PrintWriter JavaDoc(conn.getOutputStream());
246                 } catch (IOException JavaDoc e) {
247                     String JavaDoc msg = ResourceBundle.getBundle("org.netbeans.modules.web.monitor.server.Bundle").getString("MON_Warning_httpserver_problem"); // NOI18N
248
System.out.println(msg);
249                     return;
250                 }
251         if(debug) log("\tGot output stream"); //NOI18N
252
monData.write(out);
253         out.flush();
254         
255         if(debug) {
256             String JavaDoc file =
257             monData.createTempFile("notifyutil.xml"); // NOI18N
258
log("Wrote replay data to " + file); // NOI18N
259
}
260
261                 try {
262                     in = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(conn.getInputStream()));
263                 } catch (IOException JavaDoc e) {
264                     String JavaDoc msg = ResourceBundle.getBundle("org.netbeans.modules.web.monitor.server.Bundle").getString("MON_Warning_disabled_monitor"); // NOI18N
265
System.out.println(msg);
266                     return;
267                 }
268
269         String JavaDoc inputLine = null;
270
271         while ((inputLine = in.readLine()) != null) {
272
273             if(inputLine.equals(Constants.Comm.ACK)) {
274             if(debug) log("\tGot ack"); //NOI18N
275
gotAck = true;
276             break;
277             }
278         }
279         }
280         catch(IOException JavaDoc ioe) {
281         log(ioe);
282         
283         }
284         catch(NullPointerException JavaDoc npe) {
285         log(npe);
286         }
287         catch(Throwable JavaDoc t) {
288         log(t);
289         }
290         finally {
291         try {
292             in.close();
293         }
294         catch(Throwable JavaDoc t) {
295         }
296         
297         try {
298             out.close();
299         }
300         catch(Throwable JavaDoc t) {
301         }
302         }
303     }
304
305     boolean getStatus() {
306         return gotAck;
307     }
308     }
309
310
311     class RecordFetcher extends Thread JavaDoc {
312
313     URL JavaDoc url = null;
314     boolean gotAck = false;
315     String JavaDoc trace = null;
316
317     MonitorData monData = null;
318
319     RecordFetcher(URL JavaDoc url) {
320         super("HTTP Monitor, retrieves data from IDE"); //NOI18N
321
this.url = url;
322     }
323
324     MonitorData getMonitorData() {
325         return monData;
326     }
327
328     public void run() {
329
330         InputStream JavaDoc in = null;
331         InputStreamReader JavaDoc urlIn = null;
332
333         try {
334         in = url.openStream();
335         urlIn = new InputStreamReader JavaDoc(in);
336         monData = MonitorData.createGraph(urlIn);
337         gotAck = true;
338         }
339         catch(Throwable JavaDoc t) {
340         gotAck = false;
341         trace = Logger.getStackTrace(t);
342         }
343         finally {
344         try {
345             urlIn.close();
346         }
347         catch (Exception JavaDoc ex) {}
348         try {
349             in.close();
350         }
351         catch (Exception JavaDoc ex) {}
352         }
353     }
354     
355     boolean getStatus() {
356         return gotAck;
357     }
358
359     }
360
361     void log(Throwable JavaDoc t) {
362     log(Logger.getStackTrace(t));
363     }
364
365     void log(String JavaDoc msg) {
366     System.out.println("NotifyUtil::" + msg); //NOI18N
367
}
368 } // NotifyUtil
369
Popular Tags