alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 * 
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.modules.tomcat5;

import java.io.IOException;
import java.net.Socket;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.UnknownHostException;
import org.netbeans.modules.tomcat5.TomcatManager;
import org.openide.ErrorManager;

/** Utility class
* @author  Petr Jiricka
*/
public class URLWait {

    public static boolean waitForStartup(TomcatManager tm, int timeout) {
        boolean success = false;
        try {
            // Create a connection for this command
            String uri = tm.getPlainUri ();
            URL tomcatURL = new URL(uri);

            String host = tomcatURL.getHost();
            int port = tomcatURL.getPort();
            // try connecting to the port first, succeed if connecting successfully
            success = URLWait.waitForURLConnection(host, port, timeout);
        }
        catch (MalformedURLException e) {
            ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
        }
        return success;
    }
    
    /** Waits for startup of a server, waits until the connection has
     * been established. 
     *  @return true if the connection was succressfully established, false if timed out
     */ 
    public static boolean waitForURLConnection(String hostName, int port, int timeout) { 
        // this line finds out whether the host exists, if not, it makes no sense to wait for the connection
        try {
            InetAddress.getByName(hostName);
        }
        catch (UnknownHostException e) {
            return false;
        }
        return waitForURLConnection(hostName, port, timeout, 10);
    }
    
    private static boolean waitForURLConnection(String hostName, int port, int timeout, int retryTime) { 
        Connect connect = new Connect(hostName, port, retryTime); 
        Thread t = new Thread(connect);
        t.start();
        try {
            t.join(timeout);
        } catch(InterruptedException ie) {
        }
        if (t.isAlive()) {
            connect.finishLoop();
            t.interrupt();//for thread deadlock
        }
        return connect.getStatus();
    }

    private static class Connect implements Runnable  {
        String host = null;
        int port = -1;
        int retryTime;
        boolean status = false;
        volatile boolean loop = true;        

        public Connect(String host, int port, int retryTime) {
            this.host = host;
            this.port = port;
            this.retryTime = retryTime; 
        } 

        public void finishLoop() {
            loop = false;
        }

        public void run() {
            try {
                InetAddress.getByName(host);
            } catch (UnknownHostException e) {
                return;
            }
            // Issue #47048 - on some windowsXP boxes previous test, which
            // used Socket to determine whether Tomcat is running sometimes 
            // failed. The HttpURLConnection is used instead now. The 
            // requestURI is "/netbeans-tomcat-status-test" to make it 
            // possible for the http monitor to filter it out.            
            URL url = null;
            try {
                url = new URL("http://" + host + ":" + port  + "/netbeans-tomcat-status-test"); //NOI18N
            } catch(MalformedURLException e) {
                return;
            }
            HttpURLConnection con = null;
            while (loop) {
                try {
                    con = (HttpURLConnection)url.openConnection();
                    con.getResponseCode();
                    status = true;
                    return;
                } catch (IOException ioe) {//nothing to do
                } finally {
                    if (con != null) con.disconnect();
                }
                try {
                    Thread.currentThread().sleep(retryTime);
                } catch(InterruptedException ie) {
                }
            }
        }

        boolean getStatus() {
            return status;
        }
    }
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.