aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-server
diff options
context:
space:
mode:
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>2017-08-29 17:45:26 +0200
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>2017-09-13 15:50:50 +0200
commit128f5f74e20b7391331840493a899f8bec494059 (patch)
tree80b7b4c152f50e7ab76ecd2073cbb2acfd2da03c /server/sonar-server
parent45d340cd14fc49494f832943e6d7ea109af87ec3 (diff)
downloadsonarqube-128f5f74e20b7391331840493a899f8bec494059.tar.gz
sonarqube-128f5f74e20b7391331840493a899f8bec494059.zip
SONAR-9741 add PlatformLevel#addIfCluster
Diffstat (limited to 'server/sonar-server')
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel.java62
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/platform/platformlevel/PlatformLevelTest.java75
2 files changed, 119 insertions, 18 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel.java b/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel.java
index 73e108e749d..cd09c49490b 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel.java
@@ -29,7 +29,6 @@ import org.sonar.core.platform.Module;
import org.sonar.server.platform.WebServer;
import static com.google.common.base.Preconditions.checkNotNull;
-import static com.google.common.base.Preconditions.checkState;
import static java.util.Objects.requireNonNull;
public abstract class PlatformLevel {
@@ -37,6 +36,8 @@ public abstract class PlatformLevel {
@Nullable
private final PlatformLevel parent;
private final ComponentContainer container;
+ private AddIfStartupLeader addIfStartupLeader;
+ private AddIfCluster addIfCluster;
public PlatformLevel(String name) {
this.name = name;
@@ -130,41 +131,66 @@ public abstract class PlatformLevel {
}
/**
- * Add a component to container only if the server node is marked as "startupLeader" (cluster disabled
- * or first node of the cluster to be started).
+ * Add a component to container only if the web server is startup leader.
*
* @throws IllegalStateException if called from PlatformLevel1, when cluster settings are not loaded
*/
- protected AddIfStartupLeader addIfStartupLeader(Object... objects) {
- AddIfStartupLeader res = new AddIfStartupLeader(isStartupLeader());
- res.ifAdd(objects);
- return res;
+ AddIfStartupLeader addIfStartupLeader(Object... objects) {
+ if (addIfStartupLeader == null) {
+ this.addIfStartupLeader = new AddIfStartupLeader(getWebServer().isStartupLeader());
+ }
+ addIfStartupLeader.ifAdd(objects);
+ return addIfStartupLeader;
+ }
+
+ /**
+ * Add a component to container only if clustering is enabled.
+ *
+ * @throws IllegalStateException if called from PlatformLevel1, when cluster settings are not loaded
+ */
+ AddIfCluster addIfCluster(Object... objects) {
+ if (addIfCluster == null) {
+ addIfCluster = new AddIfCluster(!getWebServer().isStandalone());
+ }
+ addIfCluster.ifAdd(objects);
+ return addIfCluster;
+ }
+
+ private WebServer getWebServer() {
+ return getOptional(WebServer.class)
+ .orElseThrow(() -> new IllegalStateException("WebServer not available in Pico yet"));
}
- public final class AddIfStartupLeader {
- private final boolean startupLeader;
+ private abstract class AddIf {
+ private final boolean condition;
- private AddIfStartupLeader(boolean startupLeader) {
- this.startupLeader = startupLeader;
+ private AddIf(boolean condition) {
+ this.condition = condition;
}
- private void ifAdd(Object... objects) {
- if (startupLeader) {
+ public void ifAdd(Object... objects) {
+ if (condition) {
PlatformLevel.this.add(objects);
}
}
public void otherwiseAdd(Object... objects) {
- if (!startupLeader) {
+ if (!condition) {
PlatformLevel.this.add(objects);
}
}
}
- private boolean isStartupLeader() {
- Optional<WebServer> cluster = getOptional(WebServer.class);
- checkState(cluster.isPresent(), "Cluster settings not loaded yet");
- return cluster.get().isStartupLeader();
+ public final class AddIfStartupLeader extends AddIf {
+ private AddIfStartupLeader(boolean condition) {
+ super(condition);
+ }
+ }
+
+ public final class AddIfCluster extends AddIf {
+ private AddIfCluster(boolean condition) {
+ super(condition);
+ }
}
protected void addAll(Collection<?> objects) {
diff --git a/server/sonar-server/src/test/java/org/sonar/server/platform/platformlevel/PlatformLevelTest.java b/server/sonar-server/src/test/java/org/sonar/server/platform/platformlevel/PlatformLevelTest.java
new file mode 100644
index 00000000000..ae60e8dbdb0
--- /dev/null
+++ b/server/sonar-server/src/test/java/org/sonar/server/platform/platformlevel/PlatformLevelTest.java
@@ -0,0 +1,75 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2017 SonarSource SA
+ * mailto:info 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.server.platform.platformlevel;
+
+import java.util.Random;
+import java.util.stream.IntStream;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.mockito.Mockito;
+import org.sonar.server.platform.WebServer;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class PlatformLevelTest {
+ @Rule
+ public ExpectedException expectedException = ExpectedException.none();
+
+ private PlatformLevel underTest = new PlatformLevel("name") {
+
+ @Override
+ protected void configureLevel() {
+
+ }
+ };
+
+ @Test
+ public void addIfStartupLeader_throws_ISE_if_container_does_not_have_WebServer_object() {
+ expectedException.expect(IllegalStateException.class);
+ expectedException.expectMessage("WebServer not available in Pico yet");
+
+ underTest.addIfStartupLeader();
+ }
+
+ @Test
+ public void addIfStartupLeader_always_returns_the_same_instance() {
+ underTest.add(Mockito.mock(WebServer.class));
+
+ PlatformLevel.AddIfStartupLeader addIfStartupLeader = underTest.addIfStartupLeader();
+ IntStream.range(0, 1 + new Random().nextInt(4)).forEach(i -> assertThat(underTest.addIfStartupLeader()).isSameAs(addIfStartupLeader));
+ }
+
+ @Test
+ public void addIfCluster_throws_ISE_if_container_does_not_have_WebServer_object() {
+ expectedException.expect(IllegalStateException.class);
+ expectedException.expectMessage("WebServer not available in Pico yet");
+
+ underTest.addIfCluster();
+ }
+
+ @Test
+ public void addIfCluster_always_returns_the_same_instance() {
+ underTest.add(Mockito.mock(WebServer.class));
+
+ PlatformLevel.AddIfCluster addIfCluster = underTest.addIfCluster();
+ IntStream.range(0, 1 + new Random().nextInt(4)).forEach(i -> assertThat(underTest.addIfCluster()).isSameAs(addIfCluster));
+ }
+}