Stuart McCulloch | 42151ee | 2012-07-16 13:43:38 +0000 | [diff] [blame] | 1 | package aQute.bnd.osgi; |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 2 | |
| 3 | import java.io.*; |
| 4 | |
| 5 | import aQute.libg.command.*; |
| 6 | |
| 7 | public class CommandResource extends WriteResource { |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 8 | final long lastModified; |
| 9 | final Builder domain; |
| 10 | final String command; |
Stuart McCulloch | 2a0afd6 | 2012-09-06 18:28:06 +0000 | [diff] [blame^] | 11 | final File wd; |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 12 | |
Stuart McCulloch | 2a0afd6 | 2012-09-06 18:28:06 +0000 | [diff] [blame^] | 13 | public CommandResource(String command, Builder domain, long lastModified, File wd) { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 14 | this.lastModified = lastModified; |
| 15 | this.domain = domain; |
| 16 | this.command = command; |
Stuart McCulloch | 2a0afd6 | 2012-09-06 18:28:06 +0000 | [diff] [blame^] | 17 | this.wd = wd; |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 18 | } |
| 19 | |
| 20 | @Override |
| 21 | public void write(OutputStream out) throws IOException, Exception { |
| 22 | StringBuilder errors = new StringBuilder(); |
| 23 | StringBuilder stdout = new StringBuilder(); |
| 24 | try { |
| 25 | domain.trace("executing command %s", command); |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 26 | Command cmd = new Command("sh"); |
Stuart McCulloch | 2a0afd6 | 2012-09-06 18:28:06 +0000 | [diff] [blame^] | 27 | cmd.setCwd(wd); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 28 | cmd.inherit(); |
| 29 | String oldpath = cmd.var("PATH"); |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 30 | |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 31 | String path = domain.getProperty("-PATH"); |
| 32 | if (path != null) { |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 33 | path = path.replaceAll("\\s*,\\s*", File.pathSeparator); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 34 | path = path.replaceAll("\\$\\{@\\}", oldpath); |
| 35 | cmd.var("PATH", path); |
| 36 | domain.trace("PATH: %s", path); |
| 37 | } |
Stuart McCulloch | 669423b | 2012-06-26 16:34:24 +0000 | [diff] [blame] | 38 | OutputStreamWriter osw = new OutputStreamWriter(out, "UTF-8"); |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 39 | int result = cmd.execute(command, stdout, errors); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 40 | osw.append(stdout); |
| 41 | osw.flush(); |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 42 | if (result != 0) { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 43 | domain.error("executing command failed %s %s", command, stdout + "\n" + errors); |
| 44 | } |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 45 | } |
| 46 | catch (Exception e) { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 47 | domain.error("executing command failed %s %s", command, e.getMessage()); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | @Override |
| 52 | public long lastModified() { |
| 53 | return lastModified; |
| 54 | } |
| 55 | |
| 56 | } |