mirror of
https://github.com/ysoftdevs/DependencyCheck.git
synced 2026-03-23 17:41:28 +01:00
removed code duplication ensuring temporary directory exists
Former-commit-id: fba6dfcd3a133378c5f46f4126fa97c02ab110be
This commit is contained in:
@@ -157,12 +157,6 @@ public class ArchiveAnalyzer extends AbstractFileTypeAnalyzer {
|
|||||||
@Override
|
@Override
|
||||||
public void initializeFileTypeAnalyzer() throws Exception {
|
public void initializeFileTypeAnalyzer() throws Exception {
|
||||||
final File baseDir = Settings.getTempDirectory();
|
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);
|
tempFileLocation = File.createTempFile("check", "tmp", baseDir);
|
||||||
if (!tempFileLocation.delete()) {
|
if (!tempFileLocation.delete()) {
|
||||||
final String msg = String.format("Unable to delete temporary file '%s'.", tempFileLocation.getAbsolutePath());
|
final String msg = String.format("Unable to delete temporary file '%s'.", tempFileLocation.getAbsolutePath());
|
||||||
|
|||||||
@@ -691,8 +691,8 @@ public class JarAnalyzer extends AbstractFileTypeAnalyzer {
|
|||||||
&& !dependency.getFileName().toLowerCase().endsWith("-src.jar")
|
&& !dependency.getFileName().toLowerCase().endsWith("-src.jar")
|
||||||
&& !dependency.getFileName().toLowerCase().endsWith("-doc.jar")) {
|
&& !dependency.getFileName().toLowerCase().endsWith("-doc.jar")) {
|
||||||
LOGGER.log(Level.INFO,
|
LOGGER.log(Level.INFO,
|
||||||
String.format("Jar file '%s' does not contain a manifest.",
|
String.format("Jar file '%s' does not contain a manifest.",
|
||||||
dependency.getFileName()));
|
dependency.getFileName()));
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -920,12 +920,6 @@ public class JarAnalyzer extends AbstractFileTypeAnalyzer {
|
|||||||
@Override
|
@Override
|
||||||
public void initializeFileTypeAnalyzer() throws Exception {
|
public void initializeFileTypeAnalyzer() throws Exception {
|
||||||
final File baseDir = Settings.getTempDirectory();
|
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);
|
tempFileLocation = File.createTempFile("check", "tmp", baseDir);
|
||||||
if (!tempFileLocation.delete()) {
|
if (!tempFileLocation.delete()) {
|
||||||
final String msg = String.format("Unable to delete temporary file '%s'.", tempFileLocation.getAbsolutePath());
|
final String msg = String.format("Unable to delete temporary file '%s'.", tempFileLocation.getAbsolutePath());
|
||||||
|
|||||||
@@ -107,11 +107,6 @@ public final class FileUtils {
|
|||||||
*/
|
*/
|
||||||
public static File getTempFile(String prefix, String extension) throws IOException {
|
public static File getTempFile(String prefix, String extension) throws IOException {
|
||||||
final File dir = Settings.getTempDirectory();
|
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 String tempFileName = String.format("%s%s.%s", prefix, UUID.randomUUID().toString(), extension);
|
||||||
final File tempFile = new File(dir, tempFileName);
|
final File tempFile = new File(dir, tempFileName);
|
||||||
if (tempFile.exists()) {
|
if (tempFile.exists()) {
|
||||||
|
|||||||
@@ -249,6 +249,9 @@ public final class Settings {
|
|||||||
* Cleans up resources to prevent memory leaks.
|
* Cleans up resources to prevent memory leaks.
|
||||||
*/
|
*/
|
||||||
public static void cleanup() {
|
public static void cleanup() {
|
||||||
|
if (tempDirectory != null && tempDirectory.exists()) {
|
||||||
|
FileUtils.delete(tempDirectory);
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
localSettings.remove();
|
localSettings.remove();
|
||||||
} catch (Throwable ex) {
|
} catch (Throwable ex) {
|
||||||
@@ -462,13 +465,27 @@ public final class Settings {
|
|||||||
return str;
|
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.
|
* Returns the temporary directory.
|
||||||
*
|
*
|
||||||
* @return the temporary directory
|
* @return the temporary directory
|
||||||
*/
|
*/
|
||||||
public static File getTempDirectory() {
|
public static File getTempDirectory() throws IOException {
|
||||||
return new File(Settings.getString(Settings.KEYS.TEMP_DIRECTORY, System.getProperty("java.io.tmpdir")));
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user