From 7a4fe3eb4d66a9f85e31c207d3fce8ed92c2f0ad Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Tue, 15 Mar 2016 18:08:40 +0100 Subject: [PATCH] SONAR-6732 must mimic level from WebServer to load extensions --- .../java/org/sonar/ce/ComputeEngineImpl.java | 4 +- .../ce/container/ComputeEngineContainer.java | 4 +- .../container/ComputeEngineContainerImpl.java | 78 +++++++++------ .../org/sonar/ce/ComputeEngineImplTest.java | 7 +- .../ComputeEngineContainerImplTest.java | 95 +++++++------------ 5 files changed, 83 insertions(+), 105 deletions(-) diff --git a/server/sonar-ce/src/main/java/org/sonar/ce/ComputeEngineImpl.java b/server/sonar-ce/src/main/java/org/sonar/ce/ComputeEngineImpl.java index c6c594d23f3..a86c92c46a0 100644 --- a/server/sonar-ce/src/main/java/org/sonar/ce/ComputeEngineImpl.java +++ b/server/sonar-ce/src/main/java/org/sonar/ce/ComputeEngineImpl.java @@ -40,9 +40,7 @@ public class ComputeEngineImpl implements ComputeEngine { checkState(this.status == Status.INIT, "startup() can not be called multiple times"); try { this.status = Status.STARTING; - this.computeEngineContainer - .configure(props) - .start(); + this.computeEngineContainer.start(props); } finally { this.status = Status.STARTED; } diff --git a/server/sonar-ce/src/main/java/org/sonar/ce/container/ComputeEngineContainer.java b/server/sonar-ce/src/main/java/org/sonar/ce/container/ComputeEngineContainer.java index c58317296ef..6e4db16d121 100644 --- a/server/sonar-ce/src/main/java/org/sonar/ce/container/ComputeEngineContainer.java +++ b/server/sonar-ce/src/main/java/org/sonar/ce/container/ComputeEngineContainer.java @@ -22,9 +22,7 @@ package org.sonar.ce.container; import org.sonar.process.Props; public interface ComputeEngineContainer { - ComputeEngineContainer configure(Props props); - - ComputeEngineContainer start(); + ComputeEngineContainer start(Props props); ComputeEngineContainer stop(); } diff --git a/server/sonar-ce/src/main/java/org/sonar/ce/container/ComputeEngineContainerImpl.java b/server/sonar-ce/src/main/java/org/sonar/ce/container/ComputeEngineContainerImpl.java index 135c9a55552..d54123f8afc 100644 --- a/server/sonar-ce/src/main/java/org/sonar/ce/container/ComputeEngineContainerImpl.java +++ b/server/sonar-ce/src/main/java/org/sonar/ce/container/ComputeEngineContainerImpl.java @@ -21,6 +21,7 @@ package org.sonar.ce.container; import com.google.common.annotations.VisibleForTesting; import java.util.List; +import javax.annotation.CheckForNull; import org.sonar.api.config.EmailSettings; import org.sonar.api.issue.action.Actions; import org.sonar.api.profiles.AnnotationProfileParser; @@ -586,58 +587,75 @@ public class ComputeEngineContainerImpl implements ComputeEngineContainer { // ClearRulesOverloadedDebt.class, DB maintenance, responsibility of Web Server }; - private final ComponentContainer componentContainer; - - public ComputeEngineContainerImpl() { - this.componentContainer = new ComponentContainer(); - } + @CheckForNull + private ComponentContainer level1; + @CheckForNull + private ComponentContainer level4; @Override - public ComputeEngineContainer configure(Props props) { - this.componentContainer + public ComputeEngineContainer start(Props props) { + this.level1 = new ComponentContainer(); + this.level1 .add(props.rawProperties()) .add(LEVEL_1_COMPONENTS) .add(toArray(CorePropertyDefinitions.all())) - .add(toArray(CePropertyDefinitions.all())) - .add(LEVEL_2_COMPONENTS) - .add(LEVEL_3_COMPONENTS) - .add(LEVEL_4_COMPONENTS) - .add(STARTUP_COMPONENTS); + .add(toArray(CePropertyDefinitions.all())); + configureFromModules(this.level1); + this.level1.startComponents(); - configureFromModules(); + ComponentContainer level2 = this.level1.createChild(); + level2.add(LEVEL_2_COMPONENTS); + configureFromModules(level2); + level2.startComponents(); - return this; - } + ComponentContainer level3 = level2.createChild(); + level3.add(LEVEL_3_COMPONENTS); + configureFromModules(level3); + level3.startComponents(); - private static Object[] toArray(List list) { - return list.toArray(new Object[list.size()]); - } + this.level4 = level3.createChild(); + this.level4.add(LEVEL_4_COMPONENTS); + configureFromModules(this.level4); + ServerExtensionInstaller extensionInstaller = this.level4.getComponentByType(ServerExtensionInstaller.class); + extensionInstaller.installExtensions(this.level4); + this.level4.startComponents(); - private void configureFromModules() { - List modules = this.componentContainer.getComponentsByType(Module.class); - for (Module module : modules) { - module.configure(this.componentContainer); - } + startupTasks(); + + return this; } - @Override - public ComputeEngineContainer start() { - this.componentContainer.startComponents(); - ServerLifecycleNotifier serverLifecycleNotifier = this.componentContainer.getComponentByType(ServerLifecycleNotifier.class); + private void startupTasks() { + ComponentContainer startupLevel = this.level4.createChild(); + startupLevel.add(STARTUP_COMPONENTS); + startupLevel.startComponents(); + // done in PlatformLevelStartup + ServerLifecycleNotifier serverLifecycleNotifier = startupLevel.getComponentByType(ServerLifecycleNotifier.class); if (serverLifecycleNotifier != null) { serverLifecycleNotifier.notifyStart(); } - return this; + startupLevel.stopComponents(); } @Override public ComputeEngineContainer stop() { - this.componentContainer.stopComponents(); + this.level1.stopComponents(); return this; } @VisibleForTesting protected ComponentContainer getComponentContainer() { - return componentContainer; + return level4; + } + + private static Object[] toArray(List list) { + return list.toArray(new Object[list.size()]); + } + + private static void configureFromModules(ComponentContainer container) { + List modules = container.getComponentsByType(Module.class); + for (Module module : modules) { + module.configure(container); + } } } diff --git a/server/sonar-ce/src/test/java/org/sonar/ce/ComputeEngineImplTest.java b/server/sonar-ce/src/test/java/org/sonar/ce/ComputeEngineImplTest.java index 697a8d68032..e90a2726a20 100644 --- a/server/sonar-ce/src/test/java/org/sonar/ce/ComputeEngineImplTest.java +++ b/server/sonar-ce/src/test/java/org/sonar/ce/ComputeEngineImplTest.java @@ -64,12 +64,7 @@ public class ComputeEngineImplTest { private static class NoOpComputeEngineContainer implements ComputeEngineContainer { @Override - public ComputeEngineContainer configure(Props props) { - return this; - } - - @Override - public ComputeEngineContainer start() { + public ComputeEngineContainer start(Props props) { return this; } diff --git a/server/sonar-ce/src/test/java/org/sonar/ce/container/ComputeEngineContainerImplTest.java b/server/sonar-ce/src/test/java/org/sonar/ce/container/ComputeEngineContainerImplTest.java index 9171be576b7..e70112231e5 100644 --- a/server/sonar-ce/src/test/java/org/sonar/ce/container/ComputeEngineContainerImplTest.java +++ b/server/sonar-ce/src/test/java/org/sonar/ce/container/ComputeEngineContainerImplTest.java @@ -27,10 +27,8 @@ import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; import org.picocontainer.MutablePicoContainer; -import org.sonar.api.config.PropertyDefinitions; import org.sonar.api.database.DatabaseProperties; import org.sonar.api.utils.System2; -import org.sonar.core.platform.ComponentContainer; import org.sonar.db.DbTester; import org.sonar.process.ProcessProperties; import org.sonar.process.Props; @@ -38,7 +36,8 @@ import org.sonar.process.Props; import static org.assertj.core.api.Assertions.assertThat; public class ComputeEngineContainerImplTest { - private static final int COMPONENTS_IN_CONTAINER_AT_CONSTRUCTION = 2; + private static final int CONTAINER_ITSELF = 1; + private static final int COMPONENTS_IN_LEVEL_1_AT_CONSTRUCTION = CONTAINER_ITSELF + 1; @Rule public TemporaryFolder tempFolder = new TemporaryFolder(); @@ -48,44 +47,8 @@ public class ComputeEngineContainerImplTest { private ComputeEngineContainerImpl underTest = new ComputeEngineContainerImpl(); @Test - public void constructor_adds_only_container_and_PropertyDefinitions() { - ComponentContainer componentContainer = underTest.getComponentContainer(); - - assertThat(componentContainer.getComponentsByType(Object.class)).hasSize(2); - assertThat(componentContainer.getComponentByType(PropertyDefinitions.class)).isNotNull(); - assertThat(componentContainer.getComponentByType(ComponentContainer.class)).isSameAs(componentContainer); - } - - @Test - public void configure_adds_raw_properties_from_Props_to_container() { - Properties properties = new Properties(); - underTest.configure(new Props(properties)); - - assertThat(underTest.getComponentContainer().getComponentByType(Properties.class)).isSameAs(properties); - } - - @Test - public void verify_number_of_components_in_container() { - Properties properties = new Properties(); - underTest.configure(new Props(properties)); - - assertThat(underTest.getComponentContainer().getPicoContainer().getComponentAdapters()) - .hasSize(COMPONENTS_IN_CONTAINER_AT_CONSTRUCTION - + 22 // level 1 - + 47 // content of DaoModule - + 1 // content of EsSearchModule - + 58 // content of CorePropertyDefinitions - + 1 // content of CePropertyDefinitions - + 59 // content of MigrationStepModule - + 10 // level 2 - + 5 // level 3 - + 77 // level 4 - + 5 // content of CeModule - + 7 // content of CeQueueModule - + 4 // content of ReportProcessingModule - + 4 // content of CeTaskProcessorModule - + 4 // level startup - ); + public void constructor_does_not_create_container() { + assertThat(underTest.getComponentContainer()).isNull(); } @Test @@ -103,30 +66,36 @@ public class ComputeEngineContainerImplTest { properties.setProperty(DatabaseProperties.PROP_PASSWORD, "sonar"); underTest - .configure(new Props(properties)) - .start(); - - } - - @Test - public void start_starts_pico_container() { - MutablePicoContainer picoContainer = underTest.getComponentContainer().getPicoContainer(); - - assertThat(picoContainer.getLifecycleState().isStarted()).isFalse(); - - underTest.start(); + .start(new Props(properties)); - assertThat(picoContainer.getLifecycleState().isStarted()).isTrue(); - } - - @Test - public void stop_stops_and_dispose_pico_container() { MutablePicoContainer picoContainer = underTest.getComponentContainer().getPicoContainer(); - - assertThat(picoContainer.getLifecycleState().isStarted()).isFalse(); - assertThat(picoContainer.getLifecycleState().isStopped()).isFalse(); - - underTest.start(); + assertThat(picoContainer.getComponentAdapters()) + .hasSize( + CONTAINER_ITSELF + + 77 // level 4 + + 5 // content of CeModule + + 7 // content of CeQueueModule + + 4 // content of ReportProcessingModule + + 4 // content of CeTaskProcessorModule + ); + assertThat(picoContainer.getParent().getComponentAdapters()).hasSize( + CONTAINER_ITSELF + + 5 // level 3 + ); + assertThat(picoContainer.getParent().getParent().getComponentAdapters()).hasSize( + CONTAINER_ITSELF + + 10 // level 2 + ); + assertThat(picoContainer.getParent().getParent().getParent().getComponentAdapters()).hasSize( + COMPONENTS_IN_LEVEL_1_AT_CONSTRUCTION + + 22 // level 1 + + 47 // content of DaoModule + + 1 // content of EsSearchModule + + 58 // content of CorePropertyDefinitions + + 1 // content of CePropertyDefinitions + + 59 // content of MigrationStepModule + ); + assertThat(picoContainer.getParent().getParent().getParent().getParent()).isNull(); underTest.stop(); assertThat(picoContainer.getLifecycleState().isStarted()).isFalse(); -- 2.39.5