Coverage Report - org.owasp.dependencycheck.analyzer.AbstractAnalyzer
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractAnalyzer
88%
22/25
83%
5/6
1.4
 
 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) 2012 Jeremy Long. All Rights Reserved.
 17  
  */
 18  
 package org.owasp.dependencycheck.analyzer;
 19  
 
 20  
 import org.owasp.dependencycheck.Engine;
 21  
 import org.owasp.dependencycheck.analyzer.exception.AnalysisException;
 22  
 import org.owasp.dependencycheck.dependency.Dependency;
 23  
 import org.owasp.dependencycheck.exception.InitializationException;
 24  
 import org.owasp.dependencycheck.utils.InvalidSettingException;
 25  
 import org.owasp.dependencycheck.utils.Settings;
 26  
 import org.slf4j.Logger;
 27  
 import org.slf4j.LoggerFactory;
 28  
 
 29  
 /**
 30  
  * Base class for analyzers to avoid code duplication of initialize and close as
 31  
  * most analyzers do not need these methods.
 32  
  *
 33  
  * @author Jeremy Long
 34  
  */
 35  325
 public abstract class AbstractAnalyzer implements Analyzer {
 36  
 
 37  
     /**
 38  
      * The logger.
 39  
      */
 40  1
     private static final Logger LOGGER = LoggerFactory.getLogger(AbstractAnalyzer.class);
 41  
     /**
 42  
      * A flag indicating whether or not the analyzer is enabled.
 43  
      */
 44  325
     private volatile boolean enabled = true;
 45  
 
 46  
     /**
 47  
      * Get the value of enabled.
 48  
      *
 49  
      * @return the value of enabled
 50  
      */
 51  
     @Override
 52  
     public boolean isEnabled() {
 53  14137
         return enabled;
 54  
     }
 55  
 
 56  
     /**
 57  
      * Set the value of enabled.
 58  
      *
 59  
      * @param enabled new value of enabled
 60  
      */
 61  
     public void setEnabled(boolean enabled) {
 62  167
         this.enabled = enabled;
 63  167
     }
 64  
 
 65  
     /**
 66  
      * <p>
 67  
      * Returns the setting key to determine if the analyzer is enabled.</p>
 68  
      *
 69  
      * @return the key for the analyzer's enabled property
 70  
      */
 71  
     protected abstract String getAnalyzerEnabledSettingKey();
 72  
 
 73  
     /**
 74  
      * Analyzes a given dependency. If the dependency is an archive, such as a
 75  
      * WAR or EAR, the contents are extracted, scanned, and added to the list of
 76  
      * dependencies within the engine.
 77  
      *
 78  
      * @param dependency the dependency to analyze
 79  
      * @param engine the engine scanning
 80  
      * @throws AnalysisException thrown if there is an analysis exception
 81  
      */
 82  
     protected abstract void analyzeDependency(Dependency dependency, Engine engine) throws AnalysisException;
 83  
 
 84  
     /**
 85  
      * Initializes a given Analyzer. This will be skipped if the analyzer is disabled.
 86  
      *
 87  
      * @throws InitializationException thrown if there is an exception
 88  
      */
 89  
     protected void initializeAnalyzer() throws InitializationException {
 90  11
     }
 91  
 
 92  
     /**
 93  
      * Closes a given Analyzer. This will be skipped if the analyzer is disabled.
 94  
      *
 95  
      * @throws Exception thrown if there is an exception
 96  
      */
 97  
     protected void closeAnalyzer() throws Exception {
 98  64
     }
 99  
 
 100  
 
 101  
     /**
 102  
      * Analyzes a given dependency. If the dependency is an archive, such as a
 103  
      * WAR or EAR, the contents are extracted, scanned, and added to the list of
 104  
      * dependencies within the engine.
 105  
      *
 106  
      * @param dependency the dependency to analyze
 107  
      * @param engine the engine scanning
 108  
      * @throws AnalysisException thrown if there is an analysis exception
 109  
      */
 110  
     @Override
 111  
     public final void analyze(Dependency dependency, Engine engine) throws AnalysisException {
 112  76
         if (this.isEnabled()) {
 113  76
             analyzeDependency(dependency, engine);
 114  
         }
 115  76
     }
 116  
 
 117  
     /**
 118  
      * The initialize method does nothing for this Analyzer.
 119  
      *
 120  
      * @throws InitializationException thrown if there is an exception
 121  
      */
 122  
     @Override
 123  
     public final void initialize() throws InitializationException {
 124  126
         final String key = getAnalyzerEnabledSettingKey();
 125  
         try {
 126  126
             this.setEnabled(Settings.getBoolean(key, true));
 127  0
         } catch (InvalidSettingException ex) {
 128  0
             LOGGER.warn("Invalid setting for property '{}'", key);
 129  0
             LOGGER.debug("", ex);
 130  126
         }
 131  
 
 132  126
         if (isEnabled()) {
 133  122
             initializeAnalyzer();
 134  
         } else {
 135  4
             LOGGER.debug("{} has been disabled", getName());
 136  
         }
 137  119
     }
 138  
 
 139  
     /**
 140  
      * The close method does nothing for this Analyzer.
 141  
      *
 142  
      * @throws Exception thrown if there is an exception
 143  
      */
 144  
     @Override
 145  
     public final void close() throws Exception {
 146  121
         if (isEnabled()) {
 147  83
             closeAnalyzer();
 148  
         }
 149  121
     }
 150  
 
 151  
 
 152  
     /**
 153  
      * The default is to support parallel processing.
 154  
      *
 155  
      * @return true
 156  
      */
 157  
     @Override
 158  
     public boolean supportsParallelProcessing() {
 159  17
         return true;
 160  
     }
 161  
 }