summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@gmail.com>2012-09-28 11:23:39 +0200
committerSimon Brandhof <simon.brandhof@gmail.com>2012-09-28 11:24:33 +0200
commit67f0a5eb565ecefbcd5d0ed836012bbb61196bea (patch)
tree174a62f624bfa3ce3df64a2c472541954ebab4b0
parent8cd5a9d5523f1efa5f0ff3cfa6d196590f856845 (diff)
downloadsonarqube-67f0a5eb565ecefbcd5d0ed836012bbb61196bea.tar.gz
sonarqube-67f0a5eb565ecefbcd5d0ed836012bbb61196bea.zip
SONAR-2602 persist project language before execution of extensions
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchModule.java2
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/bootstrap/ProjectInitializer.java73
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/bootstrap/ProjectModule.java16
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/config/ProjectSettings.java11
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/database/model/ResourceModel.java42
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/resources/Language.java2
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/resources/Languages.java2
7 files changed, 105 insertions, 43 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchModule.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchModule.java
index b598f81a9ad..645907de581 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchModule.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchModule.java
@@ -22,6 +22,7 @@ package org.sonar.batch.bootstrap;
import org.sonar.api.Plugins;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.measures.Metric;
+import org.sonar.api.resources.Languages;
import org.sonar.api.resources.Project;
import org.sonar.api.resources.ResourceTypes;
import org.sonar.batch.DefaultFileLinesContextFactory;
@@ -83,6 +84,7 @@ public class BatchModule extends Module {
addCoreSingleton(DefaultNotificationManager.class);
addCoreSingleton(DefaultUserFinder.class);
addCoreSingleton(ResourceTypes.class);
+ addCoreSingleton(Languages.class);
addCoreMetrics();
addBatchExtensions();
}
diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ProjectInitializer.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ProjectInitializer.java
new file mode 100644
index 00000000000..f6ee4c48af9
--- /dev/null
+++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ProjectInitializer.java
@@ -0,0 +1,73 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.batch.bootstrap;
+
+import org.apache.commons.lang.StringUtils;
+import org.sonar.api.BatchComponent;
+import org.sonar.api.resources.*;
+import org.sonar.api.utils.SonarException;
+import org.sonar.batch.config.ProjectSettings;
+import org.sonar.core.resource.ResourceDao;
+import org.sonar.core.resource.ResourceDto;
+
+/**
+ * Should be dropped when org.sonar.api.resources.Project is fully refactored.
+ */
+public class ProjectInitializer implements BatchComponent {
+
+ private ResourceDao resourceDao;
+ private DryRun dryRun;
+ private ProjectFileSystem fileSystem;
+ private Languages languages;
+
+ public ProjectInitializer(ResourceDao resourceDao, DryRun dryRun, ProjectFileSystem fileSystem, Languages languages) {
+ this.resourceDao = resourceDao;
+ this.dryRun = dryRun;
+ this.fileSystem = fileSystem;
+ this.languages = languages;
+ }
+
+ public void execute(Project project, ProjectSettings settings) {
+ initLanguage(project, settings);
+ initFileSystem(project);
+ }
+
+ private void initLanguage(Project project, ProjectSettings settings) {
+ project.setLanguageKey(StringUtils.defaultIfBlank(settings.getString("sonar.language"), Java.KEY));
+ Language language = languages.get(project.getLanguageKey());
+ if (language == null) {
+ throw new SonarException("Language with key '" + project.getLanguageKey() + "' not found");
+ }
+ project.setLanguage(language);
+ if (!dryRun.isEnabled() && project.getId() != null) {
+ ResourceDto dto = resourceDao.getResource(project.getId());
+ dto.setLanguage(project.getLanguageKey());
+ resourceDao.insertOrUpdate(dto);
+ }
+
+ }
+
+ private void initFileSystem(Project project) {
+ // TODO See http://jira.codehaus.org/browse/SONAR-2126
+ // previously MavenProjectBuilder was responsible for creation of ProjectFileSystem
+ project.setFileSystem(fileSystem);
+
+ }
+}
diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ProjectModule.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ProjectModule.java
index 390bba94cf5..09cc6ce69f7 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ProjectModule.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ProjectModule.java
@@ -24,12 +24,8 @@ import org.slf4j.LoggerFactory;
import org.sonar.api.batch.BatchExtensionDictionnary;
import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.api.profiles.RulesProfile;
-import org.sonar.api.resources.Language;
-import org.sonar.api.resources.Languages;
import org.sonar.api.resources.Project;
-import org.sonar.api.resources.ProjectFileSystem;
import org.sonar.api.utils.IocContainer;
-import org.sonar.api.utils.SonarException;
import org.sonar.batch.*;
import org.sonar.batch.components.TimeMachineConfiguration;
import org.sonar.batch.config.ProjectSettings;
@@ -66,6 +62,7 @@ public class ProjectModule extends Module {
addCoreSingleton(projectDefinition);
addCoreSingleton(project);
addCoreSingleton(project.getConfiguration());
+ addCoreSingleton(ProjectInitializer.class);
addCoreSingleton(ProjectSettings.class);
addCoreSingleton(IocContainer.class);
@@ -84,7 +81,6 @@ public class ProjectModule extends Module {
addCoreSingleton(org.sonar.api.database.daos.MeasuresDao.class);
addCoreSingleton(ProfilesDao.class);
addCoreSingleton(DefaultSensorContext.class);
- addCoreSingleton(Languages.class);
addCoreSingleton(BatchExtensionDictionnary.class);
addCoreSingleton(DefaultTimeMachine.class);
addCoreSingleton(ViolationFilters.class);
@@ -120,22 +116,12 @@ public class ProjectModule extends Module {
*/
@Override
protected void doStart() {
- Language language = getComponentByType(Languages.class).get(project.getLanguageKey());
- if (language == null) {
- throw new SonarException("Language with key '" + project.getLanguageKey() + "' not found");
- }
- project.setLanguage(language);
-
DefaultIndex index = getComponentByType(DefaultIndex.class);
index.setCurrentProject(project,
getComponentByType(ResourceFilters.class),
getComponentByType(ViolationFilters.class),
getComponentByType(RulesProfile.class));
- // TODO See http://jira.codehaus.org/browse/SONAR-2126
- // previously MavenProjectBuilder was responsible for creation of ProjectFileSystem
- project.setFileSystem(getComponentByType(ProjectFileSystem.class));
-
getComponentByType(Phases.class).execute(project);
}
}
diff --git a/sonar-batch/src/main/java/org/sonar/batch/config/ProjectSettings.java b/sonar-batch/src/main/java/org/sonar/batch/config/ProjectSettings.java
index aca22d363f4..95822bcb32a 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/config/ProjectSettings.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/config/ProjectSettings.java
@@ -28,6 +28,7 @@ import org.sonar.api.config.PropertyDefinitions;
import org.sonar.api.config.Settings;
import org.sonar.api.resources.Java;
import org.sonar.api.resources.Project;
+import org.sonar.batch.bootstrap.ProjectInitializer;
import org.sonar.core.config.ConfigurationUtils;
import org.sonar.core.properties.PropertiesDao;
import org.sonar.core.properties.PropertyDto;
@@ -43,19 +44,15 @@ public class ProjectSettings extends Settings {
private ProjectDefinition projectDefinition;
private PropertiesDao propertiesDao;
- public ProjectSettings(PropertyDefinitions definitions, ProjectDefinition projectDefinition, PropertiesDao propertiesDao, Project project) {
+ public ProjectSettings(PropertyDefinitions definitions, ProjectDefinition projectDefinition, PropertiesDao propertiesDao, Project project, ProjectInitializer initializer) {
super(definitions);
this.deprecatedCommonsConf = project.getConfiguration(); // Configuration is not a parameter to be sure that the project conf is used, not the global one
this.projectDefinition = projectDefinition;
this.propertiesDao = propertiesDao;
load();
- updateProject(project);
- }
- private void updateProject(Project project) {
- // The class org.sonar.api.batch.Project should be deeply refactored and should load language from settings.
- // Meanwhile the language must be updated :
- project.setLanguageKey(StringUtils.defaultIfBlank(getString("sonar.language"), Java.KEY));
+ // TODO should be refactored in a clean way
+ initializer.execute(project, this);
}
public ProjectSettings load() {
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/database/model/ResourceModel.java b/sonar-plugin-api/src/main/java/org/sonar/api/database/model/ResourceModel.java
index 6191be4c9a7..b4731d84746 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/database/model/ResourceModel.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/database/model/ResourceModel.java
@@ -283,37 +283,37 @@ public class ResourceModel extends BaseIdentifiable implements Cloneable {
}
ResourceModel other = (ResourceModel) obj;
return new EqualsBuilder()
- .append(key, other.key)
- .append(enabled, other.enabled)
- .append(rootId, other.rootId)
- .isEquals();
+ .append(key, other.key)
+ .append(enabled, other.enabled)
+ .append(rootId, other.rootId)
+ .isEquals();
}
@Override
public int hashCode() {
return new HashCodeBuilder(17, 37)
- .append(key)
- .append(enabled)
- .append(rootId)
- .toHashCode();
+ .append(key)
+ .append(enabled)
+ .append(rootId)
+ .toHashCode();
}
@Override
public String toString() {
return new ToStringBuilder(this)
- .append("id", getId())
- .append("key", key)
- .append("scope", scope)
- .append("qualifier", qualifier)
- .append("name", name)
- .append("longName", longName)
- .append("lang", languageKey)
- .append("enabled", enabled)
- .append("rootId", rootId)
- .append("copyResourceId", copyResourceId)
- .append("personId", personId)
- .append("createdAt", createdAt)
- .toString();
+ .append("id", getId())
+ .append("key", key)
+ .append("scope", scope)
+ .append("qualifier", qualifier)
+ .append("name", name)
+ .append("longName", longName)
+ .append("lang", languageKey)
+ .append("enabled", enabled)
+ .append("rootId", rootId)
+ .append("copyResourceId", copyResourceId)
+ .append("personId", personId)
+ .append("createdAt", createdAt)
+ .toString();
}
@Override
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/resources/Language.java b/sonar-plugin-api/src/main/java/org/sonar/api/resources/Language.java
index 44d46dd036b..9b484c0afcc 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/resources/Language.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/resources/Language.java
@@ -21,12 +21,14 @@ package org.sonar.api.resources;
import org.sonar.api.BatchExtension;
import org.sonar.api.ServerExtension;
+import org.sonar.api.batch.InstantiationStrategy;
/**
* The extension point to define a new language
*
* @since 1.10
*/
+@InstantiationStrategy(InstantiationStrategy.PER_BATCH)
public interface Language extends BatchExtension, ServerExtension {
/**
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/resources/Languages.java b/sonar-plugin-api/src/main/java/org/sonar/api/resources/Languages.java
index 03b531d9d94..01fbc5ddedc 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/resources/Languages.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/resources/Languages.java
@@ -28,12 +28,14 @@ import com.google.common.collect.Maps;
import org.apache.commons.lang.ArrayUtils;
import org.sonar.api.BatchComponent;
import org.sonar.api.ServerComponent;
+import org.sonar.api.batch.InstantiationStrategy;
/**
* A class to store the list of languages
*
* @since 1.10
*/
+@InstantiationStrategy(InstantiationStrategy.PER_BATCH)
public class Languages implements BatchComponent, ServerComponent {
private final Map<String, Language> map = Maps.newHashMap();