diff options
Diffstat (limited to 'sonar-runner-api/src/main/java/org/sonar/runner')
5 files changed, 70 insertions, 33 deletions
diff --git a/sonar-runner-api/src/main/java/org/sonar/runner/api/CommandExecutor.java b/sonar-runner-api/src/main/java/org/sonar/runner/api/CommandExecutor.java index f54dbb9..999150d 100644 --- a/sonar-runner-api/src/main/java/org/sonar/runner/api/CommandExecutor.java +++ b/sonar-runner-api/src/main/java/org/sonar/runner/api/CommandExecutor.java @@ -19,8 +19,6 @@ */ package org.sonar.runner.api; -import org.apache.commons.io.IOUtils; - import javax.annotation.Nullable; import java.io.BufferedReader; @@ -136,10 +134,10 @@ class CommandExecutor { private void closeStreams(Process process) { if (process != null) { - IOUtils.closeQuietly(process.getInputStream()); - IOUtils.closeQuietly(process.getInputStream()); - IOUtils.closeQuietly(process.getOutputStream()); - IOUtils.closeQuietly(process.getErrorStream()); + Utils.closeQuietly(process.getInputStream()); + Utils.closeQuietly(process.getInputStream()); + Utils.closeQuietly(process.getOutputStream()); + Utils.closeQuietly(process.getErrorStream()); } } @@ -167,9 +165,8 @@ class CommandExecutor { @Override public void run() { - InputStreamReader isr = new InputStreamReader(is); - BufferedReader br = new BufferedReader(isr); - try { + try (InputStreamReader isr = new InputStreamReader(is); + BufferedReader br = new BufferedReader(isr)) { String line; while ((line = br.readLine()) != null) { consumeLine(line); @@ -177,9 +174,6 @@ class CommandExecutor { } catch (IOException ioe) { exception = ioe; - } finally { - IOUtils.closeQuietly(br); - IOUtils.closeQuietly(isr); } } diff --git a/sonar-runner-api/src/main/java/org/sonar/runner/api/Dirs.java b/sonar-runner-api/src/main/java/org/sonar/runner/api/Dirs.java index 1e9654e..369a745 100644 --- a/sonar-runner-api/src/main/java/org/sonar/runner/api/Dirs.java +++ b/sonar-runner-api/src/main/java/org/sonar/runner/api/Dirs.java @@ -19,7 +19,6 @@ */ package org.sonar.runner.api; -import org.apache.commons.io.FileUtils; import org.sonar.runner.impl.Logs; import java.io.File; @@ -54,7 +53,7 @@ class Dirs { workDir = new File(projectDir, path); } } - FileUtils.deleteQuietly(workDir); + Utils.deleteQuietly(workDir); runner.setProperty(RunnerProperties.WORK_DIR, workDir.getAbsolutePath()); Logs.info("Work directory: " + workDir.getAbsolutePath()); } diff --git a/sonar-runner-api/src/main/java/org/sonar/runner/api/ForkedRunner.java b/sonar-runner-api/src/main/java/org/sonar/runner/api/ForkedRunner.java index 02ae333..fd7dc0b 100644 --- a/sonar-runner-api/src/main/java/org/sonar/runner/api/ForkedRunner.java +++ b/sonar-runner-api/src/main/java/org/sonar/runner/api/ForkedRunner.java @@ -19,8 +19,6 @@ */ package org.sonar.runner.api; -import org.apache.commons.io.FileUtils; -import org.apache.commons.io.IOUtils; import org.sonar.runner.impl.BatchLauncherMain; import org.sonar.runner.impl.JarExtractor; @@ -163,24 +161,20 @@ public class ForkedRunner extends Runner<ForkedRunner> { } private File writeProperties() { - OutputStream output = null; try { File file = File.createTempFile("sonar-project", ".properties"); - output = new FileOutputStream(file); - properties().store(output, "Generated by sonar-runner"); - return file; - + try (OutputStream output = new FileOutputStream(file)) { + properties().store(output, "Generated by sonar-runner"); + return file; + } } catch (Exception e) { throw new IllegalStateException("Fail to export sonar-runner properties", e); - - } finally { - IOUtils.closeQuietly(output); } } private void deleteTempFiles(ForkCommand forkCommand) { - FileUtils.deleteQuietly(forkCommand.jarFile); - FileUtils.deleteQuietly(forkCommand.propertiesFile); + Utils.deleteQuietly(forkCommand.jarFile); + Utils.deleteQuietly(forkCommand.propertiesFile); } private void fork(ForkCommand forkCommand) { diff --git a/sonar-runner-api/src/main/java/org/sonar/runner/api/Runner.java b/sonar-runner-api/src/main/java/org/sonar/runner/api/Runner.java index c4013f6..b4e06bc 100644 --- a/sonar-runner-api/src/main/java/org/sonar/runner/api/Runner.java +++ b/sonar-runner-api/src/main/java/org/sonar/runner/api/Runner.java @@ -19,7 +19,6 @@ */ package org.sonar.runner.api; -import org.apache.commons.io.IOUtils; import org.sonar.runner.impl.InternalProperties; import javax.annotation.Nullable; @@ -102,14 +101,10 @@ public abstract class Runner<T extends Runner> { } private void writeProperties(File outputFile) { - OutputStream output = null; - try { - output = new FileOutputStream(outputFile); + try (OutputStream output = new FileOutputStream(outputFile)) { properties().store(output, "Generated by sonar-runner"); } catch (Exception e) { throw new IllegalStateException("Fail to export sonar-runner properties", e); - } finally { - IOUtils.closeQuietly(output); } } diff --git a/sonar-runner-api/src/main/java/org/sonar/runner/api/Utils.java b/sonar-runner-api/src/main/java/org/sonar/runner/api/Utils.java index d5e8d58..a4286c1 100644 --- a/sonar-runner-api/src/main/java/org/sonar/runner/api/Utils.java +++ b/sonar-runner-api/src/main/java/org/sonar/runner/api/Utils.java @@ -19,9 +19,19 @@ */ package org.sonar.runner.api; +import javax.annotation.Nullable; + +import java.io.Closeable; +import java.io.File; +import java.io.IOException; +import java.nio.file.FileVisitResult; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.SimpleFileVisitor; import java.util.Arrays; import java.util.Iterator; import java.util.Properties; +import java.nio.file.attribute.*; class Utils { private Utils() { @@ -33,7 +43,7 @@ class Utils { */ static String join(String[] array, String delimiter) { StringBuilder sb = new StringBuilder(); - Iterator it = Arrays.asList(array).iterator(); + Iterator<String> it = Arrays.asList(array).iterator(); while (it.hasNext()) { sb.append(it.next()); if (!it.hasNext()) { @@ -48,4 +58,49 @@ class Utils { Object task = props.get(RunnerProperties.TASK); return task == null || ScanProperties.SCAN_TASK.equals(task); } + + public static void deleteQuietly(File f) { + try { + Files.walkFileTree(f.toPath(), new DeleteFileVisitor()); + } catch (IOException e) { + // ignore + } + } + + private static class DeleteFileVisitor extends SimpleFileVisitor<Path> { + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { + Files.delete(file); + return FileVisitResult.CONTINUE; + } + + @Override + public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException { + Files.delete(file); + return FileVisitResult.CONTINUE; + } + + @Override + public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { + if (exc == null) { + Files.delete(dir); + return FileVisitResult.CONTINUE; + } else { + // directory iteration failed; propagate exception + throw exc; + } + } + } + + public static void closeQuietly(@Nullable Closeable c) { + if (c == null) { + return; + } + + try { + c.close(); + } catch (IOException e) { + // ignore + } + } } |