KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > weblogic9 > URLWait


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.j2ee.weblogic9;
21
22 import java.io.IOException JavaDoc;
23 import java.net.Socket JavaDoc;
24 import java.net.InetAddress JavaDoc;
25 import java.net.MalformedURLException JavaDoc;
26 import java.net.HttpURLConnection JavaDoc;
27 import java.net.URL JavaDoc;
28 import java.net.UnknownHostException JavaDoc;
29 import org.openide.ErrorManager;
30
31 /** Utility class
32 * @author Petr Jiricka, Pavel Buzek
33 */

34 public class URLWait {
35
36     /** Will wait until the URL is accessible and returns a valid resource
37      * (response code other then 4xx or 5xx) or the timeout is reached.
38      *
39      * @return true if non error response was obtained
40      */

41     public static boolean waitForUrlReady(URL JavaDoc url, int timeout) {
42         boolean success = false;
43         String JavaDoc host = url.getHost();
44         int port = url.getPort();
45         try {
46             InetAddress.getByName(host);
47         }
48         catch (UnknownHostException JavaDoc e) {
49             return false;
50         }
51         return waitForURLConnection(url, timeout, 100);
52     }
53     
54     private static boolean waitForURLConnection(URL JavaDoc url, int timeout, int retryTime) {
55         Connect connect = new Connect(url, retryTime);
56         Thread JavaDoc t = new Thread JavaDoc(connect);
57         t.start();
58         try {
59             t.join(timeout);
60         } catch(InterruptedException JavaDoc ie) {
61         }
62         if (t.isAlive()) {
63             connect.finishLoop();
64             t.interrupt();//for thread deadlock
65
}
66         return connect.getStatus();
67     }
68
69     private static class Connect implements Runnable JavaDoc {
70         private String JavaDoc host;
71         private URL JavaDoc url;
72         private int retryTime;
73         private boolean status = false;
74         private volatile boolean loop = true;
75
76         public Connect(URL JavaDoc url, int retryTime) {
77             this.url = url;
78             this.retryTime = retryTime;
79             host = url.getHost();
80         }
81
82         public void finishLoop() {
83             loop = false;
84         }
85
86         public void run() {
87             try {
88                 InetAddress.getByName(host);
89             } catch (UnknownHostException JavaDoc e) {
90                 return;
91             }
92             HttpURLConnection JavaDoc con = null;
93             while (loop) {
94                 try {
95                     con = (HttpURLConnection JavaDoc)url.openConnection();
96                     int code = con.getResponseCode();
97                     boolean error = (code == -1) || (code > 399 && code <600);
98                     if (!error) {
99                         status = true;
100                         return;
101                     }
102                 } catch (IOException JavaDoc ioe) {//nothing to do
103
} finally {
104                     if (con != null) con.disconnect();
105                 }
106                 try {
107                     Thread.currentThread().sleep(retryTime);
108                 } catch(InterruptedException JavaDoc ie) {
109                 }
110             }
111         }
112
113         boolean getStatus() {
114             return status;
115         }
116     }
117 }
118
Popular Tags