|
| 1 | +import java.util.* ; |
| 2 | +import java.io.*; |
| 3 | +class Command implements Runnable { |
| 4 | + protected Service service; |
| 5 | + private String cmd; |
| 6 | + protected String name; |
| 7 | + public Command(Service service, String cmd, String name) { |
| 8 | + this.service = service; |
| 9 | + this.cmd = cmd; |
| 10 | + this.name = name; |
| 11 | + } |
| 12 | + public void run() { |
| 13 | + try { |
| 14 | + final Process process = Runtime.getRuntime().exec(cmd, null, service.getWorkingDir()); |
| 15 | + |
| 16 | + |
| 17 | + final BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream())); |
| 18 | + final BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream())); |
| 19 | + |
| 20 | + new Thread(new Runnable() { |
| 21 | + public void run() { |
| 22 | + try { |
| 23 | + String s = null; |
| 24 | + while ((s = stdInput.readLine()) != null) { |
| 25 | + service.log(s); |
| 26 | + } |
| 27 | + } catch (IOException e) { |
| 28 | + service.logErr("Can't read from "+name+", IOException."); |
| 29 | + service.logErr(e); |
| 30 | + } |
| 31 | + } |
| 32 | + }).start(); |
| 33 | + |
| 34 | + new Thread(new Runnable() { |
| 35 | + public void run() { |
| 36 | + try { |
| 37 | + String s = null; |
| 38 | + while ((s = stdError.readLine()) != null) { |
| 39 | + service.logErr(s); |
| 40 | + } |
| 41 | + } catch (IOException e) { |
| 42 | + service.logErr("Can't read err from "+name+", IOException."); |
| 43 | + service.logErr(e); |
| 44 | + } |
| 45 | + } |
| 46 | + }).start(); |
| 47 | + |
| 48 | + |
| 49 | + process.waitFor(); |
| 50 | + service.log("Command "+name+" completed"); |
| 51 | + } catch (InterruptedException e) { |
| 52 | + service.logErr("Command "+name+" interrupted"); |
| 53 | + service.logErr(e); |
| 54 | + } catch (IOException e) { |
| 55 | + service.logErr("Command "+name+" died of IOException"); |
| 56 | + service.logErr(e); |
| 57 | + } |
| 58 | + } |
| 59 | + public String getName() { |
| 60 | + return name; |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +class StartCommand extends Command { |
| 65 | + public StartCommand(Service service) { |
| 66 | + super(service, null, "Start"); |
| 67 | + } |
| 68 | + public void run() { |
| 69 | + service.start(); |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +class StopCommand extends Command { |
| 74 | + public StopCommand(Service service) { |
| 75 | + super(service, null, "Stop"); |
| 76 | + } |
| 77 | + public void run() { |
| 78 | + service.stop(); |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +class RestartCommand extends Command { |
| 83 | + public RestartCommand(Service service) { |
| 84 | + super(service, null, "Restart"); |
| 85 | + } |
| 86 | + public void run() { |
| 87 | + service.stop(); |
| 88 | + service.start(); |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +class ClearLogCommand extends Command { |
| 93 | + public ClearLogCommand(Service service) { |
| 94 | + super(service, null, "Clear Log"); |
| 95 | + } |
| 96 | + public void run() { |
| 97 | + service.clearLog(); |
| 98 | + } |
| 99 | +} |
0 commit comments