]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6732 add ComputeEngineContainer + Properties from cmd line
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Thu, 10 Mar 2016 16:46:28 +0000 (17:46 +0100)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Mon, 21 Mar 2016 15:44:04 +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 [new file with mode: 0644]
server/sonar-ce/src/main/java/org/sonar/ce/container/package-info.java [new file with mode: 0644]
server/sonar-ce/src/test/java/org/sonar/ce/container/ComputeEngineContainerTest.java [new file with mode: 0644]

index 95c593f34783b2692680716ef6c132a4bef022aa..457139516d8b892c3b400c262a5c4f81d41a130a 100644 (file)
  */
 package org.sonar.ce;
 
+import org.sonar.ce.container.ComputeEngineContainer;
 import org.sonar.process.Props;
 
 import static com.google.common.base.Preconditions.checkState;
 
 public class ComputeEngineImpl implements ComputeEngine {
-  private volatile Status status = Status.INIT;
-
   private final Props props;
+  private final ComputeEngineContainer computeEngineContainer = new ComputeEngineContainer();
+
+  private Status status = Status.INIT;
 
   public ComputeEngineImpl(Props props) {
     this.props = props;
@@ -34,29 +36,24 @@ public class ComputeEngineImpl implements ComputeEngine {
 
   @Override
   public void startup() {
-    checkStateAtStartup(this.status);
+    checkState(this.status == Status.INIT, "startup() can not be called multiple times");
     try {
       this.status = Status.STARTING;
-      if (props.value("sonar.ce.startupFailure") != null) {
-        throw new IllegalStateException("Startup failed!");
-      }
+      this.computeEngineContainer
+        .configure(props)
+        .start();
     } finally {
       this.status = Status.STARTED;
     }
   }
 
-  private static void checkStateAtStartup(Status currentStatus) {
-    checkState(currentStatus == Status.INIT, "startup() can not be called multiple times");
-  }
-
   @Override
   public void shutdown() {
     checkStateAsShutdown(this.status);
+
     try {
       this.status = Status.STOPPING;
-      if (props.value("sonar.ce.shutdownFailure") != null) {
-        throw new IllegalStateException("Shutdown failed!");
-      }
+      this.computeEngineContainer.stop();
     } finally {
       this.status = Status.STOPPED;
     }
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
new file mode 100644 (file)
index 0000000..2caedf8
--- /dev/null
@@ -0,0 +1,54 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.ce.container;
+
+import com.google.common.annotations.VisibleForTesting;
+import org.sonar.core.platform.ComponentContainer;
+import org.sonar.process.Props;
+
+public class ComputeEngineContainer {
+  private final ComponentContainer componentContainer;
+
+  public ComputeEngineContainer() {
+    this.componentContainer = new ComponentContainer();
+  }
+
+  public ComputeEngineContainer configure(Props props) {
+    this.componentContainer.add(
+      props.rawProperties()
+      );
+    return this;
+  }
+
+  public ComputeEngineContainer start() {
+    this.componentContainer.startComponents();
+    return this;
+  }
+
+  public ComputeEngineContainer stop() {
+    this.componentContainer.stopComponents();
+    return this;
+  }
+
+  @VisibleForTesting
+  protected ComponentContainer getComponentContainer() {
+    return componentContainer;
+  }
+}
diff --git a/server/sonar-ce/src/main/java/org/sonar/ce/container/package-info.java b/server/sonar-ce/src/main/java/org/sonar/ce/container/package-info.java
new file mode 100644 (file)
index 0000000..7261383
--- /dev/null
@@ -0,0 +1,23 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+@ParametersAreNonnullByDefault
+package org.sonar.ce.container;
+
+import javax.annotation.ParametersAreNonnullByDefault;
diff --git a/server/sonar-ce/src/test/java/org/sonar/ce/container/ComputeEngineContainerTest.java b/server/sonar-ce/src/test/java/org/sonar/ce/container/ComputeEngineContainerTest.java
new file mode 100644 (file)
index 0000000..8108eca
--- /dev/null
@@ -0,0 +1,76 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.ce.container;
+
+import java.util.Properties;
+import org.junit.Test;
+import org.picocontainer.MutablePicoContainer;
+import org.sonar.api.config.PropertyDefinitions;
+import org.sonar.core.platform.ComponentContainer;
+import org.sonar.process.Props;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class ComputeEngineContainerTest {
+  private ComputeEngineContainer underTest = new ComputeEngineContainer();
+
+  @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 start_starts_pico_container() {
+    MutablePicoContainer picoContainer = underTest.getComponentContainer().getPicoContainer();
+
+    assertThat(picoContainer.getLifecycleState().isStarted()).isFalse();
+
+    underTest.start();
+
+    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();
+    underTest.stop();
+
+    assertThat(picoContainer.getLifecycleState().isStarted()).isFalse();
+    assertThat(picoContainer.getLifecycleState().isStopped()).isFalse();
+    assertThat(picoContainer.getLifecycleState().isDisposed()).isTrue();
+  }
+}