diff options
author | Duarte Meneses <duarte.meneses@sonarsource.com> | 2022-02-01 15:16:25 -0600 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2022-02-22 20:02:46 +0000 |
commit | 60c1a4038e041a342dda9810e6fd761d66b01bdb (patch) | |
tree | 0e76b4252e4d7d257cf4ddcb6f081996bb1e03ab /server/sonar-ce-task | |
parent | 9694d4113bf401b84e86e0223dbea8f5339388d8 (diff) | |
download | sonarqube-60c1a4038e041a342dda9810e6fd761d66b01bdb.tar.gz sonarqube-60c1a4038e041a342dda9810e6fd761d66b01bdb.zip |
SONAR-15994 Migrate Sonarqube IOC framework from Pico to Spring
Diffstat (limited to 'server/sonar-ce-task')
8 files changed, 59 insertions, 69 deletions
diff --git a/server/sonar-ce-task/build.gradle b/server/sonar-ce-task/build.gradle index 6a18c0ce329..d7d426b8b51 100644 --- a/server/sonar-ce-task/build.gradle +++ b/server/sonar-ce-task/build.gradle @@ -18,7 +18,6 @@ dependencies { // please keep the list grouped by configuration and ordered by name compile 'com.google.guava:guava' - compile 'org.picocontainer:picocontainer' compile 'org.slf4j:jul-to-slf4j' compile 'org.slf4j:slf4j-api' diff --git a/server/sonar-ce-task/src/main/java/org/sonar/ce/task/container/LazyUnlessEagerAnnotationStrategy.java b/server/sonar-ce-task/src/main/java/org/sonar/ce/task/container/LazyUnlessEagerAnnotationStrategy.java new file mode 100644 index 00000000000..b5b438892a4 --- /dev/null +++ b/server/sonar-ce-task/src/main/java/org/sonar/ce/task/container/LazyUnlessEagerAnnotationStrategy.java @@ -0,0 +1,31 @@ +/* + * SonarQube + * Copyright (C) 2009-2022 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.ce.task.container; + +import javax.annotation.Nullable; +import org.sonar.core.platform.SpringInitStrategy; +import org.springframework.beans.factory.config.BeanDefinition; + +public class LazyUnlessEagerAnnotationStrategy extends SpringInitStrategy { + @Override + protected boolean isLazyInit(BeanDefinition beanDefinition, @Nullable Class<?> clazz) { + return clazz == null || clazz.getAnnotation(EagerStart.class) == null; + } +} diff --git a/server/sonar-ce-task/src/main/java/org/sonar/ce/task/container/TaskContainer.java b/server/sonar-ce-task/src/main/java/org/sonar/ce/task/container/TaskContainer.java index 2a3979f1c7f..c38d70e5002 100644 --- a/server/sonar-ce-task/src/main/java/org/sonar/ce/task/container/TaskContainer.java +++ b/server/sonar-ce-task/src/main/java/org/sonar/ce/task/container/TaskContainer.java @@ -19,17 +19,16 @@ */ package org.sonar.ce.task.container; -import org.picocontainer.PicoContainer; import org.sonar.ce.task.CeTask; -import org.sonar.core.platform.ComponentContainer; import org.sonar.core.platform.Container; +import org.sonar.core.platform.ExtensionContainer; /** - * The Compute Engine task container. Created for a specific parent {@link ComponentContainer} and a specific {@link CeTask}. + * The Compute Engine task container. Created for a specific parent {@link ExtensionContainer} and a specific {@link CeTask}. */ public interface TaskContainer extends Container, AutoCloseable { - ComponentContainer getParent(); + ExtensionContainer getParent(); /** * Starts task container, starting any startable component in it. @@ -41,10 +40,4 @@ public interface TaskContainer extends Container, AutoCloseable { */ @Override void close(); - - /** - * Access to the underlying pico container. - */ - PicoContainer getPicoContainer(); - } diff --git a/server/sonar-ce-task/src/main/java/org/sonar/ce/task/container/TaskContainerImpl.java b/server/sonar-ce-task/src/main/java/org/sonar/ce/task/container/TaskContainerImpl.java index c8d67ab2a73..6c514a93911 100644 --- a/server/sonar-ce-task/src/main/java/org/sonar/ce/task/container/TaskContainerImpl.java +++ b/server/sonar-ce-task/src/main/java/org/sonar/ce/task/container/TaskContainerImpl.java @@ -19,55 +19,21 @@ */ package org.sonar.ce.task.container; -import java.util.List; -import org.picocontainer.ComponentAdapter; -import org.picocontainer.DefaultPicoContainer; -import org.picocontainer.LifecycleStrategy; -import org.picocontainer.MutablePicoContainer; -import org.picocontainer.behaviors.OptInCaching; -import org.picocontainer.monitors.NullComponentMonitor; -import org.sonar.api.config.PropertyDefinitions; import org.sonar.api.utils.log.Loggers; -import org.sonar.core.platform.ComponentContainer; import org.sonar.core.platform.ContainerPopulator; -import org.sonar.core.platform.Module; -import org.sonar.core.platform.StartableCloseableSafeLifecyleStrategy; +import org.sonar.core.platform.SpringComponentContainer; import static java.util.Objects.requireNonNull; -public class TaskContainerImpl extends ComponentContainer implements TaskContainer { - - public TaskContainerImpl(ComponentContainer parent, ContainerPopulator<TaskContainer> populator) { - super(createContainer(requireNonNull(parent)), parent.getComponentByType(PropertyDefinitions.class)); +public class TaskContainerImpl extends SpringComponentContainer implements TaskContainer { + public TaskContainerImpl(SpringComponentContainer parent, ContainerPopulator<TaskContainer> populator) { + super(parent, new LazyUnlessEagerAnnotationStrategy()); populateContainer(requireNonNull(populator)); } private void populateContainer(ContainerPopulator<TaskContainer> populator) { populator.populateContainer(this); - populateFromModules(); - } - - private void populateFromModules() { - List<Module> modules = getComponentsByType(Module.class); - for (Module module : modules) { - module.configure(this); - } - } - - /** - * Creates a PicContainer which extends the specified ComponentContainer <strong>but is not referenced in return</strong> - * and lazily starts its components. - */ - private static MutablePicoContainer createContainer(ComponentContainer parent) { - LifecycleStrategy lifecycleStrategy = new StartableCloseableSafeLifecyleStrategy() { - @Override - public boolean isLazy(ComponentAdapter<?> adapter) { - return adapter.getComponentImplementation().getAnnotation(EagerStart.class) == null; - } - }; - - return new DefaultPicoContainer(new OptInCaching(), lifecycleStrategy, parent.getPicoContainer(), new NullComponentMonitor()); } @Override diff --git a/server/sonar-ce-task/src/main/java/org/sonar/ce/task/setting/SettingsLoader.java b/server/sonar-ce-task/src/main/java/org/sonar/ce/task/setting/SettingsLoader.java index d5fc039e991..cae9ed665da 100644 --- a/server/sonar-ce-task/src/main/java/org/sonar/ce/task/setting/SettingsLoader.java +++ b/server/sonar-ce-task/src/main/java/org/sonar/ce/task/setting/SettingsLoader.java @@ -19,7 +19,7 @@ */ package org.sonar.ce.task.setting; -import org.picocontainer.Startable; +import org.sonar.api.Startable; import org.sonar.api.ce.ComputeEngineSide; import org.sonar.api.config.internal.Settings; import org.sonar.ce.task.container.EagerStart; diff --git a/server/sonar-ce-task/src/main/java/org/sonar/ce/task/step/ComputationStepExecutor.java b/server/sonar-ce-task/src/main/java/org/sonar/ce/task/step/ComputationStepExecutor.java index b2ecc7d421a..b00f8b1432d 100644 --- a/server/sonar-ce-task/src/main/java/org/sonar/ce/task/step/ComputationStepExecutor.java +++ b/server/sonar-ce-task/src/main/java/org/sonar/ce/task/step/ComputationStepExecutor.java @@ -25,6 +25,7 @@ import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; import org.sonar.ce.task.CeTaskInterrupter; import org.sonar.core.util.logs.Profiler; +import org.springframework.beans.factory.annotation.Autowired; import static com.google.common.base.Preconditions.checkArgument; import static java.lang.String.format; @@ -38,14 +39,12 @@ public final class ComputationStepExecutor { @CheckForNull private final Listener listener; - /** - * Used when no {@link ComputationStepExecutor.Listener} is available in pico - * container. - */ + @Autowired(required = false) public ComputationStepExecutor(ComputationSteps steps, CeTaskInterrupter taskInterrupter) { this(steps, taskInterrupter, null); } + @Autowired(required = false) public ComputationStepExecutor(ComputationSteps steps, CeTaskInterrupter taskInterrupter, @Nullable Listener listener) { this.steps = steps; this.taskInterrupter = taskInterrupter; diff --git a/server/sonar-ce-task/src/main/java/org/sonar/ce/task/step/ExecuteStatelessInitExtensionsStep.java b/server/sonar-ce-task/src/main/java/org/sonar/ce/task/step/ExecuteStatelessInitExtensionsStep.java index 04294b1dff9..08f609e9cdf 100644 --- a/server/sonar-ce-task/src/main/java/org/sonar/ce/task/step/ExecuteStatelessInitExtensionsStep.java +++ b/server/sonar-ce-task/src/main/java/org/sonar/ce/task/step/ExecuteStatelessInitExtensionsStep.java @@ -20,6 +20,7 @@ package org.sonar.ce.task.step; import org.sonar.api.ce.ComputeEngineSide; +import org.springframework.beans.factory.annotation.Autowired; /** * Execute {@link StatelessInitExtension} instances in no specific order. @@ -31,6 +32,7 @@ public class ExecuteStatelessInitExtensionsStep implements ComputationStep { private final StatelessInitExtension[] extensions; + @Autowired(required = false) public ExecuteStatelessInitExtensionsStep(StatelessInitExtension[] extensions) { this.extensions = extensions; } @@ -38,6 +40,7 @@ public class ExecuteStatelessInitExtensionsStep implements ComputationStep { /** * Used when zero {@link StatelessInitExtension} are registered into container. */ + @Autowired(required = false) public ExecuteStatelessInitExtensionsStep() { this(new StatelessInitExtension[0]); } diff --git a/server/sonar-ce-task/src/test/java/org/sonar/ce/task/container/TaskContainerImplTest.java b/server/sonar-ce-task/src/test/java/org/sonar/ce/task/container/TaskContainerImplTest.java index b39be56616b..c2df2f4923c 100644 --- a/server/sonar-ce-task/src/test/java/org/sonar/ce/task/container/TaskContainerImplTest.java +++ b/server/sonar-ce-task/src/test/java/org/sonar/ce/task/container/TaskContainerImplTest.java @@ -19,10 +19,11 @@ */ package org.sonar.ce.task.container; +import org.junit.Before; import org.junit.Test; -import org.picocontainer.Startable; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.api.Startable; import org.sonar.core.platform.ContainerPopulator; +import org.sonar.core.platform.SpringComponentContainer; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; @@ -30,8 +31,13 @@ import static org.mockito.Mockito.spy; import static org.mockito.Mockito.verify; public class TaskContainerImplTest { - private ComponentContainer parent = new ComponentContainer(); - private ContainerPopulator<TaskContainer> populator = spy(new DummyContainerPopulator()); + private final SpringComponentContainer parent = new SpringComponentContainer(); + private final ContainerPopulator<TaskContainer> populator = spy(new DummyContainerPopulator()); + + @Before + public void before() { + parent.startComponents(); + } @Test public void constructor_fails_fast_on_null_container() { @@ -41,7 +47,8 @@ public class TaskContainerImplTest { @Test public void constructor_fails_fast_on_null_item() { - assertThatThrownBy(() -> new TaskContainerImpl(new ComponentContainer(), null)) + SpringComponentContainer c = new SpringComponentContainer(); + assertThatThrownBy(() -> new TaskContainerImpl(c, null)) .isInstanceOf(NullPointerException.class); } @@ -53,17 +60,9 @@ public class TaskContainerImplTest { } @Test - public void ce_container_is_not_child_of_specified_container() { - TaskContainerImpl ceContainer = new TaskContainerImpl(parent, populator); - - assertThat(parent.getChildren()).isEmpty(); - verify(populator).populateContainer(ceContainer); - } - - @Test public void bootup_starts_components_lazily_unless_they_are_annotated_with_EagerStart() { - final DefaultStartable defaultStartable = new DefaultStartable(); - final EagerStartable eagerStartable = new EagerStartable(); + DefaultStartable defaultStartable = new DefaultStartable(); + EagerStartable eagerStartable = new EagerStartable(); TaskContainerImpl ceContainer = new TaskContainerImpl(parent, container -> { container.add(defaultStartable); container.add(eagerStartable); |