1 | package com.framsticks.running; |
---|
2 | |
---|
3 | import java.io.BufferedReader; |
---|
4 | import java.io.File; |
---|
5 | import java.io.IOException; |
---|
6 | import java.io.InputStreamReader; |
---|
7 | import java.io.OutputStreamWriter; |
---|
8 | import java.io.PrintWriter; |
---|
9 | import java.util.ArrayList; |
---|
10 | import java.util.LinkedList; |
---|
11 | import java.util.List; |
---|
12 | |
---|
13 | |
---|
14 | import org.apache.log4j.Logger; |
---|
15 | |
---|
16 | import com.framsticks.core.Entity; |
---|
17 | import com.framsticks.params.annotations.FramsClassAnnotation; |
---|
18 | import com.framsticks.params.annotations.ParamAnnotation; |
---|
19 | import com.framsticks.util.FramsticksException; |
---|
20 | import com.framsticks.util.Misc; |
---|
21 | import com.framsticks.util.dispatching.Thread; |
---|
22 | import com.framsticks.util.io.Encoding; |
---|
23 | |
---|
24 | @FramsClassAnnotation |
---|
25 | public class ExternalProcess extends Thread<ExternalProcess> implements Entity { |
---|
26 | private static final Logger log = Logger.getLogger(ExternalProcess.class); |
---|
27 | |
---|
28 | protected List<String> arguments = new ArrayList<>(); |
---|
29 | protected Process process; |
---|
30 | protected ProcessBuilder builder = new ProcessBuilder(); |
---|
31 | protected Thread<ExternalProcess> readerThread; |
---|
32 | |
---|
33 | protected PrintWriter input; |
---|
34 | protected BufferedReader output; |
---|
35 | protected Integer exitCode; |
---|
36 | |
---|
37 | protected final List<OutputListener> listeners = new LinkedList<>(); |
---|
38 | |
---|
39 | public void addListener(OutputListener listener) { |
---|
40 | synchronized (listeners) { |
---|
41 | listeners.add(listener); |
---|
42 | } |
---|
43 | } |
---|
44 | |
---|
45 | /** |
---|
46 | * |
---|
47 | */ |
---|
48 | public ExternalProcess() { |
---|
49 | super(); |
---|
50 | setName("process"); |
---|
51 | arguments.add(null); |
---|
52 | } |
---|
53 | |
---|
54 | /** |
---|
55 | * @return the command |
---|
56 | */ |
---|
57 | @ParamAnnotation |
---|
58 | public String getCommand() { |
---|
59 | return arguments.get(0); |
---|
60 | } |
---|
61 | |
---|
62 | /** |
---|
63 | * @param command the command to set |
---|
64 | */ |
---|
65 | @ParamAnnotation |
---|
66 | public void setCommand(String command) { |
---|
67 | arguments.set(0, command); |
---|
68 | } |
---|
69 | |
---|
70 | @Override |
---|
71 | protected void routine() { |
---|
72 | |
---|
73 | String line; |
---|
74 | try { |
---|
75 | try { |
---|
76 | while ((line = output.readLine()) != null) { |
---|
77 | log.trace("read line: " + line); |
---|
78 | synchronized (listeners) { |
---|
79 | for (OutputListener l : listeners) { |
---|
80 | l.onLineRead(line); |
---|
81 | } |
---|
82 | } |
---|
83 | } |
---|
84 | } catch (IOException e) { |
---|
85 | throw new FramsticksException().msg("failed to read line from output of process").cause(e); |
---|
86 | } |
---|
87 | try { |
---|
88 | exitCode = process.waitFor(); |
---|
89 | } catch (InterruptedException e) { |
---|
90 | throw new FramsticksException().msg("failed to wait for process").cause(e); |
---|
91 | } |
---|
92 | log.debug("process ended " + this); |
---|
93 | process = null; |
---|
94 | } catch (FramsticksException e) { |
---|
95 | log.error("exception caught in process " + this, e); |
---|
96 | } |
---|
97 | interrupt(); |
---|
98 | } |
---|
99 | |
---|
100 | |
---|
101 | |
---|
102 | @ParamAnnotation |
---|
103 | public void setDirectory(String directory) { |
---|
104 | builder.directory(new File(directory)); |
---|
105 | } |
---|
106 | |
---|
107 | @ParamAnnotation |
---|
108 | public String getDirectory() { |
---|
109 | return builder.directory().getName(); |
---|
110 | } |
---|
111 | |
---|
112 | @Override |
---|
113 | public void joinableStart() { |
---|
114 | builder = new ProcessBuilder(); |
---|
115 | log.debug("running process with arguments: " + arguments); |
---|
116 | builder.command(arguments); |
---|
117 | try { |
---|
118 | process = builder.start(); |
---|
119 | input = new PrintWriter(new OutputStreamWriter(process.getOutputStream(), Encoding.getDefaultCharset())); |
---|
120 | output = new BufferedReader(new InputStreamReader(process.getInputStream(), Encoding.getDefaultCharset())); |
---|
121 | } catch (IOException e) { |
---|
122 | throw new FramsticksException().msg("failed to start process").cause(e); |
---|
123 | } |
---|
124 | super.joinableStart(); |
---|
125 | |
---|
126 | } |
---|
127 | |
---|
128 | @Override |
---|
129 | public String toString() { |
---|
130 | return super.toString() + "[" + Misc.returnNotNull(getCommand(), "?") + "]"; |
---|
131 | } |
---|
132 | |
---|
133 | /** |
---|
134 | * @return the input |
---|
135 | */ |
---|
136 | public PrintWriter getInput() { |
---|
137 | return input; |
---|
138 | } |
---|
139 | |
---|
140 | @Override |
---|
141 | protected void joinableInterrupt() { |
---|
142 | super.joinableInterrupt(); |
---|
143 | finish(); |
---|
144 | } |
---|
145 | |
---|
146 | } |
---|