001 /* 002 * Sonar, open source software quality management tool. 003 * Copyright (C) 2008-2011 SonarSource 004 * mailto:contact AT sonarsource DOT com 005 * 006 * Sonar is free software; you can redistribute it and/or 007 * modify it under the terms of the GNU Lesser General Public 008 * License as published by the Free Software Foundation; either 009 * version 3 of the License, or (at your option) any later version. 010 * 011 * Sonar is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 * Lesser General Public License for more details. 015 * 016 * You should have received a copy of the GNU Lesser General Public 017 * License along with Sonar; if not, write to the Free Software 018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 019 */ 020 package org.sonar.api.utils.command; 021 022 import org.apache.commons.io.IOUtils; 023 import org.slf4j.Logger; 024 import org.slf4j.LoggerFactory; 025 026 import java.io.BufferedReader; 027 import java.io.IOException; 028 import java.io.InputStream; 029 import java.io.InputStreamReader; 030 import java.util.concurrent.*; 031 032 /** 033 * Synchronously execute a native command line. It's much more limited than the Apache Commons Exec library. 034 * For example it does not allow to get process output, to run asynchronously or to automatically quote 035 * command-line arguments. 036 * 037 * @since 2.7 038 */ 039 public final class CommandExecutor { 040 041 private static final CommandExecutor INSTANCE = new CommandExecutor(); 042 043 private CommandExecutor() { 044 } 045 046 public static CommandExecutor create() { 047 // stateless object, so a single singleton can be shared 048 return INSTANCE; 049 } 050 051 public int execute(Command command, long timeoutMilliseconds) { 052 ExecutorService executorService = null; 053 Process process = null; 054 try { 055 LoggerFactory.getLogger(getClass()).debug("Executing command: " + command); 056 ProcessBuilder builder = new ProcessBuilder(command.toStrings()); 057 if (command.getDirectory() != null) { 058 builder.directory(command.getDirectory()); 059 } 060 process = builder.start(); 061 062 // consume and display the error and output streams 063 StreamGobbler outputGobbler = new StreamGobbler(process.getInputStream()); 064 StreamGobbler errorGobbler = new StreamGobbler(process.getErrorStream()); 065 outputGobbler.start(); 066 errorGobbler.start(); 067 068 final Process finalProcess = process; 069 Callable<Integer> call = new Callable<Integer>() { 070 public Integer call() throws Exception { 071 finalProcess.waitFor(); 072 return finalProcess.exitValue(); 073 } 074 }; 075 076 executorService = Executors.newSingleThreadExecutor(); 077 Future<Integer> ft = executorService.submit(call); 078 return ft.get(timeoutMilliseconds, TimeUnit.MILLISECONDS); 079 080 } catch (TimeoutException te) { 081 process.destroy(); 082 throw new CommandException(command, "Timeout exceeded: " + timeoutMilliseconds + " ms", te); 083 084 } catch (Exception e) { 085 throw new CommandException(command, e); 086 087 } finally { 088 if (executorService != null) { 089 executorService.shutdown(); 090 } 091 } 092 } 093 094 private static class StreamGobbler extends Thread { 095 InputStream is; 096 097 StreamGobbler(InputStream is) { 098 this.is = is; 099 } 100 101 public void run() { 102 Logger logger = LoggerFactory.getLogger(CommandExecutor.class); 103 InputStreamReader isr = new InputStreamReader(is); 104 BufferedReader br = new BufferedReader(isr); 105 try { 106 String line; 107 while ((line = br.readLine()) != null) { 108 logger.info(line); 109 } 110 } catch (IOException ioe) { 111 logger.error("Error while reading stream", ioe); 112 113 } finally { 114 IOUtils.closeQuietly(br); 115 IOUtils.closeQuietly(isr); 116 } 117 } 118 } 119 }