- Timestamp:
- 2017-04-06T20:33:36+12:00 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/trunk/gli/src/org/greenstone/gatherer/util/GS3ServerThread.java
r31582 r31583 37 37 38 38 package org.greenstone.gatherer.util; 39 40 import org.greenstone.gatherer.Configuration; 41 import org.greenstone.gatherer.DebugStream; 42 39 43 import java.io.File; 40 44 … … 51 55 } 52 56 53 public void run()57 public void old_run() 54 58 { 55 59 try … … 87 91 // (For normal input and output stream handling using the Gobblers, see FormatConversionDialog.java) 88 92 SafeProcess.OutputStreamGobbler inputGobbler 89 = new SafeProcess.OutputStreamGobbler(p.getOutputStream() , null);93 = new SafeProcess.OutputStreamGobbler(p.getOutputStream()); 90 94 SafeProcess.InputStreamGobbler errorGobbler 91 95 = new SafeProcess.InputStreamGobbler(p.getErrorStream(), true); … … 116 120 } 117 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 } 118 239 }
Note:
See TracChangeset
for help on using the changeset viewer.