View Javadoc
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 java.util.ArrayList;
21  import java.util.Iterator;
22  import java.util.List;
23  import java.util.ServiceLoader;
24  import org.owasp.dependencycheck.utils.InvalidSettingException;
25  import org.owasp.dependencycheck.utils.Settings;
26  import org.slf4j.LoggerFactory;
27  
28  /**
29   * The Analyzer Service Loader. This class loads all services that implement
30   * org.owasp.dependencycheck.analyzer.Analyzer.
31   *
32   * @author Jeremy Long
33   */
34  public class AnalyzerService {
35      /**
36       * The Logger for use throughout the class.
37       */
38      private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(AnalyzerService.class);
39  
40      /**
41       * The service loader for analyzers.
42       */
43      private final ServiceLoader<Analyzer> service;
44  
45      /**
46       * Creates a new instance of AnalyzerService.
47       *
48       * @param classLoader the ClassLoader to use when dynamically loading Analyzer and Update services
49       */
50      public AnalyzerService(ClassLoader classLoader) {
51          service = ServiceLoader.load(Analyzer.class, classLoader);
52      }
53  
54      /**
55       * Returns a list of all instances of the Analyzer interface.
56       *
57       * @return a list of Analyzers.
58       */
59      public List<Analyzer> getAnalyzers() {
60          final List<Analyzer> analyzers = new ArrayList<Analyzer>();
61          final Iterator<Analyzer> iterator = service.iterator();
62          boolean experimentalEnabled = false;
63          try {
64              experimentalEnabled = Settings.getBoolean(Settings.KEYS.ANALYZER_EXPERIMENTAL_ENABLED, false);
65          } catch (InvalidSettingException ex) {
66              LOGGER.error("invalide experimental setting", ex);
67          }
68          while (iterator.hasNext()) {
69              final Analyzer a = iterator.next();
70              if (!experimentalEnabled && a.getClass().isAnnotationPresent(Experimental.class)) {
71                  continue;
72              }
73              LOGGER.debug("Loaded Analyzer {}", a.getName());
74              analyzers.add(a);
75          }
76          return analyzers;
77      }
78  }