1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 package org.opensolaris.os.vp.util.misc; 28 29 import java.io.BufferedReader; 30 import java.io.InputStreamReader; 31 import java.io.IOException; 32 import java.util.List; 33 import java.util.LinkedList; 34 import org.opensolaris.os.vp.util.misc.exception.CommandFailedException; 35 36 public class ProcessUtil { 37 // 38 // Static methods 39 // 40 41 /** 42 * Runs the given command and waits for it to complete. 43 * 44 * @param command 45 * the command array 46 * 47 * @return the exit code of the command 48 * 49 * @exception IOException 50 * if an I/O error occurs 51 * 52 * @exception InterruptedException 53 * if the current thread is interrupted while 54 * waiting for the process to finish 55 */ 56 public static int exec(String... command) throws IOException, 57 InterruptedException { 58 59 Process p = Runtime.getRuntime().exec(command); 60 p.waitFor(); 61 return p.exitValue(); 62 } 63 64 /** 65 * Runs the given command and waits for it to complete. 66 * 67 * @param expResult 68 * the expected exit code of the command 69 * 70 * @param command 71 * the command array 72 * 73 * @exception IOException 74 * if an I/O error occurs 75 * 76 * @exception InterruptedException 77 * if the current thread is interrupted while 78 * waiting for the process to finish 79 * 80 * @exception CommandFailedException 81 * if the command exits with a result other than {@code 82 * expResult} 83 */ 84 public static void exec(int expResult, String... command) 85 throws IOException, InterruptedException, CommandFailedException { 86 87 int result = exec(command); 88 if (result != expResult) { 89 throw new CommandFailedException(command, result, expResult); 90 } 91 } 92 93 public static List<String> execOutput(int expResult, String... command) 94 throws IOException, InterruptedException, CommandFailedException { 95 96 List<String> ret = new LinkedList<String>(); 97 Process p = Runtime.getRuntime().exec(command); 98 p.waitFor(); 99 if (p.exitValue() != expResult) 100 throw new CommandFailedException(command, p.exitValue(), expResult); 101 102 BufferedReader in = 103 new BufferedReader(new InputStreamReader(p.getInputStream())); 104 String line = in.readLine(); 105 while (line != null) { 106 ret.add(line); 107 line = in.readLine(); 108 } 109 return (ret); 110 } 111 } 112