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