1 /* 2 * Copyright (C) 2009 The Android Open Source Project 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 17 import java.io.BufferedReader; 18 import java.io.IOException; 19 import java.io.InputStreamReader; 20 import java.util.ArrayList; 21 import java.util.Arrays; 22 import java.util.Collection; 23 import java.util.List; 24 25 /** 26 * An out of process executable. 27 */ 28 class Command { 29 30 private final List<String> args; 31 private final boolean permitNonZeroExitStatus; 32 Command(String... args)33 Command(String... args) { 34 this(Arrays.asList(args)); 35 } 36 Command(List<String> args)37 Command(List<String> args) { 38 this.args = new ArrayList<String>(args); 39 this.permitNonZeroExitStatus = false; 40 } 41 Command(Builder builder)42 private Command(Builder builder) { 43 this.args = new ArrayList<String>(builder.args); 44 this.permitNonZeroExitStatus = builder.permitNonZeroExitStatus; 45 } 46 47 static class Builder { 48 private final List<String> args = new ArrayList<String>(); 49 private boolean permitNonZeroExitStatus = false; 50 args(String... args)51 public Builder args(String... args) { 52 return args(Arrays.asList(args)); 53 } 54 args(Collection<String> args)55 public Builder args(Collection<String> args) { 56 this.args.addAll(args); 57 return this; 58 } 59 permitNonZeroExitStatus()60 public Builder permitNonZeroExitStatus() { 61 permitNonZeroExitStatus = true; 62 return this; 63 } 64 build()65 public Command build() { 66 return new Command(this); 67 } 68 execute()69 public List<String> execute() { 70 return build().execute(); 71 } 72 } 73 execute()74 public List<String> execute() { 75 try { 76 Process process = new ProcessBuilder() 77 .command(args) 78 .redirectErrorStream(true) 79 .start(); 80 81 BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream())); 82 List<String> outputLines = new ArrayList<String>(); 83 String outputLine; 84 while ((outputLine = in.readLine()) != null) { 85 outputLines.add(outputLine); 86 } 87 88 if (process.waitFor() != 0 && !permitNonZeroExitStatus) { 89 StringBuilder message = new StringBuilder(); 90 for (String line : outputLines) { 91 message.append("\n").append(line); 92 } 93 throw new RuntimeException("Process failed: " + args + message); 94 } 95 96 return outputLines; 97 } catch (IOException e) { 98 throw new RuntimeException("Process failed: " + args, e); 99 } catch (InterruptedException e) { 100 throw new RuntimeException("Process failed: " + args, e); 101 } 102 } 103 104 } 105