Changeset 7739 for trunk/gli/src/org/greenstone/gatherer/shell
- Timestamp:
- 2004-07-16T13:48:18+12:00 (20 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/gli/src/org/greenstone/gatherer/shell/GShell.java
r7325 r7739 38 38 39 39 import java.io.*; 40 import java.net.*; 40 41 import java.util.ArrayList; 42 import java.util.Enumeration; 41 43 import javax.swing.*; 42 44 import javax.swing.event.*; … … 48 50 import org.greenstone.gatherer.shell.GShellListener; 49 51 import org.greenstone.gatherer.shell.GShellProgressMonitor; 52 import org.greenstone.gatherer.util.StaticStrings; 53 import org.greenstone.gatherer.util.Utility; 50 54 51 55 /** 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. … … 103 107 static public boolean processRunning(Process process) { 104 108 boolean process_running = false; 109 105 110 try { 106 111 process.exitValue(); // This will throw an exception if the process hasn't ended yet. … … 149 154 } */ 150 155 156 157 158 protected boolean got_stream_char(InputStreamReader isr, StringBuffer line_buffer, 159 BufferedOutputStream bos) throws IOException 160 { 161 // Hopefully this doesn't block if the process is trying to write to STDOUT/STDERR. 162 163 boolean input_status = false; 164 165 if(isr.ready()) { 166 input_status = true; 167 int c = isr.read(); 168 if(c == '\n' || c == '\r') { 169 if(line_buffer.length() > 0) { 170 String line = line_buffer.toString(); 171 Gatherer.println("* " + line + " *"); 172 fireMessage(type, typeAsString(type) + "> " + line, status, bos); 173 line = null; 174 line_buffer = new StringBuffer(); 175 } 176 } 177 else { 178 line_buffer.append((char)c); 179 } 180 } 181 182 return input_status; 183 } 184 185 186 protected StringBuffer get_stream_char(InputStreamReader isr, StringBuffer line_buffer, 187 BufferedOutputStream bos) throws IOException 188 { 189 int c = isr.read(); 190 ///atherer.println("isr: '" + (char) c + "'"); 191 if(c == '\n' || c == '\r') { 192 if(line_buffer.length() > 0) { 193 String line = line_buffer.toString(); 194 // Gatherer.println("* " + line + " *"); 195 fireMessage(type, typeAsString(type) + "> " + line, status, bos); 196 line_buffer = new StringBuffer(); 197 } 198 } 199 else { 200 line_buffer.append((char)c); 201 } 202 203 return line_buffer; 204 } 205 206 protected void runRemote(String[] args, BufferedOutputStream bos) 207 { 208 try { 209 210 String perl_cmd = args[0]; 211 int perl_cmd_root = perl_cmd.lastIndexOf(File.separator); 212 213 if (perl_cmd_root > 0) { 214 String perl_cmd_cut = perl_cmd.substring(perl_cmd_root+1); 215 perl_cmd = perl_cmd_cut; 216 } 217 218 String launch = Gatherer.cgiBase + "launch"; 219 launch = launch + "?cmd=" + perl_cmd; 220 221 for(int i = 1; i<args.length; i++) { 222 223 String arg = args[i]; 224 225 if (arg.equals("-collectdir") || arg.equals("-importdir") 226 || arg.equals("-builddir")) { 227 // skip it 228 i++; 229 continue; 230 } 231 232 launch += "&"; 233 234 if(arg.startsWith(StaticStrings.MINUS_CHARACTER)) { 235 String name = arg.substring(1); 236 launch = launch + name + "="; 237 if (i+1<args.length-1) { 238 if (!args[i+1].startsWith(StaticStrings.MINUS_CHARACTER)) { 239 i++; 240 String val = args[i]; 241 launch = launch + val; 242 } 243 } 244 } 245 else { 246 launch = launch + "c=" + arg; 247 } 248 } 249 250 System.err.println("**** launch url = " + launch); 251 252 URL launch_url = new URL(launch); 253 URLConnection launch_connection = launch_url.openConnection(); 254 InputStream stdis = launch_connection.getInputStream(); 255 InputStreamReader stdisr = new InputStreamReader(stdis, "UTF-8" ); 256 257 BufferedReader stdbr = new BufferedReader(stdisr); 258 259 if (type == GShell.NEW) { 260 while(true) { 261 String line = stdbr.readLine(); 262 if (line == null) { break; } 263 } 264 } 265 else { 266 while(!hasSignalledStop()) { 267 String line = stdbr.readLine(); 268 if (line == null) { break; } 269 fireMessage(type, typeAsString(type) + "> " + line, status, bos); 270 } 271 } 272 stdbr.close(); 273 274 status = OK; 275 fireMessage(type, typeAsString(type) + "> " + Dictionary.get("GShell.Success"), status, null); 276 } 277 // Exception 278 catch (Exception exception) { 279 Gatherer.println("Exception in GShell.runRemove() - unexpected"); 280 Gatherer.printStackTrace(exception); 281 status = ERROR; 282 } 283 } 284 285 286 protected void runLocal(String[] args, BufferedOutputStream bos) 287 { 288 try { 289 String command = ""; 290 for(int i = 0; i < args.length; i++) { 291 command = command + args[i] + " "; 292 } 293 294 Gatherer.println("Command: "+command); 295 ///ystem.err.println("Command: " + command); 296 fireMessage(type, Dictionary.get("GShell.Command") + ": " + command, status, null); 297 298 Runtime rt = Runtime.getRuntime(); 299 Process prcs = rt.exec(args); 300 301 InputStreamReader eisr = new InputStreamReader( prcs.getErrorStream(), "UTF-8" ); 302 InputStreamReader stdisr = new InputStreamReader( prcs.getInputStream(), "UTF-8" ); 303 //BufferedReader ebr = new BufferedReader( eisr ); 304 //BufferedReader stdinbr = new BufferedReader( stdinisr ); 305 // Captures the std err of a program and pipes it into std in of java 306 307 StringBuffer eline_buffer = new StringBuffer(); 308 StringBuffer stdline_buffer = new StringBuffer(); 309 310 while(type != GShell.NEW && processRunning(prcs) && !hasSignalledStop()) { 311 // Hopefully this doesn't block if the process is trying to write to STDOUT. 312 if((eisr!=null) && eisr.ready()) { 313 eline_buffer = get_stream_char(eisr,eline_buffer,bos); 314 } 315 // Hopefully this won't block if the process is trying to write to STDERR 316 else if(stdisr.ready()) { 317 stdline_buffer = get_stream_char(stdisr,stdline_buffer,bos); 318 } 319 else { 320 try { 321 sleep(100); 322 } 323 catch(Exception exception) { 324 } 325 } 326 } 327 328 if(!hasSignalledStop()) { 329 // Of course, just because the process is finished doesn't 330 // mean the incoming streams are empty. Unfortunately I've 331 // got no chance of preserving order, so I'll process the 332 // error stream first, then the out stream 333 while(eisr.ready()) { 334 eline_buffer = get_stream_char(eisr,eline_buffer,bos); 335 } 336 337 while(stdisr.ready()) { 338 stdline_buffer = get_stream_char(stdisr,stdline_buffer,bos); 339 } 340 341 // Ensure that any messages still remaining in the string buffers are fired off. 342 if(eline_buffer.length() > 0) { 343 String eline = eline_buffer.toString(); 344 //Gatherer.println("Last bit of eline: " + eline); 345 fireMessage(type, typeAsString(type) + "> " + eline, status, bos); 346 eline = null; 347 } 348 349 if(stdline_buffer.length() > 0) { 350 String stdline = stdline_buffer.toString(); 351 //Gatherer.println("Last bit of stdline: " + stdline); 352 fireMessage(type, typeAsString(type) + "> " + stdline, status, null); 353 stdline = null; 354 } 355 } 356 else { 357 System.err.println("We've been asked to stop."); 358 } 359 360 361 if(!hasSignalledStop()) { 362 // Now display final message based on exit value 363 364 prcs.waitFor(); 365 366 if(prcs.exitValue() == 0) { 367 status = OK; 368 fireMessage(type, typeAsString(type) + "> " + Dictionary.get("GShell.Success"), status, null); 369 } 370 else { 371 status = ERROR; 372 fireMessage(type, typeAsString(type) + "> " + Dictionary.get("GShell.Failure"), status, null); 373 } 374 375 eisr.close(); 376 stdisr.close(); 377 } 378 else { 379 // I need to somehow kill the child process. Unfortunately 380 // Thread.stop() and Process.destroy() both fail to do 381 // this. But now, thankx to the magic of Michaels 'close the 382 // stream suggestion', it works fine (no it doesn't!) 383 prcs.getInputStream().close(); 384 prcs.getErrorStream().close(); 385 prcs.getOutputStream().close(); 386 prcs.destroy(); 387 status = CANCELLED; 388 } 389 } 390 // Exception 391 catch (Exception exception) { 392 Gatherer.println("Exception in GShell.runLocal() - unexpected"); 393 Gatherer.printStackTrace(exception); 394 status = ERROR; 395 } 396 } 397 398 399 151 400 /** Any threaded class must include this method to allow the thread body to be run. */ 152 401 public void run() { 402 403 String col_name = args[args.length-1]; 404 153 405 // Determine if the user has asked for an outfile. 154 406 String out_name = null; … … 170 422 } 171 423 } 424 425 if (Gatherer.isGsdlRemote) { 426 if (type == IMPORT) { 427 // zip up import folder 428 Utility.zipup(col_name,"import"); 429 // upload it to gsdl server 430 Utility.upload_url_zip(col_name,"import"); 431 432 // upload etc/collect.cfg to server 433 Utility.zipup(col_name,Utility.CONFIG_FILE); 434 Utility.upload_url_zip(col_name,"etc"); 435 } 436 } 437 438 172 439 // Issue a processBegun event 173 440 fireProcessBegun(type, status); 174 try { 175 String command = ""; 176 for(int i = 0; i < args.length; i++) { 177 command = command + args[i] + " "; 178 } 179 Gatherer.println("Command: "+command); 180 ///ystem.err.println("Command: " + command); 181 fireMessage(type, Dictionary.get("GShell.Command") + ": " + command, status, null); 182 183 Runtime rt = Runtime.getRuntime(); 184 Process prcs = rt.exec(args); 185 InputStreamReader eisr = new InputStreamReader( prcs.getErrorStream(), "UTF-8" ); 186 InputStreamReader stdisr = new InputStreamReader( prcs.getInputStream(), "UTF-8" ); 187 //BufferedReader ebr = new BufferedReader( eisr ); 188 //BufferedReader stdinbr = new BufferedReader( stdinisr ); 189 // Captures the std err of a program and pipes it into std in of java 190 191 StringBuffer eline_buffer = new StringBuffer(); 192 StringBuffer stdline_buffer = new StringBuffer(); 193 while(type != GShell.NEW && processRunning(prcs) && !hasSignalledStop()) { 194 // Hopefully this doesn't block if the process is trying to write to STDOUT. 195 if(eisr.ready()) { 196 int c = eisr.read(); 197 ///atherer.println("eisr: '" + (char) c + "'"); 198 if(c == '\n' || c == '\r') { 199 if(eline_buffer.length() > 0) { 200 String eline = eline_buffer.toString(); 201 ///atherer.println("* " + eline + " *"); 202 fireMessage(type, typeAsString(type) + "> " + eline, status, bos); 203 eline = null; 204 eline_buffer = new StringBuffer(); 205 } 206 } 207 else { 208 eline_buffer.append((char)c); 209 } 210 } 211 // Hopefully this won't block if the process is trying to write to STDERR 212 else if(stdisr.ready()) { 213 int c = stdisr.read(); 214 ///atherer.println("eisr: '" + (char) c + "'"); 215 if(c == '\n' || c == '\r') { 216 if(stdline_buffer.length() > 0) { 217 String stdline = stdline_buffer.toString(); 218 ///atherer.println("+ " + stdline + " +"); 219 fireMessage(type, typeAsString(type) + "> " + stdline, status, null); 220 stdline = null; 221 stdline_buffer = new StringBuffer(); 222 } 223 } 224 else { 225 stdline_buffer.append((char)c); 226 } 227 } 228 else { 229 try { 230 sleep(100); 231 } 232 catch(Exception exception) { 233 } 234 } 235 } 236 if(!hasSignalledStop()) { 237 // Of course, just because the process is finished doesn't mean the incoming streams are empty. Unfortunately I've got no chance of preserving order, so I'll process the error stream first, then the out stream 238 while(eisr.ready()) { 239 int c = eisr.read(); 240 ///atherer.println("eisr: '" + (char) c + "'"); 241 if(c == '\n' || c == '\r') { 242 if(eline_buffer.length() > 0) { 243 String eline = eline_buffer.toString(); 244 ///atherer.println("* " + eline + " *"); 245 fireMessage(type, typeAsString(type) + "> " + eline, status, bos); 246 eline = null; 247 eline_buffer = new StringBuffer(); 248 } 249 } 250 else { 251 eline_buffer.append((char)c); 252 } 253 } 254 while(stdisr.ready()) { 255 int c = stdisr.read(); 256 ///atherer.println("eisr: '" + (char) c + "'"); 257 if(c == '\n' || c == '\r') { 258 if(stdline_buffer.length() > 0) { 259 String stdline = stdline_buffer.toString(); 260 ///atherer.println("+ " + stdline + " +"); 261 fireMessage(type, typeAsString(type) + "> " + stdline, status, null); 262 stdline = null; 263 stdline_buffer = new StringBuffer(); 264 } 265 } 266 else { 267 stdline_buffer.append((char)c); 268 } 269 } 441 if (Gatherer.isGsdlRemote) { 442 runRemote(args,bos); 443 } 444 else { 445 runLocal(args,bos); 446 } 447 448 if(status == OK) { 449 if (type == NEW) { 450 if (Gatherer.isGsdlRemote) { 451 Utility.download_url_zip(col_name,"."); 452 Utility.unzip(col_name); 453 } 454 } 455 else if(type == IMPORT) { 270 456 271 // Ensure that any messages still remaining in the string buffers are fired off. 272 if(eline_buffer.length() > 0) { 273 String eline = eline_buffer.toString(); 274 ///atherer.println("Last bit of eline: " + eline); 275 fireMessage(type, typeAsString(type) + "> " + eline, status, bos); 276 eline = null; 277 } 457 // download the archives directory (if gsdl server is remote) 458 if (Gatherer.isGsdlRemote) { 459 Utility.delete(Gatherer.c_man.getCollectionArchive()); // remove current archives 460 Utility.download_url_zip(col_name,"archives"); 461 Utility.unzip(col_name); 462 } 463 464 // extract any new metadata from the archive directory. 465 fireMessage(type, typeAsString(type) + "> " + Dictionary.get("GShell.Parsing_Metadata_Start"), status, null); 466 new GreenstoneArchiveParser(progress, this); 467 fireMessage(type, typeAsString(type) + "> " + Dictionary.get("GShell.Parsing_Metadata_Complete"), status, null); 468 } 469 470 else if(type == BUILD) { 278 471 279 if(stdline_buffer.length() > 0) { 280 String stdline = stdline_buffer.toString(); 281 ///atherer.println("Last bit of stdline: " + stdline); 282 fireMessage(type, typeAsString(type) + "> " + stdline, status, null); 283 stdline = null; 284 } 285 } 286 else { 287 System.err.println("We've been asked to stop."); 288 } 289 290 if(!hasSignalledStop()) { 291 // Now display final message based on exit value 292 prcs.waitFor(); 293 294 if(prcs.exitValue() == 0) { 295 status = OK; 296 fireMessage(type, typeAsString(type) + "> " + Dictionary.get("GShell.Success"), status, null); 297 } 298 else { 299 status = ERROR; 300 fireMessage(type, typeAsString(type) + "> " + Dictionary.get("GShell.Failure"), status, null); 301 } 302 } 303 else { 304 // 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!) 305 prcs.getInputStream().close(); 306 prcs.getErrorStream().close(); 307 prcs.getOutputStream().close(); 308 prcs.destroy(); 309 status = CANCELLED; 310 } 311 } 312 // Exception 313 catch (Exception exception) { 314 Gatherer.println("Exception in GShell.run() - unexpected"); 315 Gatherer.printStackTrace(exception); 316 status = ERROR; 317 } 318 // If no error occured, and this was an import process we now extract any new metadata from the archive directory. 319 if(status == OK && type == IMPORT) { 320 fireMessage(type, typeAsString(type) + "> " + Dictionary.get("GShell.Parsing_Metadata_Start"), status, null); 321 new GreenstoneArchiveParser(progress, this); 322 fireMessage(type, typeAsString(type) + "> " + Dictionary.get("GShell.Parsing_Metadata_Complete"), status, null); 323 } 472 // download the building directory (if gsdl server is remote) 473 if (Gatherer.isGsdlRemote) { 474 Utility.delete(Gatherer.c_man.getCollectionBuild()); // remove current build dir 475 Utility.download_url_zip(col_name,"building"); 476 Utility.unzip(col_name); 477 } 478 479 } 480 } 481 324 482 if(hasSignalledStop()) { 325 483 status = CANCELLED;
Note:
See TracChangeset
for help on using the changeset viewer.