From 3591ebf7197ad6704e9bf54f8e27cde8013988f7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Thu, 10 Mar 2016 17:46:28 +0100 Subject: [PATCH] SONAR-6732 add ComputeEngineContainer + Properties from cmd line --- .../java/org/sonar/ce/ComputeEngineImpl.java | 23 +++--- .../ce/container/ComputeEngineContainer.java | 54 +++++++++++++ .../org/sonar/ce/container/package-info.java | 23 ++++++ .../container/ComputeEngineContainerTest.java | 76 +++++++++++++++++++ 4 files changed, 163 insertions(+), 13 deletions(-) create mode 100644 server/sonar-ce/src/main/java/org/sonar/ce/container/ComputeEngineContainer.java create mode 100644 server/sonar-ce/src/main/java/org/sonar/ce/container/package-info.java create mode 100644 server/sonar-ce/src/test/java/org/sonar/ce/container/ComputeEngineContainerTest.java 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 95c593f3478..457139516d8 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 @@ -19,14 +19,16 @@ */ 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 index 00000000000..2caedf8d998 --- /dev/null +++ b/server/sonar-ce/src/main/java/org/sonar/ce/container/ComputeEngineContainer.java @@ -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 index 00000000000..72613833dfd --- /dev/null +++ b/server/sonar-ce/src/main/java/org/sonar/ce/container/package-info.java @@ -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 index 00000000000..8108ecadefa --- /dev/null +++ b/server/sonar-ce/src/test/java/org/sonar/ce/container/ComputeEngineContainerTest.java @@ -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(); + } +} -- 2.39.5