001/* 002 * Sonar, open source software quality management tool. 003 * Copyright (C) 2008-2012 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 */ 020package org.sonar.plugins.cpd; 021 022import com.google.common.annotations.VisibleForTesting; 023import org.apache.commons.configuration.Configuration; 024import org.slf4j.Logger; 025import org.slf4j.LoggerFactory; 026import org.sonar.api.CoreProperties; 027import org.sonar.api.batch.Sensor; 028import org.sonar.api.batch.SensorContext; 029import org.sonar.api.resources.Project; 030 031public class CpdSensor implements Sensor { 032 033 private static final Logger LOG = LoggerFactory.getLogger(CpdSensor.class); 034 035 private CpdEngine sonarEngine; 036 private CpdEngine sonarBridgeEngine; 037 038 public CpdSensor(SonarEngine sonarEngine, SonarBridgeEngine sonarBridgeEngine) { 039 this.sonarEngine = sonarEngine; 040 this.sonarBridgeEngine = sonarBridgeEngine; 041 } 042 043 public boolean shouldExecuteOnProject(Project project) { 044 if (isSkipped(project)) { 045 LOG.info("Detection of duplicated code is skipped"); 046 return false; 047 } 048 049 if (!getEngine(project).isLanguageSupported(project.getLanguage())) { 050 LOG.info("Detection of duplicated code is not supported for {}.", project.getLanguage()); 051 return false; 052 } 053 054 return true; 055 } 056 057 @VisibleForTesting 058 CpdEngine getEngine(Project project) { 059 if (sonarEngine.isLanguageSupported(project.getLanguage())) { 060 return sonarEngine; 061 } else { 062 return sonarBridgeEngine; 063 } 064 } 065 066 @VisibleForTesting 067 boolean isSkipped(Project project) { 068 Configuration conf = project.getConfiguration(); 069 return conf.getBoolean("sonar.cpd." + project.getLanguageKey() + ".skip", 070 conf.getBoolean(CoreProperties.CPD_SKIP_PROPERTY, false)); 071 } 072 073 public void analyse(Project project, SensorContext context) { 074 CpdEngine engine = getEngine(project); 075 LOG.info("{} is used", engine); 076 engine.analyse(project, context); 077 } 078 079 @Override 080 public String toString() { 081 return getClass().getSimpleName(); 082 } 083 084}