From e76b7521b278dbbbdf9e8bd3c885e55b7132d19d Mon Sep 17 00:00:00 2001 From: Fabrice Bellingard Date: Wed, 5 Sep 2012 16:13:54 +0200 Subject: Refactor and fix violations (again) --- src/main/java/org/sonar/runner/Main.java | 5 +- src/main/java/org/sonar/runner/Runner.java | 12 ++- .../sonar/runner/bootstrapper/Bootstrapper.java | 16 ++-- .../runner/bootstrapper/BootstrapperIOUtils.java | 105 --------------------- src/main/java/org/sonar/runner/model/Launcher.java | 5 +- .../org/sonar/runner/utils/SonarRunnerIOUtils.java | 105 +++++++++++++++++++++ .../org/sonar/runner/utils/SonarRunnerVersion.java | 3 +- 7 files changed, 127 insertions(+), 124 deletions(-) delete mode 100644 src/main/java/org/sonar/runner/bootstrapper/BootstrapperIOUtils.java create mode 100644 src/main/java/org/sonar/runner/utils/SonarRunnerIOUtils.java (limited to 'src') diff --git a/src/main/java/org/sonar/runner/Main.java b/src/main/java/org/sonar/runner/Main.java index 1bfcf7d..12580b7 100644 --- a/src/main/java/org/sonar/runner/Main.java +++ b/src/main/java/org/sonar/runner/Main.java @@ -20,10 +20,11 @@ package org.sonar.runner; +import org.sonar.runner.utils.SonarRunnerIOUtils; + import org.sonar.runner.utils.SonarRunnerVersion; import org.sonar.runner.bootstrapper.BootstrapException; -import org.sonar.runner.bootstrapper.BootstrapperIOUtils; import java.io.File; import java.io.FileInputStream; @@ -160,7 +161,7 @@ public final class Main { throw new BootstrapException(e); } finally { - BootstrapperIOUtils.closeQuietly(in); + SonarRunnerIOUtils.closeQuietly(in); } } diff --git a/src/main/java/org/sonar/runner/Runner.java b/src/main/java/org/sonar/runner/Runner.java index 0be391f..a24ae84 100644 --- a/src/main/java/org/sonar/runner/Runner.java +++ b/src/main/java/org/sonar/runner/Runner.java @@ -19,11 +19,10 @@ */ 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.utils.SonarRunnerVersion; import java.io.File; import java.lang.reflect.Constructor; @@ -91,7 +90,7 @@ public final class Runner { String path = properties.getProperty("project.home", "."); projectDir = new File(path); if (!projectDir.isDirectory() || !projectDir.exists()) { - throw new IllegalArgumentException("Project home must be an existing directory: " + path); + throw new RunnerException("Project home must be an existing directory: " + path); } // project home exists: add its absolute path as "sonar.runner.projectDir" property properties.put(PROPERTY_PROJECT_DIR, projectDir.getAbsolutePath()); @@ -101,7 +100,7 @@ public final class Runner { private File initWorkDir() { String customWorkDir = properties.getProperty(PROPERTY_WORK_DIRECTORY); if (customWorkDir == null || customWorkDir.trim().length() == 0) { - return new File(projectDir, DEF_VALUE_WORK_DIRECTORY); + return new File(getProjectDir(), DEF_VALUE_WORK_DIRECTORY); } return defineCustomizedWorkDir(new File(customWorkDir)); } @@ -110,9 +109,12 @@ public final class Runner { if (customWorkDir.isAbsolute()) { return customWorkDir; } - return new File(projectDir, customWorkDir.getPath()); + return new File(getProjectDir(), customWorkDir.getPath()); } + /** + * @return the project base directory + */ protected File getProjectDir() { return projectDir; } diff --git a/src/main/java/org/sonar/runner/bootstrapper/Bootstrapper.java b/src/main/java/org/sonar/runner/bootstrapper/Bootstrapper.java index d033364..8bf7466 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.SonarRunnerIOUtils; + import org.sonar.runner.utils.SonarRunnerVersion; import java.io.*; @@ -108,14 +110,14 @@ public class Bootstrapper { HttpURLConnection connection = newHttpConnection(new URL(fullUrl)); output = new FileOutputStream(toFile, false); input = connection.getInputStream(); - BootstrapperIOUtils.copyLarge(input, output); + SonarRunnerIOUtils.copyLarge(input, output); } catch (IOException e) { - BootstrapperIOUtils.closeQuietly(output); - BootstrapperIOUtils.deleteFileQuietly(toFile); + SonarRunnerIOUtils.closeQuietly(output); + SonarRunnerIOUtils.deleteFileQuietly(toFile); throw new BootstrapException("Fail to download the file: " + fullUrl, e); } finally { - BootstrapperIOUtils.closeQuietly(input); - BootstrapperIOUtils.closeQuietly(output); + SonarRunnerIOUtils.closeQuietly(input); + SonarRunnerIOUtils.closeQuietly(output); } } @@ -128,9 +130,9 @@ public class Bootstrapper { if (statusCode != HttpURLConnection.HTTP_OK) { throw new IOException("Status returned by url : '" + fullUrl + "' is invalid : " + statusCode); } - return BootstrapperIOUtils.toString(reader); + return SonarRunnerIOUtils.toString(reader); } finally { - BootstrapperIOUtils.closeQuietly(reader); + SonarRunnerIOUtils.closeQuietly(reader); conn.disconnect(); } } diff --git a/src/main/java/org/sonar/runner/bootstrapper/BootstrapperIOUtils.java b/src/main/java/org/sonar/runner/bootstrapper/BootstrapperIOUtils.java deleted file mode 100644 index af55370..0000000 --- a/src/main/java/org/sonar/runner/bootstrapper/BootstrapperIOUtils.java +++ /dev/null @@ -1,105 +0,0 @@ -/* - * 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.bootstrapper; - -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 { - - private BootstrapperIOUtils() { - // only static methods - } - - /** - * The default buffer size to use. - */ - private static final int DEFAULT_BUFFER_SIZE = 1024 * 4; - - /** - * Unconditionally close a Closeable. - */ - public static void closeQuietly(Closeable closeable) { - try { - if (closeable != null) { - closeable.close(); - } - } catch (IOException ioe) { - } - } - - /** - * Get the contents of a Reader as a String. - */ - public static String toString(Reader input) throws IOException { - StringWriter sw = new StringWriter(); - copyLarge(input, sw); - return sw.toString(); - } - - /** - * Copy bytes from an InputStream to an OutputStream. - */ - public static long copyLarge(InputStream input, OutputStream output) throws IOException { - byte[] buffer = new byte[DEFAULT_BUFFER_SIZE]; - long count = 0; - int n = 0; - while (-1 != (n = input.read(buffer))) { - output.write(buffer, 0, n); - count += n; - } - return count; - } - - /** - * Copy chars from a Reader to a Writer. - */ - public static long copyLarge(Reader input, Writer output) throws IOException { - char[] buffer = new char[DEFAULT_BUFFER_SIZE]; - long count = 0; - int n = 0; - while (-1 != (n = input.read(buffer))) { - output.write(buffer, 0, n); - count += n; - } - return count; - } - - /** - * Deletes a file (not a directory). - */ - public static boolean deleteFileQuietly(File file) { - if (file == null) { - return false; - } - try { - return file.delete(); - } catch (Exception e) { - return false; - } - } - -} diff --git a/src/main/java/org/sonar/runner/model/Launcher.java b/src/main/java/org/sonar/runner/model/Launcher.java index 948a6c0..24202a3 100644 --- a/src/main/java/org/sonar/runner/model/Launcher.java +++ b/src/main/java/org/sonar/runner/model/Launcher.java @@ -20,9 +20,6 @@ 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; import ch.qos.logback.core.joran.spi.JoranException; @@ -39,6 +36,8 @@ 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.Main; +import org.sonar.runner.Runner; import java.io.File; import java.io.InputStream; diff --git a/src/main/java/org/sonar/runner/utils/SonarRunnerIOUtils.java b/src/main/java/org/sonar/runner/utils/SonarRunnerIOUtils.java new file mode 100644 index 0000000..3fac758 --- /dev/null +++ b/src/main/java/org/sonar/runner/utils/SonarRunnerIOUtils.java @@ -0,0 +1,105 @@ +/* + * 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.utils; + +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 SonarRunnerIOUtils { + + private SonarRunnerIOUtils() { + // only static methods + } + + /** + * The default buffer size to use. + */ + private static final int DEFAULT_BUFFER_SIZE = 1024 * 4; + + /** + * Unconditionally close a Closeable. + */ + public static void closeQuietly(Closeable closeable) { + try { + if (closeable != null) { + closeable.close(); + } + } catch (IOException ioe) { + } + } + + /** + * Get the contents of a Reader as a String. + */ + public static String toString(Reader input) throws IOException { + StringWriter sw = new StringWriter(); + copyLarge(input, sw); + return sw.toString(); + } + + /** + * Copy bytes from an InputStream to an OutputStream. + */ + public static long copyLarge(InputStream input, OutputStream output) throws IOException { + byte[] buffer = new byte[DEFAULT_BUFFER_SIZE]; + long count = 0; + int n = 0; + while (-1 != (n = input.read(buffer))) { + output.write(buffer, 0, n); + count += n; + } + return count; + } + + /** + * Copy chars from a Reader to a Writer. + */ + public static long copyLarge(Reader input, Writer output) throws IOException { + char[] buffer = new char[DEFAULT_BUFFER_SIZE]; + long count = 0; + int n = 0; + while (-1 != (n = input.read(buffer))) { + output.write(buffer, 0, n); + count += n; + } + return count; + } + + /** + * Deletes a file (not a directory). + */ + public static boolean deleteFileQuietly(File file) { + if (file == null) { + return false; + } + try { + return file.delete(); + } catch (Exception e) { + return false; + } + } + +} diff --git a/src/main/java/org/sonar/runner/utils/SonarRunnerVersion.java b/src/main/java/org/sonar/runner/utils/SonarRunnerVersion.java index 12ebbef..12622fa 100644 --- a/src/main/java/org/sonar/runner/utils/SonarRunnerVersion.java +++ b/src/main/java/org/sonar/runner/utils/SonarRunnerVersion.java @@ -19,7 +19,6 @@ */ package org.sonar.runner.utils; -import org.sonar.runner.bootstrapper.BootstrapperIOUtils; import java.io.IOException; import java.io.InputStream; @@ -48,7 +47,7 @@ public enum SonarRunnerVersion { this.version = ""; } finally { - BootstrapperIOUtils.closeQuietly(input); + SonarRunnerIOUtils.closeQuietly(input); } } } -- cgit v1.2.3