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;
021
022 import com.google.common.collect.Lists;
023 import com.google.common.collect.Maps;
024 import org.apache.commons.lang.ObjectUtils;
025 import org.sonar.api.batch.bootstrap.ProjectDefinition;
026 import org.sonar.api.batch.bootstrap.ProjectReactor;
027 import org.sonar.api.resources.Project;
028 import org.sonar.batch.scan.ProjectReactorReady;
029
030 import java.util.List;
031 import java.util.Map;
032
033 public class DefaultProjectTree implements ProjectTree {
034
035 private final ProjectConfigurator configurator;
036 private ProjectReactor projectReactor;
037
038 private List<Project> projects;
039 private Map<ProjectDefinition, Project> projectsByDef;
040
041 public DefaultProjectTree(ProjectReactor projectReactor,
042 ProjectConfigurator projectConfigurator,
043 ProjectReactorReady reactorReady) {
044 this.projectReactor = projectReactor;
045 this.configurator = projectConfigurator;
046 }
047
048 public void start() {
049 doStart(projectReactor.getProjects());
050 }
051
052 void doStart(List<ProjectDefinition> definitions) {
053 projects = Lists.newArrayList();
054 projectsByDef = Maps.newHashMap();
055
056 for (ProjectDefinition def : definitions) {
057 Project project = configurator.create(def);
058 projectsByDef.put(def, project);
059 projects.add(project);
060 }
061
062 for (Map.Entry<ProjectDefinition, Project> entry : projectsByDef.entrySet()) {
063 ProjectDefinition def = entry.getKey();
064 Project project = entry.getValue();
065 for (ProjectDefinition module : def.getSubProjects()) {
066 projectsByDef.get(module).setParent(project);
067 }
068 }
069
070 // Configure
071 for (Project project : projects) {
072 configurator.configure(project);
073 }
074 }
075
076 public List<Project> getProjects() {
077 return projects;
078 }
079
080 public Project getRootProject() {
081 for (Project project : projects) {
082 if (project.getParent() == null) {
083 return project;
084 }
085 }
086 throw new IllegalStateException("Can not find the root project from the list of Maven modules");
087 }
088
089 public ProjectDefinition getProjectDefinition(Project project) {
090 for (Map.Entry<ProjectDefinition, Project> entry : projectsByDef.entrySet()) {
091 if (ObjectUtils.equals(entry.getValue(), project)) {
092 return entry.getKey();
093 }
094 }
095 throw new IllegalStateException("Can not find ProjectDefinition for " + project);
096 }
097 }