From 7f72ef88e0fd51a6913598e7a827170f94e1121a Mon Sep 17 00:00:00 2001 From: Jeremy Long Date: Sun, 27 Apr 2014 08:42:02 -0400 Subject: [PATCH] removed code duplication ensuring temporary directory exists Former-commit-id: fba6dfcd3a133378c5f46f4126fa97c02ab110be --- .../analyzer/ArchiveAnalyzer.java | 6 ------ .../dependencycheck/analyzer/JarAnalyzer.java | 10 ++------- .../dependencycheck/utils/FileUtils.java | 5 ----- .../owasp/dependencycheck/utils/Settings.java | 21 +++++++++++++++++-- 4 files changed, 21 insertions(+), 21 deletions(-) diff --git a/dependency-check-core/src/main/java/org/owasp/dependencycheck/analyzer/ArchiveAnalyzer.java b/dependency-check-core/src/main/java/org/owasp/dependencycheck/analyzer/ArchiveAnalyzer.java index 01dc3d2f8..b96cbf078 100644 --- a/dependency-check-core/src/main/java/org/owasp/dependencycheck/analyzer/ArchiveAnalyzer.java +++ b/dependency-check-core/src/main/java/org/owasp/dependencycheck/analyzer/ArchiveAnalyzer.java @@ -157,12 +157,6 @@ public class ArchiveAnalyzer extends AbstractFileTypeAnalyzer { @Override public void initializeFileTypeAnalyzer() throws Exception { final File baseDir = Settings.getTempDirectory(); - if (!baseDir.exists()) { - if (!baseDir.mkdirs()) { - final String msg = String.format("Unable to make a temporary folder '%s'", baseDir.getPath()); - throw new AnalysisException(msg); - } - } tempFileLocation = File.createTempFile("check", "tmp", baseDir); if (!tempFileLocation.delete()) { final String msg = String.format("Unable to delete temporary file '%s'.", tempFileLocation.getAbsolutePath()); diff --git a/dependency-check-core/src/main/java/org/owasp/dependencycheck/analyzer/JarAnalyzer.java b/dependency-check-core/src/main/java/org/owasp/dependencycheck/analyzer/JarAnalyzer.java index 93e310542..51df832cc 100644 --- a/dependency-check-core/src/main/java/org/owasp/dependencycheck/analyzer/JarAnalyzer.java +++ b/dependency-check-core/src/main/java/org/owasp/dependencycheck/analyzer/JarAnalyzer.java @@ -691,8 +691,8 @@ public class JarAnalyzer extends AbstractFileTypeAnalyzer { && !dependency.getFileName().toLowerCase().endsWith("-src.jar") && !dependency.getFileName().toLowerCase().endsWith("-doc.jar")) { LOGGER.log(Level.INFO, - String.format("Jar file '%s' does not contain a manifest.", - dependency.getFileName())); + String.format("Jar file '%s' does not contain a manifest.", + dependency.getFileName())); } return false; } @@ -920,12 +920,6 @@ public class JarAnalyzer extends AbstractFileTypeAnalyzer { @Override public void initializeFileTypeAnalyzer() throws Exception { final File baseDir = Settings.getTempDirectory(); - if (!baseDir.exists()) { - if (!baseDir.mkdirs()) { - final String msg = String.format("Unable to make a temporary folder '%s'", baseDir.getPath()); - throw new AnalysisException(msg); - } - } tempFileLocation = File.createTempFile("check", "tmp", baseDir); if (!tempFileLocation.delete()) { final String msg = String.format("Unable to delete temporary file '%s'.", tempFileLocation.getAbsolutePath()); diff --git a/dependency-check-core/src/main/java/org/owasp/dependencycheck/utils/FileUtils.java b/dependency-check-core/src/main/java/org/owasp/dependencycheck/utils/FileUtils.java index 45531981d..cfa7242e9 100644 --- a/dependency-check-core/src/main/java/org/owasp/dependencycheck/utils/FileUtils.java +++ b/dependency-check-core/src/main/java/org/owasp/dependencycheck/utils/FileUtils.java @@ -107,11 +107,6 @@ public final class FileUtils { */ public static File getTempFile(String prefix, String extension) throws IOException { final File dir = Settings.getTempDirectory(); - if (!dir.exists()) { - if (!dir.mkdirs()) { - throw new IOException("Unable to create temporary folder"); - } - } final String tempFileName = String.format("%s%s.%s", prefix, UUID.randomUUID().toString(), extension); final File tempFile = new File(dir, tempFileName); if (tempFile.exists()) { diff --git a/dependency-check-core/src/main/java/org/owasp/dependencycheck/utils/Settings.java b/dependency-check-core/src/main/java/org/owasp/dependencycheck/utils/Settings.java index 5a52c1fdb..7275ab32c 100644 --- a/dependency-check-core/src/main/java/org/owasp/dependencycheck/utils/Settings.java +++ b/dependency-check-core/src/main/java/org/owasp/dependencycheck/utils/Settings.java @@ -249,6 +249,9 @@ public final class Settings { * Cleans up resources to prevent memory leaks. */ public static void cleanup() { + if (tempDirectory != null && tempDirectory.exists()) { + FileUtils.delete(tempDirectory); + } try { localSettings.remove(); } catch (Throwable ex) { @@ -462,13 +465,27 @@ public final class Settings { return str; } + /** + * A reference to the temporary directory; used incase it needs to be deleted during cleanup. + */ + private static File tempDirectory = null; + /** * Returns the temporary directory. * * @return the temporary directory */ - public static File getTempDirectory() { - return new File(Settings.getString(Settings.KEYS.TEMP_DIRECTORY, System.getProperty("java.io.tmpdir"))); + public static File getTempDirectory() throws IOException { + File tmpDir = new File(Settings.getString(Settings.KEYS.TEMP_DIRECTORY, System.getProperty("java.io.tmpdir"))); + if (!tmpDir.exists()) { + if (!tmpDir.mkdirs()) { + final String msg = String.format("Unable to make a temporary folder '%s'", tmpDir.getPath()); + throw new IOException(msg); + } else { + tempDirectory = tmpDir; + } + } + return tmpDir; } /**