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