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.sonar.api.Plugins;
023    import org.sonar.api.measures.CoreMetrics;
024    import org.sonar.api.measures.Metric;
025    import org.sonar.api.resources.Project;
026    import org.sonar.api.resources.ResourceTypes;
027    import org.sonar.batch.DefaultFileLinesContextFactory;
028    import org.sonar.batch.DefaultResourceCreationLock;
029    import org.sonar.batch.ProjectConfigurator;
030    import org.sonar.batch.ProjectTree;
031    import org.sonar.batch.components.*;
032    import org.sonar.batch.index.*;
033    import org.sonar.core.metric.CacheMetricFinder;
034    import org.sonar.core.notification.DefaultNotificationManager;
035    import org.sonar.core.rule.CacheRuleFinder;
036    import org.sonar.core.user.DefaultUserFinder;
037    import org.sonar.jpa.dao.MeasuresDao;
038    
039    /**
040     * Level-2 components. Connected to database.
041     */
042    public class BatchModule extends Module {
043    
044      private final boolean dryRun;
045    
046      public BatchModule(boolean dryRun) {
047        this.dryRun = dryRun;
048      }
049    
050      @Override
051      protected void configure() {
052        addCoreSingleton(ProjectTree.class);
053        addCoreSingleton(ProjectFilter.class);
054        addCoreSingleton(ProjectConfigurator.class);
055        addCoreSingleton(DefaultResourceCreationLock.class);
056        addCoreSingleton(DefaultIndex.class);
057        addCoreSingleton(DefaultFileLinesContextFactory.class);
058    
059        if (dryRun) {
060          addCoreSingleton(ReadOnlyPersistenceManager.class);
061        } else {
062          addCoreSingleton(DefaultPersistenceManager.class);
063          addCoreSingleton(DependencyPersister.class);
064          addCoreSingleton(EventPersister.class);
065          addCoreSingleton(LinkPersister.class);
066          addCoreSingleton(MeasurePersister.class);
067          addCoreSingleton(MemoryOptimizer.class);
068          addCoreSingleton(DefaultResourcePersister.class);
069          addCoreSingleton(SourcePersister.class);
070        }
071    
072        addCoreSingleton(Plugins.class);
073        addCoreSingleton(MeasuresDao.class);
074        addCoreSingleton(CacheRuleFinder.class);
075        addCoreSingleton(CacheMetricFinder.class);
076        addCoreSingleton(PastSnapshotFinderByDate.class);
077        addCoreSingleton(PastSnapshotFinderByDays.class);
078        addCoreSingleton(PastSnapshotFinderByPreviousAnalysis.class);
079        addCoreSingleton(PastSnapshotFinderByVersion.class);
080        addCoreSingleton(PastSnapshotFinderByPreviousVersion.class);
081        addCoreSingleton(PastMeasuresLoader.class);
082        addCoreSingleton(PastSnapshotFinder.class);
083        addCoreSingleton(DefaultNotificationManager.class);
084        addCoreSingleton(DefaultUserFinder.class);
085        addCoreSingleton(ResourceTypes.class);
086        addCoreMetrics();
087        addBatchExtensions();
088      }
089    
090      private void addBatchExtensions() {
091        BatchExtensionInstaller installer = getComponentByType(BatchExtensionInstaller.class);
092        installer.install(this);
093      }
094    
095      void addCoreMetrics() {
096        for (Metric metric : CoreMetrics.getMetrics()) {
097          addCoreSingleton(metric);
098        }
099      }
100    
101      @Override
102      protected void doStart() {
103        ProjectTree projectTree = getComponentByType(ProjectTree.class);
104        analyze(projectTree.getRootProject());
105      }
106    
107      private void analyze(Project project) {
108        for (Project subProject : project.getModules()) {
109          analyze(subProject);
110        }
111    
112        Module projectComponents = installChild(new ProjectModule(project, dryRun));
113        try {
114          projectComponents.start();
115        } finally {
116          projectComponents.stop();
117          uninstallChild();
118        }
119      }
120    }