Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 1 | package aQute.libg.command; |
| 2 | |
| 3 | import java.io.*; |
| 4 | import java.util.*; |
| 5 | import java.util.Map.Entry; |
| 6 | import java.util.concurrent.*; |
Stuart McCulloch | 669423b | 2012-06-26 16:34:24 +0000 | [diff] [blame] | 7 | import java.util.concurrent.atomic.*; |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 8 | |
Stuart McCulloch | 669423b | 2012-06-26 16:34:24 +0000 | [diff] [blame] | 9 | import aQute.lib.io.*; |
Stuart McCulloch | 1a89055 | 2012-06-29 19:23:09 +0000 | [diff] [blame] | 10 | import aQute.service.reporter.*; |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 11 | |
| 12 | public class Command { |
| 13 | |
| 14 | boolean trace; |
| 15 | Reporter reporter; |
| 16 | List<String> arguments = new ArrayList<String>(); |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 17 | Map<String,String> variables = new LinkedHashMap<String,String>(); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 18 | long timeout = 0; |
| 19 | File cwd = new File("").getAbsoluteFile(); |
| 20 | static Timer timer = new Timer(Command.class.getName(), true); |
| 21 | Process process; |
| 22 | volatile boolean timedout; |
| 23 | String fullCommand; |
| 24 | |
| 25 | public Command(String fullCommand) { |
| 26 | this.fullCommand = fullCommand; |
| 27 | } |
| 28 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 29 | public Command() {} |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 30 | |
| 31 | public int execute(Appendable stdout, Appendable stderr) throws Exception { |
| 32 | return execute((InputStream) null, stdout, stderr); |
| 33 | } |
| 34 | |
| 35 | public int execute(String input, Appendable stdout, Appendable stderr) throws Exception { |
| 36 | InputStream in = new ByteArrayInputStream(input.getBytes("UTF-8")); |
| 37 | return execute(in, stdout, stderr); |
| 38 | } |
| 39 | |
Stuart McCulloch | 669423b | 2012-06-26 16:34:24 +0000 | [diff] [blame] | 40 | public int execute(final InputStream in, Appendable stdout, Appendable stderr) throws Exception { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 41 | if (reporter != null) { |
| 42 | reporter.trace("executing cmd: %s", arguments); |
| 43 | } |
| 44 | |
| 45 | String args[] = arguments.toArray(new String[arguments.size()]); |
| 46 | String vars[] = new String[variables.size()]; |
| 47 | int i = 0; |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 48 | for (Entry<String,String> s : variables.entrySet()) { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 49 | vars[i++] = s.getKey() + "=" + s.getValue(); |
| 50 | } |
| 51 | |
| 52 | if (fullCommand == null) |
| 53 | process = Runtime.getRuntime().exec(args, vars.length == 0 ? null : vars, cwd); |
| 54 | else |
| 55 | process = Runtime.getRuntime().exec(fullCommand, vars.length == 0 ? null : vars, cwd); |
| 56 | |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 57 | // Make sure the command will not linger when we go |
| 58 | Runnable r = new Runnable() { |
| 59 | public void run() { |
| 60 | process.destroy(); |
| 61 | } |
| 62 | }; |
| 63 | Thread hook = new Thread(r, arguments.toString()); |
| 64 | Runtime.getRuntime().addShutdownHook(hook); |
| 65 | TimerTask timer = null; |
Stuart McCulloch | 669423b | 2012-06-26 16:34:24 +0000 | [diff] [blame] | 66 | final OutputStream stdin = process.getOutputStream(); |
| 67 | Thread rdInThread = null; |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 68 | |
| 69 | if (timeout != 0) { |
| 70 | timer = new TimerTask() { |
Stuart McCulloch | 2929e2d | 2012-08-07 10:57:21 +0000 | [diff] [blame^] | 71 | @Override |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 72 | public void run() { |
| 73 | timedout = true; |
| 74 | process.destroy(); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 75 | } |
| 76 | }; |
| 77 | Command.timer.schedule(timer, timeout); |
| 78 | } |
| 79 | |
Stuart McCulloch | 669423b | 2012-06-26 16:34:24 +0000 | [diff] [blame] | 80 | final AtomicBoolean finished = new AtomicBoolean(false); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 81 | InputStream out = process.getInputStream(); |
| 82 | try { |
| 83 | InputStream err = process.getErrorStream(); |
| 84 | try { |
Stuart McCulloch | 0b639c6 | 2012-06-12 12:41:16 +0000 | [diff] [blame] | 85 | Collector cout = new Collector(out, stdout); |
| 86 | cout.start(); |
| 87 | Collector cerr = new Collector(err, stderr); |
| 88 | cerr.start(); |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 89 | |
Stuart McCulloch | 669423b | 2012-06-26 16:34:24 +0000 | [diff] [blame] | 90 | if (in != null) { |
| 91 | if (in == System.in) { |
| 92 | rdInThread = new Thread("Read Input Thread") { |
Stuart McCulloch | 2929e2d | 2012-08-07 10:57:21 +0000 | [diff] [blame^] | 93 | @Override |
Stuart McCulloch | 669423b | 2012-06-26 16:34:24 +0000 | [diff] [blame] | 94 | public void run() { |
| 95 | try { |
| 96 | while (!finished.get()) { |
| 97 | int n = in.available(); |
| 98 | if (n == 0) { |
| 99 | sleep(100); |
| 100 | } else { |
| 101 | int c = in.read(); |
| 102 | if (c < 0) { |
| 103 | stdin.close(); |
| 104 | return; |
| 105 | } |
| 106 | stdin.write(c); |
| 107 | if (c == '\n') |
| 108 | stdin.flush(); |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | catch (InterruptedIOException e) { |
| 113 | // Ignore here |
| 114 | } |
| 115 | catch (Exception e) { |
| 116 | // Who cares? |
| 117 | } |
| 118 | finally { |
| 119 | IO.close(stdin); |
| 120 | } |
| 121 | } |
| 122 | }; |
| 123 | rdInThread.setDaemon(true); |
| 124 | rdInThread.start(); |
| 125 | } else { |
| 126 | IO.copy(in, stdin); |
| 127 | stdin.close(); |
Stuart McCulloch | 0b639c6 | 2012-06-12 12:41:16 +0000 | [diff] [blame] | 128 | } |
| 129 | } |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 130 | if (reporter != null) |
Stuart McCulloch | 0b639c6 | 2012-06-12 12:41:16 +0000 | [diff] [blame] | 131 | reporter.trace("exited process"); |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 132 | |
Stuart McCulloch | 0b639c6 | 2012-06-12 12:41:16 +0000 | [diff] [blame] | 133 | cerr.join(); |
| 134 | cout.join(); |
| 135 | if (reporter != null) |
| 136 | reporter.trace("stdout/stderr streams have finished"); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 137 | } |
| 138 | finally { |
| 139 | err.close(); |
| 140 | } |
| 141 | } |
| 142 | finally { |
| 143 | out.close(); |
| 144 | if (timer != null) |
| 145 | timer.cancel(); |
| 146 | Runtime.getRuntime().removeShutdownHook(hook); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 147 | } |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 148 | |
Stuart McCulloch | 0b639c6 | 2012-06-12 12:41:16 +0000 | [diff] [blame] | 149 | byte exitValue = (byte) process.waitFor(); |
Stuart McCulloch | 669423b | 2012-06-26 16:34:24 +0000 | [diff] [blame] | 150 | finished.set(true); |
| 151 | if (rdInThread != null) { |
| 152 | if (in != null) |
| 153 | IO.close(in); |
| 154 | rdInThread.interrupt(); |
| 155 | } |
| 156 | |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 157 | if (reporter != null) |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 158 | reporter.trace("cmd %s executed with result=%d, result: %s/%s, timedout=%s", arguments, exitValue, stdout, |
| 159 | stderr, timedout); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 160 | |
| 161 | if (timedout) |
| 162 | return Integer.MIN_VALUE; |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 163 | |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 164 | return exitValue; |
| 165 | } |
| 166 | |
| 167 | public void add(String... args) { |
| 168 | for (String arg : args) |
| 169 | arguments.add(arg); |
| 170 | } |
| 171 | |
| 172 | public void addAll(Collection<String> args) { |
| 173 | arguments.addAll(args); |
| 174 | } |
| 175 | |
| 176 | public void setTimeout(long duration, TimeUnit unit) { |
| 177 | timeout = unit.toMillis(duration); |
| 178 | } |
| 179 | |
| 180 | public void setTrace() { |
| 181 | this.trace = true; |
| 182 | } |
| 183 | |
| 184 | public void setReporter(Reporter reporter) { |
| 185 | this.reporter = reporter; |
| 186 | } |
| 187 | |
| 188 | public void setCwd(File dir) { |
| 189 | if (!dir.isDirectory()) |
| 190 | throw new IllegalArgumentException("Working directory must be a directory: " + dir); |
| 191 | |
| 192 | this.cwd = dir; |
| 193 | } |
| 194 | |
| 195 | public void cancel() { |
| 196 | process.destroy(); |
| 197 | } |
| 198 | |
| 199 | class Collector extends Thread { |
| 200 | final InputStream in; |
| 201 | final Appendable sb; |
| 202 | |
| 203 | Collector(InputStream inputStream, Appendable sb) { |
| 204 | this.in = inputStream; |
| 205 | this.sb = sb; |
| 206 | setDaemon(true); |
| 207 | } |
| 208 | |
Stuart McCulloch | 2929e2d | 2012-08-07 10:57:21 +0000 | [diff] [blame^] | 209 | @Override |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 210 | public void run() { |
| 211 | try { |
| 212 | int c = in.read(); |
| 213 | while (c >= 0) { |
| 214 | sb.append((char) c); |
| 215 | c = in.read(); |
| 216 | } |
| 217 | } |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 218 | catch (IOException e) { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 219 | // We assume the socket is closed |
| 220 | } |
| 221 | catch (Exception e) { |
| 222 | try { |
| 223 | sb.append("\n**************************************\n"); |
| 224 | sb.append(e.toString()); |
| 225 | sb.append("\n**************************************\n"); |
| 226 | } |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 227 | catch (IOException e1) {} |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 228 | if (reporter != null) { |
| 229 | reporter.trace("cmd exec: %s", e); |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 235 | public Command var(String name, String value) { |
| 236 | variables.put(name, value); |
| 237 | return this; |
| 238 | } |
| 239 | |
| 240 | public Command arg(String... args) { |
| 241 | add(args); |
| 242 | return this; |
| 243 | } |
| 244 | |
| 245 | public Command full(String full) { |
| 246 | fullCommand = full; |
| 247 | return this; |
| 248 | } |
| 249 | |
| 250 | public void inherit() { |
| 251 | ProcessBuilder pb = new ProcessBuilder(); |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 252 | for (Entry<String,String> e : pb.environment().entrySet()) { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 253 | var(e.getKey(), e.getValue()); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | public String var(String name) { |
| 258 | return variables.get(name); |
| 259 | } |
| 260 | |
Stuart McCulloch | 2929e2d | 2012-08-07 10:57:21 +0000 | [diff] [blame^] | 261 | @Override |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 262 | public String toString() { |
| 263 | StringBuilder sb = new StringBuilder(); |
| 264 | String del = ""; |
| 265 | |
| 266 | for (String argument : arguments) { |
| 267 | sb.append(del); |
| 268 | sb.append(argument); |
| 269 | del = " "; |
| 270 | } |
| 271 | return sb.toString(); |
| 272 | } |
| 273 | } |