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

Last change on this file since 5840 was 5840, checked in by kjdon, 20 years ago

added in options for exporting collections

  • Property svn:keywords set to Author Date Id Revision
File size: 12.0 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: John Thompson, Greenstone Digital Library, University of Waikato
11 *
12 * <BR><BR>
13 *
14 * Copyright (C) 1999 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 */
37package org.greenstone.gatherer.shell;
38
39import java.io.*;
40import javax.swing.*;
41import javax.swing.event.*;
42import javax.swing.tree.*;
43import org.greenstone.gatherer.Dictionary;
44import org.greenstone.gatherer.Gatherer;
45import org.greenstone.gatherer.msm.GreenstoneArchiveParser;
46import org.greenstone.gatherer.shell.GShellListener;
47import org.greenstone.gatherer.shell.GShellProgressMonitor;
48
49/** 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.
50 */
51public class GShell
52 extends Thread {
53 /** A flag used to determine if this process has been asked to cancel. */
54 private boolean cancel = false;
55 /** The list of listeners associated with this class. */
56 private EventListenerList listeners = null;
57 /** The current status of this shell process. */
58 private int status = -1;
59 /** The type of message being sent. */
60 private int msg_type = -1;
61 /** The type of shell process. */
62 private int type = -1;
63 /** The caller of this process, and thus the class most interested in messages. */
64 private GShellListener caller = null;
65 /** The progress monitor associated with this process. */
66 private GShellProgressMonitor progress = null;
67 /** Arguments to be given to the process (including the executable you are calling. */
68 private String args[] = null;
69 /** Element in process type enumeration. */
70 static final public int BUILD = 0;
71 /** Element in process type enumeration. */
72 static final public int IMPORT = 1;
73 /** Element in process type enumeration. */
74 static final public int NEW = 2;
75 /** Element in process type enumeration. */
76 static final public int EXPORT = 3;
77 /** Element in process type enumeration. */
78 static final public int OTHER = 4;
79 /** Element in status type enumeration. */
80 static final public int ERROR = 0;
81 /** Element in status type enumeration. */
82 static final public int OK = 1;
83 /** Element in process type name enumeration. */
84 static public String GSHELL_BUILD = "gshell_build";
85 /** Element in process type name enumeration. */
86 static public String GSHELL_IMPORT = "gshell_import";
87 /** Element in process type name enumeration. */
88 static public String GSHELL_NEW = "gshell_new";
89 /** Element in process type name enumeration */
90 static public String GSHELL_EXPORT = "gshell_export";
91
92 /** Constructor gatherer all the data required to create a new process, and emit meaningfull messages.
93 * @param args A <strong>String[]</strong> containing the arguments to the process thread, including the name of the executable.
94 * @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).
95 * @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>.
96 * @param caller The default <i>GShellListener</i> that is interested in the progress of this process.
97 * @param progress The <i>GShellProgressMonitor</i> associated with this process.
98 * @param name A <strong>String</strong> identifier given to the process, for convience and debug reasons.
99 */
100 public GShell(String args[], int type, int msg_type, GShellListener caller, GShellProgressMonitor progress, String name) {
101 super(name);
102 this.args = args;
103 this.msg_type = msg_type;
104 this.type = type;
105 this.caller = caller;
106 this.progress = progress;
107 this.status = 0;
108 // Lower this jobs priority
109 this.setPriority(Thread.MIN_PRIORITY);
110 listeners = new EventListenerList();
111 listeners.add(GShellListener.class, caller);
112 }
113 /** This method adds another shell listener to this process.
114 * @param listener The new <i>GShellListener</i>.
115 */
116 public void addGShellListener(GShellListener listener) {
117 listeners.add(GShellListener.class, listener);
118 }
119 /** This method removes a certain shell listener from this process.
120 * @param listener The <i>GShellListener</i> to be removed.
121 */
122 /* private void removeGShellListener(GShellListener listener) {
123 listeners.remove(GShellListener.class, listener);
124 } */
125 /** Any threaded class must include this method to allow the thread body to be run. */
126 public void run() {
127 if(progress != null) {
128 progress.start();
129 }
130 // Determine if the user has asked for an outfile.
131 String out_name = null;
132 BufferedOutputStream bos = null;
133 if(type == IMPORT || type == BUILD) {
134 if(type == IMPORT) {
135 out_name = (String) Gatherer.c_man.getCollection().build_options.getImportValue("out");
136 }
137 else {
138 out_name = (String) Gatherer.c_man.getCollection().build_options.getBuildValue("out");
139 }
140 if(out_name != null && out_name.length() > 0) {
141 try {
142 bos = new BufferedOutputStream(new FileOutputStream(new File(out_name), true));
143 }
144 catch (Exception error) {
145 Gatherer.printStackTrace(error);
146 }
147 }
148 }
149 // Issue a processBegun event
150 fireProcessBegun(type, status);
151 try {
152
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, Dictionary.get("GShell.Command") + ": " + command, status);
159
160 Runtime rt = Runtime.getRuntime();
161 Process prcs = rt.exec(args);
162 InputStreamReader eisr = new InputStreamReader( prcs.getErrorStream() );
163 InputStreamReader stdinisr = new InputStreamReader( prcs.getInputStream() );
164 BufferedReader ebr = new BufferedReader( eisr );
165 BufferedReader stdinbr = new BufferedReader( stdinisr );
166 // Captures the std err of a program and pipes it into std in of java
167 String eline = null;
168 String stdinline = null;
169 while (((eline = ebr.readLine()) != null || (stdinline = stdinbr.readLine()) != null) && !hasSignalledStop()) {
170 if(eline != null) {
171 if(progress != null) {
172 progress.parse(eline);
173 }
174 if(bos != null) {
175 try {
176 bos.write(eline.getBytes(), 0, eline.length());
177 }
178 catch(Exception error) {
179 Gatherer.printStackTrace(error);
180 }
181 }
182 fireMessage(type, typeAsString(type) + "> " + eline, status);
183 }
184 if(stdinline != null) {
185 fireMessage(type, typeAsString(type) + "> " + stdinline, status);
186 }
187 }
188
189 if(!hasSignalledStop()) {
190 // Now display final message based on exit value
191 prcs.waitFor();
192
193 if(prcs.exitValue() == 0) {
194 status = OK;
195 fireMessage(type, typeAsString(type) + "> " + Dictionary.get("GShell.Success"), status);
196 }
197 else {
198 status = ERROR;
199 fireMessage(type, typeAsString(type) + "> " + Dictionary.get("GShell.Failure"), status);
200 }
201 }
202 else {
203 // 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!)
204 prcs.getInputStream().close();
205 prcs.getOutputStream().close();
206 prcs.destroy();
207 status = ERROR;
208 }
209 }
210 // Exception
211 catch (Exception error) {
212 Gatherer.printStackTrace(error);
213 status = ERROR;
214 }
215 // If no error occured, and this was an import process we now extract any new metadata from the archive directory.
216 if(status == OK && type == IMPORT) {
217 fireMessage(type, typeAsString(type) + "> " + Dictionary.get("GShell.Parsing_Metadata_Start"), status);
218 new GreenstoneArchiveParser(progress, this);
219 fireMessage(type, typeAsString(type) + "> " + Dictionary.get("GShell.Parsing_Metadata_Complete"), status);
220 }
221 // Tidy up.
222 if(progress != null) {
223 progress.stop();
224 }
225 // We're done.
226 fireProcessComplete(type, status);
227 // Close bos
228 if(bos != null) {
229 try {
230 bos.close();
231 bos = null;
232 }
233 catch(Exception error) {
234 Gatherer.printStackTrace(error);
235 }
236 }
237 }
238 /** Method for firing a message to all interested listeners.
239 * @param type An <strong>int</strong> indicating the process type.
240 * @param message The message as a <strong>String</strong>.
241 * @param status An <strong>int</strong> specifying the current status of the process.
242 */
243 public void fireMessage(int type, String message, int status) {
244 GShellEvent event = new GShellEvent(this, 0, type, message, status);
245 Object[] concerned = listeners.getListenerList();
246 for(int i = 0; i < concerned.length ; i++) {
247 if(concerned[i] == GShellListener.class) {
248 ((GShellListener)concerned[i+1]).message(event);
249 }
250 }
251 }
252
253 /** Method for firing a process begun event which is called, strangly enough, when the process begins.
254 * @param type An <strong>int</strong> indicating the process type.
255 * @param status An <strong>int</strong> specifying the current status of the process.
256 */
257 protected void fireProcessBegun(int type, int status) {
258 GShellEvent event = new GShellEvent(this, 0, type, "", status);
259 Object[] concerned = listeners.getListenerList();
260 for(int i = 0; i < concerned.length ; i++) {
261 if(concerned[i] == GShellListener.class) {
262 ((GShellListener)concerned[i+1]).processBegun(event);
263 }
264 }
265 }
266 /** Method for firing a process complete event which is called, no surprise here, when the process ends.
267 * @param type An <strong>int</strong> indicating the process type.
268 * @param status An <strong>int</strong> specifying the current status of the process.
269 */
270 protected void fireProcessComplete(int type, int status) {
271 GShellEvent event = new GShellEvent(this, 0, type, "", status);
272 Object[] concerned = listeners.getListenerList();
273 for(int i = 0; i < concerned.length ; i++) {
274 if(concerned[i] == GShellListener.class) {
275 ((GShellListener)concerned[i+1]).processComplete(event);
276 }
277 }
278 }
279
280 /** Method to determine if the user, via the progress monitor, has signalled stop.
281 * @return A <strong>boolean</strong> indicating if the user wanted to stop.
282 */
283 private boolean hasSignalledStop() {
284 boolean has_signalled_stop = false;
285 if(progress != null) {
286 return progress.hasSignalledStop();
287 }
288 return has_signalled_stop;
289 }
290
291 /** Converts a type into a text representation.
292 * @param type An <strong>int</strong> which maps to a shell process type.
293 * @return A <strong>String</strong> which is the thread process's text name.
294 */
295 public String typeAsString(int type) {
296 String name = null;
297 switch(type) {
298 case BUILD:
299 name = Dictionary.get("GShell.Build");
300 break;
301 case IMPORT:
302 name = Dictionary.get("GShell.Import");
303 break;
304 case NEW:
305 name = Dictionary.get("GShell.New");
306 break;
307 case EXPORT:
308 name= Dictionary.get("GShell.Export");
309 break;
310 default:
311 name = Dictionary.get("GShell.Other");
312 }
313 return name;
314 }
315}
Note: See TracBrowser for help on using the repository browser.