123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- /*
- * Sonar Runner - API
- * Copyright (C) 2011 SonarSource
- * dev@sonar.codehaus.org
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
- */
- package org.sonar.runner.api;
-
- import org.apache.commons.io.IOUtils;
- import org.sonar.runner.impl.BatchLauncherMain;
- import org.sonar.runner.impl.JarExtractor;
-
- import javax.annotation.Nullable;
-
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.OutputStream;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
-
- /**
- * Runner executed in a new JVM.
- *
- * @since 2.2
- */
- public class ForkedRunner extends Runner<ForkedRunner> {
-
- private static final int ONE_DAY_IN_MILLISECONDS = 24 * 60 * 60 * 1000;
-
- private final Map<String, String> jvmEnvVariables = new HashMap<String, String>();
- private final List<String> jvmArguments = new ArrayList<String>();
- private String javaExecutable;
- private StreamConsumer stdOut = null, stdErr = null;
- private final JarExtractor jarExtractor;
- private final CommandExecutor commandExecutor;
-
- ForkedRunner(JarExtractor jarExtractor, CommandExecutor commandExecutor) {
- this.jarExtractor = jarExtractor;
- this.commandExecutor = commandExecutor;
- }
-
- /**
- * Create new instance. Never return null.
- */
- public static ForkedRunner create() {
- return new ForkedRunner(new JarExtractor(), CommandExecutor.create());
- }
-
- /**
- * Path to the java executable. The JVM of the client app is used by default
- * (see the system property java.home)
- */
- public ForkedRunner setJavaExecutable(@Nullable String s) {
- this.javaExecutable = s;
- return this;
- }
-
- /**
- * See {@link #addJvmArguments(java.util.List)}
- */
- public ForkedRunner addJvmArguments(String... s) {
- return addJvmArguments(Arrays.asList(s));
- }
-
- /**
- * JVM arguments, for example "-Xmx512m"
- */
- public ForkedRunner addJvmArguments(List<String> args) {
- jvmArguments.addAll(args);
- return this;
- }
-
- /**
- * Set a JVM environment variable. By default no variables are set.
- */
- public ForkedRunner setJvmEnvVariable(String key, String value) {
- jvmEnvVariables.put(key, value);
- return this;
- }
-
- /**
- * Add some JVM environment variables. By default no variables are set.
- */
- public ForkedRunner addJvmEnvVariables(Map<String, String> map) {
- jvmEnvVariables.putAll(map);
- return this;
- }
-
- /**
- * Subscribe to the standard output. By default output is {@link System.out}
- */
- public ForkedRunner setStdOut(@Nullable StreamConsumer stream) {
- this.stdOut = stream;
- return this;
- }
-
- /**
- * Subscribe to the error output. By default output is {@link System.err}
- */
- public ForkedRunner setStdErr(@Nullable StreamConsumer stream) {
- this.stdErr = stream;
- return this;
- }
-
- @Override
- protected void doExecute() {
- fork(createCommand());
- }
-
- private Command createCommand() {
- File propertiesFile = writeProperties();
- File jarFile = jarExtractor.extract("sonar-runner-impl");
- if (javaExecutable == null) {
- javaExecutable = new Os().thisJavaExe().getAbsolutePath();
- }
- return Command.builder()
- .setExecutable(javaExecutable)
- .addEnvVariables(jvmEnvVariables)
- .addArguments(jvmArguments)
- .addArguments("-cp", jarFile.getAbsolutePath(), BatchLauncherMain.class.getName(), propertiesFile.getAbsolutePath())
- .build();
- }
-
- private File writeProperties() {
- OutputStream output = null;
- try {
- File file = File.createTempFile("sonar-project", ".properties");
- output = new FileOutputStream(file);
- properties().store(output, "Generated by sonar-runner");
- return file;
-
- } catch (Exception e) {
- throw new IllegalStateException("Fail to export sonar-runner properties", e);
-
- } finally {
- IOUtils.closeQuietly(output);
- }
- }
-
- private void fork(Command command) {
- if (stdOut == null) {
- stdOut = new PrintStreamConsumer(System.out);
- }
- if (stdErr == null) {
- stdErr = new PrintStreamConsumer(System.err);
- }
- int status = commandExecutor.execute(command, stdOut, stdErr, ONE_DAY_IN_MILLISECONDS);
- if (status != 0) {
- throw new IllegalStateException("Error status: " + status);
- }
- }
-
- }
|