aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-runner-api/src/main/java/org
diff options
context:
space:
mode:
authorDuarte Meneses <duarte.meneses@sonarsource.com>2015-06-16 17:38:52 +0200
committerDuarte Meneses <duarte.meneses@sonarsource.com>2015-06-19 17:08:54 +0200
commitf2e74b6d6935082e4d75a5ddfaf66e8ba1079f1f (patch)
treeddecebafb089a6a2518aa96a3fcbd03b852ec13c /sonar-runner-api/src/main/java/org
parente4ba9869d57dc4dd92648b5c8406d26fcd550d46 (diff)
downloadsonar-scanner-cli-f2e74b6d6935082e4d75a5ddfaf66e8ba1079f1f.tar.gz
sonar-scanner-cli-f2e74b6d6935082e4d75a5ddfaf66e8ba1079f1f.zip
SONARUNNER-138 Allow to redirect logs to a custom stream
Diffstat (limited to 'sonar-runner-api/src/main/java/org')
-rw-r--r--sonar-runner-api/src/main/java/org/sonar/runner/api/CommandExecutor.java7
-rw-r--r--sonar-runner-api/src/main/java/org/sonar/runner/api/EmbeddedRunner.java28
-rw-r--r--sonar-runner-api/src/main/java/org/sonar/runner/api/ForkedRunner.java39
-rw-r--r--sonar-runner-api/src/main/java/org/sonar/runner/api/Runner.java16
-rw-r--r--sonar-runner-api/src/main/java/org/sonar/runner/api/Utils.java12
5 files changed, 79 insertions, 23 deletions
diff --git a/sonar-runner-api/src/main/java/org/sonar/runner/api/CommandExecutor.java b/sonar-runner-api/src/main/java/org/sonar/runner/api/CommandExecutor.java
index d9903bf..fd5b6d8 100644
--- a/sonar-runner-api/src/main/java/org/sonar/runner/api/CommandExecutor.java
+++ b/sonar-runner-api/src/main/java/org/sonar/runner/api/CommandExecutor.java
@@ -19,6 +19,8 @@
*/
package org.sonar.runner.api;
+import org.sonar.runner.impl.Logs;
+
import javax.annotation.Nullable;
import java.io.BufferedReader;
@@ -101,7 +103,7 @@ class CommandExecutor {
}
}
- private void monitorProcess(final ProcessMonitor processMonitor, final ExecutorService executor, final Process process) {
+ private static void monitorProcess(final ProcessMonitor processMonitor, final ExecutorService executor, final Process process) {
new Thread() {
@Override
public void run() {
@@ -149,8 +151,7 @@ class CommandExecutor {
try {
thread.join();
} catch (InterruptedException e) {
- System.err.println("InterruptedException while waiting finish of " + thread.toString());
- e.printStackTrace();
+ Logs.error("InterruptedException while waiting finish of " + thread.toString(), e);
}
}
}
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 9dfad19..e1c04b7 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
@@ -19,13 +19,15 @@
*/
package org.sonar.runner.api;
+import org.sonar.home.log.LogListener;
+
+import org.sonar.runner.impl.Logs;
import org.sonar.runner.batch.IsolatedLauncher;
import org.sonar.runner.impl.IsolatedLauncherFactory;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
-import java.util.Map;
import java.util.Properties;
/**
@@ -36,9 +38,9 @@ import java.util.Properties;
* @since 2.2
*/
public class EmbeddedRunner extends Runner<EmbeddedRunner> {
-
private final IsolatedLauncherFactory launcherFactory;
private IsolatedLauncher launcher;
+ private String sqVersion;
private final List<Object> extensions = new ArrayList<Object>();
private static final String MASK_RULES_PROP = "sonarRunner.maskRules";
@@ -53,6 +55,11 @@ public class EmbeddedRunner extends Runner<EmbeddedRunner> {
return new EmbeddedRunner(new IsolatedLauncherFactory());
}
+ public static EmbeddedRunner create(LogListener logListener) {
+ Logs.setListener(logListener);
+ return new EmbeddedRunner(new IsolatedLauncherFactory());
+ }
+
/**
* Sonar is executed in an almost fully isolated classloader (mask everything by default). This method allows to unmask some classes based on
* a prefix of their fully qualified name. It is related to the extensions provided by {@link #addExtensions(Object...)}.
@@ -104,16 +111,27 @@ public class EmbeddedRunner extends Runner<EmbeddedRunner> {
@Override
protected void doStart() {
launcher = launcherFactory.createLauncher(globalProperties());
- launcher.start(globalProperties(), extensions);
+ if (Utils.isAtLeast52(launcher.getVersion())) {
+ launcher.start(globalProperties(), extensions, Logs.getListener());
+ }
}
@Override
protected void doStop() {
- launcher.stop();
+ if (Utils.isAtLeast52(launcher.getVersion())) {
+ launcher.stop();
+ }
}
@Override
protected void doExecute(Properties analysisProperties) {
- launcher.execute(analysisProperties);
+ if (Utils.isAtLeast52(launcher.getVersion())) {
+ launcher.execute(analysisProperties);
+ } else {
+ Properties prop = new Properties();
+ prop.putAll(globalProperties());
+ prop.putAll(analysisProperties);
+ launcher.executeOldVersion(prop, 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 02fdae7..31a932f 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
@@ -19,6 +19,7 @@
*/
package org.sonar.runner.api;
+import org.sonar.runner.impl.Logs;
import org.sonar.runner.impl.BatchLauncherMain;
import org.sonar.runner.impl.JarExtractor;
@@ -46,7 +47,8 @@ public class ForkedRunner extends Runner<ForkedRunner> {
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 StreamConsumer stdOut = null;
+ private StreamConsumer stdErr = null;
private final JarExtractor jarExtractor;
private final CommandExecutor commandExecutor;
@@ -121,7 +123,8 @@ public class ForkedRunner extends Runner<ForkedRunner> {
}
/**
- * Subscribe to the standard output. By default output is {@link System.out}
+ * @deprecated Since 2.5. Use {@link ForkedRunner#setLogListener} instead.
+ * Subscribe to the standard output from the forked process. By default, logs messages are sent with INFO level to the log listener set.
*/
public ForkedRunner setStdOut(@Nullable StreamConsumer stream) {
this.stdOut = stream;
@@ -129,7 +132,8 @@ public class ForkedRunner extends Runner<ForkedRunner> {
}
/**
- * Subscribe to the error output. By default output is {@link System.err}
+ * @deprecated Since 2.5. Use {@link ForkedRunner#setLogListener}instead.
+ * Subscribe to the error output from the forked process. By default, logs messages are sent with ERROR level to the log listener set.
*/
public ForkedRunner setStdErr(@Nullable StreamConsumer stream) {
this.stdErr = stream;
@@ -138,12 +142,12 @@ public class ForkedRunner extends Runner<ForkedRunner> {
@Override
protected void doExecute(Properties props) {
- //merge both global and analysis-specific properties because it will be used both to start and to execute.
+ // merge both global and analysis-specific properties because it will be used both to start and to execute.
Properties p = new Properties();
-
+
p.putAll(globalProperties());
p.putAll(props);
-
+
ForkCommand forkCommand = createCommand(p);
try {
fork(forkCommand);
@@ -151,15 +155,15 @@ public class ForkedRunner extends Runner<ForkedRunner> {
deleteTempFiles(forkCommand);
}
}
-
+
@Override
protected void doStop() {
- //nothing to do
+ // nothing to do
}
-
+
@Override
protected void doStart() {
- //nothing to do
+ // nothing to do
}
ForkCommand createCommand(Properties p) {
@@ -196,11 +200,22 @@ public class ForkedRunner extends Runner<ForkedRunner> {
private void fork(ForkCommand forkCommand) {
if (stdOut == null) {
- stdOut = new PrintStreamConsumer(System.out);
+ stdOut = new StreamConsumer() {
+ @Override
+ public void consumeLine(String line) {
+ Logs.info(line);
+ }
+ };
}
if (stdErr == null) {
- stdErr = new PrintStreamConsumer(System.err);
+ stdErr = new StreamConsumer() {
+ @Override
+ public void consumeLine(String line) {
+ Logs.error(line);
+ }
+ };
}
+
int status = commandExecutor.execute(forkCommand.command, stdOut, stdErr, ONE_DAY_IN_MILLISECONDS, processMonitor);
if (status != 0) {
if (processMonitor != null && processMonitor.stop()) {
diff --git a/sonar-runner-api/src/main/java/org/sonar/runner/api/Runner.java b/sonar-runner-api/src/main/java/org/sonar/runner/api/Runner.java
index 081b7f7..890ce46 100644
--- a/sonar-runner-api/src/main/java/org/sonar/runner/api/Runner.java
+++ b/sonar-runner-api/src/main/java/org/sonar/runner/api/Runner.java
@@ -19,6 +19,9 @@
*/
package org.sonar.runner.api;
+import org.sonar.home.log.LogListener;
+
+import org.sonar.runner.impl.Logs;
import org.sonar.runner.impl.InternalProperties;
import javax.annotation.Nullable;
@@ -30,7 +33,6 @@ import java.util.Properties;
* @since 2.2
*/
public abstract class Runner<T extends Runner> {
-
private final Properties globalProperties = new Properties();
protected Runner() {
@@ -41,6 +43,16 @@ public abstract class Runner<T extends Runner> {
clone.putAll(globalProperties);
return clone;
}
+
+ /**
+ * Set a log stream. All log events will be redirected to the listener.
+ * By default, all logs are sent to stdout, except for logs of ERROR level, which are sent to stderr.
+ * If null is given, the default is behavior is set.
+ */
+ public T setLogListener(LogListener stream) {
+ Logs.setListener(stream);
+ return (T) this;
+ }
/**
* Declare Sonar properties, for example sonar.projectKey=>foo.
@@ -93,7 +105,7 @@ public abstract class Runner<T extends Runner> {
if (dumpToFile != null) {
File dumpFile = new File(dumpToFile);
Utils.writeProperties(dumpFile, copy);
- System.out.println("Simulation mode. Configuration written to " + dumpFile.getAbsolutePath());
+ Logs.info("Simulation mode. Configuration written to " + dumpFile.getAbsolutePath());
} else {
doExecute(copy);
}
diff --git a/sonar-runner-api/src/main/java/org/sonar/runner/api/Utils.java b/sonar-runner-api/src/main/java/org/sonar/runner/api/Utils.java
index 4d0524a..4d4907a 100644
--- a/sonar-runner-api/src/main/java/org/sonar/runner/api/Utils.java
+++ b/sonar-runner-api/src/main/java/org/sonar/runner/api/Utils.java
@@ -33,12 +33,22 @@ import java.nio.file.SimpleFileVisitor;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Properties;
-import java.nio.file.attribute.*;
+import java.nio.file.attribute.BasicFileAttributes;
class Utils {
private Utils() {
// only util static methods
}
+
+ static boolean isAtLeast52(String version) {
+ //it can be snapshot (5.2-SNAPSHOT)
+ if(version == null) {
+ return false;
+ }
+
+ int endIndex = Math.min(3, version.length());
+ return Double.parseDouble(version.substring(0, endIndex)) >= 5.2;
+ }
/**
* Similar to org.apache.commons.lang.StringUtils#join()