Changeset 31665
- Timestamp:
- 2017-05-08T21:18:21+12:00 (7 years ago)
- Location:
- main/trunk/greenstone3/src/java/org/greenstone
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
main/trunk/greenstone3/src/java/org/greenstone/admin/guiext/Command.java
r25635 r31665 12 12 import javax.swing.JTextArea; 13 13 import javax.swing.JOptionPane; 14 15 import org.greenstone.util.SafeProcess; 14 16 15 17 public class Command implements Runnable … … 57 59 58 60 File workingDirectory = new File(_parent.getParent().getParent().getExtensionDirectory()); 59 Process commandLineProc = null;61 SafeProcess commandLineProc = null; 60 62 try{ 61 63 commandLineProc = null; 64 String[] args = new String[3]; 62 65 63 66 if(System.getProperty("os.name").contains("Windows")){ 64 String[] args = new String[3];65 67 args[0] = "cmd.exe"; 66 68 args[1] = "/C"; … … 75 77 messageArea.append("\nExecuting \"" + allArgs + "\" on the command line\n"); 76 78 77 commandLineProc = Runtime.getRuntime().exec(args, null, workingDirectory);79 //commandLineProc = Runtime.getRuntime().exec(args, null, workingDirectory); 78 80 } 79 81 else{ 80 String[] args = new String[3];81 82 args[0] = "sh"; 82 83 args[1] = "-c"; … … 90 91 91 92 messageArea.append("\nExecuting \"" + allArgs + "\" on the command line\n"); 92 commandLineProc = Runtime.getRuntime().exec(args, null, workingDirectory); 93 } 94 93 //commandLineProc = Runtime.getRuntime().exec(args, null, workingDirectory); 94 } 95 96 commandLineProc = new SafeProcess(args, null, workingDirectory); 97 98 /* 95 99 BufferedReader stdInput = new BufferedReader(new InputStreamReader(commandLineProc.getInputStream())); 96 100 … … 104 108 105 109 int success = commandLineProc.waitFor(); 106 110 */ 111 112 // Replacing the above and its use of the PrinterThread inner class with SafeProcess.java: 113 SafeProcess.LineByLineHandler outLineHandler = new ProcessLineHandler(messageArea, SafeProcess.STDOUT); 114 SafeProcess.LineByLineHandler errLineHandler = new ProcessLineHandler(messageArea, SafeProcess.STDERR); 115 116 int success = commandLineProc.runProcess(outLineHandler, errLineHandler); 117 107 118 if(success != 0){ 108 119 System.err.println("Command line process \"" + command + "\" returned unsuccessfully with the value \"" + success + "\""); … … 148 159 } 149 160 161 /* 150 162 public class PrinterThread extends Thread 151 163 { … … 173 185 } 174 186 } 187 }*/ 188 189 public class ProcessLineHandler extends SafeProcess.LineByLineHandler 190 { 191 // These members need to be final in order to synchronize on them 192 private final JTextArea _messageArea; 193 194 public ProcessLineHandler(JTextArea messageArea, int src) 195 { 196 super(src); // will set this.source to STDERR or STDOUT 197 _messageArea = messageArea; 198 } 199 200 public void gotLine(String line) { // first non-null line 201 202 // messageArea needs to be synchronized, since both the process' 203 // stderr and stdout will be attempting to append to it 204 205 synchronized(_messageArea) { 206 _messageArea.append(line + "\n"); 207 _messageArea.setSelectionEnd(_messageArea.getDocument().getLength()); 208 } 209 } 210 public void gotException(Exception e) { 211 e.printStackTrace(); 212 } 213 175 214 } 176 215 } -
main/trunk/greenstone3/src/java/org/greenstone/gsdl3/service/MapRetrieve.java
r28966 r31665 23 23 import org.greenstone.gsdl3.util.GSXML; 24 24 import org.greenstone.gsdl3.util.XMLConverter; 25 import org.greenstone.util.SafeProcess; 25 26 26 27 import org.w3c.dom.Document; … … 459 460 // get the map size 460 461 String get_size[] = { "identify", "-size", "10000", temp_image_file }; 462 /* 461 463 Process proc; 462 464 proc = Runtime.getRuntime().exec(get_size); … … 464 466 img_size = br.readLine(); 465 467 proc.waitFor(); 468 */ 469 SafeProcess proc = new SafeProcess(get_size); 470 proc.runProcess(); 471 img_size = proc.getStdOutput(); 472 proc = null; 473 466 474 img_size = img_size.substring(img_size.indexOf("JPEG") + 5, img_size.indexOf(" ", img_size.indexOf("JPEG") + 5)); 467 475 width = Integer.parseInt(img_size.substring(0, img_size.indexOf("x"))); … … 541 549 BufferedWriter bw = new BufferedWriter(new FileWriter(this.temp_files_dir + "add_x_" + uid)); 542 550 ; 543 Process proc;551 SafeProcess proc; 544 552 545 553 // if a new search … … 547 555 { 548 556 // copy requested map to temp.jpg 549 proc = Runtime.getRuntime().exec("cp " + this.files_home_dir + "maps" + File.separator + img_num + ".jpg " + temp_image_file); 550 proc.waitFor(); 557 /*proc = Runtime.getRuntime().exec("cp " + this.files_home_dir + "maps" + File.separator + img_num + ".jpg " + temp_image_file); 558 proc.waitFor();*/ 559 proc = new SafeProcess("cp " + this.files_home_dir + "maps" + File.separator + img_num + ".jpg " + temp_image_file); 560 proc.runProcess(); 561 proc = null; 551 562 } 552 563 553 564 //get the image size 554 565 String get_size[] = { "identify", "-size", "10000", temp_image_file }; 555 proc = Runtime.getRuntime().exec(get_size);566 /*proc = Runtime.getRuntime().exec(get_size); 556 567 BufferedReader br = new BufferedReader(new InputStreamReader(proc.getInputStream())); 557 568 img_size = br.readLine(); 558 569 proc.waitFor(); 570 */ 571 proc = new SafeProcess(get_size); 572 proc.runProcess(); 573 img_size = proc.getStdOutput(); 574 proc = null; 575 559 576 img_size = img_size.substring(img_size.indexOf("JPEG") + 5, img_size.indexOf(" ", img_size.indexOf("JPEG") + 5)); 560 577 if (img_size.indexOf("+") != -1) … … 754 771 buf.close(); 755 772 // execute the command for the legend image 756 proc = Runtime.getRuntime().exec("sh " + this.temp_files_dir + "add_l_" + uid); 757 proc.waitFor(); 773 /*proc = Runtime.getRuntime().exec("sh " + this.temp_files_dir + "add_l_" + uid); 774 proc.waitFor();*/ 775 proc = new SafeProcess("sh " + this.temp_files_dir + "add_l_" + uid); 776 proc.runProcess(); 777 proc = null; 758 778 } 759 779 inType.close(); … … 763 783 764 784 // execute the convert commands etc. 765 proc = Runtime.getRuntime().exec("sh " + this.temp_files_dir + "add_x_" + uid);785 /*proc = Runtime.getRuntime().exec("sh " + this.temp_files_dir + "add_x_" + uid); 766 786 proc.waitFor(); 787 */ 788 proc = new SafeProcess("sh " + this.temp_files_dir + "add_x_" + uid); 789 proc.runProcess(); 790 proc = null; 767 791 768 792 } -
main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/GDBMWrapper.java
r31230 r31665 29 29 import java.util.ArrayList; 30 30 31 import org.greenstone.util.SafeProcess; 32 31 33 /** 32 34 * java wrapper class for gdbm - uses Java-GDBM written by Martin Pool replaces … … 113 115 String cmdTest = "perl -v 2>&1"; 114 116 //String cmdTest = "echo %PATH%"; 115 int returnValue = Processing.runProcess(cmdTest); 117 118 //int returnValue = Processing.runProcess(cmdTest); 119 120 // replace Processing.java with SafeProcess.java 121 // so retain the same behaviour (logging) 122 SafeProcess process = new SafeProcess(cmdTest); 123 logger.error("executing command "+cmdTest); 124 int returnValue = process.runProcess(); 125 // do something with the messages 126 logger.error("err>"+process.getStdError()); 127 logger.error("out>"+process.getStdOutput()); 128 process = null; 129 116 130 if (returnValue != 0) 117 131 { … … 122 136 123 137 String cmd = "perl -S txtgz-to-gdbm.pl \"" + txtgzFilename + "\" \"" + filename + "\""; 124 returnValue = Processing.runProcess(cmd); 138 //returnValue = Processing.runProcess(cmd); 139 140 // replace Processing.java with SafeProcess.java 141 // so retain the same behaviour (logging) 142 process = new SafeProcess(cmd); 143 logger.error("executing command "+cmd); 144 returnValue = process.runProcess(); 145 // do something with the messages 146 logger.error("err>"+process.getStdError()); 147 logger.error("out>"+process.getStdOutput()); 148 process = null; 149 125 150 // For some reason, launching this command with gsdl_system() still returns 1 126 151 // even when it returns 0 when run from the command-line. We can check whether -
main/trunk/greenstone3/src/java/org/greenstone/server/BaseServer.java
r24479 r31665 86 86 if (configure_required_){ 87 87 server_control_.displayMessage(dictionary.get("ServerControl.Configuring")); 88 int state = run Target(CONFIGURE_CMD);88 int state = run(CONFIGURE_CMD); 89 89 90 90 if (state != RunTarget.SUCCESS){ … … 114 114 115 115 protected abstract int runTarget(String cmd); 116 117 protected int run(String cmd) { 118 int result = runTarget(cmd); 119 server_control_.repaint(); 120 // from Java 7, can just call revalidate() instead of invalidate() and validate(): 121 server_control_.invalidate(); 122 server_control_.validate(); 123 return result; 124 } 125 116 126 public abstract String getBrowserURL(); 117 127 public abstract void reload(); // reload properties, since they may have changed … … 136 146 if (configure_required_){ 137 147 server_control_.displayMessage(dictionary.get("ServerControl.Configuring")); 138 state = run Target(CONFIGURE_CMD);148 state = run(CONFIGURE_CMD); 139 149 140 150 if (state != RunTarget.SUCCESS){ … … 153 163 logger_.error("Exception trying to sleep: " + e); 154 164 } 155 state = run Target(START_CMD);165 state = run(START_CMD); 156 166 157 167 if (state != RunTarget.SUCCESS){ … … 239 249 server_control_.displayMessage(dictionary.get("ServerControl.Stopping")); 240 250 } 241 int state = run Target(STOP_CMD);251 int state = run(STOP_CMD); 242 252 243 253 if (state != RunTarget.SUCCESS){ -
main/trunk/greenstone3/src/java/org/greenstone/util/BrowserLauncher.java
r22085 r31665 7 7 import org.greenstone.server.BaseProperty; 8 8 import org.greenstone.util.Misc; 9 import org.greenstone.util.SafeProcess; 9 10 import org.apache.log4j.*; 10 11 … … 50 51 // we try to look for a browser 51 52 for (int i=0; i<default_browsers.length; i++) { 52 if ( isAvailable(default_browsers[i])) {53 if (SafeProcess.isAvailable(default_browsers[i])) { 53 54 this.command = default_browsers[i] + " %1"; 54 55 break; … … 60 61 } 61 62 63 // Replaced by SafeProcess.isAvailable(program) 64 /* 62 65 protected boolean isAvailable(String program) { 63 66 try { … … 76 79 return false; 77 80 } 78 } 81 }*/ 79 82 80 83 public int getBrowserState(){ … … 100 103 logger.info(new_command); 101 104 Runtime rt = Runtime.getRuntime(); 102 Process process = rt.exec(new_command);105 //Process process = rt.exec(new_command); 103 106 state = LAUNCHSUCCESS; 104 exitCode = process.waitFor(); 107 //exitCode = process.waitFor(); 108 SafeProcess process = new SafeProcess(new_command); 109 exitCode = process.runProcess(); 110 process = null; 105 111 logger.info("ExitCode:" + exitCode); 106 112 if (exitCode != 0) { // if Netscape or mozilla was not open 107 113 logger.info("couldn't do remote, trying original command"); 108 114 logger.info(this.command); 109 process = rt.exec(this.command); // try the original command115 //process = rt.exec(this.command); // try the original command 110 116 state = LAUNCHSUCCESS; 111 117 //for some reason the following part is not executed sometimes. 112 exitCode = process.waitFor(); 118 //exitCode = process.waitFor(); 119 process = new SafeProcess(this.command); 120 exitCode = process.runProcess(); 121 process = null; 113 122 } 114 123 } else { 115 124 logger.info(this.command); 116 125 Runtime rt = Runtime.getRuntime(); 117 Process process = rt.exec(this.command);126 //Process process = rt.exec(this.command); 118 127 state = LAUNCHSUCCESS; 119 128 //for some reason the following part is not executed sometimes. 120 exitCode = process.waitFor(); 129 //exitCode = process.waitFor(); 130 SafeProcess process = new SafeProcess(this.command); 131 exitCode = process.runProcess(); 121 132 } 122 133 -
main/trunk/greenstone3/src/java/org/greenstone/util/RunTarget.java
r29946 r31665 3 3 import java.io.IOException; 4 4 import java.io.BufferedReader; 5 import java.io.Closeable; 5 6 import java.io.InputStream; 6 7 import java.io.InputStreamReader; … … 28 29 try { 29 30 state = -1; 30 Runtime run = Runtime.getRuntime();31 32 31 String targetCmd = getTargetCmd(); 33 32 logger.info("Target: " + targetCmd); 34 33 34 /* 35 Runtime run = Runtime.getRuntime(); 35 36 Process process = run.exec(targetCmd); 36 37 BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream())); … … 55 56 56 57 br.close(); 58 */ 59 60 61 SafeProcess process = new SafeProcess(targetCmd); 62 process.setSplitStdOutputNewLines(true); 63 process.runProcess(); 64 String output = process.getStdOutput(); 65 String[] lines = output.split("[\\r\\n]+"); // http://stackoverflow.com/questions/454908/split-java-string-by-new-line 66 for(int i = 0; i < lines.length; i++) { 67 //System.err.println("*** Got line:|" + lines[i] + "|***"); 68 String line = lines[i].trim(); 69 if (line.equals(targetSuccess)){ 70 state = 0; 71 } 72 73 if (line.equals(targetFailed)){ 74 state = 1; 75 } 57 76 77 if(line.startsWith(targetFinished)){ 78 break; 79 } 80 } 81 //System.err.println("\n\n"); 82 83 58 84 if(state < 0) { 59 85 logger.info("Unexpected end of input when running target: " + targetCmd); -
main/trunk/greenstone3/src/java/org/greenstone/util/SafeProcess.java
r31663 r31665 33 33 34 34 public class SafeProcess { 35 public static int DEBUG = 0;35 public static int DEBUG = 1; 36 36 37 37 public static final int STDERR = 0; … … 741 741 SafeProcess prcs = new SafeProcess("which " + program); 742 742 prcs.runProcess(); 743 String output = prcs.getStdOutput(); 743 String output = prcs.getStdOutput().trim(); 744 ///System.err.println("*** 'which " + program + "' returned: |" + output + "|"); 744 745 if(output.equals("")) { 746 return false; 747 } else if(output.indexOf("no "+program) !=-1) { // from GS3's org.greenstone.util.BrowserLauncher.java's isAvailable(program) 748 log("@@@ SafeProcess.isAvailable(): " + program + "is not available"); 745 749 return false; 746 750 } … … 1010 1014 logger.info(msg); 1011 1015 1012 //System.err.println(msg);1016 System.err.println(msg); 1013 1017 1014 1018 //DebugStream.println(msg); … … 1019 1023 logger.error(msg, e); 1020 1024 1021 //System.err.println(msg);1022 //e.printStackTrace();1025 System.err.println(msg); 1026 e.printStackTrace(); 1023 1027 1024 1028 //DebugStream.println(msg); … … 1030 1034 logger.error(e); 1031 1035 1032 //e.printStackTrace();1036 e.printStackTrace(); 1033 1037 1034 1038 //DebugStream.printStackTrace(e);
Note:
See TracChangeset
for help on using the changeset viewer.