001 /*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2008-2011 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * Sonar 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 * Sonar 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
017 * License along with Sonar; if not, write to the Free Software
018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
019 */
020 package org.sonar.plugins.cpd;
021
022 import org.apache.commons.configuration.Configuration;
023 import org.apache.commons.lang.StringUtils;
024 import org.slf4j.LoggerFactory;
025 import org.sonar.api.CoreProperties;
026 import org.sonar.api.batch.Sensor;
027 import org.sonar.api.batch.SensorContext;
028 import org.sonar.api.resources.Project;
029 import org.sonar.api.utils.Logs;
030
031 public class CpdSensor implements Sensor {
032
033 private CpdEngine sonarEngine;
034 private CpdEngine pmdEngine;
035
036 public CpdSensor(SonarEngine sonarEngine, PmdEngine pmdEngine) {
037 this.sonarEngine = sonarEngine;
038 this.pmdEngine = pmdEngine;
039 }
040
041 public boolean shouldExecuteOnProject(Project project) {
042 if (isSkipped(project)) {
043 LoggerFactory.getLogger(getClass()).info("Detection of duplicated code is skipped");
044 return false;
045 }
046
047 if (!getEngine(project).isLanguageSupported(project.getLanguage())) {
048 LoggerFactory.getLogger(getClass()).info("Detection of duplication code is not supported for {}.", project.getLanguage());
049 return false;
050 }
051
052 return true;
053 }
054
055 private CpdEngine getEngine(Project project) {
056 if (isSonarEngineEnabled(project)) {
057 if (sonarEngine.isLanguageSupported(project.getLanguage())) {
058 return sonarEngine;
059 } else {
060 // fallback to PMD
061 return pmdEngine;
062 }
063 } else {
064 return pmdEngine;
065 }
066 }
067
068 boolean isSonarEngineEnabled(Project project) {
069 Configuration conf = project.getConfiguration();
070 return StringUtils.equalsIgnoreCase(conf.getString(CoreProperties.CPD_ENGINE, CoreProperties.CPD_ENGINE_DEFAULT_VALUE), "sonar");
071 }
072
073 boolean isSkipped(Project project) {
074 Configuration conf = project.getConfiguration();
075 return conf.getBoolean("sonar.cpd." + project.getLanguageKey() + ".skip",
076 conf.getBoolean(CoreProperties.CPD_SKIP_PROPERTY, false));
077 }
078
079 public void analyse(Project project, SensorContext context) {
080 CpdEngine engine = getEngine(project);
081 Logs.INFO.info("{} is used", engine);
082 engine.analyse(project, context);
083 }
084
085 @Override
086 public String toString() {
087 return getClass().getSimpleName();
088 }
089 }