Changeset 29947 for main/trunk/greenstone3/src
- Timestamp:
- 2015-06-05T20:58:20+12:00 (9 years ago)
- Location:
- main/trunk/greenstone3/src/java/org/greenstone/gsdl3
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
main/trunk/greenstone3/src/java/org/greenstone/gsdl3/build/GS2PerlConstructor.java
r29869 r29947 13 13 import java.io.BufferedReader; 14 14 import java.io.BufferedWriter; 15 import java.io.Closeable; 15 16 import java.io.FileWriter; 16 17 import java.io.InputStreamReader; 17 18 import java.io.File; 19 import java.io.IOException; 18 20 import java.util.ArrayList; 19 21 import java.util.Vector; … … 278 280 command.add(GSFile.collectDir(this.site_home)); 279 281 command.add("-removeold"); // saves some seconds processing time when this flag's added in explicitly 282 command.add("-skipactivation"); // gsdl3/util/GS2Construct does the activation and reactivation 280 283 command.addAll(extractParameters(this.process_params)); 281 284 command.add(this.collection_name); … … 365 368 return runPerlCommand(command, null, null); 366 369 } 367 368 protected boolean runPerlCommand(String[] command, String[] envvars, File dir) 369 { 370 371 protected boolean runPerlCommand(String[] command, String[] envvars, File dir) 372 { 373 boolean success = true; 374 370 375 int sepIndex = this.gsdl3home.lastIndexOf(File.separator); 371 376 String srcHome = this.gsdl3home.substring(0, sepIndex); … … 397 402 398 403 sendMessage(new ConstructionEvent(this, GSStatus.INFO, "command = " + command_str)); 404 Process prcs = null; 405 BufferedReader ebr = null; 406 BufferedReader stdinbr = null; 399 407 try 400 408 { 401 409 Runtime rt = Runtime.getRuntime(); 402 410 sendProcessBegun(new ConstructionEvent(this, GSStatus.ACCEPTED, "starting")); 403 Processprcs = (dir == null)411 prcs = (dir == null) 404 412 ? rt.exec(command, args.toArray(new String[args.size()])) 405 413 : rt.exec(command, args.toArray(new String[args.size()]), dir); … … 407 415 InputStreamReader eisr = new InputStreamReader(prcs.getErrorStream()); 408 416 InputStreamReader stdinisr = new InputStreamReader(prcs.getInputStream()); 409 BufferedReaderebr = new BufferedReader(eisr);410 BufferedReaderstdinbr = new BufferedReader(stdinisr);417 ebr = new BufferedReader(eisr); 418 stdinbr = new BufferedReader(stdinisr); 411 419 // Captures the std err of a program and pipes it into 412 420 // std in of java … … 440 448 } 441 449 } 442 bw.close();443 450 closeResource(bw); 451 444 452 if (!this.cancel) 445 453 { … … 448 456 449 457 if (prcs.exitValue() == 0) 450 { 458 { 451 459 //status = OK; 452 460 sendProcessStatus(new ConstructionEvent(this, GSStatus.CONTINUING, "Success")); 453 461 462 success = true; 454 463 } 455 464 else … … 458 467 sendProcessStatus(new ConstructionEvent(this, GSStatus.ERROR, "Failure")); 459 468 460 return false; 469 //return false; 470 success = false; 461 471 462 472 } … … 466 476 // 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. 467 477 sendProcessStatus(new ConstructionEvent(this, GSStatus.HALTED, "killing the process")); 468 prcs.getOutputStream().close(); 469 prcs.destroy(); 470 //status = ERROR; 471 return false; 478 //prcs.getOutputStream().close(); 479 //prcs.destroy(); 480 ////status = ERROR; 481 482 //return false; 483 success = false; 472 484 } 473 485 } … … 476 488 e.printStackTrace(); 477 489 sendProcessStatus(new ConstructionEvent(this, GSStatus.ERROR, "Exception occurred " + e.toString())); 478 } 479 480 // we're done, but we dont send a process complete message here cos there ight be stuff to do after this has finished. 481 return true; 482 483 } 484 490 } finally { 491 // http://steveliles.github.io/invoking_processes_from_java.html 492 // http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html?page=2 493 // http://mark.koli.ch/leaky-pipes-remember-to-close-your-streams-when-using-javas-runtimegetruntimeexec 494 495 if( prcs != null ) { 496 closeResource(prcs.getErrorStream()); 497 closeResource(prcs.getOutputStream()); 498 closeResource(prcs.getInputStream()); 499 prcs.destroy(); 500 } 501 502 closeResource(ebr); 503 closeResource(stdinbr); 504 } 505 506 // we're done, but we don't send a process complete message here cos there might be stuff to do after this has finished. 507 //return true; 508 return success; 509 } 510 511 public static void closeResource(Closeable resourceHandle) { 512 try { 513 if(resourceHandle != null) { 514 resourceHandle.close(); 515 resourceHandle = null; 516 } 517 } catch(Exception e) { 518 System.err.println("Exception closing resource: " + e.getMessage()); 519 e.printStackTrace(); 520 } 521 } 485 522 } -
main/trunk/greenstone3/src/java/org/greenstone/gsdl3/service/GS2Construct.java
r29869 r29947 330 330 String lang = request.getAttribute(GSXML.LANG_ATT); 331 331 332 Element response = runCommand(request, GS2PerlConstructor.ACTIVATE); 332 UserContext userContext = new UserContext(request); 333 String request_type = request.getAttribute(GSXML.TYPE_ATT); 334 335 // now we de-activate the collection before running activate.pl, and then re-activate at end 336 // So activate.pl only does the moving, no activation. This way will prevent java from launching 337 // perl, exiting and then leaving dangling file handles (on index/text/col.gdb) in perl. 338 if (!request_type.equals(GSXML.REQUEST_TYPE_STATUS)) { 339 systemRequest("delete", coll_name, null, userContext); // deactivate collection 340 } 341 342 Element response = runCommand(request, GS2PerlConstructor.ACTIVATE); // if request is for STATUS, then this won't run activate.pl 343 333 344 Element status = (Element) GSXML.getChildByTagName(response, GSXML.STATUS_ELEM); 334 335 String request_type = request.getAttribute(GSXML.TYPE_ATT);336 if (request_type.equals(GSXML.REQUEST_TYPE_STATUS))337 {338 return response;339 }340 341 UserContext userContext = new UserContext(request);342 systemRequest("delete", coll_name, null, userContext);343 345 344 346 // check for finished … … 384 386 status.appendChild(t); 385 387 // once have got here, we assume 386 // the first bit proceeded successfully, now reload the collection 388 // the first bit proceeded successfully, now reload the collection (sends a collection reactivation request) 387 389 systemRequest("reload", coll_name, status, userContext); // this will append more messages to the status, and overwrite the error code att 388 390 return response;
Note:
See TracChangeset
for help on using the changeset viewer.