From d099e513e4066280677117e130df54a4ef66c3b5 Mon Sep 17 00:00:00 2001 From: Julien HENRY Date: Tue, 11 Jun 2013 15:17:11 +0200 Subject: [PATCH] SONAR-4386 Refactor ProjectBuilder extension --- .../sonar/batch/scan/ProjectReactorReady.java | 7 +++++- .../batch/scan/ProjectScanContainer.java | 7 ------ .../batch/scan/ProjectSettingsReady.java | 5 ++++ .../api/batch/bootstrap/ProjectBuilder.java | 24 +++++++++++++++---- .../batch/bootstrap/ProjectBuilderTest.java | 17 ++++++------- 5 files changed, 38 insertions(+), 22 deletions(-) diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectReactorReady.java b/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectReactorReady.java index cfe6a4b99a1..550ec324b82 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectReactorReady.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectReactorReady.java @@ -40,17 +40,22 @@ public class ProjectReactorReady { private final ProjectReactor reactor; private final Settings settings; + private ProjectBuilder[] projectBuilders; public ProjectReactorReady(ProjectExclusions exclusions, ProjectReactor reactor, Settings settings, @Nullable ProjectBuilder[] projectBuilders) { this.reactor = reactor; this.settings = settings; + this.projectBuilders = projectBuilders; } public ProjectReactorReady(ProjectExclusions exclusions, ProjectReactor reactor, Settings settings) { - this(exclusions, reactor, settings, null); + this(exclusions, reactor, settings, new ProjectBuilder[0]); } public void start() { + for (ProjectBuilder projectBuilder : projectBuilders) { + projectBuilder.doBuild(reactor); + } ProjectReactorValidator validator = new ProjectReactorValidator(settings); validator.validate(reactor); } diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java b/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java index aaa1413b901..06030f10f2a 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java @@ -30,7 +30,6 @@ import org.sonar.batch.DefaultFileLinesContextFactory; import org.sonar.batch.DefaultResourceCreationLock; import org.sonar.batch.ProjectConfigurator; import org.sonar.batch.ProjectTree; -import org.sonar.batch.bootstrap.BatchSettings; import org.sonar.batch.bootstrap.ExtensionInstaller; import org.sonar.batch.bootstrap.ExtensionMatcher; import org.sonar.batch.bootstrap.ExtensionUtils; @@ -151,12 +150,6 @@ public class ProjectScanContainer extends ComponentContainer { scanRecursively(tree.getRootProject()); } - public void stop() { - // Remove project specific settings - BatchSettings settings = getComponentByType(BatchSettings.class); - settings.restore(); - } - private void scanRecursively(Project module) { for (Project subModules : module.getModules()) { scanRecursively(subModules); diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectSettingsReady.java b/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectSettingsReady.java index 4a267d55196..d839b281d10 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectSettingsReady.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectSettingsReady.java @@ -39,4 +39,9 @@ public class ProjectSettingsReady { public void start() { settings.init(reactor); } + + public void stop() { + // Remove project specific settings + settings.restore(); + } } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/bootstrap/ProjectBuilder.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/bootstrap/ProjectBuilder.java index 7f874f07619..b3adf46ad6d 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/bootstrap/ProjectBuilder.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/bootstrap/ProjectBuilder.java @@ -37,15 +37,31 @@ import org.sonar.api.batch.InstantiationStrategy; @InstantiationStrategy(InstantiationStrategy.PER_BATCH) public abstract class ProjectBuilder implements BatchExtension { - private ProjectReactor reactor; - + /** + * Don't inject ProjectReactor as it may not be available + * @deprecated since 3.7 use {@link #ProjectBuilder()} + */ + @Deprecated protected ProjectBuilder(final ProjectReactor reactor) { - this.reactor = reactor; } - public final void start() { + /** + * @since 3.7 + */ + protected ProjectBuilder() { + } + + /** + * This method was introduced to relax visibility of {@link #build(ProjectReactor)} + * @since 3.7 + */ + public final void doBuild(ProjectReactor reactor) { build(reactor); } + /** + * This method will be called by Sonar core to let you a chance to change project reactor structure. + * @param reactor + */ protected abstract void build(ProjectReactor reactor); } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/bootstrap/ProjectBuilderTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/bootstrap/ProjectBuilderTest.java index 9def3de0aa8..2ae474e3866 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/batch/bootstrap/ProjectBuilderTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/batch/bootstrap/ProjectBuilderTest.java @@ -19,9 +19,8 @@ */ package org.sonar.api.batch.bootstrap; -import org.apache.commons.configuration.Configuration; -import org.apache.commons.configuration.PropertiesConfiguration; import org.junit.Test; +import org.sonar.api.config.Settings; import java.io.File; @@ -33,11 +32,11 @@ public class ProjectBuilderTest { @Test public void shouldChangeProject() { - // this reactor is created and injected by Sonar + // this reactor is created and provided by Sonar ProjectReactor projectReactor = new ProjectReactor(ProjectDefinition.create()); - ProjectBuilder builder = new ProjectBuilderSample(projectReactor, new PropertiesConfiguration()); - builder.start(); + ProjectBuilder builder = new ProjectBuilderSample(new Settings()); + builder.doBuild(projectReactor); assertThat(projectReactor.getProjects().size(), is(2)); ProjectDefinition root = projectReactor.getRoot(); @@ -47,12 +46,10 @@ public class ProjectBuilderTest { } final static class ProjectBuilderSample extends ProjectBuilder { - private Configuration conf; + private Settings conf; - public ProjectBuilderSample(ProjectReactor reactor, Configuration conf) { - super(reactor); - - // A real implementation should for example use the configuration + public ProjectBuilderSample(Settings conf) { + // A real implementation should for example use the settings this.conf = conf; } -- 2.39.5