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.core.sensors;
021    
022    import org.apache.commons.lang.StringUtils;
023    import org.apache.maven.model.CiManagement;
024    import org.apache.maven.model.IssueManagement;
025    import org.apache.maven.model.Scm;
026    import org.apache.maven.project.MavenProject;
027    import org.sonar.api.batch.Sensor;
028    import org.sonar.api.batch.SensorContext;
029    import org.sonar.api.batch.SupportedEnvironment;
030    import org.sonar.api.resources.Project;
031    import org.sonar.api.resources.ProjectLink;
032    
033    @SupportedEnvironment("maven")
034    public class ProjectLinksSensor implements Sensor {
035    
036      public static final String KEY_HOME = "homepage";
037      public static final String KEY_CONTINUOUS_INTEGRATION = "ci";
038      public static final String KEY_ISSUE_TRACKER = "issue";
039      public static final String KEY_SCM = "scm";
040      public static final String KEY_SCM_DEVELOPER_CONNECTION = "scm_dev";
041    
042      private MavenProject pom;
043    
044      public ProjectLinksSensor(MavenProject pom) {
045        this.pom = pom;
046      }
047    
048      public boolean shouldExecuteOnProject(Project project) {
049        return project.isLatestAnalysis();
050      }
051    
052      public void analyse(Project project, SensorContext context) {
053        updateLink(context, KEY_HOME, "Home", pom.getUrl());
054    
055        Scm scm = pom.getScm();
056        if (scm == null) {
057          scm = new Scm();
058        }
059        updateLink(context, KEY_SCM, "Sources", scm.getUrl());
060        updateLink(context, KEY_SCM_DEVELOPER_CONNECTION, "Developer connection", scm.getDeveloperConnection());
061    
062        CiManagement ci = pom.getCiManagement();
063        if (ci == null) {
064          ci = new CiManagement();
065        }
066        updateLink(context, KEY_CONTINUOUS_INTEGRATION, "Continuous integration", ci.getUrl());
067    
068        IssueManagement issues = pom.getIssueManagement();
069        if (issues == null) {
070          issues = new IssueManagement();
071        }
072        updateLink(context, KEY_ISSUE_TRACKER, "Issues", issues.getUrl());
073      }
074    
075      private void updateLink(SensorContext context, String key, String name, String url) {
076        if (StringUtils.isBlank(url)) {
077          context.deleteLink(key);
078        } else {
079          context.saveLink(new ProjectLink(key, name, url));
080        }
081      }
082    
083      @Override
084      public String toString() {
085        return getClass().getSimpleName();
086      }
087    }