From 5482aa0fb73622ca321461de339015e0c6a43c18 Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Wed, 30 Mar 2016 16:09:41 +0200 Subject: [PATCH] SONAR-6732 detect DevCockpit without causing premature start reading adapter rather that getting the PersistDevelopersDelegate instance avoid triggering start of the component and/or any of its dependencies in fact, previous implementation was causing premature start of DeveloperRepositoryImpl before Settings were loaded --- .../container/ComputeEngineContainer.java | 8 +++++++- .../step/ReportComputationSteps.java | 17 ++++++++++++----- ...portComputeEngineContainerPopulatorTest.java | 11 ++++++++++- .../computation/step/ComputationStepsTest.java | 5 +++-- 4 files changed, 32 insertions(+), 9 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/container/ComputeEngineContainer.java b/server/sonar-server/src/main/java/org/sonar/server/computation/container/ComputeEngineContainer.java index 89d644ea212..c510f198cae 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/container/ComputeEngineContainer.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/container/ComputeEngineContainer.java @@ -19,9 +19,10 @@ */ package org.sonar.server.computation.container; +import org.picocontainer.PicoContainer; +import org.sonar.ce.queue.CeTask; import org.sonar.core.platform.ComponentContainer; import org.sonar.core.platform.ContainerPopulator; -import org.sonar.ce.queue.CeTask; /** * The Compute Engine container. Created for a specific parent {@link ComponentContainer} and a specific {@link CeTask}. @@ -35,4 +36,9 @@ public interface ComputeEngineContainer extends ContainerPopulator.Container { */ void cleanup(); + /** + * Access to the underlying pico container. + */ + PicoContainer getPicoContainer(); + } diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/step/ReportComputationSteps.java b/server/sonar-server/src/main/java/org/sonar/server/computation/step/ReportComputationSteps.java index 5300cf398b5..80530f2c540 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/step/ReportComputationSteps.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/step/ReportComputationSteps.java @@ -23,6 +23,7 @@ import com.google.common.base.Predicate; import java.util.Arrays; import java.util.List; import javax.annotation.Nonnull; +import org.picocontainer.ComponentAdapter; import org.sonar.server.computation.container.ComputeEngineContainer; import org.sonar.server.computation.developer.PersistDevelopersDelegate; @@ -33,8 +34,6 @@ import static com.google.common.collect.FluentIterable.from; */ public class ReportComputationSteps extends AbstractComputationSteps { - private static final String DEVELOPER_COCKPIT_PLUGIN_NAME = "Developer Cockpit"; - private static final List> STEPS = Arrays.asList( ExtractReportStep.class, LogScannerContextStep.class, @@ -123,13 +122,21 @@ public class ReportComputationSteps extends AbstractComputationSteps { @Override public List> orderedStepClasses() { return from(STEPS) - .filter(new AllowPersistDevelopersStepIfDevCockpitPluginInstalled()) + .filter(new AllowPersistDevelopersStepIfDevCockpitPluginInstalled(computeEngineContainer)) .toList(); } - private class AllowPersistDevelopersStepIfDevCockpitPluginInstalled implements Predicate> { + private static class AllowPersistDevelopersStepIfDevCockpitPluginInstalled implements Predicate> { + private final boolean devCockpitIsInstalled; + + private AllowPersistDevelopersStepIfDevCockpitPluginInstalled(ComputeEngineContainer computeEngineContainer) { + this.devCockpitIsInstalled = isDevCockpitInstalled(computeEngineContainer); + } - private final boolean devCockpitIsInstalled = computeEngineContainer.getComponentByType(PersistDevelopersDelegate.class) != null; + private static boolean isDevCockpitInstalled(ComputeEngineContainer computeEngineContainer) { + List> componentAdapters = computeEngineContainer.getPicoContainer().getComponentAdapters(PersistDevelopersDelegate.class); + return !componentAdapters.isEmpty(); + } @Override public boolean apply(@Nonnull Class input) { diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/container/ReportComputeEngineContainerPopulatorTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/container/ReportComputeEngineContainerPopulatorTest.java index 7254400aff3..662cf5b336e 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/container/ReportComputeEngineContainerPopulatorTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/container/ReportComputeEngineContainerPopulatorTest.java @@ -29,6 +29,8 @@ import java.util.Set; import javax.annotation.Nonnull; import javax.annotation.Nullable; import org.junit.Test; +import org.picocontainer.DefaultPicoContainer; +import org.picocontainer.PicoContainer; import org.sonar.ce.queue.CeTask; import org.sonar.core.platform.ComponentContainer; import org.sonar.plugin.ce.ReportAnalysisComponentProvider; @@ -107,7 +109,7 @@ public class ReportComputeEngineContainerPopulatorTest { ReportAnalysisComponentProvider componentProvider = mock(ReportAnalysisComponentProvider.class); when(componentProvider.getComponents()).thenReturn(ImmutableList.of(object, clazz)); - underTest = new ReportComputeEngineContainerPopulator(task, new ReportAnalysisComponentProvider[]{componentProvider}); + underTest = new ReportComputeEngineContainerPopulator(task, new ReportAnalysisComponentProvider[] {componentProvider}); AddedObjectsRecorderComputeEngineContainer container = new AddedObjectsRecorderComputeEngineContainer(); container.add(componentProvider); underTest.populateContainer(container); @@ -129,6 +131,8 @@ public class ReportComputeEngineContainerPopulatorTest { } private static class AddedObjectsRecorderComputeEngineContainer implements ComputeEngineContainer { + private static final DefaultPicoContainer SOME_EMPTY_PICO_CONTAINER = new DefaultPicoContainer(); + private List added = new ArrayList<>(); @Override @@ -141,6 +145,11 @@ public class ReportComputeEngineContainerPopulatorTest { throw new UnsupportedOperationException("cleanup is not implemented"); } + @Override + public PicoContainer getPicoContainer() { + return SOME_EMPTY_PICO_CONTAINER; + } + @Override public ComponentContainer add(Object... objects) { added.addAll(Arrays.asList(objects)); diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/step/ComputationStepsTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/step/ComputationStepsTest.java index 6bb76038f49..31ff8881635 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/step/ComputationStepsTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/step/ComputationStepsTest.java @@ -25,12 +25,13 @@ import com.google.common.collect.Lists; import java.util.Set; import org.junit.Test; import org.picocontainer.ComponentAdapter; +import org.sonar.ce.queue.CeTask; import org.sonar.ce.settings.ThreadLocalSettings; import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ContainerPopulator; import org.sonar.server.computation.container.ComputeEngineContainerImpl; import org.sonar.server.computation.container.ReportComputeEngineContainerPopulator; import org.sonar.server.computation.container.StepsExplorer; -import org.sonar.ce.queue.CeTask; import static com.google.common.collect.FluentIterable.from; import static com.google.common.collect.Sets.difference; @@ -43,7 +44,7 @@ public class ComputationStepsTest { @Test public void fail_if_a_step_is_not_registered_in_picocontainer() { try { - Lists.newArrayList(new ReportComputationSteps(mock(ComputeEngineContainerImpl.class)).instances()); + Lists.newArrayList(new ReportComputationSteps(new ComputeEngineContainerImpl(new ComponentContainer(), mock(ContainerPopulator.class))).instances()); fail(); } catch (IllegalStateException e) { assertThat(e).hasMessageContaining("Component not found"); -- 2.39.5