001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2009 SonarSource SA
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;
021    
022    import org.apache.commons.configuration.*;
023    import org.apache.maven.execution.MavenSession;
024    import org.picocontainer.Characteristics;
025    import org.picocontainer.MutablePicoContainer;
026    import org.slf4j.Logger;
027    import org.slf4j.LoggerFactory;
028    import org.sonar.api.database.DatabaseSessionProvider;
029    import org.sonar.api.database.DriverDatabaseConnector;
030    import org.sonar.api.database.ThreadLocalDatabaseSessionFactory;
031    import org.sonar.api.resources.Project;
032    import org.sonar.api.utils.IocContainer;
033    import org.sonar.batch.indexer.DefaultSonarIndex;
034    
035    import java.io.IOException;
036    
037    public class AggregatorBatch {
038    
039      private static final Logger LOG = LoggerFactory.getLogger(AggregatorBatch.class);
040    
041      private MutablePicoContainer topContainer;
042    
043      public AggregatorBatch(MavenSession mavenSession) {
044        this.topContainer = createContainer(mavenSession);
045      }
046    
047      private static MutablePicoContainer createContainer(MavenSession mavenSession) {
048        MutablePicoContainer container = IocContainer.buildPicoContainer();
049        container.as(Characteristics.CACHE).addComponent(getStartupConfiguration(mavenSession));
050        container.as(Characteristics.CACHE).addComponent(DriverDatabaseConnector.class);
051        container.as(Characteristics.CACHE).addComponent(ThreadLocalDatabaseSessionFactory.class);
052        container.addAdapter(new DatabaseSessionProvider());
053        container.as(Characteristics.CACHE).addComponent(mavenSession);
054        return container;
055      }
056    
057      public AggregatorBatch register(Object component) {
058        topContainer.as(Characteristics.CACHE).addComponent(component);
059        return this;
060      }
061    
062      private static Configuration getStartupConfiguration(MavenSession session) {
063        CompositeConfiguration configuration = new CompositeConfiguration();
064        configuration.addConfiguration(new SystemConfiguration());
065        configuration.addConfiguration(new EnvironmentConfiguration());
066        configuration.addConfiguration(new MapConfiguration(session.getCurrentProject().getModel().getProperties()));
067        return configuration;
068      }
069    
070    
071      public void execute() throws IOException {
072        try {
073          topContainer.start();
074          analyzeProjects();
075    
076        } finally {
077          topContainer.stop();
078        }
079      }
080    
081      private void analyzeProjects() throws IOException {
082        MutablePicoContainer batchContainer = topContainer.makeChildContainer();
083        batchContainer.as(Characteristics.CACHE).addComponent(ProjectTree.class);
084        batchContainer.as(Characteristics.CACHE).addComponent(DefaultSonarIndex.class);
085        batchContainer.start();
086    
087        ProjectTree projectTree = batchContainer.getComponent(ProjectTree.class);
088        DefaultSonarIndex index = batchContainer.getComponent(DefaultSonarIndex.class);
089    
090        analyzeProject(batchContainer, index, projectTree.getRootProject());
091      }
092    
093      private void analyzeProject(MutablePicoContainer batchContainer, DefaultSonarIndex index, Project project) {
094        for (Project module : project.getModules()) {
095          analyzeProject(batchContainer, index, module);
096        }
097        LOG.info("-------------  Analyzing " + project.getName());
098        new ProjectBatch(batchContainer).execute(index, project);
099      }
100    }