]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-20578 remove dependency from common part to extension
authorAurelien Poscia <aurelien.poscia@sonarsource.com>
Thu, 28 Sep 2023 08:15:21 +0000 (10:15 +0200)
committersonartech <sonartech@sonarsource.com>
Thu, 28 Sep 2023 20:03:12 +0000 (20:03 +0000)
server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/config/PlatformLevel4WebConfig.java
server/sonar-webserver/src/main/java/org/sonar/server/platform/web/ApiV2Servlet.java
sonar-core/src/main/java/org/sonar/core/extension/CoreExtension.java
sonar-core/src/main/java/org/sonar/core/extension/CoreExtensionsInstaller.java
sonar-core/src/main/java/org/sonar/core/platform/ExtensionContainer.java
sonar-core/src/main/java/org/sonar/core/platform/ListContainer.java
sonar-core/src/main/java/org/sonar/core/platform/SpringComponentContainer.java
sonar-core/src/test/java/org/sonar/core/platform/ListContainerTest.java
sonar-core/src/test/java/org/sonar/core/platform/SpringComponentContainerTest.java

index 32801254bb1c850024785ef06d0f6194b1d2bb61..9231cc10c6b5481a54ef41bc3d4f0fba982a9ac9 100644 (file)
@@ -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
index 6daa36d58c920c3cf139232af4c957a53113b0d2..9c32d81238421cf8035f939708f1681d706cb5b1 100644 (file)
@@ -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) {
index fb5b98df5cc248c43e7030432e1bd7eda5fe93e4..d95c2729fc392d34a06f42a49d813659e018e2e1 100644 (file)
@@ -50,6 +50,8 @@ public interface CoreExtension {
       addExtensions(asList(otherComponents));
       return this;
     }
+
+    void addWebApiV2ConfigurationClass(Class<?> clazz);
   }
 
   void load(Context context);
index d9f8c96296c571f00a65fc2f4a17d9d359209463..2fcf66028185ec904c1cd1644f59cf20fdbc7077 100644 (file)
@@ -128,6 +128,11 @@ public abstract class CoreExtensionsInstaller {
       return this;
     }
 
+    @Override
+    public void addWebApiV2ConfigurationClass(Class<?> clazz) {
+      container.addWebApiV2ConfigurationClass(clazz);
+    }
+
     @Override
     public <T> CoreExtension.Context addExtensions(Collection<T> components) {
       requireNonNull(components, "components can't be null");
index 18378e9a5a24b7401ddfbd2f3ace20e60fffd8fd..5a188c04b998648d53c42cd28c3de92701befa0b 100644 (file)
@@ -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<Class<?>> getWebApiV2ConfigurationClasses();
+
   @CheckForNull
   ExtensionContainer getParent();
 }
index b96609d3f5c489b78d44d4d0f5f0b1f46f18b571..cd2f93369976a93e94bca383b8dd519bc76d4f3c 100644 (file)
@@ -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<Object> objects = new ArrayList<>();
+  private final Set<Class<?>> 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<Class<?>> getWebApiV2ConfigurationClasses() {
+    return webConfigurationClasses;
+  }
+
   @Override
   public ExtensionContainer getParent() {
     throw new UnsupportedOperationException();
index 596398dc4e01bca8e2643a99b155a77898a73087..142b578048344598ccb745d2a0c9c6ba4f7e6ee3 100644 (file)
@@ -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<SpringComponentContainer> children = new ArrayList<>();
+  private final Set<Class<?>> 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<Class<?>> getWebApiV2ConfigurationClasses() {
+    return Set.copyOf(webConfigurationClasses);
+  }
+
   private <T> void registerInstance(T instance) {
     Supplier<T> supplier = () -> instance;
     Class<T> clazz = (Class<T>) instance.getClass();
index a2d2dad863362eaa27f6aad9a821179a6b16a777..492b8149be6530e26c42dc5593e9daeb9f2ee511 100644 (file)
@@ -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 {
   }
 
index 15528d99cab9ef3c94f6caa0a748628002931a0a..02a2ba31d440fdeaf3506995c1be60993200e1f8 100644 (file)
@@ -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();