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.batch.bootstrap;
021
022 import org.slf4j.Logger;
023 import org.slf4j.LoggerFactory;
024 import org.sonar.api.batch.BatchExtensionDictionnary;
025 import org.sonar.api.batch.bootstrap.ProjectDefinition;
026 import org.sonar.api.profiles.RulesProfile;
027 import org.sonar.api.resources.Languages;
028 import org.sonar.api.resources.Project;
029 import org.sonar.api.utils.IocContainer;
030 import org.sonar.batch.*;
031 import org.sonar.batch.components.TimeMachineConfiguration;
032 import org.sonar.batch.config.ProjectSettings;
033 import org.sonar.batch.config.UnsupportedProperties;
034 import org.sonar.batch.events.EventBus;
035 import org.sonar.batch.index.DefaultIndex;
036 import org.sonar.batch.index.DefaultResourcePersister;
037 import org.sonar.batch.phases.Phases;
038 import org.sonar.batch.phases.PhasesTimeProfiler;
039 import org.sonar.core.qualitymodel.DefaultModelFinder;
040 import org.sonar.jpa.dao.ProfilesDao;
041 import org.sonar.jpa.dao.RulesDao;
042
043 public class ProjectModule extends Module {
044 private static final Logger LOG = LoggerFactory.getLogger(ProjectModule.class);
045 private Project project;
046 private boolean dryRun;
047
048 public ProjectModule(Project project, boolean dryRun) {
049 this.project = project;
050 this.dryRun = dryRun;
051 }
052
053 @Override
054 protected void configure() {
055 logSettings();
056 addCoreComponents();
057 addProjectComponents();
058 addProjectPluginExtensions();
059 }
060
061
062 private void addProjectComponents() {
063 ProjectDefinition projectDefinition = getComponentByType(ProjectTree.class).getProjectDefinition(project);
064 addCoreSingleton(projectDefinition);
065 addCoreSingleton(project);
066 addCoreSingleton(project.getConfiguration());
067 addCoreSingleton(ProjectSettings.class);
068 addCoreSingleton(UnsupportedProperties.class);
069 addCoreSingleton(IocContainer.class);
070
071 for (Object component : projectDefinition.getContainerExtensions()) {
072 addCoreSingleton(component);
073 }
074 addCoreSingleton(Languages.class);
075 addCoreSingleton(DefaultProjectClasspath.class);
076 addCoreSingleton(DefaultProjectFileSystem2.class);
077 addCoreSingleton(RulesDao.class);
078
079 if (!dryRun) {
080 // the Snapshot component will be removed when asynchronous measures are improved (required for AsynchronousMeasureSensor)
081 addCoreSingleton(getComponentByType(DefaultResourcePersister.class).getSnapshot(project));
082 }
083 addCoreSingleton(TimeMachineConfiguration.class);
084 addCoreSingleton(org.sonar.api.database.daos.MeasuresDao.class);
085 addCoreSingleton(ProfilesDao.class);
086 addCoreSingleton(DefaultSensorContext.class);
087 addCoreSingleton(BatchExtensionDictionnary.class);
088 addCoreSingleton(DefaultTimeMachine.class);
089 addCoreSingleton(ViolationFilters.class);
090 addCoreSingleton(ResourceFilters.class);
091 addCoreSingleton(DefaultModelFinder.class);
092 addCoreSingleton(DefaultProfileLoader.class);
093 addAdapter(new ProfileProvider());
094 }
095
096 private void addCoreComponents() {
097 addCoreSingleton(EventBus.class);
098 addCoreSingleton(Phases.class);
099 addCoreSingleton(PhasesTimeProfiler.class);
100 for (Class clazz : Phases.getPhaseClasses(dryRun)) {
101 addCoreSingleton(clazz);
102 }
103 }
104
105 private void addProjectPluginExtensions() {
106 addCoreSingleton(ProjectExtensionInstaller.class);
107 ProjectExtensionInstaller installer = getComponentByType(ProjectExtensionInstaller.class);
108 installer.install(this);
109 }
110
111
112 private void logSettings() {
113 // TODO move these logs in a dedicated component
114 LOG.info("------------- Analyzing {}", project.getName());
115 }
116
117 /**
118 * Analyze project
119 */
120 @Override
121 protected void doStart() {
122 DefaultIndex index = getComponentByType(DefaultIndex.class);
123 index.setCurrentProject(project,
124 getComponentByType(ResourceFilters.class),
125 getComponentByType(ViolationFilters.class),
126 getComponentByType(RulesProfile.class));
127
128 getComponentByType(Phases.class).execute(project);
129 }
130 }