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.plugins.cobertura;
021    
022    import org.apache.commons.configuration.Configuration;
023    import org.sonar.api.CoreProperties;
024    import org.sonar.api.batch.CoverageExtension;
025    import org.sonar.api.batch.Initializer;
026    import org.sonar.api.batch.maven.DependsUponMavenPlugin;
027    import org.sonar.api.batch.maven.MavenPlugin;
028    import org.sonar.api.batch.maven.MavenPluginHandler;
029    import org.sonar.api.resources.Project;
030    import org.sonar.plugins.cobertura.api.CoberturaUtils;
031    
032    /**
033     * Provides {@link CoberturaMavenPluginHandler} and configures correct path to report.
034     * Enabled only in Maven environment.
035     */
036    public class CoberturaMavenInitializer extends Initializer implements CoverageExtension, DependsUponMavenPlugin {
037    
038      private CoberturaMavenPluginHandler handler;
039    
040      public CoberturaMavenInitializer(CoberturaMavenPluginHandler handler) {
041        this.handler = handler;
042      }
043    
044      public MavenPluginHandler getMavenPluginHandler(Project project) {
045        if (project.getAnalysisType().equals(Project.AnalysisType.DYNAMIC)) {
046          return handler;
047        }
048        return null;
049      }
050    
051      @Override
052      public boolean shouldExecuteOnProject(Project project) {
053        return project.getAnalysisType().isDynamic(true) &&
054            project.getFileSystem().hasJavaSourceFiles();
055      }
056    
057      @Override
058      public void execute(Project project) {
059        Configuration conf = project.getConfiguration();
060        if (!conf.containsKey(CoreProperties.COBERTURA_REPORT_PATH_PROPERTY)) {
061          String report = getReportPathFromPluginConfiguration(project);
062          if (report == null) {
063            report = getDefaultReportPath(project);
064          }
065          conf.setProperty(CoreProperties.COBERTURA_REPORT_PATH_PROPERTY, report);
066        }
067      }
068    
069      private static String getDefaultReportPath(Project project) {
070        return project.getFileSystem().getReportOutputDir() + "/cobertura/coverage.xml";
071      }
072    
073      private static String getReportPathFromPluginConfiguration(Project project) {
074        MavenPlugin mavenPlugin = MavenPlugin.getPlugin(
075            project.getPom(),
076            CoberturaUtils.COBERTURA_GROUP_ID,
077            CoberturaUtils.COBERTURA_ARTIFACT_ID);
078        if (mavenPlugin != null) {
079          String path = mavenPlugin.getParameter("outputDirectory");
080          if (path != null) {
081            return path + "/coverage.xml";
082          }
083        }
084        return null;
085      }
086    
087    }