mirror of
https://github.com/ysoftdevs/DependencyCheck.git
synced 2026-01-18 09:37:38 +01:00
290: Further refactoring for readability.
This commit is contained in:
@@ -263,22 +263,28 @@ public class ArchiveAnalyzer extends AbstractFileTypeAnalyzer {
|
|||||||
|
|
||||||
private static final Set<Dependency> EMPTY_DEPENDENCY_SET = Collections.emptySet();
|
private static final Set<Dependency> EMPTY_DEPENDENCY_SET = Collections.emptySet();
|
||||||
|
|
||||||
private Set<Dependency> findMoreDependencies(Engine engine, File tmpDir) {
|
/**
|
||||||
//make a copy
|
* Scan the given file/folder, and return any new dependencies found.
|
||||||
List<Dependency> dependencies = new ArrayList<Dependency>(engine.getDependencies());
|
*
|
||||||
engine.scan(tmpDir);
|
* @param engine used to scan
|
||||||
List<Dependency> newDependencies = engine.getDependencies();
|
* @param file target of scanning
|
||||||
final boolean sizeChanged = dependencies.size() != newDependencies.size();
|
* @return any dependencies that weren't known to the engine before
|
||||||
final Set<Dependency> dependencySet;
|
*/
|
||||||
|
private static Set<Dependency> findMoreDependencies(Engine engine, File file) {
|
||||||
|
List<Dependency> before = new ArrayList<Dependency>(engine.getDependencies());
|
||||||
|
engine.scan(file);
|
||||||
|
List<Dependency> after = engine.getDependencies();
|
||||||
|
final boolean sizeChanged = before.size() != after.size();
|
||||||
|
final Set<Dependency> newDependencies;
|
||||||
if (sizeChanged) {
|
if (sizeChanged) {
|
||||||
//get the new dependencies
|
//get the new dependencies
|
||||||
dependencySet = new HashSet<Dependency>();
|
newDependencies = new HashSet<Dependency>();
|
||||||
dependencySet.addAll(newDependencies);
|
newDependencies.addAll(after);
|
||||||
dependencySet.removeAll(dependencies);
|
newDependencies.removeAll(before);
|
||||||
} else {
|
} else {
|
||||||
dependencySet = EMPTY_DEPENDENCY_SET;
|
newDependencies = EMPTY_DEPENDENCY_SET;
|
||||||
}
|
}
|
||||||
return dependencySet;
|
return newDependencies;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -311,47 +317,41 @@ public class ArchiveAnalyzer extends AbstractFileTypeAnalyzer {
|
|||||||
* @throws AnalysisException thrown if the archive is not found
|
* @throws AnalysisException thrown if the archive is not found
|
||||||
*/
|
*/
|
||||||
private void extractFiles(File archive, File destination, Engine engine) throws AnalysisException {
|
private void extractFiles(File archive, File destination, Engine engine) throws AnalysisException {
|
||||||
if (archive == null || destination == null) {
|
if (archive != null && destination != null) {
|
||||||
return;
|
FileInputStream fis;
|
||||||
}
|
|
||||||
|
|
||||||
FileInputStream fis;
|
|
||||||
try {
|
|
||||||
fis = new FileInputStream(archive);
|
|
||||||
} catch (FileNotFoundException ex) {
|
|
||||||
LOGGER.debug("", ex);
|
|
||||||
throw new AnalysisException("Archive file was not found.", ex);
|
|
||||||
}
|
|
||||||
final String archiveExt = FileUtils.getFileExtension(archive.getName()).toLowerCase();
|
|
||||||
try {
|
|
||||||
if (ZIPPABLES.contains(archiveExt)) {
|
|
||||||
extractArchive(new ZipArchiveInputStream(new BufferedInputStream(fis)), destination, engine);
|
|
||||||
} else if ("tar".equals(archiveExt)) {
|
|
||||||
extractArchive(new TarArchiveInputStream(new BufferedInputStream(fis)), destination, engine);
|
|
||||||
} else if ("gz".equals(archiveExt) || "tgz".equals(archiveExt)) {
|
|
||||||
final String uncompressedName = GzipUtils.getUncompressedFilename(archive.getName());
|
|
||||||
final File f = new File(destination, uncompressedName);
|
|
||||||
if (engine.accept(f)) {
|
|
||||||
decompressFile(new GzipCompressorInputStream(new BufferedInputStream(fis)), f);
|
|
||||||
}
|
|
||||||
} else if ("bz2".equals(archiveExt) || "tbz2".equals(archiveExt)) {
|
|
||||||
final String uncompressedName = BZip2Utils.getUncompressedFilename(archive.getName());
|
|
||||||
final File f = new File(destination, uncompressedName);
|
|
||||||
if (engine.accept(f)) {
|
|
||||||
decompressFile(new BZip2CompressorInputStream(new BufferedInputStream(fis)), f);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (ArchiveExtractionException ex) {
|
|
||||||
LOGGER.warn("Exception extracting archive '{}'.", archive.getName());
|
|
||||||
LOGGER.debug("", ex);
|
|
||||||
} catch (IOException ex) {
|
|
||||||
LOGGER.warn("Exception reading archive '{}'.", archive.getName());
|
|
||||||
LOGGER.debug("", ex);
|
|
||||||
} finally {
|
|
||||||
try {
|
try {
|
||||||
fis.close();
|
fis = new FileInputStream(archive);
|
||||||
} catch (IOException ex) {
|
} catch (FileNotFoundException ex) {
|
||||||
LOGGER.debug("", ex);
|
LOGGER.debug("", ex);
|
||||||
|
throw new AnalysisException("Archive file was not found.", ex);
|
||||||
|
}
|
||||||
|
final String archiveExt = FileUtils.getFileExtension(archive.getName()).toLowerCase();
|
||||||
|
try {
|
||||||
|
if (ZIPPABLES.contains(archiveExt)) {
|
||||||
|
extractArchive(new ZipArchiveInputStream(new BufferedInputStream(fis)), destination, engine);
|
||||||
|
} else if ("tar".equals(archiveExt)) {
|
||||||
|
extractArchive(new TarArchiveInputStream(new BufferedInputStream(fis)), destination, engine);
|
||||||
|
} else if ("gz".equals(archiveExt) || "tgz".equals(archiveExt)) {
|
||||||
|
final String uncompressedName = GzipUtils.getUncompressedFilename(archive.getName());
|
||||||
|
final File f = new File(destination, uncompressedName);
|
||||||
|
if (engine.accept(f)) {
|
||||||
|
decompressFile(new GzipCompressorInputStream(new BufferedInputStream(fis)), f);
|
||||||
|
}
|
||||||
|
} else if ("bz2".equals(archiveExt) || "tbz2".equals(archiveExt)) {
|
||||||
|
final String uncompressedName = BZip2Utils.getUncompressedFilename(archive.getName());
|
||||||
|
final File f = new File(destination, uncompressedName);
|
||||||
|
if (engine.accept(f)) {
|
||||||
|
decompressFile(new BZip2CompressorInputStream(new BufferedInputStream(fis)), f);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (ArchiveExtractionException ex) {
|
||||||
|
LOGGER.warn("Exception extracting archive '{}'.", archive.getName());
|
||||||
|
LOGGER.debug("", ex);
|
||||||
|
} catch (IOException ex) {
|
||||||
|
LOGGER.warn("Exception reading archive '{}'.", archive.getName());
|
||||||
|
LOGGER.debug("", ex);
|
||||||
|
} finally {
|
||||||
|
close(fis);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -368,75 +368,54 @@ public class ArchiveAnalyzer extends AbstractFileTypeAnalyzer {
|
|||||||
ArchiveEntry entry;
|
ArchiveEntry entry;
|
||||||
try {
|
try {
|
||||||
while ((entry = input.getNextEntry()) != null) {
|
while ((entry = input.getNextEntry()) != null) {
|
||||||
|
final File file = new File(destination, entry.getName());
|
||||||
if (entry.isDirectory()) {
|
if (entry.isDirectory()) {
|
||||||
final File d = new File(destination, entry.getName());
|
if (!file.exists() && !file.mkdirs()) {
|
||||||
if (!d.exists()) {
|
final String msg = String.format("Unable to create directory '%s'.", file.getAbsolutePath());
|
||||||
if (!d.mkdirs()) {
|
throw new AnalysisException(msg);
|
||||||
final String msg = String.format("Unable to create directory '%s'.", d.getAbsolutePath());
|
|
||||||
throw new AnalysisException(msg);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
final File file = new File(destination, entry.getName());
|
|
||||||
if (engine.accept(file)) {
|
|
||||||
LOGGER.debug("Extracting '{}'", file.getPath());
|
|
||||||
BufferedOutputStream bos = null;
|
|
||||||
FileOutputStream fos = null;
|
|
||||||
try {
|
|
||||||
final File parent = file.getParentFile();
|
|
||||||
if (!parent.isDirectory()) {
|
|
||||||
if (!parent.mkdirs()) {
|
|
||||||
final String msg = String.format("Unable to build directory '%s'.", parent.getAbsolutePath());
|
|
||||||
throw new AnalysisException(msg);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fos = new FileOutputStream(file);
|
|
||||||
bos = new BufferedOutputStream(fos, BUFFER_SIZE);
|
|
||||||
int count;
|
|
||||||
final byte[] data = new byte[BUFFER_SIZE];
|
|
||||||
while ((count = input.read(data, 0, BUFFER_SIZE)) != -1) {
|
|
||||||
bos.write(data, 0, count);
|
|
||||||
}
|
|
||||||
bos.flush();
|
|
||||||
} catch (FileNotFoundException ex) {
|
|
||||||
LOGGER.debug("", ex);
|
|
||||||
final String msg = String.format("Unable to find file '%s'.", file.getName());
|
|
||||||
throw new AnalysisException(msg, ex);
|
|
||||||
} catch (IOException ex) {
|
|
||||||
LOGGER.debug("", ex);
|
|
||||||
final String msg = String.format("IO Exception while parsing file '%s'.", file.getName());
|
|
||||||
throw new AnalysisException(msg, ex);
|
|
||||||
} finally {
|
|
||||||
if (bos != null) {
|
|
||||||
try {
|
|
||||||
bos.close();
|
|
||||||
} catch (IOException ex) {
|
|
||||||
LOGGER.trace("", ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (fos != null) {
|
|
||||||
try {
|
|
||||||
fos.close();
|
|
||||||
} catch (IOException ex) {
|
|
||||||
LOGGER.trace("", ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
} else if (engine.accept(file)) {
|
||||||
|
extractAcceptedFile(input, file);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (IOException ex) {
|
|
||||||
throw new ArchiveExtractionException(ex);
|
|
||||||
} catch (Throwable ex) {
|
} catch (Throwable ex) {
|
||||||
throw new ArchiveExtractionException(ex);
|
throw new ArchiveExtractionException(ex);
|
||||||
} finally {
|
} finally {
|
||||||
if (input != null) {
|
close(input);
|
||||||
try {
|
}
|
||||||
input.close();
|
}
|
||||||
} catch (IOException ex) {
|
|
||||||
LOGGER.trace("", ex);
|
private static void extractAcceptedFile(ArchiveInputStream input, File file) throws AnalysisException {
|
||||||
|
LOGGER.debug("Extracting '{}'", file.getPath());
|
||||||
|
BufferedOutputStream bos = null;
|
||||||
|
FileOutputStream fos = null;
|
||||||
|
try {
|
||||||
|
final File parent = file.getParentFile();
|
||||||
|
if (!parent.isDirectory()) {
|
||||||
|
if (!parent.mkdirs()) {
|
||||||
|
final String msg = String.format("Unable to build directory '%s'.", parent.getAbsolutePath());
|
||||||
|
throw new AnalysisException(msg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
fos = new FileOutputStream(file);
|
||||||
|
bos = new BufferedOutputStream(fos, BUFFER_SIZE);
|
||||||
|
int count;
|
||||||
|
final byte[] data = new byte[BUFFER_SIZE];
|
||||||
|
while ((count = input.read(data, 0, BUFFER_SIZE)) != -1) {
|
||||||
|
bos.write(data, 0, count);
|
||||||
|
}
|
||||||
|
bos.flush();
|
||||||
|
} catch (FileNotFoundException ex) {
|
||||||
|
LOGGER.debug("", ex);
|
||||||
|
final String msg = String.format("Unable to find file '%s'.", file.getName());
|
||||||
|
throw new AnalysisException(msg, ex);
|
||||||
|
} catch (IOException ex) {
|
||||||
|
LOGGER.debug("", ex);
|
||||||
|
final String msg = String.format("IO Exception while parsing file '%s'.", file.getName());
|
||||||
|
throw new AnalysisException(msg, ex);
|
||||||
|
} finally {
|
||||||
|
close(bos);
|
||||||
|
close(fos);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -464,12 +443,21 @@ public class ArchiveAnalyzer extends AbstractFileTypeAnalyzer {
|
|||||||
LOGGER.debug("", ex);
|
LOGGER.debug("", ex);
|
||||||
throw new ArchiveExtractionException(ex);
|
throw new ArchiveExtractionException(ex);
|
||||||
} finally {
|
} finally {
|
||||||
if (out != null) {
|
close(out);
|
||||||
try {
|
}
|
||||||
out.close();
|
}
|
||||||
} catch (IOException ex) {
|
|
||||||
LOGGER.trace("", ex);
|
/**
|
||||||
}
|
* Close the given {@link Closeable} instance, ignoring nulls, and logging any thrown {@link IOException}.
|
||||||
|
*
|
||||||
|
* @param closeable to be closed
|
||||||
|
*/
|
||||||
|
private static void close(Closeable closeable){
|
||||||
|
if (null != closeable) {
|
||||||
|
try {
|
||||||
|
closeable.close();
|
||||||
|
} catch (IOException ex) {
|
||||||
|
LOGGER.trace("", ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user