/**
*#########################################################################
*
* A component of the Gatherer application, part of the Greenstone digital
* library suite from the New Zealand Digital Library Project at the
* University of Waikato, New Zealand.
*
*
*
* Author: Sam McIntosh, Greenstone Digital Library, University of Waikato
*
*
*
* Copyright (C) 2011 New Zealand Digital Library Project
*
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*########################################################################
*/
package org.greenstone.gatherer.util;
import java.io.File;
/** Class for GS3, for issuing ant commands from GLI */
public class GS3ServerThread extends Thread
{
String _gsdl3_src_path = "";
String _ant_command = "";
public GS3ServerThread(String gsdl3_src_path, String ant_command)
{
_gsdl3_src_path = gsdl3_src_path;
_ant_command = ant_command; // "restart"
}
public void run()
{
try
{
///System.err.println("**** GS3 server : " + _ant_command);
String shellCommand = null;
Process p = null;
if (Utility.isWindows())
{
if(_ant_command.indexOf("start") != -1) { // running an "ant (re)start" command on windows, run start
_ant_command = "start";
}
// The path in quotes, and the entire sequence of commands in quotes as well
// E.g. the following works: cmd /C "cd "C:\path\to\greenstone3" && ant stop"
// and it preserves any spaces in the path to GSDL3SRCHOME (_gsdl3_src_path).
p = Runtime.getRuntime().exec("cmd /C \"cd \"" + _gsdl3_src_path + File.separator + "\" && ant " + _ant_command + "\"");
}
else
{
if(_ant_command.indexOf("start") != -1) { // if running an "ant (re)start" command on non-Windows, run restart
_ant_command = "restart";
}
p = Runtime.getRuntime().exec(new String[]{"/bin/bash", "-c", "ant " + _ant_command + " -f \"" + _gsdl3_src_path + File.separator + "build.xml\""});
}
if (p != null) {
// in order for the process.waitFor() method to work with Java 6 (JRE 6 is included in GS binaries)
// need to make sure the IOstreams of the process are not blocked. For Java 7, this is not necessary
// and a waitFor() is sufficient. But with Java 6, the waitFor() causes the server to finally start
// after the user has quit GLI.
// Process takes no input, but we will still catch this iostream too
// And we'll catch the error and output streams to prevent them from blocking during waitFor()
// (For normal input and output stream handling using the Gobblers, see FormatConversionDialog.java)
OutputStreamGobbler inputGobbler = new OutputStreamGobbler(p.getOutputStream(), null);
InputStreamGobbler errorGobbler = new InputStreamGobbler(p.getErrorStream(), true);
InputStreamGobbler outputGobbler = new InputStreamGobbler(p.getInputStream());
errorGobbler.start();
outputGobbler.start();
inputGobbler.start();
// the important part: wait for the process (ant stop or start or re-start to terminate)
int result = p.waitFor();
if(result != 0) {
System.err.println("**** Failed to successfully " + _ant_command + " the GS3 server.");
}
outputGobbler.join();
errorGobbler.join();
inputGobbler.join();
} else {
System.err.println("**** Could not start the Process to " + _ant_command + " the GS3 server.");
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}