001 /*
002 * SonarQube, open source software quality management tool.
003 * Copyright (C) 2008-2014 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * SonarQube 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 * SonarQube 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 License
017 * along with this program; if not, write to the Free Software Foundation,
018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
019 */
020 package org.sonar.batch;
021
022 import org.apache.commons.configuration.PropertiesConfiguration;
023 import org.apache.commons.lang.StringUtils;
024 import org.apache.maven.project.MavenProject;
025 import org.slf4j.Logger;
026 import org.slf4j.LoggerFactory;
027 import org.sonar.api.BatchComponent;
028 import org.sonar.api.CoreProperties;
029 import org.sonar.api.batch.bootstrap.ProjectDefinition;
030 import org.sonar.api.config.Settings;
031 import org.sonar.api.database.DatabaseSession;
032 import org.sonar.api.database.model.ResourceModel;
033 import org.sonar.api.database.model.Snapshot;
034 import org.sonar.api.resources.Project;
035 import org.sonar.api.utils.DateUtils;
036 import org.sonar.api.utils.SonarException;
037
038 import java.util.Date;
039
040 public class ProjectConfigurator implements BatchComponent {
041
042 private static final Logger LOG = LoggerFactory.getLogger(ProjectConfigurator.class);
043
044 private DatabaseSession databaseSession;
045 private Settings settings;
046
047 public ProjectConfigurator(DatabaseSession databaseSession, Settings settings) {
048 this.databaseSession = databaseSession;
049 this.settings = settings;
050 }
051
052 public Project create(ProjectDefinition definition) {
053 Project project = new Project(definition.getKey(), loadProjectBranch(), definition.getName());
054
055 // For backward compatibility we must set POM and actual packaging
056 project.setDescription(StringUtils.defaultString(definition.getDescription()));
057 project.setPackaging("jar");
058
059 for (Object component : definition.getContainerExtensions()) {
060 if (component instanceof MavenProject) {
061 MavenProject pom = (MavenProject) component;
062 project.setPom(pom);
063 project.setPackaging(pom.getPackaging());
064 }
065 }
066 return project;
067 }
068
069 private String loadProjectBranch() {
070 return settings.getString(CoreProperties.PROJECT_BRANCH_PROPERTY);
071 }
072
073 public ProjectConfigurator configure(Project project) {
074 Date analysisDate = loadAnalysisDate();
075 checkCurrentAnalysisIsTheLatestOne(project.getKey(), analysisDate);
076
077 project
078 // will be populated by ProjectSettings
079 .setConfiguration(new PropertiesConfiguration())
080 .setAnalysisDate(analysisDate)
081 .setAnalysisVersion(loadAnalysisVersion())
082 .setAnalysisType(loadAnalysisType());
083 return this;
084 }
085
086 private void checkCurrentAnalysisIsTheLatestOne(String projectKey, Date analysisDate) {
087 ResourceModel persistedProject = databaseSession.getSingleResult(ResourceModel.class, "key", projectKey, "enabled", true);
088 if (persistedProject != null) {
089 Snapshot lastSnapshot = databaseSession.getSingleResult(Snapshot.class, "resourceId", persistedProject.getId(), "last", true);
090 if (lastSnapshot != null && !lastSnapshot.getCreatedAt().before(analysisDate)) {
091 throw new IllegalArgumentException(
092 "'sonar.projectDate' property cannot be older than the date of the last known quality snapshot on this project. Value: '"+
093 settings.getString(CoreProperties.PROJECT_DATE_PROPERTY) + "'. " +
094 "Latest quality snapshot: '"+ DateUtils.formatDate(lastSnapshot.getCreatedAt()) +"'. This property may only be used to rebuild the past in a chronological order."
095 );
096 }
097 }
098
099 }
100
101 private Date loadAnalysisDate() {
102 Date date;
103 try {
104 // sonar.projectDate may have been specified as a time
105 date = settings.getDateTime(CoreProperties.PROJECT_DATE_PROPERTY);
106 } catch (SonarException e) {
107 // this is probably just a date
108 date = settings.getDate(CoreProperties.PROJECT_DATE_PROPERTY);
109 }
110 if (date == null) {
111 date = new Date();
112 settings.setProperty(CoreProperties.PROJECT_DATE_PROPERTY, date, true);
113 }
114 return date;
115 }
116
117 private Project.AnalysisType loadAnalysisType() {
118 String value = settings.getString(CoreProperties.DYNAMIC_ANALYSIS_PROPERTY);
119 if (value == null) {
120 return Project.AnalysisType.DYNAMIC;
121 }
122
123 LOG.warn("'sonar.dynamicAnalysis' is deprecated since version 4.3 and should no longer be used.");
124 if ("true".equals(value)) {
125 return Project.AnalysisType.DYNAMIC;
126 }
127 if ("reuseReports".equals(value)) {
128 return Project.AnalysisType.REUSE_REPORTS;
129 }
130 return Project.AnalysisType.STATIC;
131 }
132
133 private String loadAnalysisVersion() {
134 return settings.getString(CoreProperties.PROJECT_VERSION_PROPERTY);
135 }
136 }