From 43cf9933e690a773d4d3477e2584ff43aa02e7ac Mon Sep 17 00:00:00 2001 From: Wojtek Wajerowicz <115081248+wojciech-wajerowicz-sonarsource@users.noreply.github.com> Date: Mon, 6 Mar 2023 15:15:05 +0100 Subject: [PATCH] SONAR-18654 Component for indicating if instance is externally managed. --- .../DelegatingManagedInstanceService.java | 39 ++++++++++++++ .../management/ManagedInstanceService.java | 27 ++++++++++ .../DelegatingManagedInstanceServiceTest.java | 52 +++++++++++++++++++ .../platformlevel/PlatformLevel4.java | 5 +- 4 files changed, 121 insertions(+), 2 deletions(-) create mode 100644 server/sonar-server-common/src/main/java/org/sonar/server/management/DelegatingManagedInstanceService.java create mode 100644 server/sonar-server-common/src/main/java/org/sonar/server/management/ManagedInstanceService.java create mode 100644 server/sonar-server-common/src/test/java/org/sonar/server/management/DelegatingManagedInstanceServiceTest.java diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/management/DelegatingManagedInstanceService.java b/server/sonar-server-common/src/main/java/org/sonar/server/management/DelegatingManagedInstanceService.java new file mode 100644 index 00000000000..6fa3c9782af --- /dev/null +++ b/server/sonar-server-common/src/main/java/org/sonar/server/management/DelegatingManagedInstanceService.java @@ -0,0 +1,39 @@ +/* + * SonarQube + * Copyright (C) 2009-2023 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.management; + +import java.util.Set; +import javax.annotation.Priority; +import org.sonar.api.server.ServerSide; + +@ServerSide +@Priority(ManagedInstanceService.DELEGATING_INSTANCE_PRIORITY) +public class DelegatingManagedInstanceService implements ManagedInstanceService { + + private final Set delegates; + + public DelegatingManagedInstanceService(Set delegates) { + this.delegates = delegates; + } + + public final boolean isInstanceExternallyManaged() { + return delegates.stream().anyMatch(ManagedInstanceService::isInstanceExternallyManaged); + } +} diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/management/ManagedInstanceService.java b/server/sonar-server-common/src/main/java/org/sonar/server/management/ManagedInstanceService.java new file mode 100644 index 00000000000..00add360ad1 --- /dev/null +++ b/server/sonar-server-common/src/main/java/org/sonar/server/management/ManagedInstanceService.java @@ -0,0 +1,27 @@ +/* + * SonarQube + * Copyright (C) 2009-2023 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.management; + +public interface ManagedInstanceService { + + int DELEGATING_INSTANCE_PRIORITY = 1; + + boolean isInstanceExternallyManaged(); +} diff --git a/server/sonar-server-common/src/test/java/org/sonar/server/management/DelegatingManagedInstanceServiceTest.java b/server/sonar-server-common/src/test/java/org/sonar/server/management/DelegatingManagedInstanceServiceTest.java new file mode 100644 index 00000000000..a772a261002 --- /dev/null +++ b/server/sonar-server-common/src/test/java/org/sonar/server/management/DelegatingManagedInstanceServiceTest.java @@ -0,0 +1,52 @@ +/* + * SonarQube + * Copyright (C) 2009-2023 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.management; + +import java.util.Collections; +import java.util.Set; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.junit.MockitoJUnitRunner; + +import static org.assertj.core.api.Assertions.assertThat; + +@RunWith(MockitoJUnitRunner.class) +public class DelegatingManagedInstanceServiceTest { + + @Test + public void isInstanceExternallyManaged_whenNoChecksDefined_returnsFalse() { + DelegatingManagedInstanceService checker = new DelegatingManagedInstanceService(Collections.emptySet()); + assertThat(checker.isInstanceExternallyManaged()).isFalse(); + } + + @Test + public void isInstanceExternallyManaged_whenAllChecksReturnFalse_returnsFalse() { + + DelegatingManagedInstanceService checker = new DelegatingManagedInstanceService(Set.of(() -> false, () -> false)); + assertThat(checker.isInstanceExternallyManaged()).isFalse(); + } + + @Test + public void isInstanceExternallyManaged_whenOneCheckReturnsTrue_returnsTrue() { + DelegatingManagedInstanceService checker = new DelegatingManagedInstanceService(Set.of(() -> false, () -> true, () -> false)); + assertThat(checker.isInstanceExternallyManaged()).isTrue(); + } + +} diff --git a/server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java b/server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java index c57259da763..10fe7bc7554 100644 --- a/server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java +++ b/server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java @@ -123,6 +123,7 @@ import org.sonar.server.language.LanguageValidation; import org.sonar.server.language.ws.LanguageWs; import org.sonar.server.log.ServerLogging; import org.sonar.server.loginmessage.LoginMessageFeature; +import org.sonar.server.management.DelegatingManagedInstanceService; import org.sonar.server.measure.index.ProjectsEsModule; import org.sonar.server.measure.live.LiveMeasureModule; import org.sonar.server.measure.ws.MeasuresWsModule; @@ -302,6 +303,7 @@ public class PlatformLevel4 extends PlatformLevel { new MonitoringWsModule(), DefaultBranchNameResolver.class, DefaultDocumentationLinkGenerator.class, + DelegatingManagedInstanceService.class, // batch new BatchWsModule(), @@ -634,8 +636,7 @@ public class PlatformLevel4 extends PlatformLevel { MainCollector.class, - PluginsRiskConsentFilter.class - ); + PluginsRiskConsentFilter.class); // system info add(new SystemInfoWriterModule(getWebServer())); -- 2.39.5