From 5bfc3521a7245389ca2dbf3fb6f8d32291df82de Mon Sep 17 00:00:00 2001 From: Aurelien Poscia Date: Thu, 28 Sep 2023 10:15:21 +0200 Subject: [PATCH] SONAR-20578 remove dependency from common part to extension --- .../server/v2/config/PlatformLevel4WebConfig.java | 2 -- .../org/sonar/server/platform/web/ApiV2Servlet.java | 4 ++++ .../org/sonar/core/extension/CoreExtension.java | 2 ++ .../core/extension/CoreExtensionsInstaller.java | 5 +++++ .../org/sonar/core/platform/ExtensionContainer.java | 5 +++++ .../java/org/sonar/core/platform/ListContainer.java | 13 +++++++++++++ .../core/platform/SpringComponentContainer.java | 13 +++++++++++++ .../org/sonar/core/platform/ListContainerTest.java | 7 +++++++ .../core/platform/SpringComponentContainerTest.java | 7 +++++++ 9 files changed, 56 insertions(+), 2 deletions(-) diff --git a/server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/config/PlatformLevel4WebConfig.java b/server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/config/PlatformLevel4WebConfig.java index 32801254bb1..9231cc10c6b 100644 --- a/server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/config/PlatformLevel4WebConfig.java +++ b/server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/config/PlatformLevel4WebConfig.java @@ -38,13 +38,11 @@ import org.sonar.server.v2.api.user.controller.DefaultUserController; import org.sonar.server.v2.api.user.controller.UserController; import org.sonar.server.v2.api.user.converter.UsersSearchRestResponseGenerator; import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; @Configuration @Import(CommonWebConfig.class) -@ComponentScan("com.sonar.github.provisioning.permissions.web.config") public class PlatformLevel4WebConfig { @Bean diff --git a/server/sonar-webserver/src/main/java/org/sonar/server/platform/web/ApiV2Servlet.java b/server/sonar-webserver/src/main/java/org/sonar/server/platform/web/ApiV2Servlet.java index 6daa36d58c9..9c32d812384 100644 --- a/server/sonar-webserver/src/main/java/org/sonar/server/platform/web/ApiV2Servlet.java +++ b/server/sonar-webserver/src/main/java/org/sonar/server/platform/web/ApiV2Servlet.java @@ -90,6 +90,10 @@ public class ApiV2Servlet implements Servlet { AnnotationConfigWebApplicationContext springMvcContext = new AnnotationConfigWebApplicationContext(); springMvcContext.setParent(platformLevel.getContainer().context()); springMvcContext.register(configClass); + if (PlatformLevel4WebConfig.class.equals(configClass)) { + platformLevel.getContainer().getWebApiV2ConfigurationClasses().forEach(springMvcContext::register); + } + DispatcherServlet dispatcher = servletProvider.apply(springMvcContext); try { if (config != null) { diff --git a/sonar-core/src/main/java/org/sonar/core/extension/CoreExtension.java b/sonar-core/src/main/java/org/sonar/core/extension/CoreExtension.java index fb5b98df5cc..d95c2729fc3 100644 --- a/sonar-core/src/main/java/org/sonar/core/extension/CoreExtension.java +++ b/sonar-core/src/main/java/org/sonar/core/extension/CoreExtension.java @@ -50,6 +50,8 @@ public interface CoreExtension { addExtensions(asList(otherComponents)); return this; } + + void addWebApiV2ConfigurationClass(Class clazz); } void load(Context context); diff --git a/sonar-core/src/main/java/org/sonar/core/extension/CoreExtensionsInstaller.java b/sonar-core/src/main/java/org/sonar/core/extension/CoreExtensionsInstaller.java index d9f8c96296c..2fcf6602818 100644 --- a/sonar-core/src/main/java/org/sonar/core/extension/CoreExtensionsInstaller.java +++ b/sonar-core/src/main/java/org/sonar/core/extension/CoreExtensionsInstaller.java @@ -128,6 +128,11 @@ public abstract class CoreExtensionsInstaller { return this; } + @Override + public void addWebApiV2ConfigurationClass(Class clazz) { + container.addWebApiV2ConfigurationClass(clazz); + } + @Override public CoreExtension.Context addExtensions(Collection components) { requireNonNull(components, "components can't be null"); diff --git a/sonar-core/src/main/java/org/sonar/core/platform/ExtensionContainer.java b/sonar-core/src/main/java/org/sonar/core/platform/ExtensionContainer.java index 18378e9a5a2..5a188c04b99 100644 --- a/sonar-core/src/main/java/org/sonar/core/platform/ExtensionContainer.java +++ b/sonar-core/src/main/java/org/sonar/core/platform/ExtensionContainer.java @@ -19,6 +19,7 @@ */ package org.sonar.core.platform; +import java.util.Set; import javax.annotation.CheckForNull; import javax.annotation.Nullable; @@ -31,6 +32,10 @@ public interface ExtensionContainer extends Container { ExtensionContainer declareExtension(@Nullable String defaultCategory, Object extension); + void addWebApiV2ConfigurationClass(Class clazz); + + Set> getWebApiV2ConfigurationClasses(); + @CheckForNull ExtensionContainer getParent(); } diff --git a/sonar-core/src/main/java/org/sonar/core/platform/ListContainer.java b/sonar-core/src/main/java/org/sonar/core/platform/ListContainer.java index b96609d3f5c..cd2f9336997 100644 --- a/sonar-core/src/main/java/org/sonar/core/platform/ListContainer.java +++ b/sonar-core/src/main/java/org/sonar/core/platform/ListContainer.java @@ -21,8 +21,10 @@ package org.sonar.core.platform; import com.google.common.collect.Iterables; import java.util.ArrayList; +import java.util.HashSet; import java.util.List; import java.util.Optional; +import java.util.Set; import javax.annotation.Nullable; import static java.util.Collections.unmodifiableList; @@ -32,6 +34,7 @@ import static java.util.Collections.unmodifiableList; */ public class ListContainer implements ExtensionContainer { private final List objects = new ArrayList<>(); + private final Set> webConfigurationClasses = new HashSet<>(); @Override public Container add(Object... objects) { @@ -88,6 +91,16 @@ public class ListContainer implements ExtensionContainer { return this; } + @Override + public void addWebApiV2ConfigurationClass(Class clazz) { + webConfigurationClasses.add(clazz); + } + + @Override + public Set> getWebApiV2ConfigurationClasses() { + return webConfigurationClasses; + } + @Override public ExtensionContainer getParent() { throw new UnsupportedOperationException(); diff --git a/sonar-core/src/main/java/org/sonar/core/platform/SpringComponentContainer.java b/sonar-core/src/main/java/org/sonar/core/platform/SpringComponentContainer.java index 596398dc4e0..142b5780483 100644 --- a/sonar-core/src/main/java/org/sonar/core/platform/SpringComponentContainer.java +++ b/sonar-core/src/main/java/org/sonar/core/platform/SpringComponentContainer.java @@ -22,8 +22,10 @@ package org.sonar.core.platform; import com.google.common.collect.Iterables; import com.google.common.collect.Lists; import java.util.ArrayList; +import java.util.HashSet; import java.util.List; import java.util.Optional; +import java.util.Set; import java.util.function.Supplier; import javax.annotation.CheckForNull; import javax.annotation.Nullable; @@ -41,6 +43,7 @@ public class SpringComponentContainer implements StartableContainer { @Nullable protected final SpringComponentContainer parent; protected final List children = new ArrayList<>(); + private final Set> webConfigurationClasses = new HashSet<>(); private final PropertyDefinitions propertyDefinitions; private final ComponentKeys componentKeys = new ComponentKeys(); @@ -107,6 +110,16 @@ public class SpringComponentContainer implements StartableContainer { return this; } + @Override + public void addWebApiV2ConfigurationClass(Class clazz) { + webConfigurationClasses.add(clazz); + } + + @Override + public Set> getWebApiV2ConfigurationClasses() { + return Set.copyOf(webConfigurationClasses); + } + private void registerInstance(T instance) { Supplier supplier = () -> instance; Class clazz = (Class) instance.getClass(); diff --git a/sonar-core/src/test/java/org/sonar/core/platform/ListContainerTest.java b/sonar-core/src/test/java/org/sonar/core/platform/ListContainerTest.java index a2d2dad8633..492b8149be6 100644 --- a/sonar-core/src/test/java/org/sonar/core/platform/ListContainerTest.java +++ b/sonar-core/src/test/java/org/sonar/core/platform/ListContainerTest.java @@ -67,6 +67,13 @@ public class ListContainerTest { assertThatThrownBy(container::getParent).isInstanceOf(UnsupportedOperationException.class); } + @Test + public void addWebApiV2ConfigurationClass_whenClassIsAdded_isReturnedByGetWebApiV2ConfigurationClasses() { + ListContainer container = new ListContainer(); + container.addWebApiV2ConfigurationClass(org.sonar.core.test.Test.class); + assertThat(container.getWebApiV2ConfigurationClasses()).containsOnly(org.sonar.core.test.Test.class); + } + class A { } diff --git a/sonar-core/src/test/java/org/sonar/core/platform/SpringComponentContainerTest.java b/sonar-core/src/test/java/org/sonar/core/platform/SpringComponentContainerTest.java index 15528d99cab..02a2ba31d44 100644 --- a/sonar-core/src/test/java/org/sonar/core/platform/SpringComponentContainerTest.java +++ b/sonar-core/src/test/java/org/sonar/core/platform/SpringComponentContainerTest.java @@ -78,6 +78,13 @@ public class SpringComponentContainerTest { assertThat(container.getComponentByType(B.class)).isNotNull(); } + @Test + public void addWebConfigurationClass_addsClass() { + SpringComponentContainer container = new SpringComponentContainer(); + container.addWebApiV2ConfigurationClass(org.sonar.core.test.Test.class); + assertThat(container.getWebApiV2ConfigurationClasses()).contains(org.sonar.core.test.Test.class); + } + @Test public void add_configures_module_instances() { SpringComponentContainer container = new SpringComponentContainer(); -- 2.39.5