Browse Source

SONAR-6732 add ComputeEngineContainer + Properties from cmd line

tags/5.5-M11
Sébastien Lesaint 8 years ago
parent
commit
3591ebf719

+ 10
- 13
server/sonar-ce/src/main/java/org/sonar/ce/ComputeEngineImpl.java View File

@@ -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;
}

+ 54
- 0
server/sonar-ce/src/main/java/org/sonar/ce/container/ComputeEngineContainer.java View File

@@ -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;
}
}

+ 23
- 0
server/sonar-ce/src/main/java/org/sonar/ce/container/package-info.java View File

@@ -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;

+ 76
- 0
server/sonar-ce/src/test/java/org/sonar/ce/container/ComputeEngineContainerTest.java View File

@@ -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();
}
}

Loading…
Cancel
Save