]> source.dussan.org Git - sonar-scanner-cli.git/commitdiff
Refactor and fix violations (again)
authorFabrice Bellingard <fabrice.bellingard@sonarsource.com>
Wed, 5 Sep 2012 14:13:54 +0000 (16:13 +0200)
committerFabrice Bellingard <fabrice.bellingard@sonarsource.com>
Wed, 5 Sep 2012 14:13:54 +0000 (16:13 +0200)
src/main/java/org/sonar/runner/Main.java
src/main/java/org/sonar/runner/Runner.java
src/main/java/org/sonar/runner/bootstrapper/Bootstrapper.java
src/main/java/org/sonar/runner/bootstrapper/BootstrapperIOUtils.java [deleted file]
src/main/java/org/sonar/runner/model/Launcher.java
src/main/java/org/sonar/runner/utils/SonarRunnerIOUtils.java [new file with mode: 0644]
src/main/java/org/sonar/runner/utils/SonarRunnerVersion.java

index 1bfcf7d6158cb1bd4024858fcb47f790f4604fbb..12580b7b6ba168f53b7cdac1dad7a71418a389a0 100644 (file)
 
 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);
     }
   }
 
index 0be391fee1f87d7086de76e58c0aacbc4e801e90..a24ae84337e53cf3ce71d667270153b482b80394 100644 (file)
  */
 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;
   }
index d0333640205c8a5144b5a7c913035e462584f4cc..8bf74665b697d0203838ba9fc5122dcfdf240c04 100644 (file)
@@ -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 (file)
index af55370..0000000
+++ /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 <code>Closeable</code>.
-   */
-  public static void closeQuietly(Closeable closeable) {
-    try {
-      if (closeable != null) {
-        closeable.close();
-      }
-    } catch (IOException ioe) {
-    }
-  }
-
-  /**
-   * Get the contents of a <code>Reader</code> 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 <code>InputStream</code> to an <code>OutputStream</code>.
-   */
-  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 <code>Reader</code> to a <code>Writer</code>.
-   */
-  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;
-    }
-  }
-
-}
index 948a6c0e3eef1eb3f530bd6bffe0b79e325351e1..24202a3575dc4cbd021f0cdfc149eda38f8b5fd8 100644 (file)
@@ -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 (file)
index 0000000..3fac758
--- /dev/null
@@ -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 <code>Closeable</code>.
+   */
+  public static void closeQuietly(Closeable closeable) {
+    try {
+      if (closeable != null) {
+        closeable.close();
+      }
+    } catch (IOException ioe) {
+    }
+  }
+
+  /**
+   * Get the contents of a <code>Reader</code> 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 <code>InputStream</code> to an <code>OutputStream</code>.
+   */
+  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 <code>Reader</code> to a <code>Writer</code>.
+   */
+  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;
+    }
+  }
+
+}
index 12ebbef1267fe94e78fff7a18a909ee3e659e4af..12622fad7c775e39282dab0dfe4ac9628ef164e0 100644 (file)
@@ -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);
     }
   }
 }