From: Julien HENRY Date: Mon, 12 Jan 2015 13:46:53 +0000 (+0100) Subject: SONAR-6014 ResourcePersister should be executed before MeasurePersister X-Git-Tag: latest-silver-master-#65~248 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=058fdebd832e2e8d1ca9612aa939a1894eac5315;p=sonarqube.git SONAR-6014 ResourcePersister should be executed before MeasurePersister --- diff --git a/sonar-batch/src/main/java/org/sonar/batch/index/MeasurePersister.java b/sonar-batch/src/main/java/org/sonar/batch/index/MeasurePersister.java index b6f3f4b61d6..c74a8fc5bac 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/index/MeasurePersister.java +++ b/sonar-batch/src/main/java/org/sonar/batch/index/MeasurePersister.java @@ -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; diff --git a/sonar-batch/src/main/java/org/sonar/batch/phases/DefaultPhaseExecutor.java b/sonar-batch/src/main/java/org/sonar/batch/phases/DefaultPhaseExecutor.java index 1d70b78d0f2..cf2dfa308c5 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/phases/DefaultPhaseExecutor.java +++ b/sonar-batch/src/main/java/org/sonar/batch/phases/DefaultPhaseExecutor.java @@ -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 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 sortedPersisters() { + // Sort by reverse name so that ResourcePersister is executed before MeasurePersister + List sortedPersisters = new ArrayList<>(Arrays.asList(persisters)); + Collections.sort(sortedPersisters, new Comparator() { + @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));