001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2008-2011 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.phases;
021    
022    import com.google.common.collect.Lists;
023    import org.sonar.api.batch.SensorContext;
024    import org.sonar.api.resources.Project;
025    import org.sonar.batch.events.EventBus;
026    import org.sonar.batch.index.DefaultIndex;
027    import org.sonar.batch.index.PersistenceManager;
028    
029    import java.util.Collection;
030    import java.util.List;
031    
032    public final class Phases {
033    
034      public static Collection<Class> getPhaseClasses(boolean dryRun) {
035        List<Class> classes = Lists.<Class>newArrayList(DecoratorsExecutor.class, MavenPhaseExecutor.class, MavenPluginsConfigurator.class,
036            PostJobsExecutor.class, SensorsExecutor.class,
037            InitializersExecutor.class);
038        if (!dryRun) {
039          classes.add(UpdateStatusJob.class);
040        }
041        return classes;
042      }
043    
044      private EventBus eventBus;
045      private DecoratorsExecutor decoratorsExecutor;
046      private MavenPhaseExecutor mavenPhaseExecutor;
047      private MavenPluginsConfigurator mavenPluginsConfigurator;
048      private PostJobsExecutor postJobsExecutor;
049      private InitializersExecutor initializersExecutor;
050      private SensorsExecutor sensorsExecutor;
051      private UpdateStatusJob updateStatusJob;
052      private PersistenceManager persistenceManager;
053      private SensorContext sensorContext;
054      private DefaultIndex index;
055    
056      public Phases(DecoratorsExecutor decoratorsExecutor, MavenPhaseExecutor mavenPhaseExecutor,
057                    MavenPluginsConfigurator mavenPluginsConfigurator, InitializersExecutor initializersExecutor,
058                    PostJobsExecutor postJobsExecutor, SensorsExecutor sensorsExecutor,
059                    PersistenceManager persistenceManager, SensorContext sensorContext, DefaultIndex index,
060                    EventBus eventBus, UpdateStatusJob updateStatusJob) {
061        this.decoratorsExecutor = decoratorsExecutor;
062        this.mavenPhaseExecutor = mavenPhaseExecutor;
063        this.mavenPluginsConfigurator = mavenPluginsConfigurator;
064        this.postJobsExecutor = postJobsExecutor;
065        this.initializersExecutor = initializersExecutor;
066        this.sensorsExecutor = sensorsExecutor;
067        this.persistenceManager = persistenceManager;
068        this.sensorContext = sensorContext;
069        this.index = index;
070        this.eventBus = eventBus;
071        this.updateStatusJob = updateStatusJob;
072      }
073    
074      public Phases(DecoratorsExecutor decoratorsExecutor, MavenPhaseExecutor mavenPhaseExecutor,
075                    MavenPluginsConfigurator mavenPluginsConfigurator, InitializersExecutor initializersExecutor,
076                    PostJobsExecutor postJobsExecutor, SensorsExecutor sensorsExecutor,
077                    PersistenceManager persistenceManager, SensorContext sensorContext, DefaultIndex index,
078                    EventBus eventBus) {
079        this(decoratorsExecutor, mavenPhaseExecutor, mavenPluginsConfigurator, initializersExecutor, postJobsExecutor,
080            sensorsExecutor, persistenceManager, sensorContext, index, eventBus, null);
081      }
082    
083      /**
084       * Executed on each module
085       */
086      public void execute(Project project) {
087        eventBus.fireEvent(new ProjectAnalysisEvent(project, true));
088        mavenPluginsConfigurator.execute(project);
089        mavenPhaseExecutor.execute(project);
090        initializersExecutor.execute();
091    
092        persistenceManager.setDelayedMode(true);
093        sensorsExecutor.execute(sensorContext);
094        decoratorsExecutor.execute();
095        persistenceManager.dump();
096        persistenceManager.setDelayedMode(false);
097    
098        if (project.isRoot()) {
099          if (updateStatusJob != null) {
100            updateStatusJob.execute();
101          }
102          postJobsExecutor.execute(sensorContext);
103        }
104        cleanMemory();
105        eventBus.fireEvent(new ProjectAnalysisEvent(project, false));
106      }
107    
108      private void cleanMemory() {
109        persistenceManager.clear();
110        index.clear();
111      }
112    }