FileDocCategorySizeDatePackage
TomcatSupport.javaAPI DocExample6139Wed Mar 19 19:38:06 GMT 2003com.oreilly.javaxp.tomcat.tasks

TomcatSupport

public class TomcatSupport extends Object
author
Brian M. Coyner
version
$Id: TomcatSupport.java,v 1.8 2003/03/18 00:02:38 jepc Exp $

Fields Summary
private org.apache.tools.ant.Task
task
private URL
testURL
private String
catalinaHome
private String
scriptToExecute
private long
timeout
private boolean
isStarting
Constructors Summary
public TomcatSupport(org.apache.tools.ant.Task task, String scriptToExecute, boolean isStarting)

param
task the task that invoked this class. It's used only for logging.
param
scriptToExecute the Tomcat 4.1.12 script used to start or stop the server.

        this.task = task;
        this.scriptToExecute = getScriptWithExtension(scriptToExecute);
        this.isStarting = isStarting;

        // I have a pretty slow machine and this seems to be long enough.
        this.timeout = 20000;
    
Methods Summary
public voidexecute()
Executes a Tomcat script to start or stop the server.


        // if the server is up and we are trying to start it then return
        // if the server is down and we are tryint to stop it then return
        if (isTomcatRunning() == this.isStarting) {
            if (this.isStarting) {
                this.task.log("Tomcat is already started!");
            } else {
                this.task.log("Tomcat is *not* running!");
            }
            return;
        }

        runScript();

        boolean didTimeout = true;
        this.timeout = System.currentTimeMillis() + this.timeout;
        while (System.currentTimeMillis() < this.timeout) {
            sleep(500);
            if (isTomcatRunning() == this.isStarting) {
                didTimeout = false;
                break;
            }
        }

        if (didTimeout) {
            throw new BuildException("The server was not started " +
                                     "successfully. Please make sure that your buildfile " +
                                     "specifies a long enough timeout period AND that " +
                                     "your environment is setup correctly.");
        }

        if (this.isStarting) {
            this.task.log("Tomcat is started!");
        } else {
            this.task.log("Tomcat is stopped!");
        }
    
private java.lang.StringgetScriptWithExtension(java.lang.String scriptToExecute)

        String os = System.getProperty("os.name").toLowerCase(Locale.US);
        // if the OS name is a flavor of windows then execute a .bat file.
        if (os.indexOf("windows") != -1) {
            return scriptToExecute + ".bat";
        } else {
            return scriptToExecute + ".sh";
        }
    
private booleanisTomcatRunning()


        HttpURLConnection conn = null;
        try {
            conn = (HttpURLConnection) this.testURL.openConnection();
            conn.connect();
            isURLValid(conn);
            return true;
        } catch (IOException e) {
            logException(e);
            return false;
        } finally {
            if (conn != null) {
                conn.disconnect();
            }
        }
    
private booleanisURLValid(java.net.HttpURLConnection conn)

        int responseCode = 0;
        try {
            responseCode = conn.getResponseCode();
            // Response Codes 400 and above represent errors according
            // to the HTTP 1.1 specification available in RFC 2616 at
            // http://www.ietf.org/rfc/rfc2616.txt
            return (responseCode >= 100 && responseCode < 400);
        } catch (IOException e) {
            logException(e);
            return false;
        }
    
private voidlogException(java.lang.Exception e)

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PrintWriter writer = new PrintWriter(baos);
        e.printStackTrace(writer);
        writer.close();
        this.task.log(new String(baos.toByteArray()), Project.MSG_DEBUG);
    
public voidrunScript()

        validateScript(this.catalinaHome, this.scriptToExecute);

        this.task.log((this.isStarting ? "Starting" : "Stopping") +
                      " Tomcat...");

        String script = this.catalinaHome +
                File.separator + "bin" + File.separator +
                this.scriptToExecute;


        this.task.log("    Using script: " + script);

        try {
            Runtime.getRuntime().exec(script);
        } catch (IOException e) {
            throw new BuildException(e);
        }
    
public voidsetCatalinaHome(java.lang.String catalinaHome)

        this.catalinaHome = catalinaHome;
    
public voidsetScriptToExecute(java.lang.String scriptToExecute)

        this.scriptToExecute = scriptToExecute;
    
public voidsetTestURL(java.lang.String testURL)

        try {
            this.testURL = new URL(testURL);
        } catch (MalformedURLException e) {
            throw new BuildException("Invalid URL: " + testURL);
        }
    
public voidsetTimeout(long timeout)

        this.timeout = timeout;
    
private voidsleep(long ms)

        try {
            Thread.sleep(ms);
        } catch (InterruptedException e) {
        }
    
private static voidvalidateScript(java.lang.String path, java.lang.String script)

        File file = new File(path + File.separator + "bin" +
                             File.separator + script);
        if (!file.exists() || file.isDirectory()) {
            throw new BuildException("Invalid File: " + file.getAbsolutePath());
        }