diff options
author | Simon Brandhof <simon.brandhof@gmail.com> | 2012-09-11 22:39:21 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@gmail.com> | 2012-09-11 22:39:21 +0200 |
commit | e7e6e5a30984f3c5b154da7885647a1d0d7229fb (patch) | |
tree | d1b579e78beb6cd8dcaf4055e1ea0b378ebb6b25 /src/main/java | |
parent | ef12da86339cd8225b8b774c9cd8412ddd550421 (diff) | |
download | sonar-scanner-cli-e7e6e5a30984f3c5b154da7885647a1d0d7229fb.tar.gz sonar-scanner-cli-e7e6e5a30984f3c5b154da7885647a1d0d7229fb.zip |
Refactor org.sonar.runner.Main
Diffstat (limited to 'src/main/java')
-rw-r--r-- | src/main/java/org/sonar/runner/Logs.java | 29 | ||||
-rw-r--r-- | src/main/java/org/sonar/runner/Main.java | 91 | ||||
-rw-r--r-- | src/main/java/org/sonar/runner/Stats.java | 63 |
3 files changed, 122 insertions, 61 deletions
diff --git a/src/main/java/org/sonar/runner/Logs.java b/src/main/java/org/sonar/runner/Logs.java new file mode 100644 index 0000000..154a3ab --- /dev/null +++ b/src/main/java/org/sonar/runner/Logs.java @@ -0,0 +1,29 @@ +/* + * Sonar Standalone Runner + * 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; + +class Logs { + private Logs() { + } + + static void info(String message) { + System.out.println(message); // NOSONAR + } +} diff --git a/src/main/java/org/sonar/runner/Main.java b/src/main/java/org/sonar/runner/Main.java index ab93f8e..7c237cd 100644 --- a/src/main/java/org/sonar/runner/Main.java +++ b/src/main/java/org/sonar/runner/Main.java @@ -63,57 +63,30 @@ public final class Main { } private void execute(String[] args) { - long startTime = System.currentTimeMillis(); + Stats stats = new Stats().start(); try { Properties props = loadProperties(args); Runner runner = Runner.create(props); - log("Runner version: " + SonarRunnerVersion.getVersion()); - log("Java version: " + System.getProperty("java.version", "<unknown java version>") - + ", vendor: " + System.getProperty("java.vendor", "<unknown vendor>")); - log("OS name: \"" + System.getProperty("os.name") + "\", version: \"" + System.getProperty("os.version") + "\", arch: \"" + System.getProperty("os.arch") + "\""); + Logs.info("Runner version: " + SonarRunnerVersion.getVersion()); + Logs.info("Java version: " + System.getProperty("java.version", "<unknown>") + + ", vendor: " + System.getProperty("java.vendor", "<unknown>")); + Logs.info("OS name: \"" + System.getProperty("os.name") + "\", version: \"" + System.getProperty("os.version") + "\", arch: \"" + System.getProperty("os.arch") + "\""); if (debugMode) { - log("Other system properties:"); - log(" - sun.arch.data.model: \"" + System.getProperty("sun.arch.data.model") + "\""); + Logs.info("Other system properties:"); + Logs.info(" - sun.arch.data.model: \"" + System.getProperty("sun.arch.data.model") + "\""); } - log("Server: " + runner.getSonarServerURL()); + Logs.info("Server: " + runner.getSonarServerURL()); try { - log("Work directory: " + runner.getWorkDir().getCanonicalPath()); + Logs.info("Work directory: " + runner.getWorkDir().getCanonicalPath()); } catch (IOException e) { throw new RunnerException(e); } runner.execute(); } finally { - printStats(startTime); + stats.stop(); } } - private void printStats(long startTime) { - long time = System.currentTimeMillis() - startTime; - log("Total time: " + formatTime(time)); - - System.gc(); - Runtime r = Runtime.getRuntime(); - long mb = 1024L * 1024; - log("Final Memory: " + (r.totalMemory() - r.freeMemory()) / mb + "M/" + r.totalMemory() / mb + "M"); - } - - @VisibleForTesting - static String formatTime(long time) { - long h = time / (60 * 60 * 1000); - long m = (time - h * 60 * 60 * 1000) / (60 * 1000); - long s = (time - h * 60 * 60 * 1000 - m * 60 * 1000) / 1000; - long ms = time % 1000; - final String format; - if (h > 0) { - format = "%1$d:%2$02d:%3$02d.%4$03ds"; - } else if (m > 0) { - format = "%2$d:%3$02d.%4$03ds"; - } else { - format = "%3$d.%4$03ds"; - } - return String.format(format, h, m, s, ms); - } - @VisibleForTesting Properties loadProperties(String[] args) { Properties commandLineProps = new Properties(); @@ -139,22 +112,20 @@ public final class Main { Properties loadRunnerProperties(Properties props) { File settingsFile = locatePropertiesFile(props, RUNNER_HOME, "conf/sonar-runner.properties", RUNNER_SETTINGS); if (settingsFile != null && settingsFile.isFile() && settingsFile.exists()) { - log("Runner configuration file: " + settingsFile.getAbsolutePath()); + Logs.info("Runner configuration file: " + settingsFile.getAbsolutePath()); return toProperties(settingsFile); - } else { - log("Runner configuration file: NONE"); } + Logs.info("Runner configuration file: NONE"); return new Properties(); } private Properties loadProjectProperties(Properties props) { File settingsFile = locatePropertiesFile(props, PROJECT_HOME, "sonar-project.properties", PROJECT_SETTINGS); if (settingsFile != null && settingsFile.isFile() && settingsFile.exists()) { - log("Project configuration file: " + settingsFile.getAbsolutePath()); + Logs.info("Project configuration file: " + settingsFile.getAbsolutePath()); return toProperties(settingsFile); - } else { - log("Project configuration file: NONE"); } + Logs.info("Project configuration file: NONE"); return new Properties(); } @@ -208,10 +179,12 @@ public final class Main { printError("Missing argument for option --define"); } arg = args[i]; - parseProperty(arg, props); + appendPropertyTo(arg, props); + } else if (arg.startsWith("-D")) { arg = arg.substring(2); - parseProperty(arg, props); + appendPropertyTo(arg, props); + } else { printError("Unrecognized option: " + arg); } @@ -219,7 +192,7 @@ public final class Main { return props; } - private void parseProperty(String arg, Properties props) { + private void appendPropertyTo(String arg, Properties props) { final String key, value; int j = arg.indexOf('='); if (j == -1) { @@ -232,24 +205,20 @@ public final class Main { props.setProperty(key, value); } - private void printUsage() { - log(""); - log("usage: sonar-runner [options]"); - log(""); - log("Options:"); - log(" -h,--help Display help information"); - log(" -X,--debug Produce execution debug output"); - log(" -D,--define <arg> Define property"); - System.exit(0); // NOSONAR - } - private void printError(String message) { - log(""); - log(message); + Logs.info(""); + Logs.info(message); printUsage(); } - private void log(String message) { - System.out.println(message); // NOSONAR + private void printUsage() { + Logs.info(""); + Logs.info("usage: sonar-runner [options]"); + Logs.info(""); + Logs.info("Options:"); + Logs.info(" -h,--help Display help information"); + Logs.info(" -X,--debug Produce execution debug output"); + Logs.info(" -D,--define <arg> Define property"); + System.exit(0); // NOSONAR } } diff --git a/src/main/java/org/sonar/runner/Stats.java b/src/main/java/org/sonar/runner/Stats.java new file mode 100644 index 0000000..966228f --- /dev/null +++ b/src/main/java/org/sonar/runner/Stats.java @@ -0,0 +1,63 @@ +/* + * Sonar Standalone Runner + * 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 com.google.common.annotations.VisibleForTesting; + +class Stats { + private long startTime; + + Stats() { + } + + Stats start() { + startTime = System.currentTimeMillis(); + return this; + } + + Stats stop() { + long stopTime = System.currentTimeMillis() - startTime; + Logs.info("Total time: " + formatTime(stopTime)); + + System.gc(); + Runtime r = Runtime.getRuntime(); + long mb = 1024L * 1024; + Logs.info("Final Memory: " + (r.totalMemory() - r.freeMemory()) / mb + "M/" + r.totalMemory() / mb + "M"); + + return this; + } + + @VisibleForTesting + static String formatTime(long time) { + long h = time / (60 * 60 * 1000); + long m = (time - h * 60 * 60 * 1000) / (60 * 1000); + long s = (time - h * 60 * 60 * 1000 - m * 60 * 1000) / 1000; + long ms = time % 1000; + final String format; + if (h > 0) { + format = "%1$d:%2$02d:%3$02d.%4$03ds"; + } else if (m > 0) { + format = "%2$d:%3$02d.%4$03ds"; + } else { + format = "%3$d.%4$03ds"; + } + return String.format(format, h, m, s, ms); + } +} |