source: trunk/gli/src/org/greenstone/gatherer/shell/GShell.java@ 4317

Last change on this file since 4317 was 4317, checked in by jmt12, 21 years ago

tried to fix import/build process not stopping error. Don't think I did though

  • Property svn:keywords set to Author Date Id Revision
File size: 13.0 KB
Line 
1package org.greenstone.gatherer.shell;
2/**
3 *#########################################################################
4 *
5 * A component of the Gatherer application, part of the Greenstone digital
6 * library suite from the New Zealand Digital Library Project at the
7 * University of Waikato, New Zealand.
8 *
9 * <BR><BR>
10 *
11 * Author: John Thompson, Greenstone Digital Library, University of Waikato
12 *
13 * <BR><BR>
14 *
15 * Copyright (C) 1999 New Zealand Digital Library Project
16 *
17 * <BR><BR>
18 *
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
23 *
24 * <BR><BR>
25 *
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
30 *
31 * <BR><BR>
32 *
33 * You should have received a copy of the GNU General Public License
34 * along with this program; if not, write to the Free Software
35 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
36 *########################################################################
37 */
38import java.io.*;
39import javax.swing.*;
40import javax.swing.event.*;
41import javax.swing.tree.*;
42import org.greenstone.gatherer.Gatherer;
43import org.greenstone.gatherer.Message;
44import org.greenstone.gatherer.msm.GreenstoneArchiveParser;
45import org.greenstone.gatherer.shell.GShellListener;
46import org.greenstone.gatherer.shell.GShellProgressMonitor;
47/** The <strong>GShell</strong> is reponsible for running a separately threaded process in the command shell. This is necessary for executing the Perl Scripts and also for other system related funcitonality.
48 */
49public class GShell
50 extends Thread {
51 /** A flag used to determine if this process has been asked to cancel. */
52 private boolean cancel = false;
53 /** The list of listeners associated with this class. */
54 private EventListenerList listeners = null;
55 /** The current status of this shell process. */
56 private int status = -1;
57 /** The type of message being sent. */
58 private int msg_type = -1;
59 /** The type of shell process. */
60 private int type = -1;
61 /** The caller of this process, and thus the class most interested in messages. */
62 private GShellListener caller = null;
63 /** The progress monitor associated with this process. */
64 private GShellProgressMonitor progress = null;
65 /** Arguments to be given to the process (including the executable you are calling. */
66 private String args[] = null;
67 /** Element in process type enumeration. */
68 static final public int BUILD = 0;
69 /** Element in process type enumeration. */
70 static final public int IMPORT = 1;
71 /** Element in process type enumeration. */
72 static final public int NEW = 2;
73 /** Element in process type enumeration. */
74 static final public int OTHER = 3;
75 /** Element in status type enumeration. */
76 static final public int ERROR = 0;
77 /** Element in status type enumeration. */
78 static final public int OK = 1;
79 /** Element in process type name enumeration. */
80 static public String GSHELL_BUILD = "gshell_build";
81 /** Element in process type name enumeration. */
82 static public String GSHELL_IMPORT = "gshell_import";
83 /** Element in process type name enumeration. */
84 static public String GSHELL_NEW = "gshell_new";
85 /** Constructor gatherer all the data required to create a new process, and emit meaningfull messages.
86 * @param args A <strong>String[]</strong> containing the arguments to the process thread, including the name of the executable.
87 * @param type An <strong>int</strong> that indicates what group of processes this process belongs to, as some are treated slightly differently (i.e an IMPORT type process is always followed by a BUILD one).
88 * @param msg_type As process threads may be background (like a makecol.pl call) or important processes in their own right (such as an IMPORT-BUILD) we must be able to set what level messages posted by this class will have by usings this <strong>int</strong>.
89 * @param caller The default <i>GShellListener</i> that is interested in the progress of this process.
90 * @param progress The <i>GShellProgressMonitor</i> associated with this process.
91 * @param name A <strong>String</strong> identifier given to the process, for convience and debug reasons.
92 */
93 public GShell(String args[], int type, int msg_type, GShellListener caller, GShellProgressMonitor progress, String name) {
94 super(name);
95 this.args = args;
96 this.msg_type = msg_type;
97 this.type = type;
98 this.caller = caller;
99 this.progress = progress;
100 this.status = 0;
101 // Lower this jobs priority
102 this.setPriority(Thread.MIN_PRIORITY);
103 listeners = new EventListenerList();
104 listeners.add(GShellListener.class, caller);
105 }
106 /** This method adds another shell listener to this process.
107 * @param listener The new <i>GShellListener</i>.
108 */
109 public void addGShellListener(GShellListener listener) {
110 listeners.add(GShellListener.class, listener);
111 }
112 /** This method removes a certain shell listener from this process.
113 * @param listener The <i>GShellListener</i> to be removed.
114 */
115 public void removeGShellListener(GShellListener listener) {
116 listeners.remove(GShellListener.class, listener);
117 }
118 /** Any threaded class must include this method to allow the thread body to be run.
119 */
120 public void run() {
121 // Setup
122 if(progress != null) {
123 progress.start();
124 }
125 // Determine if the user has asked for an outfile.
126 String out_name = null;
127 FileOutputStream fos = null;
128 BufferedOutputStream bos = null;
129 if(type == IMPORT || type == BUILD) {
130 if(type == IMPORT) {
131 out_name = (String) Gatherer.c_man.getCollection().build_options.getImportValue("out");
132 }
133 else {
134 out_name = (String) Gatherer.c_man.getCollection().build_options.getBuildValue("out");
135 }
136 if(out_name != null && out_name.length() > 0) {
137 File out = new File(out_name);
138 try {
139 if(out.exists()) {
140 boolean append = true;
141 fos = new FileOutputStream(out, append);
142 bos = new BufferedOutputStream(fos);
143 }
144 }
145 catch (Exception error) {
146 error.printStackTrace();
147 }
148 }
149 }
150 // Issue a processBegun event
151 fireProcessBegun(type, status);
152 try {
153 String command = "";
154 for(int i = 0; i < args.length; i++) {
155 command = command + args[i] + " ";
156 }
157 ///ystem.err.println("Command: " + command);
158 fireMessage(type, get("Command") + ": " + command, status);
159 message(Message.EVENT, get("Command") + ": " + command);
160
161 Runtime rt = Runtime.getRuntime();
162 Process prcs = rt.exec(args);
163 InputStreamReader eisr = new InputStreamReader( prcs.getErrorStream() );
164 InputStreamReader stdinisr = new InputStreamReader( prcs.getInputStream() );
165 BufferedReader ebr = new BufferedReader( eisr );
166 BufferedReader stdinbr = new BufferedReader( stdinisr );
167 // Captures the std err of a program and pipes it into
168 // std in of java
169 String eline = null;
170 String stdinline = null;
171 while (((eline = ebr.readLine()) != null || (stdinline = stdinbr.readLine()) != null) && !hasSignalledStop()) {
172 if(eline != null) {
173 if(progress != null) {
174 progress.parse(eline);
175 }
176 if(bos != null) {
177 bos.write(eline.getBytes(), 0, eline.length());
178 }
179 ///ystem.err.println("stderr: " + eline);
180 fireMessage(type, typeAsString(type) + "> " + eline,
181 status);
182 message(Message.INFO, eline);
183 }
184 if(stdinline != null) {
185 if(bos != null) {
186 //bos.write(stdinline.getBytes(), 0, stdinline.length());
187 }
188 ///ystem.err.println("stdin: " + stdinline);
189 fireMessage(type, typeAsString(type) + "> " + stdinline,
190 status);
191 message(Message.INFO, stdinline);
192 }
193 }
194
195 if(!hasSignalledStop()) {
196 // Now display final message based on exit value
197 prcs.waitFor();
198
199 if(prcs.exitValue() == 0) {
200 status = OK;
201 fireMessage(type, typeAsString(type) + "> " + get("Success"), status);
202 message(Message.MAIN, get("Success"));
203 } else {
204 status = ERROR;
205 fireMessage(type, typeAsString(type) + "> " + get("Failure"), status);
206 message(Message.ERROR, get("Failure"));
207 }
208 }
209 else {
210 // I need to somehow kill the child process. Unfortunately Thread.stop() and Process.destroy() both fail to do this. But now, thankx to the magic of Michaels 'close the stream suggestion', it works fine (no it doesn't!)
211 prcs.getOutputStream().close();
212 prcs.destroy();
213 status = ERROR;
214 }
215 }
216 // Exception
217 catch (Exception error) {
218 message(Message.ERROR, get("Failure"));
219 message(Message.ERROR, error.toString());
220 Gatherer.printStackTrace(error);
221 status = ERROR;
222 }
223 // If no error occured, and this was an import process we now extract any new metadata from the archive directory.
224 if(status == OK && type == IMPORT) {
225 fireMessage(type, typeAsString(type) + "> " + get("Parsing_Metadata_Start"), status);
226 new GreenstoneArchiveParser(progress, this);
227 fireMessage(type, typeAsString(type) + "> " + get("Parsing_Metadata_Complete"), status);
228 }
229 // Tidy up.
230 if(progress != null) {
231 progress.stop();
232 }
233 if(bos != null) {
234 try {
235 bos.close();
236 }
237 catch (Exception e) {
238 }
239 }
240 // We're done.
241 fireProcessComplete(type, status);
242 }
243 /** Method for firing a message to all interested listeners.
244 * @param type An <strong>int</strong> indicating the process type.
245 * @param message The message as a <strong>String</strong>.
246 * @param status An <strong>int</strong> specifying the current status of the process.
247 */
248 public void fireMessage(int type, String message, int status) {
249 GShellEvent event = new GShellEvent(this, 0, type, message, status);
250 Object[] concerned = listeners.getListenerList();
251 for(int i = 0; i < concerned.length ; i++) {
252 if(concerned[i] == GShellListener.class) {
253 ((GShellListener)concerned[i+1]).message(event);
254 }
255 }
256 }
257 /** Method for firing a process begun event which is called, strangly enough, when the process begins.
258 * @param type An <strong>int</strong> indicating the process type.
259 * @param status An <strong>int</strong> specifying the current status of the process.
260 */
261 protected void fireProcessBegun(int type, int status) {
262 GShellEvent event = new GShellEvent(this, 0, type, "", status);
263 Object[] concerned = listeners.getListenerList();
264 for(int i = 0; i < concerned.length ; i++) {
265 if(concerned[i] == GShellListener.class) {
266 ((GShellListener)concerned[i+1]).processBegun(event);
267 }
268 }
269 }
270 /** Method for firing a process complete event which is called, no surprise here, when the process ends.
271 * @param type An <strong>int</strong> indicating the process type.
272 * @param status An <strong>int</strong> specifying the current status of the process.
273 */
274 protected void fireProcessComplete(int type, int status) {
275 GShellEvent event = new GShellEvent(this, 0, type, "", status);
276 Object[] concerned = listeners.getListenerList();
277 for(int i = 0; i < concerned.length ; i++) {
278 if(concerned[i] == GShellListener.class) {
279 ((GShellListener)concerned[i+1]).processComplete(event);
280 }
281 }
282 }
283 /** Retrieve a phrase from the dictionary based on the given key.
284 * @param key A <strong>String</strong> used to match against a phrase from the <strong>Dictionary</strong>.
285 * @return The phrase as a <strong>String</strong>.
286 */
287 private String get(String key) {
288 if(key.indexOf('.') == -1) {
289 key = "GShell." + key;
290 }
291 return Gatherer.dictionary.get(key);
292 }
293 /** Method to determine if the user, via the progress monitor, has signalled stop.
294 * @return A <strong>boolean</strong> indicating if the user wanted to stop.
295 */
296 private boolean hasSignalledStop() {
297 boolean has_signalled_stop = false;
298 if(progress != null) {
299 return progress.hasSignalledStop();
300 }
301 return has_signalled_stop;
302 }
303 /** Creates and dispatches a message given the initial details.
304 * @param level An <strong>int</strong> indicating the message level for this message.
305 * @param message A <strong>String</strong> which contains the payload of this message.
306 */
307 private void message(int level, String message) {
308 Message msg = new Message(msg_type, level, message);
309 if(Gatherer.g_man != null) {
310 Gatherer.log.add(msg);
311 }
312 }
313 /** Converts a type into a text representation.
314 * @param type An <strong>int</strong> which maps to a shell process type.
315 * @return A <strong>String</strong> which is the thread process's text name.
316 */
317 public String typeAsString(int type) {
318 String name = null;
319 switch(type) {
320 case BUILD:
321 name = get("Build");
322 break;
323 case IMPORT:
324 name = get("Import");
325 break;
326 case NEW:
327 name = get("New");
328 break;
329 default:
330 name = get("Other");
331 }
332 return name;
333 }
334}
335
336
Note: See TracBrowser for help on using the repository browser.