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

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

Related to previous commit, revision 32049. Now tested successfully on Windows. Suppressing some debug statements.

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