[33053] | 1 | /**
|
---|
| 2 | *#########################################################################
|
---|
| 3 | *
|
---|
| 4 | * A component of the Gatherer application, part of the Greenstone digital
|
---|
| 5 | * library suite from the New Zealand Digital Library Project at the
|
---|
| 6 | * University of Waikato, New Zealand.
|
---|
| 7 | *
|
---|
| 8 | * <BR><BR>
|
---|
| 9 | *
|
---|
| 10 | * Author: Sam McIntosh, Greenstone Digital Library, University of Waikato
|
---|
| 11 | *
|
---|
| 12 | * <BR><BR>
|
---|
| 13 | *
|
---|
| 14 | * Copyright (C) 2011 New Zealand Digital Library Project
|
---|
| 15 | *
|
---|
| 16 | * <BR><BR>
|
---|
| 17 | *
|
---|
| 18 | * This program is free software; you can redistribute it and/or modify
|
---|
| 19 | * it under the terms of the GNU General Public License as published by
|
---|
| 20 | * the Free Software Foundation; either version 2 of the License, or
|
---|
| 21 | * (at your option) any later version.
|
---|
| 22 | *
|
---|
| 23 | * <BR><BR>
|
---|
| 24 | *
|
---|
| 25 | * This program is distributed in the hope that it will be useful,
|
---|
| 26 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 27 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 28 | * GNU General Public License for more details.
|
---|
| 29 | *
|
---|
| 30 | * <BR><BR>
|
---|
| 31 | *
|
---|
| 32 | * You should have received a copy of the GNU General Public License
|
---|
| 33 | * along with this program; if not, write to the Free Software
|
---|
| 34 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
| 35 | *########################################################################
|
---|
| 36 | */
|
---|
| 37 |
|
---|
| 38 | package org.greenstone.gatherer.util;
|
---|
| 39 | import java.io.File;
|
---|
| 40 |
|
---|
| 41 | /** Class for GS3, for issuing ant commands from GLI */
|
---|
| 42 | public class GS3ServerThread extends Thread
|
---|
| 43 | {
|
---|
| 44 | String _gsdl3_src_path = "";
|
---|
| 45 | String _ant_command = "";
|
---|
| 46 |
|
---|
| 47 | public GS3ServerThread(String gsdl3_src_path, String ant_command)
|
---|
| 48 | {
|
---|
| 49 | _gsdl3_src_path = gsdl3_src_path;
|
---|
| 50 | _ant_command = ant_command; // "restart"
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | public void run()
|
---|
| 54 | {
|
---|
| 55 | try
|
---|
| 56 | {
|
---|
| 57 | ///System.err.println("**** GS3 server : " + _ant_command);
|
---|
| 58 |
|
---|
| 59 | String shellCommand = null;
|
---|
| 60 | Process p = null;
|
---|
| 61 | if (Utility.isWindows())
|
---|
| 62 | {
|
---|
| 63 | if(_ant_command.indexOf("start") != -1) { // running an "ant (re)start" command on windows, run start
|
---|
| 64 | _ant_command = "start";
|
---|
| 65 | }
|
---|
| 66 | // The path in quotes, and the entire sequence of commands in quotes as well
|
---|
| 67 | // E.g. the following works: cmd /C "cd "C:\path\to\greenstone3" && ant stop"
|
---|
| 68 | // and it preserves any spaces in the path to GSDL3SRCHOME (_gsdl3_src_path).
|
---|
| 69 | p = Runtime.getRuntime().exec("cmd /C \"cd \"" + _gsdl3_src_path + File.separator + "\" && ant " + _ant_command + "\"");
|
---|
| 70 | }
|
---|
| 71 | else
|
---|
| 72 | {
|
---|
| 73 | if(_ant_command.indexOf("start") != -1) { // if running an "ant (re)start" command on non-Windows, run restart
|
---|
| 74 | _ant_command = "restart";
|
---|
| 75 | }
|
---|
| 76 | p = Runtime.getRuntime().exec(new String[]{"/bin/bash", "-c", "ant " + _ant_command + " -f \"" + _gsdl3_src_path + File.separator + "build.xml\""});
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | if (p != null) {
|
---|
| 80 | // in order for the process.waitFor() method to work with Java 6 (JRE 6 is included in GS binaries)
|
---|
| 81 | // need to make sure the IOstreams of the process are not blocked. For Java 7, this is not necessary
|
---|
| 82 | // and a waitFor() is sufficient. But with Java 6, the waitFor() causes the server to finally start
|
---|
| 83 | // after the user has quit GLI.
|
---|
| 84 |
|
---|
| 85 | // Process takes no input, but we will still catch this iostream too
|
---|
| 86 | // And we'll catch the error and output streams to prevent them from blocking during waitFor()
|
---|
| 87 | // (For normal input and output stream handling using the Gobblers, see FormatConversionDialog.java)
|
---|
| 88 | OutputStreamGobbler inputGobbler = new OutputStreamGobbler(p.getOutputStream(), null);
|
---|
| 89 | InputStreamGobbler errorGobbler = new InputStreamGobbler(p.getErrorStream(), true);
|
---|
| 90 | InputStreamGobbler outputGobbler = new InputStreamGobbler(p.getInputStream());
|
---|
| 91 |
|
---|
| 92 | errorGobbler.start();
|
---|
| 93 | outputGobbler.start();
|
---|
| 94 | inputGobbler.start();
|
---|
| 95 |
|
---|
| 96 | // the important part: wait for the process (ant stop or start or re-start to terminate)
|
---|
| 97 | int result = p.waitFor();
|
---|
| 98 | if(result != 0) {
|
---|
| 99 | System.err.println("**** Failed to successfully " + _ant_command + " the GS3 server.");
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | outputGobbler.join();
|
---|
| 103 | errorGobbler.join();
|
---|
| 104 | inputGobbler.join();
|
---|
| 105 | } else {
|
---|
| 106 | System.err.println("**** Could not start the Process to " + _ant_command + " the GS3 server.");
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | }
|
---|
| 110 | catch(Exception ex)
|
---|
| 111 | {
|
---|
| 112 | ex.printStackTrace();
|
---|
| 113 | }
|
---|
| 114 | }
|
---|
| 115 | }
|
---|