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.batch.bootstrap; 021 022import org.apache.commons.lang.ArrayUtils; 023import org.apache.commons.lang.StringUtils; 024import org.sonar.api.config.Settings; 025import org.sonar.api.resources.Project; 026 027/** 028 * Filter projects to analyze by using the properties sonar.skippedModules and sonar.includedModules 029 * 030 * @since 2.12 031 */ 032public class ProjectFilter { 033 034 private Settings settings; 035 036 public ProjectFilter(Settings settings) { 037 this.settings = settings; 038 } 039 040 public boolean isExcluded(Project project) { 041 Project p = project; 042 while (p != null) { 043 if (isExcludedModule(getArtifactId(p), p.isRoot())) { 044 return true; 045 } 046 p = p.getParent(); 047 } 048 return false; 049 } 050 051 private boolean isExcludedModule(String artifactId, boolean isRoot) { 052 String[] includedArtifactIds = settings.getStringArray("sonar.includedModules"); 053 boolean excluded = false; 054 if (!isRoot && includedArtifactIds.length > 0) { 055 excluded = !ArrayUtils.contains(includedArtifactIds, artifactId); 056 } 057 if (!excluded) { 058 String[] excludedArtifactIds = settings.getStringArray("sonar.skippedModules"); 059 excluded = ArrayUtils.contains(excludedArtifactIds, artifactId); 060 } 061 if (excluded && isRoot) { 062 throw new IllegalArgumentException("The root module can't be skipped. Please check the parameter sonar.skippedModules."); 063 } 064 return excluded; 065 } 066 067 // TODO see http://jira.codehaus.org/browse/SONAR-2324 068 static String getArtifactId(Project project) { 069 String key = project.getKey(); 070 if (StringUtils.isNotBlank(project.getBranch())) { 071 // remove branch part 072 key = StringUtils.removeEnd(project.getKey(), ":" + project.getBranch()); 073 } 074 if (key.contains(":")) { 075 return StringUtils.substringAfter(key, ":"); 076 } 077 return key; 078 } 079}