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

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

In place of the Input- and OutputStreamGobbler classes, am now shifting GLI code to use SafeProcess too, copied across from GS3 src code. Class SafeProcess includes the two streamgobblers as static inner classes and some more functionality with safely running an external process from Java.

File size: 4.2 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;
39import java.io.File;
40
41/** Class for GS3, for issuing ant commands from GLI */
42public class GS3ServerThread extends Thread
43{
44 String _gsdl3_src_path = "";
45 String _ant_command = "";
46
47 public GS3ServerThread(String gsdl3_src_path, String ant_command)
48 {
49 _gsdl3_src_path = gsdl3_src_path;
50 _ant_command = ant_command; // "restart"
51 }
52
53 public void run()
54 {
55 try
56 {
57 ///System.err.println("**** GS3 server : " + _ant_command);
58
59 String shellCommand = null;
60 Process p = null;
61 if (Utility.isWindows())
62 {
63 if(_ant_command.indexOf("start") != -1) { // running an "ant (re)start" command on windows, run start
64 _ant_command = "start";
65 }
66 // The path in quotes, and the entire sequence of commands in quotes as well
67 // E.g. the following works: cmd /C "cd "C:\path\to\greenstone3" && ant stop"
68 // and it preserves any spaces in the path to GSDL3SRCHOME (_gsdl3_src_path).
69 p = Runtime.getRuntime().exec("cmd /C \"cd \"" + _gsdl3_src_path + File.separator + "\" && ant " + _ant_command + "\"");
70 }
71 else
72 {
73 if(_ant_command.indexOf("start") != -1) { // if running an "ant (re)start" command on non-Windows, run restart
74 _ant_command = "restart";
75 }
76 p = Runtime.getRuntime().exec(new String[]{"/bin/bash", "-c", "ant " + _ant_command + " -f \"" + _gsdl3_src_path + File.separator + "build.xml\""});
77 }
78
79 if (p != null) {
80 // in order for the process.waitFor() method to work with Java 6 (JRE 6 is included in GS binaries)
81 // need to make sure the IOstreams of the process are not blocked. For Java 7, this is not necessary
82 // and a waitFor() is sufficient. But with Java 6, the waitFor() causes the server to finally start
83 // after the user has quit GLI.
84
85 // Process takes no input, but we will still catch this iostream too
86 // And we'll catch the error and output streams to prevent them from blocking during waitFor()
87 // (For normal input and output stream handling using the Gobblers, see FormatConversionDialog.java)
88 SafeProcess.OutputStreamGobbler inputGobbler
89 = new SafeProcess.OutputStreamGobbler(p.getOutputStream(), null);
90 SafeProcess.InputStreamGobbler errorGobbler
91 = new SafeProcess.InputStreamGobbler(p.getErrorStream(), true);
92 SafeProcess.InputStreamGobbler outputGobbler
93 = new SafeProcess.InputStreamGobbler(p.getInputStream());
94
95 errorGobbler.start();
96 outputGobbler.start();
97 inputGobbler.start();
98
99 // the important part: wait for the process (ant stop or start or re-start to terminate)
100 int result = p.waitFor();
101 if(result != 0) {
102 System.err.println("**** Failed to successfully " + _ant_command + " the GS3 server.");
103 }
104
105 outputGobbler.join();
106 errorGobbler.join();
107 inputGobbler.join();
108 } else {
109 System.err.println("**** Could not start the Process to " + _ant_command + " the GS3 server.");
110 }
111
112 }
113 catch(Exception ex)
114 {
115 ex.printStackTrace();
116 }
117 }
118}
Note: See TracBrowser for help on using the repository browser.