Methods Summary |
---|
public void | execute()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.String | getScriptWithExtension(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 boolean | isTomcatRunning()
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 boolean | isURLValid(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 void | logException(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 void | runScript()
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 void | setCatalinaHome(java.lang.String catalinaHome)
this.catalinaHome = catalinaHome;
|
public void | setScriptToExecute(java.lang.String scriptToExecute)
this.scriptToExecute = scriptToExecute;
|
public void | setTestURL(java.lang.String testURL)
try {
this.testURL = new URL(testURL);
} catch (MalformedURLException e) {
throw new BuildException("Invalid URL: " + testURL);
}
|
public void | setTimeout(long timeout)
this.timeout = timeout;
|
private void | sleep(long ms)
try {
Thread.sleep(ms);
} catch (InterruptedException e) {
}
|
private static void | validateScript(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());
}
|