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;
}
import org.sonar.process.Props;
public interface ComputeEngineContainer {
- ComputeEngineContainer configure(Props props);
-
- ComputeEngineContainer start();
+ ComputeEngineContainer start(Props props);
ComputeEngineContainer stop();
}
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;
// 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<Module> 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<Module> modules = container.getComponentsByType(Module.class);
+ for (Module module : modules) {
+ module.configure(container);
+ }
}
}
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;
}
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;
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();
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
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();