001 /*
002 * SonarQube, open source software quality management tool.
003 * Copyright (C) 2008-2014 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * SonarQube is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * SonarQube is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public License
017 * along with this program; if not, write to the Free Software Foundation,
018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
019 */
020 package org.sonar.batch.scan2;
021
022 import org.slf4j.Logger;
023 import org.slf4j.LoggerFactory;
024 import org.sonar.api.BatchComponent;
025 import org.sonar.api.batch.fs.FilePredicate;
026 import org.sonar.api.batch.fs.FileSystem;
027 import org.sonar.api.batch.fs.InputFile;
028 import org.sonar.api.batch.rule.ActiveRules;
029 import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor;
030
031 public class AnalyzerOptimizer implements BatchComponent {
032
033 private static final Logger LOG = LoggerFactory.getLogger(AnalyzerOptimizer.class);
034
035 private final FileSystem fs;
036 private final ActiveRules activeRules;
037
038 public AnalyzerOptimizer(FileSystem fs, ActiveRules activeRules) {
039 this.fs = fs;
040 this.activeRules = activeRules;
041 }
042
043 /**
044 * Decide if the given Analyzer should be executed.
045 */
046 public boolean shouldExecute(DefaultSensorDescriptor descriptor) {
047 if (!fsCondition(descriptor)) {
048 LOG.debug("'{}' skipped because there is no related file in current project", descriptor.name());
049 return false;
050 }
051 if (!activeRulesCondition(descriptor)) {
052 LOG.debug("'{}' skipped because there is no related rule activated in the quality profile", descriptor.name());
053 return false;
054 }
055 return true;
056 }
057
058 private boolean activeRulesCondition(DefaultSensorDescriptor descriptor) {
059 if (!descriptor.ruleRepositories().isEmpty()) {
060 for (String repoKey : descriptor.ruleRepositories()) {
061 if (!activeRules.findByRepository(repoKey).isEmpty()) {
062 return true;
063 }
064 }
065 return false;
066 }
067 return true;
068 }
069
070 private boolean fsCondition(DefaultSensorDescriptor descriptor) {
071 if (!descriptor.languages().isEmpty() || !descriptor.types().isEmpty()) {
072 FilePredicate langPredicate = descriptor.languages().isEmpty() ? fs.predicates().all() : fs.predicates().hasLanguages(descriptor.languages());
073
074 FilePredicate typePredicate = descriptor.types().isEmpty() ? fs.predicates().all() : fs.predicates().none();
075 for (InputFile.Type type : descriptor.types()) {
076 typePredicate = fs.predicates().or(
077 typePredicate,
078 fs.predicates().hasType(type));
079 }
080 return fs.hasFiles(fs.predicates().and(langPredicate, typePredicate));
081 }
082 return true;
083 }
084
085 }