]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6732 must mimic level from WebServer to load extensions
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Tue, 15 Mar 2016 17:08:40 +0000 (18:08 +0100)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Mon, 21 Mar 2016 15:44:05 +0000 (16:44 +0100)
server/sonar-ce/src/main/java/org/sonar/ce/ComputeEngineImpl.java
server/sonar-ce/src/main/java/org/sonar/ce/container/ComputeEngineContainer.java
server/sonar-ce/src/main/java/org/sonar/ce/container/ComputeEngineContainerImpl.java
server/sonar-ce/src/test/java/org/sonar/ce/ComputeEngineImplTest.java
server/sonar-ce/src/test/java/org/sonar/ce/container/ComputeEngineContainerImplTest.java

index c6c594d23f36be5f48c6c7cd54982232fe058ff9..a86c92c46a02535178dc22853209a2282b5bd569 100644 (file)
@@ -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;
     }
index c58317296efb6b5e5f8caab758ff30140286a4f0..6e4db16d121b57b6a6f08f6c958295ce47dd2d03 100644 (file)
@@ -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();
 }
index 135c9a55552358ca6cf67fc5d9ae2dd70b977c73..d54123f8afc11ecdd1c59ce6ee7a13f4749467c7 100644 (file)
@@ -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<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);
+    }
   }
 }
index 697a8d680320b243244d7ecdb68e7234b4ac2ce4..e90a2726a202069b01ea7d4b48da21c96e6e6a3d 100644 (file)
@@ -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;
     }
 
index 9171be576b70152f1f95d917829fd6cd8ea48d1c..e70112231e52b8b6847beaacdd29da21463ab5a9 100644 (file)
@@ -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();