]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6014 ResourcePersister should be executed before MeasurePersister
authorJulien HENRY <julien.henry@sonarsource.com>
Mon, 12 Jan 2015 13:46:53 +0000 (14:46 +0100)
committerJulien HENRY <julien.henry@sonarsource.com>
Mon, 12 Jan 2015 13:47:35 +0000 (14:47 +0100)
sonar-batch/src/main/java/org/sonar/batch/index/MeasurePersister.java
sonar-batch/src/main/java/org/sonar/batch/phases/DefaultPhaseExecutor.java

index b6f3f4b61d6cdcceae681f7200ae144cc8eb7d18..c74a8fc5bace499cdb53afae2d2fcf432f1edfa9 100644 (file)
@@ -38,7 +38,7 @@ import org.sonar.core.persistence.MyBatis;
 
 import javax.annotation.Nullable;
 
-public final class MeasurePersister implements ScanPersister {
+public class MeasurePersister implements ScanPersister {
   private final MyBatis mybatis;
   private final RuleFinder ruleFinder;
   private final MeasureCache measureCache;
index 1d70b78d0f280255e40762f879e6d593b341c9e8..cf2dfa308c5e0d7a69c05a1375fea1199e63d2f6 100644 (file)
@@ -19,7 +19,6 @@
  */
 package org.sonar.batch.phases;
 
-import com.google.common.collect.Lists;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.sonar.api.batch.SensorContext;
@@ -38,6 +37,12 @@ import org.sonar.batch.scan.filesystem.FileSystemLogger;
 import org.sonar.batch.scan.maven.MavenPluginsConfigurator;
 import org.sonar.batch.scan.report.JsonReport;
 
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+
 public final class DefaultPhaseExecutor implements PhaseExecutor {
 
   public static final Logger LOGGER = LoggerFactory.getLogger(DefaultPhaseExecutor.class);
@@ -152,18 +157,31 @@ public final class DefaultPhaseExecutor implements PhaseExecutor {
   private void executePersisters() {
     if (!analysisMode.isPreview()) {
       LOGGER.info("Store results in database");
-      eventBus.fireEvent(new PersistersPhaseEvent(Lists.newArrayList(persisters), true));
-      for (ScanPersister persister : persisters) {
+      List<ScanPersister> sortedPersisters = sortedPersisters();
+      eventBus.fireEvent(new PersistersPhaseEvent(sortedPersisters, true));
+      for (ScanPersister persister : sortedPersisters) {
         LOGGER.debug("Execute {}", persister.getClass().getName());
         eventBus.fireEvent(new PersisterExecutionEvent(persister, true));
         persister.persist();
         eventBus.fireEvent(new PersisterExecutionEvent(persister, false));
       }
 
-      eventBus.fireEvent(new PersistersPhaseEvent(Lists.newArrayList(persisters), false));
+      eventBus.fireEvent(new PersistersPhaseEvent(sortedPersisters, false));
     }
   }
 
+  List<ScanPersister> sortedPersisters() {
+    // Sort by reverse name so that ResourcePersister is executed before MeasurePersister
+    List<ScanPersister> sortedPersisters = new ArrayList<>(Arrays.asList(persisters));
+    Collections.sort(sortedPersisters, new Comparator<ScanPersister>() {
+      @Override
+      public int compare(ScanPersister o1, ScanPersister o2) {
+        return o2.getClass().getName().compareTo(o1.getClass().getName());
+      }
+    });
+    return sortedPersisters;
+  }
+
   private void publishReportJob() {
     String stepName = "Publish report";
     eventBus.fireEvent(new BatchStepEvent(stepName, true));