]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-2602 persist project language before execution of extensions
authorSimon Brandhof <simon.brandhof@gmail.com>
Fri, 28 Sep 2012 09:23:39 +0000 (11:23 +0200)
committerSimon Brandhof <simon.brandhof@gmail.com>
Fri, 28 Sep 2012 09:24:33 +0000 (11:24 +0200)
sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchModule.java
sonar-batch/src/main/java/org/sonar/batch/bootstrap/ProjectInitializer.java [new file with mode: 0644]
sonar-batch/src/main/java/org/sonar/batch/bootstrap/ProjectModule.java
sonar-batch/src/main/java/org/sonar/batch/config/ProjectSettings.java
sonar-plugin-api/src/main/java/org/sonar/api/database/model/ResourceModel.java
sonar-plugin-api/src/main/java/org/sonar/api/resources/Language.java
sonar-plugin-api/src/main/java/org/sonar/api/resources/Languages.java

index b598f81a9ad1ee1f26bfe019c1e5987911a6b038..645907de5812ed5c983af8e662519768088f4859 100644 (file)
@@ -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 (file)
index 0000000..f6ee4c4
--- /dev/null
@@ -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);
+
+  }
+}
index 390bba94cf57e225005edfd2e15c3cedcb6a4e7b..09cc6ce69f7236f230e21425652148a75e2a6e71 100644 (file)
@@ -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);
   }
 }
index aca22d363f46effb9a99a69d2fed81077238085f..95822bcb32af3bfde448e1b3c84f6b9551c11a01 100644 (file)
@@ -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() {
index 6191be4c9a72e2ece0e07a4a23b6bc6214941444..b4731d84746cf09214c5a4cc162e45107a828928 100644 (file)
@@ -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
index 44d46dd036bbea061ded01f0c66e3e5bb34555d9..9b484c0afcca830fa30c3ec8fbef062cf6372f7d 100644 (file)
@@ -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 {
 
   /**
index 03b531d9d94a89fe23117e243e51c9dba4d89c56..01fbc5ddedc945d898242cae2b863ac6d6f457e6 100644 (file)
@@ -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();