diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2013-01-04 16:20:00 +0100 |
---|---|---|
committer | Julien HENRY <julien.henry@sonarsource.com> | 2013-01-04 16:20:00 +0100 |
commit | f240fe25d0cc920f96dfe35d8c826eb06514241f (patch) | |
tree | 940d9b7ae9dabae76d968e87938e6135c546e702 /src | |
parent | 48138df1a8a5ff33a1ef71e2fe5ce413b20b95b2 (diff) | |
download | sonar-scanner-cli-f240fe25d0cc920f96dfe35d8c826eb06514241f.tar.gz sonar-scanner-cli-f240fe25d0cc920f96dfe35d8c826eb06514241f.zip |
SONARPLUGINS-1233 Refactoring to reuse IOUtils
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/org/sonar/runner/Bootstrapper.java | 2 | ||||
-rw-r--r-- | src/main/java/org/sonar/runner/IOUtils.java | 74 | ||||
-rw-r--r-- | src/main/java/org/sonar/runner/Runner.java | 93 |
3 files changed, 77 insertions, 92 deletions
diff --git a/src/main/java/org/sonar/runner/Bootstrapper.java b/src/main/java/org/sonar/runner/Bootstrapper.java index 00d3d8c..63fdad5 100644 --- a/src/main/java/org/sonar/runner/Bootstrapper.java +++ b/src/main/java/org/sonar/runner/Bootstrapper.java @@ -116,7 +116,7 @@ class Bootstrapper { IOUtils.copyLarge(input, output); } catch (IOException e) { IOUtils.closeQuietly(output); - IOUtils.deleteFileQuietly(toFile); + IOUtils.deleteQuietly(toFile); throw new IllegalStateException("Fail to download the file: " + fullUrl, e); } finally { IOUtils.closeQuietly(input); diff --git a/src/main/java/org/sonar/runner/IOUtils.java b/src/main/java/org/sonar/runner/IOUtils.java index 9109f3f..6c566a2 100644 --- a/src/main/java/org/sonar/runner/IOUtils.java +++ b/src/main/java/org/sonar/runner/IOUtils.java @@ -21,6 +21,7 @@ package org.sonar.runner; import java.io.Closeable; import java.io.File; +import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -97,19 +98,86 @@ final class IOUtils { } /** - * Deletes a file (not a directory). + * Duplicated from Commons IO */ - static boolean deleteFileQuietly(File file) { + static boolean deleteQuietly(File file) { if (file == null) { return false; } try { + if (file.isDirectory()) { + cleanDirectory(file); + } + } catch (Exception ignored) { + } + + try { return file.delete(); - } catch (Exception e) { + } catch (Exception ignored) { return false; } } + private static void cleanDirectory(File directory) throws IOException { + if (!directory.exists()) { + String message = directory + " does not exist"; + throw new IllegalArgumentException(message); + } + + if (!directory.isDirectory()) { + String message = directory + " is not a directory"; + throw new IllegalArgumentException(message); + } + + File[] files = directory.listFiles(); + if (files == null) { // null if security restricted + throw new IOException("Failed to list contents of " + directory); + } + + IOException exception = null; + for (File file : files) { + try { + forceDelete(file); + } catch (IOException ioe) { + exception = ioe; + } + } + + if (null != exception) { + throw exception; + } + } + + private static void forceDelete(File file) throws IOException { + if (file.isDirectory()) { + deleteDirectory(file); + } else { + boolean filePresent = file.exists(); + if (!file.delete()) { + if (!filePresent) { + throw new FileNotFoundException("File does not exist: " + file); + } + String message = + "Unable to delete file: " + file; + throw new IOException(message); + } + } + } + + private static void deleteDirectory(File directory) throws IOException { + if (!directory.exists()) { + return; + } + + cleanDirectory(directory); + + if (!directory.delete()) { + String message = + "Unable to delete directory " + directory + "."; + throw new IOException(message); + } + } + /** * Parse out a charset from a content type header. * diff --git a/src/main/java/org/sonar/runner/Runner.java b/src/main/java/org/sonar/runner/Runner.java index 865d7a9..2b26856 100644 --- a/src/main/java/org/sonar/runner/Runner.java +++ b/src/main/java/org/sonar/runner/Runner.java @@ -20,8 +20,6 @@ package org.sonar.runner; import java.io.File; -import java.io.FileNotFoundException; -import java.io.IOException; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; @@ -169,97 +167,16 @@ public final class Runner { } private File initWorkDir() { - File workDir; + File newWorkDir; String customWorkDir = properties.getProperty(PROPERTY_WORK_DIRECTORY); if (customWorkDir == null || "".equals(customWorkDir.trim())) { - workDir = new File(getProjectDir(), DEF_VALUE_WORK_DIRECTORY); + newWorkDir = new File(getProjectDir(), DEF_VALUE_WORK_DIRECTORY); } else { - workDir = defineCustomizedWorkDir(new File(customWorkDir)); - } - deleteQuietly(workDir); - return workDir; - } - - /** - * Duplicated from Commons IO because we don't want dependencies in Sonar Runner - */ - private static boolean deleteQuietly(File file) { - if (file == null) { - return false; - } - try { - if (file.isDirectory()) { - cleanDirectory(file); - } - } catch (Exception ignored) { - } - - try { - return file.delete(); - } catch (Exception ignored) { - return false; - } - } - - private static void cleanDirectory(File directory) throws IOException { - if (!directory.exists()) { - String message = directory + " does not exist"; - throw new IllegalArgumentException(message); - } - - if (!directory.isDirectory()) { - String message = directory + " is not a directory"; - throw new IllegalArgumentException(message); - } - - File[] files = directory.listFiles(); - if (files == null) { // null if security restricted - throw new IOException("Failed to list contents of " + directory); - } - - IOException exception = null; - for (File file : files) { - try { - forceDelete(file); - } catch (IOException ioe) { - exception = ioe; - } - } - - if (null != exception) { - throw exception; - } - } - - private static void forceDelete(File file) throws IOException { - if (file.isDirectory()) { - deleteDirectory(file); - } else { - boolean filePresent = file.exists(); - if (!file.delete()) { - if (!filePresent) { - throw new FileNotFoundException("File does not exist: " + file); - } - String message = - "Unable to delete file: " + file; - throw new IOException(message); - } - } - } - - private static void deleteDirectory(File directory) throws IOException { - if (!directory.exists()) { - return; - } - - cleanDirectory(directory); - - if (!directory.delete()) { - String message = - "Unable to delete directory " + directory + "."; - throw new IOException(message); + newWorkDir = defineCustomizedWorkDir(new File(customWorkDir)); } + IOUtils.deleteQuietly(newWorkDir); + return newWorkDir; } private File defineCustomizedWorkDir(File customWorkDir) { |