diff options
author | Fabrice Bellingard <fabrice.bellingard@sonarsource.com> | 2012-09-05 16:05:35 +0200 |
---|---|---|
committer | Fabrice Bellingard <fabrice.bellingard@sonarsource.com> | 2012-09-05 16:05:35 +0200 |
commit | 7fb9922dd7e9e97dbdf697d98e1e47ff00f35c5a (patch) | |
tree | 9f5166ce4bab633e5a58b8c263c4c27a32bb3ea1 /src/main/java/org/sonar/runner | |
parent | 312863a0ebf3d68a4fdb34446d94819d5252578d (diff) | |
download | sonar-scanner-cli-7fb9922dd7e9e97dbdf697d98e1e47ff00f35c5a.tar.gz sonar-scanner-cli-7fb9922dd7e9e97dbdf697d98e1e47ff00f35c5a.zip |
Refactor and fix violations
Diffstat (limited to 'src/main/java/org/sonar/runner')
-rw-r--r-- | src/main/java/org/sonar/runner/Main.java | 6 | ||||
-rw-r--r-- | src/main/java/org/sonar/runner/Runner.java | 58 | ||||
-rw-r--r-- | src/main/java/org/sonar/runner/bootstrapper/Bootstrapper.java | 4 | ||||
-rw-r--r-- | src/main/java/org/sonar/runner/bootstrapper/BootstrapperIOUtils.java | 11 | ||||
-rw-r--r-- | src/main/java/org/sonar/runner/model/Launcher.java (renamed from src/main/java/org/sonar/runner/Launcher.java) | 6 | ||||
-rw-r--r-- | src/main/java/org/sonar/runner/model/SonarProjectBuilder.java | 7 | ||||
-rw-r--r-- | src/main/java/org/sonar/runner/utils/SonarRunnerVersion.java (renamed from src/main/java/org/sonar/runner/bootstrapper/BootstrapperVersion.java) | 8 |
7 files changed, 41 insertions, 59 deletions
diff --git a/src/main/java/org/sonar/runner/Main.java b/src/main/java/org/sonar/runner/Main.java index d133b5f..1bfcf7d 100644 --- a/src/main/java/org/sonar/runner/Main.java +++ b/src/main/java/org/sonar/runner/Main.java @@ -20,6 +20,8 @@ package org.sonar.runner; +import org.sonar.runner.utils.SonarRunnerVersion; + import org.sonar.runner.bootstrapper.BootstrapException; import org.sonar.runner.bootstrapper.BootstrapperIOUtils; @@ -50,7 +52,7 @@ public final class Main { try { Properties props = loadProperties(args); Runner runner = Runner.create(props); - log("Runner version: " + runner.getRunnerVersion()); + 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") + "\""); @@ -58,7 +60,7 @@ public final class Main { log("Other system properties:"); log(" - sun.arch.data.model: \"" + System.getProperty("sun.arch.data.model") + "\""); } - log("Server: " + runner.getServerURL()); + log("Server: " + runner.getSonarServerURL()); try { log("Work directory: " + runner.getWorkDir().getCanonicalPath()); } catch (IOException e) { diff --git a/src/main/java/org/sonar/runner/Runner.java b/src/main/java/org/sonar/runner/Runner.java index 130bc8c..0be391f 100644 --- a/src/main/java/org/sonar/runner/Runner.java +++ b/src/main/java/org/sonar/runner/Runner.java @@ -19,14 +19,13 @@ */ package org.sonar.runner; +import org.sonar.runner.utils.SonarRunnerVersion; + import org.sonar.runner.bootstrapper.BootstrapClassLoader; import org.sonar.runner.bootstrapper.BootstrapException; import org.sonar.runner.bootstrapper.Bootstrapper; -import org.sonar.runner.bootstrapper.BootstrapperIOUtils; import java.io.File; -import java.io.IOException; -import java.io.InputStream; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; @@ -63,11 +62,6 @@ public final class Runner { */ private static final String[] UNSUPPORTED_VERSIONS = {"1", "2.0", "2.1", "2.2", "2.3", "2.4", "2.5", "2.6", "2.7", "2.8", "2.9", "2.10"}; - /** - * Array of all mandatory properties required to execute runner. - */ - private static final String[] MANDATORY_PROPERTIES = {"sonar.projectKey", "sonar.projectName", "sonar.projectVersion", "sources"}; - private File projectDir; private File workDir; private Properties properties; @@ -82,28 +76,14 @@ public final class Runner { } public void execute() { - checkMandatoryProperties(); - Bootstrapper bootstrapper = new Bootstrapper("SonarRunner/" + getRunnerVersion(), getServerURL(), getWorkDir()); + String sonarRunnerVersion = SonarRunnerVersion.getVersion(); + properties.put(PROPERTY_RUNNER_VERSION, sonarRunnerVersion); + Bootstrapper bootstrapper = new Bootstrapper("SonarRunner/" + sonarRunnerVersion, getSonarServerURL(), getWorkDir()); checkSonarVersion(bootstrapper); delegateExecution(createClassLoader(bootstrapper)); } - void checkMandatoryProperties() { - StringBuilder missing = new StringBuilder(); - for (String mandatoryProperty : MANDATORY_PROPERTIES) { - if (!properties.containsKey(mandatoryProperty)) { - if (missing.length() > 0) { - missing.append(", "); - } - missing.append(mandatoryProperty); - } - } - if (missing.length() != 0) { - throw new RunnerException("You must define mandatory properties: " + missing); - } - } - - public String getServerURL() { + protected String getSonarServerURL() { return properties.getProperty("sonar.host.url", "http://localhost:9000"); } @@ -113,7 +93,7 @@ public final class Runner { if (!projectDir.isDirectory() || !projectDir.exists()) { throw new IllegalArgumentException("Project home must be an existing directory: " + path); } - // project home exist, add its absolute path as "sonar.runner.projectDir" property + // project home exists: add its absolute path as "sonar.runner.projectDir" property properties.put(PROPERTY_PROJECT_DIR, projectDir.getAbsolutePath()); workDir = initWorkDir(); } @@ -133,38 +113,24 @@ public final class Runner { return new File(projectDir, customWorkDir.getPath()); } - public File getProjectDir() { + protected File getProjectDir() { return projectDir; } /** * @return work directory, default is ".sonar" in project directory */ - public File getWorkDir() { + protected File getWorkDir() { return workDir; } /** * @return global properties, project properties and command-line properties */ - public Properties getProperties() { + protected Properties getProperties() { return properties; } - public String getRunnerVersion() { - InputStream in = null; - try { - in = Runner.class.getResourceAsStream("/org/sonar/runner/version.txt"); - Properties props = new Properties(); - props.load(in); - return props.getProperty("version"); - } catch (IOException e) { - throw new BootstrapException("Could not load the version information for Sonar Standalone Runner", e); - } finally { - BootstrapperIOUtils.closeQuietly(in); - } - } - protected void checkSonarVersion(Bootstrapper bootstrapper) { String serverVersion = bootstrapper.getServerVersion(); if (isUnsupportedVersion(serverVersion)) { @@ -194,7 +160,7 @@ public final class Runner { } /** - * Loads {@link Launcher} from specified {@link org.sonar.batch.bootstrapper.BootstrapClassLoader} and passes control to it. + * Loads Launcher class from specified {@link org.sonar.batch.bootstrapper.BootstrapClassLoader} and passes control to it. * * @see Launcher#execute() */ @@ -202,7 +168,7 @@ public final class Runner { ClassLoader oldContextClassLoader = Thread.currentThread().getContextClassLoader(); try { Thread.currentThread().setContextClassLoader(sonarClassLoader); - Class<?> launcherClass = sonarClassLoader.findClass("org.sonar.runner.Launcher"); + Class<?> launcherClass = sonarClassLoader.findClass("org.sonar.runner.model.Launcher"); Constructor<?> constructor = launcherClass.getConstructor(Properties.class); Object launcher = constructor.newInstance(getProperties()); Method method = launcherClass.getMethod("execute"); diff --git a/src/main/java/org/sonar/runner/bootstrapper/Bootstrapper.java b/src/main/java/org/sonar/runner/bootstrapper/Bootstrapper.java index d8e2187..d033364 100644 --- a/src/main/java/org/sonar/runner/bootstrapper/Bootstrapper.java +++ b/src/main/java/org/sonar/runner/bootstrapper/Bootstrapper.java @@ -19,6 +19,8 @@ */ package org.sonar.runner.bootstrapper; +import org.sonar.runner.utils.SonarRunnerVersion; + import java.io.*; import java.net.HttpURLConnection; import java.net.MalformedURLException; @@ -137,7 +139,7 @@ public class Bootstrapper { * By convention, the product tokens are listed in order of their significance for identifying the application. */ String getUserAgent() { - return "sonar-bootstrapper/" + BootstrapperVersion.getVersion() + " " + productToken; + return "sonar-bootstrapper/" + SonarRunnerVersion.getVersion() + " " + productToken; } HttpURLConnection newHttpConnection(URL url) throws IOException { diff --git a/src/main/java/org/sonar/runner/bootstrapper/BootstrapperIOUtils.java b/src/main/java/org/sonar/runner/bootstrapper/BootstrapperIOUtils.java index c47186a..af55370 100644 --- a/src/main/java/org/sonar/runner/bootstrapper/BootstrapperIOUtils.java +++ b/src/main/java/org/sonar/runner/bootstrapper/BootstrapperIOUtils.java @@ -19,7 +19,14 @@ */ package org.sonar.runner.bootstrapper; -import java.io.*; +import java.io.Closeable; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.Reader; +import java.io.StringWriter; +import java.io.Writer; public final class BootstrapperIOUtils { @@ -40,7 +47,7 @@ public final class BootstrapperIOUtils { if (closeable != null) { closeable.close(); } - } catch (IOException ioe) { // NOSONAR + } catch (IOException ioe) { } } diff --git a/src/main/java/org/sonar/runner/Launcher.java b/src/main/java/org/sonar/runner/model/Launcher.java index 2549300..948a6c0 100644 --- a/src/main/java/org/sonar/runner/Launcher.java +++ b/src/main/java/org/sonar/runner/model/Launcher.java @@ -18,7 +18,10 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 */ -package org.sonar.runner; +package org.sonar.runner.model; + +import org.sonar.runner.Main; +import org.sonar.runner.Runner; import ch.qos.logback.classic.LoggerContext; import ch.qos.logback.classic.joran.JoranConfigurator; @@ -36,7 +39,6 @@ import org.sonar.api.batch.bootstrap.ProjectReactor; import org.sonar.api.utils.SonarException; import org.sonar.batch.Batch; import org.sonar.batch.bootstrapper.EnvironmentInformation; -import org.sonar.runner.model.SonarProjectBuilder; import java.io.File; import java.io.InputStream; diff --git a/src/main/java/org/sonar/runner/model/SonarProjectBuilder.java b/src/main/java/org/sonar/runner/model/SonarProjectBuilder.java index 1fd4e70..d1c5fde 100644 --- a/src/main/java/org/sonar/runner/model/SonarProjectBuilder.java +++ b/src/main/java/org/sonar/runner/model/SonarProjectBuilder.java @@ -40,8 +40,10 @@ import java.util.Properties; /** * Class that creates a Sonar project definition based on a set of properties. + * + * @since 1.5 */ -public class SonarProjectBuilder { +public final class SonarProjectBuilder { private static final String PROPERTY_SONAR_MODULES = "sonar.modules"; private static final String PROPERTY_MODULE_FILE = "file"; @@ -76,8 +78,7 @@ public class SonarProjectBuilder { } public static SonarProjectBuilder create(File baseDir, Properties properties) { - SonarProjectBuilder builder = new SonarProjectBuilder(baseDir, properties); - return builder; + return new SonarProjectBuilder(baseDir, properties); } public ProjectDefinition generateProjectDefinition() { diff --git a/src/main/java/org/sonar/runner/bootstrapper/BootstrapperVersion.java b/src/main/java/org/sonar/runner/utils/SonarRunnerVersion.java index b50c03b..12ebbef 100644 --- a/src/main/java/org/sonar/runner/bootstrapper/BootstrapperVersion.java +++ b/src/main/java/org/sonar/runner/utils/SonarRunnerVersion.java @@ -17,13 +17,15 @@ * 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.bootstrapper; +package org.sonar.runner.utils; + +import org.sonar.runner.bootstrapper.BootstrapperIOUtils; import java.io.IOException; import java.io.InputStream; import java.util.Properties; -public enum BootstrapperVersion { +public enum SonarRunnerVersion { INSTANCE; @@ -34,7 +36,7 @@ public enum BootstrapperVersion { return INSTANCE.version; } - private BootstrapperVersion() { + private SonarRunnerVersion() { InputStream input = getClass().getResourceAsStream(PROPERTIES_PATH); try { Properties properties = new Properties(); |