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 |
|
---|
38 | package org.greenstone.gatherer.util;
|
---|
39 |
|
---|
40 | import org.greenstone.gatherer.Configuration;
|
---|
41 | import org.greenstone.gatherer.DebugStream;
|
---|
42 |
|
---|
43 | import java.io.File;
|
---|
44 |
|
---|
45 | /** Class for GS3, for issuing ant commands from GLI */
|
---|
46 | public 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 |
|
---|
58 | public void run()
|
---|
59 | {
|
---|
60 |
|
---|
61 | ///System.err.println("**** GS3 server : " + _ant_command);
|
---|
62 |
|
---|
63 | SafeProcess p = null;
|
---|
64 | if (Utility.isWindows()) {
|
---|
65 | if(_ant_command.indexOf("start") != -1) { // running an "ant (re)start" command on windows, run start
|
---|
66 | _ant_command = "start";
|
---|
67 | }
|
---|
68 |
|
---|
69 | // The path in quotes, and the entire sequence of commands in quotes as well
|
---|
70 | // E.g. the following works in a Runtime.exec() call:
|
---|
71 | // 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 = new SafeProcess("cmd /C \"cd \"" + _gsdl3_src_path + File.separator + "\" && ant " + _ant_command + "\"");
|
---|
74 | }
|
---|
75 | else {
|
---|
76 | if(_ant_command.indexOf("start") != -1) { // if running an "ant (re)start" command on non-Windows, run restart
|
---|
77 | _ant_command = "restart";
|
---|
78 | }
|
---|
79 | p = new SafeProcess(new String[]{"/bin/bash", "-c", "ant " + _ant_command + " -f \"" + _gsdl3_src_path + File.separator + "build.xml\""});
|
---|
80 | }
|
---|
81 |
|
---|
82 |
|
---|
83 | // in order for the process.waitFor() method to work with Java 6 (JRE 6 is included in GS binaries)
|
---|
84 | // need to make sure the IOstreams of the process are not blocked. For Java 7, this is not necessary
|
---|
85 | // and a waitFor() is sufficient. But with Java 6, the waitFor() causes the server to finally start
|
---|
86 | // after the user has quit GLI.
|
---|
87 | // Process takes no input, but we will still catch the process' instream too
|
---|
88 | // And we'll catch the error and output streams to prevent them from blocking during waitFor()
|
---|
89 | // (For normal input and output stream handling using the Gobblers, see FormatConversionDialog.java)
|
---|
90 |
|
---|
91 |
|
---|
92 | // prepare our SafeProcess object
|
---|
93 | p.setSplitStdErrorNewLines(true);
|
---|
94 |
|
---|
95 | // run it
|
---|
96 | int result = p.runProcess(); // uses default process streamgobbler behaviours and
|
---|
97 | // does the important part: waitFor() the process (ant stop or start or re-start) to terminate.
|
---|
98 | // The int result returned is the exitvalue upon Process.waitFor() returning
|
---|
99 |
|
---|
100 | if(result != 0) {
|
---|
101 | System.err.println("**** Failed to successfully " + _ant_command + " the GS3 server.");
|
---|
102 | }
|
---|
103 | ///else {
|
---|
104 | ///System.err.println("**** " + _ant_command + " of the GS3 server successful.");
|
---|
105 | ///}
|
---|
106 |
|
---|
107 | }
|
---|
108 |
|
---|
109 |
|
---|
110 | // can't call ant stop from its own thread - what if GLI has exited by then?
|
---|
111 | // issue call to ant stop from the main GLI thread
|
---|
112 | //GS3ServerThread thread = new GS3ServerThread(Configuration.gsdl_path, "stop");
|
---|
113 | //thread.start();
|
---|
114 | // So, static function to issue the command to stop the server from GLI's own thread.
|
---|
115 | // This will block the main GLI thread until the server has stopped.
|
---|
116 | public static void stopServer() {
|
---|
117 |
|
---|
118 | SafeProcess p = null;
|
---|
119 | if (Utility.isWindows()) {
|
---|
120 | // cmd /C "cd "C:\path\to\greenstone3" && ant stop"
|
---|
121 | p = new SafeProcess("cmd /C \"cd \"" + Configuration.gsdl3_src_path + File.separator + "\" && ant stop\"");
|
---|
122 | } else {
|
---|
123 | p = new SafeProcess(new String[]{"/bin/bash", "-c", "ant stop -f \"" + Configuration.gsdl3_src_path + File.separator + "build.xml\""});
|
---|
124 | }
|
---|
125 |
|
---|
126 | System.err.println("Issuing stop command to GS3 Server. Waiting for GS3 server to stop...");
|
---|
127 | int result = p.runProcess();
|
---|
128 | if(result == 0) {
|
---|
129 | System.err.println("Successfully stopped GS3 server.");
|
---|
130 | //DebugStream.println("********** SUCCESSFULLY stopped THE GS3 SERVER ON EXIT");
|
---|
131 | }
|
---|
132 | else {
|
---|
133 | System.err.println("********** FAILED TO SUCCESSFULLY stop THE GS3 SERVER ON EXIT");
|
---|
134 | //throw new Exception ("Failed to successfully stop the GS3 server on exit.");
|
---|
135 | }
|
---|
136 |
|
---|
137 | // doing a p.waitFor() without processing the Process' IOstreams causes blocking with Java 6
|
---|
138 | // (i.e. when JRE 6 included with GS binaries). However, p.waitFor() with Java 7 is fine.
|
---|
139 | /*if(p != null && p.waitFor() == 0) {
|
---|
140 | DebugStream.println("********** SUCCESSFULLY stopped THE GS3 SERVER ON EXIT");
|
---|
141 | }
|
---|
142 | else {
|
---|
143 | System.err.println("********** FAILED TO SUCCESSFULLY stop THE GS3 SERVER ON EXIT");
|
---|
144 | //throw new Exception ("Failed to successfully stop the GS3 server on exit.");
|
---|
145 | }*/
|
---|
146 |
|
---|
147 | }
|
---|
148 | }
|
---|