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 */ 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 private CpdEngine sonarBridgeEngine; 036 037 public CpdSensor(SonarEngine sonarEngine, PmdEngine pmdEngine) { 038 this.sonarEngine = sonarEngine; 039 this.pmdEngine = pmdEngine; 040 } 041 042 public CpdSensor(SonarEngine sonarEngine, PmdEngine pmdEngine, SonarBridgeEngine sonarBridgeEngine) { 043 this.sonarEngine = sonarEngine; 044 this.pmdEngine = pmdEngine; 045 this.sonarBridgeEngine = sonarBridgeEngine; 046 } 047 048 public boolean shouldExecuteOnProject(Project project) { 049 if (isSkipped(project)) { 050 LoggerFactory.getLogger(getClass()).info("Detection of duplicated code is skipped"); 051 return false; 052 } 053 054 if (!getEngine(project).isLanguageSupported(project.getLanguage())) { 055 LoggerFactory.getLogger(getClass()).info("Detection of duplication code is not supported for {}.", project.getLanguage()); 056 return false; 057 } 058 059 return true; 060 } 061 062 private CpdEngine getEngine(Project project) { 063 if (isEngineEnabled(project, "sonar")) { 064 if (sonarEngine.isLanguageSupported(project.getLanguage())) { 065 return sonarEngine; 066 } 067 // falback to bridge 068 } else if (isEngineEnabled(project, "pmd")) { 069 return pmdEngine; 070 } 071 return sonarBridgeEngine; 072 } 073 074 boolean isEngineEnabled(Project project, String engineName) { 075 Configuration conf = project.getConfiguration(); 076 return StringUtils.equalsIgnoreCase(conf.getString(CoreProperties.CPD_ENGINE, CoreProperties.CPD_ENGINE_DEFAULT_VALUE), engineName); 077 } 078 079 boolean isSonarEngineEnabled(Project project) { 080 return isEngineEnabled(project, "sonar"); 081 } 082 083 boolean isSkipped(Project project) { 084 Configuration conf = project.getConfiguration(); 085 return conf.getBoolean("sonar.cpd." + project.getLanguageKey() + ".skip", 086 conf.getBoolean(CoreProperties.CPD_SKIP_PROPERTY, false)); 087 } 088 089 public void analyse(Project project, SensorContext context) { 090 CpdEngine engine = getEngine(project); 091 Logs.INFO.info("{} is used", engine); 092 engine.analyse(project, context); 093 } 094 095 @Override 096 public String toString() { 097 return getClass().getSimpleName(); 098 } 099 }