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

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

User on mailing list found it frustrating that GLI stops the GS3 Server on exit. Gli tends to launch and stop the GS3 server on GLI start and quit. Now it first checks if the server was already running on startup (launched externally) and only launches and stops the GS3 server if a server had not been launched external to GLI on GLI startup.

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