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