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

Last change on this file since 31583 was 31583, checked in by ak19, 7 years ago

GS3ServerThread now uses SafeProcess. And moved the code in Gatherer that stopped the GS3 server in the main thread into the GS3ServerThread as a static stopServer method. Hopefully the static is enough to indicate that it is not part of any specific thread other than the main GLI thread.

File size: 9.4 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 public GS3ServerThread(String gsdl3_src_path, String ant_command)
52 {
53 _gsdl3_src_path = gsdl3_src_path;
54 _ant_command = ant_command; // "restart"
55 }
56
57 public void old_run()
58 {
59 try
60 {
61 ///System.err.println("**** GS3 server : " + _ant_command);
62
63 String shellCommand = null;
64 Process p = null;
65 if (Utility.isWindows())
66 {
67 if(_ant_command.indexOf("start") != -1) { // running an "ant (re)start" command on windows, run start
68 _ant_command = "start";
69 }
70 // The path in quotes, and the entire sequence of commands in quotes as well
71 // E.g. the following works: cmd /C "cd "C:\path\to\greenstone3" && ant stop"
72 // and it preserves any spaces in the path to GSDL3SRCHOME (_gsdl3_src_path).
73 p = Runtime.getRuntime().exec("cmd /C \"cd \"" + _gsdl3_src_path + File.separator + "\" && ant " + _ant_command + "\"");
74 }
75 else
76 {
77 if(_ant_command.indexOf("start") != -1) { // if running an "ant (re)start" command on non-Windows, run restart
78 _ant_command = "restart";
79 }
80 p = Runtime.getRuntime().exec(new String[]{"/bin/bash", "-c", "ant " + _ant_command + " -f \"" + _gsdl3_src_path + File.separator + "build.xml\""});
81 }
82
83 if (p != null) {
84 // in order for the process.waitFor() method to work with Java 6 (JRE 6 is included in GS binaries)
85 // need to make sure the IOstreams of the process are not blocked. For Java 7, this is not necessary
86 // and a waitFor() is sufficient. But with Java 6, the waitFor() causes the server to finally start
87 // after the user has quit GLI.
88
89 // Process takes no input, but we will still catch this iostream too
90 // And we'll catch the error and output streams to prevent them from blocking during waitFor()
91 // (For normal input and output stream handling using the Gobblers, see FormatConversionDialog.java)
92 SafeProcess.OutputStreamGobbler inputGobbler
93 = new SafeProcess.OutputStreamGobbler(p.getOutputStream());
94 SafeProcess.InputStreamGobbler errorGobbler
95 = new SafeProcess.InputStreamGobbler(p.getErrorStream(), true);
96 SafeProcess.InputStreamGobbler outputGobbler
97 = new SafeProcess.InputStreamGobbler(p.getInputStream());
98
99 errorGobbler.start();
100 outputGobbler.start();
101 inputGobbler.start();
102
103 // the important part: wait for the process (ant stop or start or re-start to terminate)
104 int result = p.waitFor();
105 if(result != 0) {
106 System.err.println("**** Failed to successfully " + _ant_command + " the GS3 server.");
107 }
108
109 outputGobbler.join();
110 errorGobbler.join();
111 inputGobbler.join();
112 } else {
113 System.err.println("**** Could not start the Process to " + _ant_command + " the GS3 server.");
114 }
115
116 }
117 catch(Exception ex)
118 {
119 ex.printStackTrace();
120 }
121 }
122
123 public void run()
124 {
125
126 ///System.err.println("**** GS3 server : " + _ant_command);
127
128 SafeProcess p = null;
129 if (Utility.isWindows()) {
130 if(_ant_command.indexOf("start") != -1) { // running an "ant (re)start" command on windows, run start
131 _ant_command = "start";
132 }
133
134 // The path in quotes, and the entire sequence of commands in quotes as well
135 // E.g. the following works in a Runtime.exec() call:
136 // cmd /C "cd "C:\path\to\greenstone3" && ant stop"
137 // and it preserves any spaces in the path to GSDL3SRCHOME (_gsdl3_src_path).
138 p = new SafeProcess("cmd /C \"cd \"" + _gsdl3_src_path + File.separator + "\" && ant " + _ant_command + "\"");
139 }
140 else {
141 if(_ant_command.indexOf("start") != -1) { // if running an "ant (re)start" command on non-Windows, run restart
142 _ant_command = "restart";
143 }
144 p = new SafeProcess(new String[]{"/bin/bash", "-c", "ant " + _ant_command + " -f \"" + _gsdl3_src_path + File.separator + "build.xml\""});
145 }
146
147
148 // in order for the process.waitFor() method to work with Java 6 (JRE 6 is included in GS binaries)
149 // need to make sure the IOstreams of the process are not blocked. For Java 7, this is not necessary
150 // and a waitFor() is sufficient. But with Java 6, the waitFor() causes the server to finally start
151 // after the user has quit GLI.
152 // Process takes no input, but we will still catch the process' instream too
153 // And we'll catch the error and output streams to prevent them from blocking during waitFor()
154 // (For normal input and output stream handling using the Gobblers, see FormatConversionDialog.java)
155
156
157 // prepare our SafeProcess object
158 p.setSplitStdErrorNewLines(true);
159
160 // run it
161 int result = p.runProcess(); // uses default process streamgobbler behaviours and
162 // does the important part: waitFor() the process (ant stop or start or re-start) to terminate.
163 // The int result returned is the exitvalue upon Process.waitFor() returning
164
165 if(result != 0) {
166 System.err.println("**** Failed to successfully " + _ant_command + " the GS3 server.");
167 }
168 ///else {
169 ///System.err.println("**** " + _ant_command + " of the GS3 server successful.");
170 ///}
171
172 }
173
174 public static void old_stopServer() {
175
176 System.err.println("Stopping GS3 Server");
177 try {
178 String shellCommand = null;
179 Process p = null;
180 if (Utility.isWindows()) {
181 // cmd /C "cd "C:\path\to\greenstone3" && ant stop"
182 p = Runtime.getRuntime().exec("cmd /C \"cd \"" + Configuration.gsdl3_src_path + File.separator + "\" && ant stop\"");
183 } else {
184 p = Runtime.getRuntime().exec(new String[]{"/bin/bash", "-c", "ant stop -f \"" + Configuration.gsdl3_src_path + File.separator + "build.xml\""});
185 }
186 // doing a p.waitFor() without processing the Process' IOstreams causes blocking with Java 6
187 // (i.e. when JRE 6 included with GS binaries). However, p.waitFor() with Java 7 is fine.
188 /*if(p != null && p.waitFor() == 0) {
189 DebugStream.println("********** SUCCESSFULLY stopped THE GS3 SERVER ON EXIT");
190 }
191 else {
192 System.err.println("********** FAILED TO SUCCESSFULLY stop THE GS3 SERVER ON EXIT");
193 //throw new Exception ("Failed to successfully stop the GS3 server on exit.");
194 }*/
195 } catch(Exception e) {
196 System.err.println("Exception when trying to stop the tomcat web server: " + e);
197 DebugStream.printStackTrace(e);
198 }
199 }
200
201 // can't call ant stop from its own thread - what if GLI has exited by then?
202 // issue call to ant stop from the main GLI thread
203 //GS3ServerThread thread = new GS3ServerThread(Configuration.gsdl_path, "stop");
204 //thread.start();
205 // So, static function to issue the command to stop the server from GLI's own thread.
206 // This will block the main GLI thread until the server has stopped.
207 public static void stopServer() {
208
209 SafeProcess p = null;
210 if (Utility.isWindows()) {
211 // cmd /C "cd "C:\path\to\greenstone3" && ant stop"
212 p = new SafeProcess("cmd /C \"cd \"" + Configuration.gsdl3_src_path + File.separator + "\" && ant stop\"");
213 } else {
214 p = new SafeProcess(new String[]{"/bin/bash", "-c", "ant stop -f \"" + Configuration.gsdl3_src_path + File.separator + "build.xml\""});
215 }
216
217 System.err.println("Issuing stop command to GS3 Server. Waiting for GS3 server to stop...");
218 int result = p.runProcess();
219 if(result == 0) {
220 System.err.println("Successfully stopped GS3 server.");
221 //DebugStream.println("********** SUCCESSFULLY stopped THE GS3 SERVER ON EXIT");
222 }
223 else {
224 System.err.println("********** FAILED TO SUCCESSFULLY stop THE GS3 SERVER ON EXIT");
225 //throw new Exception ("Failed to successfully stop the GS3 server on exit.");
226 }
227
228 // doing a p.waitFor() without processing the Process' IOstreams causes blocking with Java 6
229 // (i.e. when JRE 6 included with GS binaries). However, p.waitFor() with Java 7 is fine.
230 /*if(p != null && p.waitFor() == 0) {
231 DebugStream.println("********** SUCCESSFULLY stopped THE GS3 SERVER ON EXIT");
232 }
233 else {
234 System.err.println("********** FAILED TO SUCCESSFULLY stop THE GS3 SERVER ON EXIT");
235 //throw new Exception ("Failed to successfully stop the GS3 server on exit.");
236 }*/
237
238 }
239}
Note: See TracBrowser for help on using the repository browser.