aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-runner-api/src/main
diff options
context:
space:
mode:
authorSimonBrandhof <simon.brandhof@gmail.com>2013-04-04 16:12:41 +0200
committerSimonBrandhof <simon.brandhof@gmail.com>2013-04-04 16:12:41 +0200
commit0aba058c75832c2db746f09604a4ea467074a088 (patch)
tree6fba138191ce213c30536ee12f0e436250aada27 /sonar-runner-api/src/main
parent0594af385e24d5103324a4bc723ad697e170ef0a (diff)
downloadsonar-scanner-cli-0aba058c75832c2db746f09604a4ea467074a088.tar.gz
sonar-scanner-cli-0aba058c75832c2db746f09604a4ea467074a088.zip
SONARPLUGINS-2574 refactoring of ForkedRunner
Diffstat (limited to 'sonar-runner-api/src/main')
-rw-r--r--sonar-runner-api/src/main/java/org/sonar/runner/api/EmbeddedRunner.java2
-rw-r--r--sonar-runner-api/src/main/java/org/sonar/runner/api/ForkedRunner.java102
2 files changed, 51 insertions, 53 deletions
diff --git a/sonar-runner-api/src/main/java/org/sonar/runner/api/EmbeddedRunner.java b/sonar-runner-api/src/main/java/org/sonar/runner/api/EmbeddedRunner.java
index 3357e9a..0218a32 100644
--- a/sonar-runner-api/src/main/java/org/sonar/runner/api/EmbeddedRunner.java
+++ b/sonar-runner-api/src/main/java/org/sonar/runner/api/EmbeddedRunner.java
@@ -50,7 +50,7 @@ public class EmbeddedRunner extends Runner<EmbeddedRunner> {
}
@Override
- public void doExecute() {
+ protected void doExecute() {
new BatchLauncher().execute(properties(), extensions);
}
}
diff --git a/sonar-runner-api/src/main/java/org/sonar/runner/api/ForkedRunner.java b/sonar-runner-api/src/main/java/org/sonar/runner/api/ForkedRunner.java
index 7a6c3cc..3e77267 100644
--- a/sonar-runner-api/src/main/java/org/sonar/runner/api/ForkedRunner.java
+++ b/sonar-runner-api/src/main/java/org/sonar/runner/api/ForkedRunner.java
@@ -23,54 +23,72 @@ 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.Arrays;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
public class ForkedRunner extends Runner<ForkedRunner> {
private static final int ONE_DAY_IN_MILLISECONDS = 24 * 60 * 60 * 1000;
- private final Command.Builder commandBuilder;
+ private final Map<String, String> jvmEnvVariables = new HashMap<String, String>();
+ private final List<String> jvmArguments = new ArrayList<String>();
+ private String javaCommand;
+
private final JarExtractor jarExtractor;
- ForkedRunner(Command.Builder commandBuilder, JarExtractor jarExtractor) {
- this.commandBuilder = commandBuilder;
+ ForkedRunner(JarExtractor jarExtractor) {
this.jarExtractor = jarExtractor;
}
public static ForkedRunner create() {
- Os os = new Os();
- Command.Builder builder = Command.builder().setExecutable(os.usedJavaExe().getAbsolutePath());
- return new ForkedRunner(builder, new JarExtractor());
+ return new ForkedRunner(new JarExtractor());
}
- @Override
- public void doExecute() {
- File propertiesFile = writeProperties();
- File jarFile = extractJar();
- fork(jarFile, propertiesFile);
+ public ForkedRunner setJavaCommand(@Nullable String s) {
+ this.javaCommand = s;
+ return this;
}
- private File extractJar() {
- return jarExtractor.extract("sonar-runner-impl");
+ public ForkedRunner addJvmArguments(String... s) {
+ return addJvmArguments(Arrays.asList(s));
}
- private void fork(File jarFile, File propertiesFile) {
- // java -jar sonar-runner-impl.jar path/to/propertiesFile
- Command command = commandBuilder
- .addArguments("-cp", jarFile.getAbsolutePath())
- .addArguments(BatchLauncherMain.class.getName())
- .addArguments(propertiesFile.getAbsolutePath())
- .build();
- System.out.println("---------- execute: " + command);
- int status = CommandExecutor.create().execute(command, ONE_DAY_IN_MILLISECONDS);
- if (status != 0) {
- throw new IllegalStateException("TODO");
+ public ForkedRunner addJvmArguments(List<String> args) {
+ jvmArguments.addAll(args);
+ return this;
+ }
+
+ public ForkedRunner setJvmEnvVariable(String key, String value) {
+ jvmEnvVariables.put(key, value);
+ return this;
+ }
+
+ public ForkedRunner addJvmEnvVariables(Map<String, String> map) {
+ jvmEnvVariables.putAll(map);
+ return this;
+ }
+
+ @Override
+ protected void doExecute() {
+ fork(createCommand());
+ }
+
+ Command createCommand() {
+ File propertiesFile = writeProperties();
+ File jarFile = jarExtractor.extract("sonar-runner-impl");
+
+ Os os = new Os();
+ if (javaCommand == null) {
+ javaCommand = os.usedJavaExe().getAbsolutePath();
}
+ return Command.builder()
+ .setExecutable(javaCommand)
+ .addArguments(jvmArguments)
+ .addArguments("-cp", jarFile.getAbsolutePath(), BatchLauncherMain.class.getName(), propertiesFile.getAbsolutePath())
+ .build();
}
private File writeProperties() {
@@ -89,30 +107,10 @@ public class ForkedRunner extends Runner<ForkedRunner> {
}
}
- public ForkedRunner setJavaCommand(String s) {
- commandBuilder.setExecutable(s);
- return this;
- }
-
- public ForkedRunner addJvmArgument(String... s) {
- commandBuilder.addArguments(Arrays.asList(s));
- return this;
- }
-
- public ForkedRunner addJvmArguments(List<String> args) {
- commandBuilder.addArguments(args);
- return this;
- }
-
- public ForkedRunner setJvmEnvVariable(String key, String value) {
- commandBuilder.setEnvVariable(key, value);
- return this;
- }
-
- public ForkedRunner addJvmEnvVariables(Map<String, String> map) {
- commandBuilder.addEnvVariables(map);
- return this;
+ private void fork(Command command) {
+ int status = CommandExecutor.create().execute(command, ONE_DAY_IN_MILLISECONDS);
+ if (status != 0) {
+ throw new IllegalStateException("TODO");
+ }
}
-
-
}