Coverage Report - org.owasp.dependencycheck.analyzer.ArchiveAnalyzer
 
Classes in this File Line Coverage Branch Coverage Complexity
ArchiveAnalyzer
34%
78/225
14%
20/136
6.412
 
 1  
 /*
 2  
  * This file is part of dependency-check-core.
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  *
 16  
  * Copyright (c) 2013 Jeremy Long. All Rights Reserved.
 17  
  */
 18  
 package org.owasp.dependencycheck.analyzer;
 19  
 
 20  
 import java.io.BufferedInputStream;
 21  
 import java.io.Closeable;
 22  
 import java.io.File;
 23  
 import java.io.FileFilter;
 24  
 import java.io.FileInputStream;
 25  
 import java.io.FileNotFoundException;
 26  
 import java.io.FileOutputStream;
 27  
 import java.io.IOException;
 28  
 import java.util.ArrayList;
 29  
 import java.util.Collections;
 30  
 import java.util.Enumeration;
 31  
 import java.util.HashSet;
 32  
 import java.util.List;
 33  
 import java.util.Set;
 34  
 
 35  
 import org.apache.commons.compress.archivers.ArchiveEntry;
 36  
 import org.apache.commons.compress.archivers.ArchiveInputStream;
 37  
 import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
 38  
 import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
 39  
 import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
 40  
 import org.apache.commons.compress.archivers.zip.ZipFile;
 41  
 import org.apache.commons.compress.compressors.CompressorInputStream;
 42  
 import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
 43  
 import org.apache.commons.compress.compressors.bzip2.BZip2Utils;
 44  
 import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
 45  
 import org.apache.commons.compress.compressors.gzip.GzipUtils;
 46  
 import org.apache.commons.compress.utils.IOUtils;
 47  
 
 48  
 import org.owasp.dependencycheck.Engine;
 49  
 import org.owasp.dependencycheck.analyzer.exception.AnalysisException;
 50  
 import org.owasp.dependencycheck.analyzer.exception.ArchiveExtractionException;
 51  
 import org.owasp.dependencycheck.dependency.Dependency;
 52  
 import org.owasp.dependencycheck.exception.InitializationException;
 53  
 import org.owasp.dependencycheck.utils.FileFilterBuilder;
 54  
 import org.owasp.dependencycheck.utils.FileUtils;
 55  
 import org.owasp.dependencycheck.utils.Settings;
 56  
 
 57  
 import org.slf4j.Logger;
 58  
 import org.slf4j.LoggerFactory;
 59  
 
 60  
 /**
 61  
  * <p>
 62  
  * An analyzer that extracts files from archives and ensures any supported files
 63  
  * contained within the archive are added to the dependency list.</p>
 64  
  *
 65  
  * @author Jeremy Long
 66  
  */
 67  6
 public class ArchiveAnalyzer extends AbstractFileTypeAnalyzer {
 68  
 
 69  
     /**
 70  
      * The logger.
 71  
      */
 72  1
     private static final Logger LOGGER = LoggerFactory.getLogger(ArchiveAnalyzer.class);
 73  
     /**
 74  
      * The count of directories created during analysis. This is used for
 75  
      * creating temporary directories.
 76  
      */
 77  1
     private static int dirCount = 0;
 78  
     /**
 79  
      * The parent directory for the individual directories per archive.
 80  
      */
 81  6
     private File tempFileLocation = null;
 82  
     /**
 83  
      * The max scan depth that the analyzer will recursively extract nested
 84  
      * archives.
 85  
      */
 86  1
     private static final int MAX_SCAN_DEPTH = Settings.getInt("archive.scan.depth", 3);
 87  
     /**
 88  
      * Tracks the current scan/extraction depth for nested archives.
 89  
      */
 90  6
     private int scanDepth = 0;
 91  
 
 92  
     //<editor-fold defaultstate="collapsed" desc="All standard implementation details of Analyzer">
 93  
     /**
 94  
      * The name of the analyzer.
 95  
      */
 96  
     private static final String ANALYZER_NAME = "Archive Analyzer";
 97  
     /**
 98  
      * The phase that this analyzer is intended to run in.
 99  
      */
 100  1
     private static final AnalysisPhase ANALYSIS_PHASE = AnalysisPhase.INITIAL;
 101  
     /**
 102  
      * The set of things we can handle with Zip methods
 103  
      */
 104  1
     private static final Set<String> ZIPPABLES = newHashSet("zip", "ear", "war", "jar", "sar", "apk", "nupkg");
 105  
     /**
 106  
      * The set of file extensions supported by this analyzer. Note for
 107  
      * developers, any additions to this list will need to be explicitly handled
 108  
      * in {@link #extractFiles(File, File, Engine)}.
 109  
      */
 110  1
     private static final Set<String> EXTENSIONS = newHashSet("tar", "gz", "tgz", "bz2", "tbz2");
 111  
 
 112  
     /**
 113  
      * Detects files with extensions to remove from the engine's collection of
 114  
      * dependencies.
 115  
      */
 116  1
     private static final FileFilter REMOVE_FROM_ANALYSIS = FileFilterBuilder.newInstance().addExtensions("zip", "tar", "gz", "tgz", "bz2", "tbz2")
 117  1
             .build();
 118  
 
 119  
     static {
 120  1
         final String additionalZipExt = Settings.getString(Settings.KEYS.ADDITIONAL_ZIP_EXTENSIONS);
 121  1
         if (additionalZipExt != null) {
 122  0
             final String[] ext = additionalZipExt.split("\\s*,\\s*");
 123  0
             Collections.addAll(ZIPPABLES, ext);
 124  
         }
 125  1
         EXTENSIONS.addAll(ZIPPABLES);
 126  
     }
 127  
 
 128  
     /**
 129  
      * The file filter used to filter supported files.
 130  
      */
 131  1
     private static final FileFilter FILTER = FileFilterBuilder.newInstance().addExtensions(EXTENSIONS).build();
 132  
 
 133  
     @Override
 134  
     protected FileFilter getFileFilter() {
 135  10
         return FILTER;
 136  
     }
 137  
 
 138  
     /**
 139  
      * Detects files with .zip extension.
 140  
      */
 141  1
     private static final FileFilter ZIP_FILTER = FileFilterBuilder.newInstance().addExtensions("zip").build();
 142  
 
 143  
     /**
 144  
      * Returns the name of the analyzer.
 145  
      *
 146  
      * @return the name of the analyzer.
 147  
      */
 148  
     @Override
 149  
     public String getName() {
 150  16
         return ANALYZER_NAME;
 151  
     }
 152  
 
 153  
     /**
 154  
      * Returns the phase that the analyzer is intended to run in.
 155  
      *
 156  
      * @return the phase that the analyzer is intended to run in.
 157  
      */
 158  
     @Override
 159  
     public AnalysisPhase getAnalysisPhase() {
 160  4
         return ANALYSIS_PHASE;
 161  
     }
 162  
     //</editor-fold>
 163  
 
 164  
     /**
 165  
      * Returns the key used in the properties file to reference the analyzer's
 166  
      * enabled property.
 167  
      *
 168  
      * @return the analyzer's enabled property setting key
 169  
      */
 170  
     @Override
 171  
     protected String getAnalyzerEnabledSettingKey() {
 172  6
         return Settings.KEYS.ANALYZER_ARCHIVE_ENABLED;
 173  
     }
 174  
 
 175  
     /**
 176  
      * The initialize method does nothing for this Analyzer.
 177  
      *
 178  
      * @throws InitializationException is thrown if there is an exception
 179  
      * deleting or creating temporary files
 180  
      */
 181  
     @Override
 182  
     public void initializeFileTypeAnalyzer() throws InitializationException {
 183  
         try {
 184  1
             final File baseDir = Settings.getTempDirectory();
 185  1
             tempFileLocation = File.createTempFile("check", "tmp", baseDir);
 186  1
             if (!tempFileLocation.delete()) {
 187  0
                 setEnabled(false);
 188  0
                 final String msg = String.format("Unable to delete temporary file '%s'.", tempFileLocation.getAbsolutePath());
 189  0
                 throw new InitializationException(msg);
 190  
             }
 191  1
             if (!tempFileLocation.mkdirs()) {
 192  0
                 setEnabled(false);
 193  0
                 final String msg = String.format("Unable to create directory '%s'.", tempFileLocation.getAbsolutePath());
 194  0
                 throw new InitializationException(msg);
 195  
             }
 196  0
         } catch (IOException ex) {
 197  0
             setEnabled(false);
 198  0
             throw new InitializationException("Unable to create a temporary file", ex);
 199  1
         }
 200  1
     }
 201  
 
 202  
     /**
 203  
      * The close method deletes any temporary files and directories created
 204  
      * during analysis.
 205  
      *
 206  
      * @throws Exception thrown if there is an exception deleting temporary
 207  
      * files
 208  
      */
 209  
     @Override
 210  
     public void close() throws Exception {
 211  2
         if (tempFileLocation != null && tempFileLocation.exists()) {
 212  1
             LOGGER.debug("Attempting to delete temporary files");
 213  1
             final boolean success = FileUtils.delete(tempFileLocation);
 214  1
             if (!success && tempFileLocation.exists()) {
 215  0
                 final String[] l = tempFileLocation.list();
 216  0
                 if (l != null && l.length > 0) {
 217  0
                     LOGGER.warn("Failed to delete some temporary files, see the log for more details");
 218  
                 }
 219  
             }
 220  
         }
 221  2
     }
 222  
 
 223  
     /**
 224  
      * Analyzes a given dependency. If the dependency is an archive, such as a
 225  
      * WAR or EAR, the contents are extracted, scanned, and added to the list of
 226  
      * dependencies within the engine.
 227  
      *
 228  
      * @param dependency the dependency to analyze
 229  
      * @param engine the engine scanning
 230  
      * @throws AnalysisException thrown if there is an analysis exception
 231  
      */
 232  
     @Override
 233  
     public void analyzeFileType(Dependency dependency, Engine engine) throws AnalysisException {
 234  2
         final File f = new File(dependency.getActualFilePath());
 235  2
         final File tmpDir = getNextTempDirectory();
 236  2
         extractFiles(f, tmpDir, engine);
 237  
 
 238  
         //make a copy
 239  2
         final Set<Dependency> dependencySet = findMoreDependencies(engine, tmpDir);
 240  2
         if (!dependencySet.isEmpty()) {
 241  0
             for (Dependency d : dependencySet) {
 242  
                 //fix the dependency's display name and path
 243  0
                 final String displayPath = String.format("%s%s",
 244  0
                         dependency.getFilePath(),
 245  0
                         d.getActualFilePath().substring(tmpDir.getAbsolutePath().length()));
 246  0
                 final String displayName = String.format("%s: %s",
 247  0
                         dependency.getFileName(),
 248  0
                         d.getFileName());
 249  0
                 d.setFilePath(displayPath);
 250  0
                 d.setFileName(displayName);
 251  
 
 252  
                 //TODO - can we get more evidence from the parent? EAR contains module name, etc.
 253  
                 //analyze the dependency (i.e. extract files) if it is a supported type.
 254  0
                 if (this.accept(d.getActualFile()) && scanDepth < MAX_SCAN_DEPTH) {
 255  0
                     scanDepth += 1;
 256  0
                     analyze(d, engine);
 257  0
                     scanDepth -= 1;
 258  
                 }
 259  0
             }
 260  
         }
 261  2
         if (REMOVE_FROM_ANALYSIS.accept(dependency.getActualFile())) {
 262  0
             addDisguisedJarsToDependencies(dependency, engine);
 263  0
             engine.getDependencies().remove(dependency);
 264  
         }
 265  2
         Collections.sort(engine.getDependencies());
 266  2
     }
 267  
 
 268  
     /**
 269  
      * If a zip file was identified as a possible JAR, this method will add the
 270  
      * zip to the list of dependencies.
 271  
      *
 272  
      * @param dependency the zip file
 273  
      * @param engine the engine
 274  
      * @throws AnalysisException thrown if there is an issue
 275  
      */
 276  
     private void addDisguisedJarsToDependencies(Dependency dependency, Engine engine) throws AnalysisException {
 277  0
         if (ZIP_FILTER.accept(dependency.getActualFile()) && isZipFileActuallyJarFile(dependency)) {
 278  0
             final File tdir = getNextTempDirectory();
 279  0
             final String fileName = dependency.getFileName();
 280  
 
 281  0
             LOGGER.info("The zip file '{}' appears to be a JAR file, making a copy and analyzing it as a JAR.", fileName);
 282  
 
 283  0
             final File tmpLoc = new File(tdir, fileName.substring(0, fileName.length() - 3) + "jar");
 284  
             try {
 285  0
                 org.apache.commons.io.FileUtils.copyFile(tdir, tmpLoc);
 286  0
                 final Set<Dependency> dependencySet = findMoreDependencies(engine, tmpLoc);
 287  0
                 if (!dependencySet.isEmpty()) {
 288  0
                     if (dependencySet.size() != 1) {
 289  0
                         LOGGER.info("Deep copy of ZIP to JAR file resulted in more than one dependency?");
 290  
                     }
 291  0
                     for (Dependency d : dependencySet) {
 292  
                         //fix the dependency's display name and path
 293  0
                         d.setFilePath(dependency.getFilePath());
 294  0
                         d.setDisplayFileName(dependency.getFileName());
 295  0
                     }
 296  
                 }
 297  0
             } catch (IOException ex) {
 298  0
                 LOGGER.debug("Unable to perform deep copy on '{}'", dependency.getActualFile().getPath(), ex);
 299  0
             }
 300  
         }
 301  0
     }
 302  
     /**
 303  
      * An empty dependency set.
 304  
      */
 305  1
     private static final Set<Dependency> EMPTY_DEPENDENCY_SET = Collections.emptySet();
 306  
 
 307  
     /**
 308  
      * Scan the given file/folder, and return any new dependencies found.
 309  
      *
 310  
      * @param engine used to scan
 311  
      * @param file target of scanning
 312  
      * @return any dependencies that weren't known to the engine before
 313  
      */
 314  
     private static Set<Dependency> findMoreDependencies(Engine engine, File file) {
 315  2
         final List<Dependency> before = new ArrayList<Dependency>(engine.getDependencies());
 316  2
         engine.scan(file);
 317  2
         final List<Dependency> after = engine.getDependencies();
 318  2
         final boolean sizeChanged = before.size() != after.size();
 319  
         final Set<Dependency> newDependencies;
 320  2
         if (sizeChanged) {
 321  
             //get the new dependencies
 322  0
             newDependencies = new HashSet<Dependency>(after);
 323  0
             newDependencies.removeAll(before);
 324  
         } else {
 325  2
             newDependencies = EMPTY_DEPENDENCY_SET;
 326  
         }
 327  2
         return newDependencies;
 328  
     }
 329  
 
 330  
     /**
 331  
      * Retrieves the next temporary directory to extract an archive too.
 332  
      *
 333  
      * @return a directory
 334  
      * @throws AnalysisException thrown if unable to create temporary directory
 335  
      */
 336  
     private File getNextTempDirectory() throws AnalysisException {
 337  2
         dirCount += 1;
 338  2
         final File directory = new File(tempFileLocation, String.valueOf(dirCount));
 339  
         //getting an exception for some directories not being able to be created; might be because the directory already exists?
 340  2
         if (directory.exists()) {
 341  0
             return getNextTempDirectory();
 342  
         }
 343  2
         if (!directory.mkdirs()) {
 344  0
             final String msg = String.format("Unable to create temp directory '%s'.", directory.getAbsolutePath());
 345  0
             throw new AnalysisException(msg);
 346  
         }
 347  2
         return directory;
 348  
     }
 349  
 
 350  
     /**
 351  
      * Extracts the contents of an archive into the specified directory.
 352  
      *
 353  
      * @param archive an archive file such as a WAR or EAR
 354  
      * @param destination a directory to extract the contents to
 355  
      * @param engine the scanning engine
 356  
      * @throws AnalysisException thrown if the archive is not found
 357  
      */
 358  
     private void extractFiles(File archive, File destination, Engine engine) throws AnalysisException {
 359  2
         if (archive != null && destination != null) {
 360  
             FileInputStream fis;
 361  
             try {
 362  2
                 fis = new FileInputStream(archive);
 363  0
             } catch (FileNotFoundException ex) {
 364  0
                 LOGGER.debug("", ex);
 365  0
                 throw new AnalysisException("Archive file was not found.", ex);
 366  2
             }
 367  2
             final String archiveExt = FileUtils.getFileExtension(archive.getName()).toLowerCase();
 368  
             try {
 369  2
                 if (ZIPPABLES.contains(archiveExt)) {
 370  2
                     final BufferedInputStream in = new BufferedInputStream(fis);
 371  2
                     ensureReadableJar(archiveExt, in);
 372  2
                     extractArchive(new ZipArchiveInputStream(in), destination, engine);
 373  2
                 } else if ("tar".equals(archiveExt)) {
 374  0
                     extractArchive(new TarArchiveInputStream(new BufferedInputStream(fis)), destination, engine);
 375  0
                 } else if ("gz".equals(archiveExt) || "tgz".equals(archiveExt)) {
 376  0
                     final String uncompressedName = GzipUtils.getUncompressedFilename(archive.getName());
 377  0
                     final File f = new File(destination, uncompressedName);
 378  0
                     if (engine.accept(f)) {
 379  0
                         decompressFile(new GzipCompressorInputStream(new BufferedInputStream(fis)), f);
 380  
                     }
 381  0
                 } else if ("bz2".equals(archiveExt) || "tbz2".equals(archiveExt)) {
 382  0
                     final String uncompressedName = BZip2Utils.getUncompressedFilename(archive.getName());
 383  0
                     final File f = new File(destination, uncompressedName);
 384  0
                     if (engine.accept(f)) {
 385  0
                         decompressFile(new BZip2CompressorInputStream(new BufferedInputStream(fis)), f);
 386  
                     }
 387  
                 }
 388  0
             } catch (ArchiveExtractionException ex) {
 389  0
                 LOGGER.warn("Exception extracting archive '{}'.", archive.getName());
 390  0
                 LOGGER.debug("", ex);
 391  0
             } catch (IOException ex) {
 392  0
                 LOGGER.warn("Exception reading archive '{}'.", archive.getName());
 393  0
                 LOGGER.debug("", ex);
 394  
             } finally {
 395  2
                 close(fis);
 396  2
             }
 397  
         }
 398  2
     }
 399  
 
 400  
     /**
 401  
      * Checks if the file being scanned is a JAR that begins with '#!/bin' which
 402  
      * indicates it is a fully executable jar. If a fully executable JAR is
 403  
      * identified the input stream will be advanced to the start of the actual
 404  
      * JAR file ( skipping the script).
 405  
      *
 406  
      * @see
 407  
      * <a href="http://docs.spring.io/spring-boot/docs/1.3.0.BUILD-SNAPSHOT/reference/htmlsingle/#deployment-install">Installing
 408  
      * Spring Boot Applications</a>
 409  
      * @param archiveExt the file extension
 410  
      * @param in the input stream
 411  
      * @throws IOException thrown if there is an error reading the stream
 412  
      */
 413  
     private void ensureReadableJar(final String archiveExt, BufferedInputStream in) throws IOException {
 414  2
         if ("jar".equals(archiveExt) && in.markSupported()) {
 415  2
             in.mark(7);
 416  2
             final byte[] b = new byte[7];
 417  2
             in.read(b);
 418  2
             if (b[0] == '#'
 419  
                     && b[1] == '!'
 420  
                     && b[2] == '/'
 421  
                     && b[3] == 'b'
 422  
                     && b[4] == 'i'
 423  
                     && b[5] == 'n'
 424  
                     && b[6] == '/') {
 425  0
                 boolean stillLooking = true;
 426  
                 int chr, nxtChr;
 427  0
                 while (stillLooking && (chr = in.read()) != -1) {
 428  0
                     if (chr == '\n' || chr == '\r') {
 429  0
                         in.mark(4);
 430  0
                         if ((chr = in.read()) != -1) {
 431  0
                             if (chr == 'P' && (chr = in.read()) != -1) {
 432  0
                                 if (chr == 'K' && (chr = in.read()) != -1) {
 433  0
                                     if ((chr == 3 || chr == 5 || chr == 7) && (nxtChr = in.read()) != -1) {
 434  0
                                         if (nxtChr == chr + 1) {
 435  0
                                             stillLooking = false;
 436  0
                                             in.reset();
 437  
                                         }
 438  
                                     }
 439  
                                 }
 440  
                             }
 441  
                         }
 442  
                     }
 443  
                 }
 444  
             }
 445  
         }
 446  2
     }
 447  
 
 448  
     /**
 449  
      * Extracts files from an archive.
 450  
      *
 451  
      * @param input the archive to extract files from
 452  
      * @param destination the location to write the files too
 453  
      * @param engine the dependency-check engine
 454  
      * @throws ArchiveExtractionException thrown if there is an exception
 455  
      * extracting files from the archive
 456  
      */
 457  
     private void extractArchive(ArchiveInputStream input, File destination, Engine engine) throws ArchiveExtractionException {
 458  
         ArchiveEntry entry;
 459  
         try {
 460  2
             while ((entry = input.getNextEntry()) != null) {
 461  0
                 final File file = new File(destination, entry.getName());
 462  0
                 if (entry.isDirectory()) {
 463  0
                     if (!file.exists() && !file.mkdirs()) {
 464  0
                         final String msg = String.format("Unable to create directory '%s'.", file.getAbsolutePath());
 465  0
                         throw new AnalysisException(msg);
 466  
                     }
 467  0
                 } else if (engine.accept(file)) {
 468  0
                     extractAcceptedFile(input, file);
 469  
                 }
 470  0
             }
 471  0
         } catch (Throwable ex) {
 472  0
             throw new ArchiveExtractionException(ex);
 473  
         } finally {
 474  2
             close(input);
 475  2
         }
 476  2
     }
 477  
 
 478  
     /**
 479  
      * Extracts a file from an archive.
 480  
      *
 481  
      * @param input the archives input stream
 482  
      * @param file the file to extract
 483  
      * @throws AnalysisException thrown if there is an error
 484  
      */
 485  
     private static void extractAcceptedFile(ArchiveInputStream input, File file) throws AnalysisException {
 486  0
         LOGGER.debug("Extracting '{}'", file.getPath());
 487  0
         FileOutputStream fos = null;
 488  
         try {
 489  0
             final File parent = file.getParentFile();
 490  0
             if (!parent.isDirectory() && !parent.mkdirs()) {
 491  0
                 final String msg = String.format("Unable to build directory '%s'.", parent.getAbsolutePath());
 492  0
                 throw new AnalysisException(msg);
 493  
             }
 494  0
             fos = new FileOutputStream(file);
 495  0
             IOUtils.copy(input, fos);
 496  0
         } catch (FileNotFoundException ex) {
 497  0
             LOGGER.debug("", ex);
 498  0
             final String msg = String.format("Unable to find file '%s'.", file.getName());
 499  0
             throw new AnalysisException(msg, ex);
 500  0
         } catch (IOException ex) {
 501  0
             LOGGER.debug("", ex);
 502  0
             final String msg = String.format("IO Exception while parsing file '%s'.", file.getName());
 503  0
             throw new AnalysisException(msg, ex);
 504  
         } finally {
 505  0
             close(fos);
 506  0
         }
 507  0
     }
 508  
 
 509  
     /**
 510  
      * Decompresses a file.
 511  
      *
 512  
      * @param inputStream the compressed file
 513  
      * @param outputFile the location to write the decompressed file
 514  
      * @throws ArchiveExtractionException thrown if there is an exception
 515  
      * decompressing the file
 516  
      */
 517  
     private void decompressFile(CompressorInputStream inputStream, File outputFile) throws ArchiveExtractionException {
 518  0
         LOGGER.debug("Decompressing '{}'", outputFile.getPath());
 519  0
         FileOutputStream out = null;
 520  
         try {
 521  0
             out = new FileOutputStream(outputFile);
 522  0
             IOUtils.copy(inputStream, out);
 523  0
         } catch (FileNotFoundException ex) {
 524  0
             LOGGER.debug("", ex);
 525  0
             throw new ArchiveExtractionException(ex);
 526  0
         } catch (IOException ex) {
 527  0
             LOGGER.debug("", ex);
 528  0
             throw new ArchiveExtractionException(ex);
 529  
         } finally {
 530  0
             close(out);
 531  0
         }
 532  0
     }
 533  
 
 534  
     /**
 535  
      * Close the given {@link Closeable} instance, ignoring nulls, and logging
 536  
      * any thrown {@link IOException}.
 537  
      *
 538  
      * @param closeable to be closed
 539  
      */
 540  
     private static void close(Closeable closeable) {
 541  4
         if (null != closeable) {
 542  
             try {
 543  4
                 closeable.close();
 544  0
             } catch (IOException ex) {
 545  0
                 LOGGER.trace("", ex);
 546  4
             }
 547  
         }
 548  4
     }
 549  
 
 550  
     /**
 551  
      * Attempts to determine if a zip file is actually a JAR file.
 552  
      *
 553  
      * @param dependency the dependency to check
 554  
      * @return true if the dependency appears to be a JAR file; otherwise false
 555  
      */
 556  
     private boolean isZipFileActuallyJarFile(Dependency dependency) {
 557  0
         boolean isJar = false;
 558  0
         ZipFile zip = null;
 559  
         try {
 560  0
             zip = new ZipFile(dependency.getActualFilePath());
 561  0
             if (zip.getEntry("META-INF/MANIFEST.MF") != null
 562  0
                     || zip.getEntry("META-INF/maven") != null) {
 563  0
                 final Enumeration<ZipArchiveEntry> entries = zip.getEntries();
 564  0
                 while (entries.hasMoreElements()) {
 565  0
                     final ZipArchiveEntry entry = entries.nextElement();
 566  0
                     if (!entry.isDirectory()) {
 567  0
                         final String name = entry.getName().toLowerCase();
 568  0
                         if (name.endsWith(".class")) {
 569  0
                             isJar = true;
 570  0
                             break;
 571  
                         }
 572  
                     }
 573  0
                 }
 574  
             }
 575  0
         } catch (IOException ex) {
 576  0
             LOGGER.debug("Unable to unzip zip file '{}'", dependency.getFilePath(), ex);
 577  
         } finally {
 578  0
             ZipFile.closeQuietly(zip);
 579  0
         }
 580  
 
 581  0
         return isJar;
 582  
     }
 583  
 }