source: main/trunk/gli/src/org/greenstone/gatherer/util/GS3ServerThread.java

Last change on this file was 35661, checked in by davidb, 3 years ago

Adjusted solution to how -noclasspath is explicitly set by Greenstone3 so we don't get the ant vs ant.jar cross-contamination problem. Previously it had been dealt with inside the GLI GS3ServerThread.java code (explicitly adding it in). In this update, the ANT_ARGS environment variable is set in gs3-setup.bash, thereby triggering the same outcome, but only needs to be done in one place.

File size: 6.8 KB
Line 
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
38package org.greenstone.gatherer.util;
39
40import org.greenstone.gatherer.Configuration;
41import org.greenstone.gatherer.DebugStream;
42
43import java.io.File;
44
45/** Class for GS3, for issuing ant commands from GLI */
46public class GS3ServerThread extends Thread
47{
48 String _gsdl3_src_path = "";
49 String _ant_command = "";
50
51 // isIndependentServer is true if tomcat was already running when GLI was started up
52 // in which case GLI doesn't need start tomcat itself, nor should it need to stop the server on exit
53 static private final boolean isIndependentServer = GS3ServerThread.isServerRunning();
54
55 public GS3ServerThread(String gsdl3_src_path, String ant_command)
56 {
57 _gsdl3_src_path = gsdl3_src_path;
58 _ant_command = ant_command; // "restart"
59 }
60
61
62 public void run()
63 {
64 ///System.err.println("**** GS3 server : " + _ant_command);
65
66 SafeProcess p = null;
67 if (Utility.isWindows()) {
68 if(_ant_command.indexOf("start") != -1) { // running an "ant (re)start" command on windows, run start
69 _ant_command = "start";
70 }
71
72 // The path in quotes, and the entire sequence of commands in quotes as well
73 // E.g. the following works in a Runtime.exec() call:
74 // cmd /C "cd "C:\path\to\greenstone3" && ant stop"
75 // and it preserves any spaces in the path to GSDL3SRCHOME (_gsdl3_src_path).
76 p = new SafeProcess("cmd /C \"cd \"" + _gsdl3_src_path + File.separator + "\" && ant " + _ant_command + "\"");
77 }
78 else {
79 if(_ant_command.indexOf("start") != -1) { // if running an "ant (re)start" command on non-Windows, run restart
80 _ant_command = "restart";
81 }
82 p = new SafeProcess(new String[]{"/bin/bash", "-c", "ant " + _ant_command + " -f \"" + _gsdl3_src_path + File.separator + "build.xml\""});
83 }
84
85
86 // in order for the process.waitFor() method to work with Java 6 (JRE 6 is included in GS binaries)
87 // need to make sure the IOstreams of the process are not blocked. For Java 7, this is not necessary
88 // and a waitFor() is sufficient. But with Java 6, the waitFor() causes the server to finally start
89 // after the user has quit GLI.
90 // Process takes no input, but we will still catch the process' instream too
91 // And we'll catch the error and output streams to prevent them from blocking during waitFor()
92 // (For normal input and output stream handling using the Gobblers, see FormatConversionDialog.java)
93
94
95 // prepare our SafeProcess object
96 p.setSplitStdErrorNewLines(true);
97
98 // run it
99 int result = p.runProcess(); // uses default process streamgobbler behaviours and
100 // does the important part: waitFor() the process (ant stop or start or re-start) to terminate.
101 // The int result returned is the exitvalue upon Process.waitFor() returning
102
103 if(result != 0) {
104 System.err.println("Error: Failed to successfully " + _ant_command + " the GS3 server.");
105 }
106 ///else {
107 ///System.err.println("**** " + _ant_command + " of the GS3 server successful.");
108 ///}
109
110 }
111
112
113 // can't call ant stop from its own thread - what if GLI has exited by then?
114 // issue call to ant stop from the main GLI thread
115 //GS3ServerThread thread = new GS3ServerThread(Configuration.gsdl_path, "stop");
116 //thread.start();
117 // So, static function to issue the command to stop the server from GLI's own thread.
118 // This will block the main GLI thread until the server has stopped.
119 public static void stopServer() {
120
121 SafeProcess p = null;
122 if (Utility.isWindows()) {
123 // cmd /C "cd "C:\path\to\greenstone3" && ant stop"
124 p = new SafeProcess("cmd /C \"cd \"" + Configuration.gsdl3_src_path + File.separator + "\" && ant stop\"");
125 } else {
126 p = new SafeProcess(new String[]{"/bin/bash", "-c", "ant stop -f \"" + Configuration.gsdl3_src_path + File.separator + "build.xml\""});
127 }
128
129 System.err.println("Issuing stop command to GS3 Server. Waiting for GS3 server to stop...");
130 int result = p.runProcess();
131 if(result == 0) {
132 System.err.println("Successfully stopped GS3 server.");
133 //DebugStream.println("********** SUCCESSFULLY stopped THE GS3 SERVER ON EXIT");
134 }
135 else {
136 System.err.println("********** FAILED TO SUCCESSFULLY stop THE GS3 SERVER ON EXIT");
137 }
138
139 // doing a p.waitFor() without processing the Process' IOstreams causes blocking with Java 6
140 // (i.e. when JRE 6 included with GS binaries). However, p.waitFor() with Java 7 is fine.
141 /*if(p != null && p.waitFor() == 0) {
142 DebugStream.println("********** SUCCESSFULLY stopped THE GS3 SERVER ON EXIT");
143 }
144 else {
145 System.err.println("********** FAILED TO SUCCESSFULLY stop THE GS3 SERVER ON EXIT");
146 //throw new Exception ("Failed to successfully stop the GS3 server on exit.");
147 }*/
148
149 }
150
151 public static boolean isServerRunning() {
152 String antCmd = "ant verbose-check-tomcat-running";
153
154 SafeProcess p = null;
155 if (Utility.isWindows()) {
156 p = new SafeProcess("cmd /C \"cd \"" + Configuration.gsdl3_src_path + File.separator + "\" && "+antCmd+"\"");
157 } else {
158 p = new SafeProcess(new String[]{"/bin/bash", "-c", antCmd+" -f \"" + Configuration.gsdl3_src_path + File.separator + "build.xml\""});
159 }
160
161 //System.err.println("**** Checking if tomcat is running");
162 p.runProcess();
163 String output = p.getStdOutput();
164
165 if(output.contains("Tomcat is running: true")) {
166 //System.err.println("**** Tomcat was running");
167 return true;
168 }
169
170 //System.err.println("**** Tomcat was not running");
171 return false;
172 }
173
174 // first time, call this on startup, before running GS3ServerThread
175 public static boolean wasServerLaunchedOutsideGLI() {
176 //System.err.println("@@@ Was server launched outside GLI: " + isIndependentServer);
177 return isIndependentServer;
178 }
179}
Note: See TracBrowser for help on using the repository browser.