source: greenstone3/trunk/src/java/org/greenstone/server/RunTarget.java@ 18729

Last change on this file since 18729 was 18729, checked in by ak19, 15 years ago

Dr Bainbridge made some corrections in here in the run() method, such as closing the stream reader, and reorganising the code.

File size: 1.7 KB
Line 
1package org.greenstone.server;
2
3import java.io.IOException;
4import java.io.BufferedReader;
5import java.io.InputStream;
6import java.io.InputStreamReader;
7
8import org.greenstone.server.StreamGobbler;
9
10import org.apache.log4j.*;
11
12public 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}
Note: See TracBrowser for help on using the repository browser.