Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) OSGi Alliance (2012). All Rights Reserved. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 17 | package aQute.lib.osgi; |
| 18 | |
| 19 | import java.io.*; |
| 20 | |
| 21 | import aQute.libg.command.*; |
| 22 | |
| 23 | public class CommandResource extends WriteResource { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 24 | final long lastModified; |
| 25 | final Builder domain; |
| 26 | final String command; |
| 27 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 28 | public CommandResource(String command, Builder domain, long lastModified) { |
| 29 | this.lastModified = lastModified; |
| 30 | this.domain = domain; |
| 31 | this.command = command; |
| 32 | } |
| 33 | |
| 34 | @Override |
| 35 | public void write(OutputStream out) throws IOException, Exception { |
| 36 | StringBuilder errors = new StringBuilder(); |
| 37 | StringBuilder stdout = new StringBuilder(); |
| 38 | try { |
| 39 | domain.trace("executing command %s", command); |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 40 | Command cmd = new Command("sh"); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 41 | cmd.inherit(); |
| 42 | String oldpath = cmd.var("PATH"); |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 43 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 44 | String path = domain.getProperty("-PATH"); |
| 45 | if (path != null) { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 46 | path = path.replaceAll("\\s*,\\s*", File.pathSeparator); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 47 | path = path.replaceAll("\\$\\{@\\}", oldpath); |
| 48 | cmd.var("PATH", path); |
| 49 | domain.trace("PATH: %s", path); |
| 50 | } |
| 51 | OutputStreamWriter osw = new OutputStreamWriter(out); |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 52 | int result = cmd.execute(command, stdout, errors); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 53 | osw.append(stdout); |
| 54 | osw.flush(); |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 55 | if (result != 0) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 56 | domain.error("executing command failed %s %s", command, stdout + "\n" + errors); |
| 57 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 58 | } |
| 59 | catch (Exception e) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 60 | domain.error("executing command failed %s %s", command, e.getMessage()); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public long lastModified() { |
| 66 | return lastModified; |
| 67 | } |
| 68 | |
| 69 | } |