1 | package org.greenstone.util;
|
---|
2 |
|
---|
3 | import java.io.IOException;
|
---|
4 | import java.io.BufferedReader;
|
---|
5 | import java.io.Closeable;
|
---|
6 | import java.io.InputStream;
|
---|
7 | import java.io.InputStreamReader;
|
---|
8 |
|
---|
9 | import org.greenstone.util.LogStreamGobbler;
|
---|
10 |
|
---|
11 | import org.apache.log4j.*;
|
---|
12 |
|
---|
13 | public 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 | }
|
---|