Stuart McCulloch | 39cc9ac | 2012-07-16 13:43:38 +0000 | [diff] [blame] | 1 | package aQute.bnd.osgi; |
Stuart McCulloch | bb01437 | 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 | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 8 | final long lastModified; |
| 9 | final Builder domain; |
| 10 | final String command; |
Stuart McCulloch | b215bfd | 2012-09-06 18:28:06 +0000 | [diff] [blame] | 11 | final File wd; |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 12 | |
Stuart McCulloch | b215bfd | 2012-09-06 18:28:06 +0000 | [diff] [blame] | 13 | public CommandResource(String command, Builder domain, long lastModified, File wd) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 14 | this.lastModified = lastModified; |
| 15 | this.domain = domain; |
| 16 | this.command = command; |
Stuart McCulloch | b215bfd | 2012-09-06 18:28:06 +0000 | [diff] [blame] | 17 | this.wd = wd; |
Stuart McCulloch | bb01437 | 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(); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 24 | domain.trace("executing command %s", command); |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 25 | Command cmd = new Command("sh"); |
Stuart McCulloch | b215bfd | 2012-09-06 18:28:06 +0000 | [diff] [blame] | 26 | cmd.setCwd(wd); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 27 | cmd.inherit(); |
| 28 | String oldpath = cmd.var("PATH"); |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 29 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 30 | String path = domain.getProperty("-PATH"); |
| 31 | if (path != null) { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 32 | path = path.replaceAll("\\s*,\\s*", File.pathSeparator); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 33 | path = path.replaceAll("\\$\\{@\\}", oldpath); |
| 34 | cmd.var("PATH", path); |
| 35 | domain.trace("PATH: %s", path); |
| 36 | } |
Stuart McCulloch | d482610 | 2012-06-26 16:34:24 +0000 | [diff] [blame] | 37 | OutputStreamWriter osw = new OutputStreamWriter(out, "UTF-8"); |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 38 | int result = cmd.execute(command, stdout, errors); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 39 | osw.append(stdout); |
| 40 | osw.flush(); |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 41 | if (result != 0) { |
Stuart McCulloch | ec47fe7 | 2012-09-19 12:56:05 +0000 | [diff] [blame] | 42 | throw new Exception("executing command failed" + command + "\n"+ stdout + "\n" + errors); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 43 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | @Override |
| 47 | public long lastModified() { |
| 48 | return lastModified; |
| 49 | } |
| 50 | |
| 51 | } |