1 19 20 package org.netbeans.modules.j2ee.weblogic9; 21 22 import java.io.IOException ; 23 import java.net.Socket ; 24 import java.net.InetAddress ; 25 import java.net.MalformedURLException ; 26 import java.net.HttpURLConnection ; 27 import java.net.URL ; 28 import java.net.UnknownHostException ; 29 import org.openide.ErrorManager; 30 31 34 public class URLWait { 35 36 41 public static boolean waitForUrlReady(URL url, int timeout) { 42 boolean success = false; 43 String host = url.getHost(); 44 int port = url.getPort(); 45 try { 46 InetAddress.getByName(host); 47 } 48 catch (UnknownHostException e) { 49 return false; 50 } 51 return waitForURLConnection(url, timeout, 100); 52 } 53 54 private static boolean waitForURLConnection(URL url, int timeout, int retryTime) { 55 Connect connect = new Connect(url, retryTime); 56 Thread t = new Thread (connect); 57 t.start(); 58 try { 59 t.join(timeout); 60 } catch(InterruptedException ie) { 61 } 62 if (t.isAlive()) { 63 connect.finishLoop(); 64 t.interrupt(); } 66 return connect.getStatus(); 67 } 68 69 private static class Connect implements Runnable { 70 private String host; 71 private URL url; 72 private int retryTime; 73 private boolean status = false; 74 private volatile boolean loop = true; 75 76 public Connect(URL 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 e) { 90 return; 91 } 92 HttpURLConnection con = null; 93 while (loop) { 94 try { 95 con = (HttpURLConnection )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 ioe) { } finally { 104 if (con != null) con.disconnect(); 105 } 106 try { 107 Thread.currentThread().sleep(retryTime); 108 } catch(InterruptedException ie) { 109 } 110 } 111 } 112 113 boolean getStatus() { 114 return status; 115 } 116 } 117 } 118 | Popular Tags |