Coverage Report - org.owasp.dependencycheck.maven.Engine
 
Classes in this File Line Coverage Branch Coverage Complexity
Engine
0%
0/43
0%
0/24
2.75
 
 1  
 /*
 2  
  * This file is part of dependency-check-maven.
 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) 2014 Jeremy Long. All Rights Reserved.
 17  
  */
 18  
 package org.owasp.dependencycheck.maven;
 19  
 
 20  
 import java.util.logging.Logger;
 21  
 import org.apache.maven.project.MavenProject;
 22  
 import org.owasp.dependencycheck.analyzer.Analyzer;
 23  
 import org.owasp.dependencycheck.analyzer.CPEAnalyzer;
 24  
 import org.owasp.dependencycheck.data.nvdcve.DatabaseException;
 25  
 import org.owasp.dependencycheck.utils.Settings;
 26  
 
 27  
 /**
 28  
  * A modified version of the core engine specifically designed to persist some data between multiple executions of a
 29  
  * multi-module Maven project.
 30  
  *
 31  
  * @author Jeremy Long <jeremy.long@owasp.org>
 32  
  */
 33  
 public class Engine extends org.owasp.dependencycheck.Engine {
 34  
 
 35  
     /**
 36  
      * The logger.
 37  
      */
 38  0
     private static final transient Logger LOGGER = Logger.getLogger(Engine.class.getName());
 39  
     /**
 40  
      * A key used to persist an object in the MavenProject.
 41  
      */
 42  
     private static final String CPE_ANALYZER_KEY = "dependency-check-CPEAnalyzer";
 43  
     /**
 44  
      * The current MavenProject.
 45  
      */
 46  
     private MavenProject currentProject;
 47  
 
 48  
     /**
 49  
      * Creates a new Engine to perform anyalsis on dependencies.
 50  
      *
 51  
      * @param project the current Maven project
 52  
      * @throws DatabaseException thrown if there is an issue connecting to the database
 53  
      */
 54  0
     public Engine(MavenProject project) throws DatabaseException {
 55  0
         this.currentProject = project;
 56  0
         final MavenProject parent = getRootParent();
 57  0
         if (parent != null && parent.getContextValue("dependency-check-data-was-updated") != null) {
 58  0
             System.setProperty(Settings.KEYS.AUTO_UPDATE, Boolean.FALSE.toString());
 59  
         }
 60  0
         initializeEngine();
 61  0
         if (parent != null) {
 62  0
             parent.setContextValue("dependency-check-data-was-updated", Boolean.valueOf(true));
 63  
         }
 64  0
     }
 65  
 
 66  
     /**
 67  
      * This constructor should not be called. Use Engine(MavenProject) instead.
 68  
      *
 69  
      * @throws DatabaseException thrown if there is an issue connecting to the database
 70  
      */
 71  0
     private Engine() throws DatabaseException {
 72  0
     }
 73  
 
 74  
     /**
 75  
      * Initializes the given analyzer. This skips the initialization of the CPEAnalyzer if it has been initialized by a
 76  
      * previous execution.
 77  
      *
 78  
      * @param analyzer the analyzer to initialize
 79  
      * @return the initialized analyzer
 80  
      */
 81  
     @Override
 82  
     protected Analyzer initializeAnalyzer(Analyzer analyzer) {
 83  0
         if ((analyzer instanceof CPEAnalyzer)) {
 84  0
             CPEAnalyzer cpe = getPreviouslyLoadedAnalyzer();
 85  0
             if (cpe != null) {
 86  0
                 return cpe;
 87  
             }
 88  0
             cpe = (CPEAnalyzer) super.initializeAnalyzer(analyzer);
 89  0
             storeCPEAnalyzer(cpe);
 90  
         }
 91  0
         return super.initializeAnalyzer(analyzer);
 92  
     }
 93  
 
 94  
     /**
 95  
      * Closes the given analyzer. This skips closing the CPEAnalyzer.
 96  
      *
 97  
      * @param analyzer the analyzer to close
 98  
      */
 99  
     @Override
 100  
     protected void closeAnalyzer(Analyzer analyzer) {
 101  0
         if ((analyzer instanceof CPEAnalyzer)) {
 102  0
             if (getPreviouslyLoadedAnalyzer() == null) {
 103  0
                 super.closeAnalyzer(analyzer);
 104  
             }
 105  
         } else {
 106  0
             super.closeAnalyzer(analyzer);
 107  
         }
 108  0
     }
 109  
 
 110  
     /**
 111  
      * Closes the CPEAnalyzer if it has been created and persisted in the root parent MavenProject context.
 112  
      */
 113  
     public void cleanupFinal() {
 114  0
         final CPEAnalyzer cpe = getPreviouslyLoadedAnalyzer();
 115  0
         if (cpe != null) {
 116  0
             cpe.close();
 117  
         }
 118  0
     }
 119  
 
 120  
     /**
 121  
      * Gets the CPEAnalyzer from the root Maven Project.
 122  
      *
 123  
      * @return an initialized CPEAnalyzer
 124  
      */
 125  
     private CPEAnalyzer getPreviouslyLoadedAnalyzer() {
 126  0
         CPEAnalyzer cpe = null;
 127  0
         final MavenProject project = getRootParent();
 128  0
         if (project != null) {
 129  0
             cpe = (CPEAnalyzer) project.getContextValue(CPE_ANALYZER_KEY);
 130  
         }
 131  0
         return cpe;
 132  
     }
 133  
 
 134  
     /**
 135  
      * Stores a CPEAnalyzer in the root Maven Project.
 136  
      *
 137  
      * @param cpe the CPEAnalyzer to store
 138  
      */
 139  
     private void storeCPEAnalyzer(CPEAnalyzer cpe) {
 140  0
         final MavenProject p = getRootParent();
 141  0
         if (p != null) {
 142  0
             p.setContextValue(CPE_ANALYZER_KEY, cpe);
 143  
         }
 144  0
     }
 145  
 
 146  
     /**
 147  
      * Returns the root Maven Project.
 148  
      *
 149  
      * @return the root Maven Project
 150  
      */
 151  
     private MavenProject getRootParent() {
 152  0
         if (this.currentProject == null) {
 153  0
             return null;
 154  
         }
 155  0
         MavenProject p = this.currentProject;
 156  0
         while (p.getParent() != null) {
 157  0
             p = p.getParent();
 158  
         }
 159  0
         return p;
 160  
     }
 161  
 }