Changeset 4364 for trunk/gli/src/org/greenstone/gatherer/shell/GShell.java
- Timestamp:
- 2003-05-27T15:40:47+12:00 (21 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/gli/src/org/greenstone/gatherer/shell/GShell.java
r4317 r4364 48 48 */ 49 49 public class GShell 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 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 107 * @param listener The new <i>GShellListener</i>. 108 108 */ 109 110 111 112 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 113 * @param listener The <i>GShellListener</i> to be removed. 114 114 */ 115 116 117 118 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 119 */ 120 120 public void run() { 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 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 157 ///ystem.err.println("Command: " + command); 158 159 158 fireMessage(type, get("Command") + ": " + command, status); 159 message(Message.EVENT, get("Command") + ": " + command); 160 160 161 162 163 164 165 166 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 167 // Captures the std err of a program and pipes it into 168 168 // std in of java 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 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 194 195 196 197 195 if(!hasSignalledStop()) { 196 // Now display final message based on exit value 197 prcs.waitFor(); 198 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 } 243 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 244 * @param type An <strong>int</strong> indicating the process type. 245 245 * @param message The message as a <strong>String</strong>. 246 246 * @param status An <strong>int</strong> specifying the current status of the process. 247 247 */ 248 249 250 251 252 253 254 255 256 257 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 258 * @param type An <strong>int</strong> indicating the process type. 259 259 * @param status An <strong>int</strong> specifying the current status of the process. 260 260 */ 261 262 263 264 265 266 267 268 269 270 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 271 * @param type An <strong>int</strong> indicating the process type. 272 272 * @param status An <strong>int</strong> specifying the current status of the process. 273 273 */ 274 275 276 277 278 279 280 281 282 283 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 284 * @param key A <strong>String</strong> used to match against a phrase from the <strong>Dictionary</strong>. 285 285 * @return The phrase as a <strong>String</strong>. 286 286 */ 287 288 289 290 291 292 293 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 294 * @return A <strong>boolean</strong> indicating if the user wanted to stop. 295 295 */ 296 297 298 299 300 301 302 303 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 304 * @param level An <strong>int</strong> indicating the message level for this message. 305 305 * @param message A <strong>String</strong> which contains the payload of this message. 306 306 */ 307 308 309 310 311 312 313 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 314 * @param type An <strong>int</strong> which maps to a shell process type. 315 315 * @return A <strong>String</strong> which is the thread process's text name. 316 316 */ 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 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 334 } 335 336
Note:
See TracChangeset
for help on using the changeset viewer.