org.owasp.dependencycheck.analyzer.AnalyzerService Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dependency-check-core Show documentation
Show all versions of dependency-check-core Show documentation
dependency-check-core is the engine and reporting tool used to identify and report if there are any known, publicly disclosed vulnerabilities in the scanned project's dependencies. The engine extracts meta-data from the dependencies and uses this to do fuzzy key-word matching against the Common Platfrom Enumeration (CPE), if any CPE identifiers are found the associated Common Vulnerability and Exposure (CVE) entries are added to the generated report.
/*
* This file is part of dependency-check-core.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright (c) 2012 Jeremy Long. All Rights Reserved.
*/
package org.owasp.dependencycheck.analyzer;
import java.util.ArrayList;
import org.slf4j.LoggerFactory;
import static java.util.Arrays.asList;
import java.util.Iterator;
import java.util.List;
import java.util.ServiceLoader;
import javax.annotation.concurrent.ThreadSafe;
import org.owasp.dependencycheck.utils.InvalidSettingException;
import org.owasp.dependencycheck.utils.Settings;
/**
* The Analyzer Service Loader. This class loads all services that implement
* {@link org.owasp.dependencycheck.analyzer.Analyzer}.
*
* @author Jeremy Long
*/
@ThreadSafe
public class AnalyzerService {
/**
* The Logger for use throughout the class.
*/
private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(AnalyzerService.class);
/**
* The service loader for analyzers.
*/
private final ServiceLoader service;
/**
* The configured settings.
*/
private final Settings settings;
/**
* Creates a new instance of AnalyzerService.
*
* @param classLoader the ClassLoader to use when dynamically loading
* Analyzer and Update services
* @param settings the configured settings
*/
public AnalyzerService(ClassLoader classLoader, Settings settings) {
service = ServiceLoader.load(Analyzer.class, classLoader);
this.settings = settings;
}
/**
* Returns a list of all instances of the Analyzer interface.
*
* @return a list of Analyzers.
*/
public List getAnalyzers() {
return getAnalyzers(AnalysisPhase.values());
}
/**
* Returns a list of all instances of the Analyzer interface that are bound
* to one of the given phases.
*
* @param phases the phases to obtain analyzers for
* @return a list of Analyzers.
*/
public List getAnalyzers(AnalysisPhase... phases) {
return getAnalyzers(asList(phases));
}
/**
* Returns a list of all instances of the Analyzer interface that are bound
* to one of the given phases.
*
* @param phases the phases to obtain analyzers for
* @return a list of Analyzers
*/
public List getAnalyzers(List phases) {
final List analyzers = new ArrayList<>();
final Iterator iterator = service.iterator();
boolean experimentalEnabled = false;
boolean retiredEnabled = false;
try {
experimentalEnabled = settings.getBoolean(Settings.KEYS.ANALYZER_EXPERIMENTAL_ENABLED, false);
retiredEnabled = settings.getBoolean(Settings.KEYS.ANALYZER_RETIRED_ENABLED, false);
} catch (InvalidSettingException ex) {
LOGGER.error("invalid experimental or retired setting", ex);
}
while (iterator.hasNext()) {
final Analyzer a = iterator.next();
if (!phases.contains(a.getAnalysisPhase())) {
continue;
}
if (!experimentalEnabled && a.getClass().isAnnotationPresent(Experimental.class)) {
continue;
}
if (!retiredEnabled && a.getClass().isAnnotationPresent(Retired.class)) {
continue;
}
LOGGER.debug("Loaded Analyzer {}", a.getName());
analyzers.add(a);
}
return analyzers;
}
}