1 | package org.greenstone.util;
|
---|
2 |
|
---|
3 | import java.io.IOException;
|
---|
4 | import java.io.BufferedReader;
|
---|
5 | import java.io.InputStream;
|
---|
6 | import java.io.InputStreamReader;
|
---|
7 |
|
---|
8 | import org.greenstone.util.StreamGobbler;
|
---|
9 |
|
---|
10 | import org.apache.log4j.*;
|
---|
11 |
|
---|
12 | public abstract class RunTarget extends Thread
|
---|
13 | {
|
---|
14 | protected String targetCmd = "";
|
---|
15 | protected static Logger logger = Logger.getLogger(RunTarget.class.getName());
|
---|
16 |
|
---|
17 | protected int state = -1; //success: 0 error: 1
|
---|
18 | public static int SUCCESS = 0;
|
---|
19 | public static int FAILED = 1;
|
---|
20 |
|
---|
21 | protected String targetSuccess;
|
---|
22 | protected String targetFailed;
|
---|
23 | protected String targetFinished;
|
---|
24 |
|
---|
25 | public void run()
|
---|
26 | {
|
---|
27 |
|
---|
28 | try {
|
---|
29 | state = -1;
|
---|
30 | Runtime run = Runtime.getRuntime();
|
---|
31 |
|
---|
32 | String targetCmd = getTargetCmd();
|
---|
33 | logger.info("Target: " + targetCmd);
|
---|
34 |
|
---|
35 | Process process = run.exec(targetCmd);
|
---|
36 | BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
|
---|
37 | String line = null;
|
---|
38 |
|
---|
39 | while ((line = br.readLine()) != null) {
|
---|
40 |
|
---|
41 | logger.info(line);
|
---|
42 |
|
---|
43 | if (line.equals(targetSuccess)){
|
---|
44 | state = 0;
|
---|
45 | }
|
---|
46 |
|
---|
47 | if (line.equals(targetFailed)){
|
---|
48 | state = 1;
|
---|
49 | }
|
---|
50 |
|
---|
51 | if(line.startsWith(targetFinished)){
|
---|
52 | break;
|
---|
53 | }
|
---|
54 | }
|
---|
55 |
|
---|
56 | br.close();
|
---|
57 |
|
---|
58 | if(state < 0) {
|
---|
59 | logger.info("Unexpected end of input when running target: " + targetCmd);
|
---|
60 | }
|
---|
61 | } catch (Exception e) {
|
---|
62 | e.printStackTrace();
|
---|
63 | logger.error(e);
|
---|
64 | state = 1;
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 | public int getTargetState()
|
---|
69 | {
|
---|
70 | return state;
|
---|
71 | }
|
---|
72 |
|
---|
73 | public abstract void setTargetCmd(String cmd);
|
---|
74 |
|
---|
75 | public String getTargetCmd()
|
---|
76 | {
|
---|
77 | return this.targetCmd;
|
---|
78 | }
|
---|
79 | }
|
---|