From 0d9baa7b02f94e1706c6922cc56b1fae2a2bf0f5 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Sat, 6 Apr 2013 00:07:52 +0200 Subject: [PATCH] Fix build on Java 6 --- .../org/sonar/runner/api/RunnerVersion.java | 5 +- .../src/main/java/org/sonar/runner/Main.java | 18 +----- .../java/org/sonar/runner/SystemInfo.java | 56 +++++++++++++++++++ .../java/org/sonar/runner/SystemInfoTest.java | 36 ++++++++++++ .../org/sonar/runner/impl/ServerVersion.java | 1 + 5 files changed, 97 insertions(+), 19 deletions(-) create mode 100644 sonar-runner-dist/src/main/java/org/sonar/runner/SystemInfo.java create mode 100644 sonar-runner-dist/src/test/java/org/sonar/runner/SystemInfoTest.java diff --git a/sonar-runner-api/src/main/java/org/sonar/runner/api/RunnerVersion.java b/sonar-runner-api/src/main/java/org/sonar/runner/api/RunnerVersion.java index b93a39f..61b356f 100644 --- a/sonar-runner-api/src/main/java/org/sonar/runner/api/RunnerVersion.java +++ b/sonar-runner-api/src/main/java/org/sonar/runner/api/RunnerVersion.java @@ -19,12 +19,11 @@ */ package org.sonar.runner.api; -import org.apache.commons.io.IOUtils; - import java.util.Scanner; /** * Version of this sonar-runner API. + * * @since 2.2 */ public enum RunnerVersion { @@ -42,7 +41,7 @@ public enum RunnerVersion { try { this.version = scanner.next(); } finally { - IOUtils.closeQuietly(scanner); + scanner.close(); } } } diff --git a/sonar-runner-dist/src/main/java/org/sonar/runner/Main.java b/sonar-runner-dist/src/main/java/org/sonar/runner/Main.java index 69022f3..8e3a833 100644 --- a/sonar-runner-dist/src/main/java/org/sonar/runner/Main.java +++ b/sonar-runner-dist/src/main/java/org/sonar/runner/Main.java @@ -20,7 +20,6 @@ package org.sonar.runner; import org.sonar.runner.api.EmbeddedRunner; -import org.sonar.runner.api.RunnerVersion; import org.sonar.runner.impl.Logs; import java.util.Properties; @@ -51,19 +50,13 @@ public class Main { } void execute() { - printSystem(); + SystemInfo.print(); if (!cli.isDisplayVersionOnly()) { int status = doExecute(new Conf(cli)); System.exit(status); } } - private void printSystem() { - System.out.println("Sonar Runner " + RunnerVersion.version()); - System.out.println("Java " + System.getProperty("java.version") + " (" + System.getProperty("java.vendor") + ")"); - System.out.println(System.getProperty("os.name") + " " + System.getProperty("os.version") + " " + System.getProperty("os.arch")); - } - private int doExecute(Conf conf) { if (cli.isDisplayStackTrace()) { Logs.info("Error stacktraces are turned on."); @@ -72,15 +65,8 @@ public class Main { try { Properties properties = conf.load(); EmbeddedRunner.create().addProperties(properties).execute(); + // Logs.info("Work directory: " + runner.getWorkDir().getCanonicalPath()); -// Logs.debug("Other system properties:"); -// Logs.debug(" - sun.arch.data.model: \"" + System.getProperty("sun.arch.data.model") + "\""); -// Logs.info("Server: " + runner.getSonarServerURL()); -// try { -// Logs.info("Work directory: " + runner.getWorkDir().getCanonicalPath()); -// } catch (IOException e) { -// throw new RunnerException("Unable to resolve directory", e); -// } } catch (Exception e) { displayExecutionResult(stats, "FAILURE"); showError("Error during Sonar runner execution", e, cli.isDisplayStackTrace()); diff --git a/sonar-runner-dist/src/main/java/org/sonar/runner/SystemInfo.java b/sonar-runner-dist/src/main/java/org/sonar/runner/SystemInfo.java new file mode 100644 index 0000000..c199d5b --- /dev/null +++ b/sonar-runner-dist/src/main/java/org/sonar/runner/SystemInfo.java @@ -0,0 +1,56 @@ +/* + * Sonar Runner - Distribution + * 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; + +import org.sonar.runner.api.RunnerVersion; + +class SystemInfo { + + static void print() { + System.out.println("Sonar Runner " + RunnerVersion.version()); + System.out.println(java()); + System.out.println(os()); + } + + static String java() { + StringBuilder sb = new StringBuilder(); + sb + .append("Java ") + .append(System.getProperty("java.version")) + .append(" ") + .append(System.getProperty("java.vendor")); + String bits = System.getProperty("sun.arch.data.model"); + if ("32".equals(bits) || "64".equals(bits)) { + sb.append(" (").append(bits).append("-bit)"); + } + return sb.toString(); + } + + static String os() { + StringBuilder sb = new StringBuilder(); + sb + .append(System.getProperty("os.name")) + .append(" ") + .append(System.getProperty("os.version")) + .append(" ") + .append(System.getProperty("os.arch")); + return sb.toString(); + } +} diff --git a/sonar-runner-dist/src/test/java/org/sonar/runner/SystemInfoTest.java b/sonar-runner-dist/src/test/java/org/sonar/runner/SystemInfoTest.java new file mode 100644 index 0000000..0e57284 --- /dev/null +++ b/sonar-runner-dist/src/test/java/org/sonar/runner/SystemInfoTest.java @@ -0,0 +1,36 @@ +/* + * Sonar Runner - Distribution + * 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; + +import org.junit.Test; + +import static org.fest.assertions.Assertions.assertThat; + +public class SystemInfoTest { + @Test + public void test_java() { + assertThat(SystemInfo.java()).matches("Java .* \\((32|64)-bit\\)"); + } + + @Test + public void test_os() { + assertThat(SystemInfo.os()).isNotEmpty(); + } +} diff --git a/sonar-runner-impl/src/main/java/org/sonar/runner/impl/ServerVersion.java b/sonar-runner-impl/src/main/java/org/sonar/runner/impl/ServerVersion.java index b4db8a5..8f5af45 100644 --- a/sonar-runner-impl/src/main/java/org/sonar/runner/impl/ServerVersion.java +++ b/sonar-runner-impl/src/main/java/org/sonar/runner/impl/ServerVersion.java @@ -38,6 +38,7 @@ class ServerVersion { // Guava Suppliers#memoize() would be great here :D if (version == null) { version = downloadVersion(); + Logs.info("Sonar Server " + version); } return version; } -- 2.39.5