source: main/trunk/greenstone3/src/java/org/greenstone/util/RunTarget.java@ 31665

Last change on this file since 31665 was 31665, checked in by ak19, 7 years ago

GS3 source code now updated to use SafeProcess instead of Process (calling Runtime.exec() directly). The use of SafeProcess in RunTarget and BrowserLauncher has been tested on Linux. GDBMWrapper, MapRetrieve and admin/guiext's Command.java are not tested. For GDBMWrapper, because it's in a bit of code that works with txtgz databases. MapRetrive.java and Command.java are untested because I don't know how to test them.

File size: 2.4 KB
Line 
1package org.greenstone.util;
2
3import java.io.IOException;
4import java.io.BufferedReader;
5import java.io.Closeable;
6import java.io.InputStream;
7import java.io.InputStreamReader;
8
9import org.greenstone.util.LogStreamGobbler;
10
11import org.apache.log4j.*;
12
13public abstract class RunTarget extends Thread
14{
15 protected String targetCmd = "";
16 protected static Logger logger = Logger.getLogger(RunTarget.class.getName());
17
18 protected int state = -1; //success: 0 error: 1
19 public static int SUCCESS = 0;
20 public static int FAILED = 1;
21
22 protected String targetSuccess;
23 protected String targetFailed;
24 protected String targetFinished;
25
26 public void run()
27 {
28
29 try {
30 state = -1;
31 String targetCmd = getTargetCmd();
32 logger.info("Target: " + targetCmd);
33
34 /*
35 Runtime run = Runtime.getRuntime();
36 Process process = run.exec(targetCmd);
37 BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
38 String line = null;
39
40 while ((line = br.readLine()) != null) {
41
42 logger.info(line);
43
44 if (line.equals(targetSuccess)){
45 state = 0;
46 }
47
48 if (line.equals(targetFailed)){
49 state = 1;
50 }
51
52 if(line.startsWith(targetFinished)){
53 break;
54 }
55 }
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 }
76
77 if(line.startsWith(targetFinished)){
78 break;
79 }
80 }
81 //System.err.println("\n\n");
82
83
84 if(state < 0) {
85 logger.info("Unexpected end of input when running target: " + targetCmd);
86 }
87 } catch (Exception e) {
88 e.printStackTrace();
89 logger.error(e);
90 state = 1;
91 }
92 }
93
94 public int getTargetState()
95 {
96 return state;
97 }
98
99 public abstract void setTargetCmd(String cmd);
100
101 public String getTargetCmd()
102 {
103 return this.targetCmd;
104 }
105}
Note: See TracBrowser for help on using the repository browser.