diff options
11 files changed, 96 insertions, 21 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/AbstractTaskModule.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/AbstractTaskModule.java index 651d24ef9b9..de66cbbf39a 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/AbstractTaskModule.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/AbstractTaskModule.java @@ -64,9 +64,11 @@ public abstract class AbstractTaskModule extends Module { private static final Logger LOG = LoggerFactory.getLogger(AbstractTaskModule.class); private TaskDefinition task; + private boolean projectPresent; - public AbstractTaskModule(TaskDefinition task) { + public AbstractTaskModule(TaskDefinition task, boolean projectPresent) { this.task = task; + this.projectPresent = projectPresent; } @Override @@ -130,7 +132,7 @@ public abstract class AbstractTaskModule extends Module { private void registerTaskExtensions() { ExtensionInstaller installer = container.getComponentByType(ExtensionInstaller.class); - installer.installTaskExtensions(container); + installer.installTaskExtensions(container, projectPresent); } private void logSettings() { diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ExtensionInstaller.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ExtensionInstaller.java index 068a2b69730..396a60841d3 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ExtensionInstaller.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ExtensionInstaller.java @@ -98,37 +98,38 @@ public class ExtensionInstaller implements BatchComponent { return installed; } - public void installTaskExtensions(ComponentContainer container) { + public void installTaskExtensions(ComponentContainer container, boolean projectPresent) { for (Map.Entry<PluginMetadata, Plugin> entry : pluginRepository.getPluginsByMetadata().entrySet()) { PluginMetadata metadata = entry.getKey(); Plugin plugin = entry.getValue(); container.addExtension(metadata, plugin); for (Object extension : plugin.getExtensions()) { - installTaskExtension(container, metadata, extension); + installTaskExtension(container, metadata, extension, projectPresent); } } List<ExtensionProvider> providers = container.getComponentsByType(ExtensionProvider.class); for (ExtensionProvider provider : providers) { - executeTaskExtensionProvider(container, provider); + executeTaskExtensionProvider(container, provider, projectPresent); } } - private void executeTaskExtensionProvider(ComponentContainer container, ExtensionProvider provider) { + private void executeTaskExtensionProvider(ComponentContainer container, ExtensionProvider provider, boolean projectPresent) { Object obj = provider.provide(); if (obj instanceof Iterable) { for (Object extension : (Iterable) obj) { - installTaskExtension(container, null, extension); + installTaskExtension(container, null, extension, projectPresent); } } else { - installTaskExtension(container, null, obj); + installTaskExtension(container, null, obj, projectPresent); } } - boolean installTaskExtension(ComponentContainer container, @Nullable PluginMetadata plugin, Object extension) { + boolean installTaskExtension(ComponentContainer container, @Nullable PluginMetadata plugin, Object extension, boolean projectPresent) { boolean installed; if (ExtensionUtils.isTaskExtension(extension) && + (projectPresent || !ExtensionUtils.requireProject(extension)) && ExtensionUtils.supportsEnvironment(extension, environment)) { if (plugin != null) { LOG.debug("Installing task extension {} from plugin {}", extension.toString(), plugin.getKey()); diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ExtensionUtils.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ExtensionUtils.java index fd6a1ba2252..40fa0286aca 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ExtensionUtils.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ExtensionUtils.java @@ -19,6 +19,8 @@ */ package org.sonar.batch.bootstrap; +import org.sonar.api.batch.RequiresProject; + import org.apache.commons.lang.StringUtils; import org.sonar.api.BatchExtension; import org.sonar.api.Extension; @@ -73,6 +75,10 @@ final class ExtensionUtils { return AnnotationUtils.getAnnotation(extension, DryRunIncompatible.class) == null; } + static boolean requireProject(Object extension) { + return AnnotationUtils.getAnnotation(extension, RequiresProject.class) == null; + } + static boolean isMavenExtensionOnly(Object extension) { SupportedEnvironment env = AnnotationUtils.getAnnotation(extension, SupportedEnvironment.class); return env != null && env.value().length == 1 && StringUtils.equalsIgnoreCase("maven", env.value()[0]); diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ProjectExclusions.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ProjectExclusions.java index e2c9c53db01..4cd50965e29 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ProjectExclusions.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ProjectExclusions.java @@ -23,8 +23,8 @@ import org.apache.commons.lang.ArrayUtils; import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.sonar.api.BatchComponent; import org.sonar.api.CoreProperties; +import org.sonar.api.TaskComponent; import org.sonar.api.batch.bootstrap.ProjectBuilder; import org.sonar.api.batch.bootstrap.ProjectDefinition; import org.sonar.api.batch.bootstrap.ProjectReactor; @@ -35,7 +35,7 @@ import org.sonar.api.config.Settings; * * @since 2.12 */ -public class ProjectExclusions implements BatchComponent { +public class ProjectExclusions implements TaskComponent { private static final Logger LOG = LoggerFactory.getLogger(ProjectExclusions.class); diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ProjectLessTaskModule.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ProjectLessTaskModule.java index 75f932deeb1..603a39fa185 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ProjectLessTaskModule.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ProjectLessTaskModule.java @@ -24,7 +24,7 @@ import org.sonar.api.batch.TaskDefinition; public class ProjectLessTaskModule extends AbstractTaskModule { public ProjectLessTaskModule(TaskDefinition task) { - super(task); + super(task, false); } @Override diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ProjectTaskModule.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ProjectTaskModule.java index a7c7d3b97ee..124ac8b70bd 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ProjectTaskModule.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ProjectTaskModule.java @@ -28,7 +28,7 @@ import org.sonar.batch.index.DefaultIndex; public class ProjectTaskModule extends AbstractTaskModule { public ProjectTaskModule(TaskDefinition task) { - super(task); + super(task, true); } @Override diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/TaskBootstrapModule.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/TaskBootstrapModule.java index 5c98364162f..f25bc762354 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/TaskBootstrapModule.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/TaskBootstrapModule.java @@ -61,11 +61,12 @@ public class TaskBootstrapModule extends Module { } private void executeTask(TaskDefinition task) { - if (task.getTaskDescriptor().isRequiresProject() && container.getComponentByType(ProjectReactor.class) == null) { + boolean projectPresent = container.getComponentByType(ProjectReactor.class) != null; + if (task.getTaskDescriptor().isRequiresProject() && !projectPresent) { throw new SonarException("Task " + task.getTaskDescriptor().getName() + " requires to be run on a project"); } Module childModule; - if (task.getTaskDescriptor().isRequiresProject()) { + if (projectPresent) { childModule = new ProjectTaskModule(task); } else { diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/TaskComponent.java b/sonar-plugin-api/src/main/java/org/sonar/api/TaskComponent.java new file mode 100644 index 00000000000..9355625a708 --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/TaskComponent.java @@ -0,0 +1,29 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api; + +/** + * Dependency Injection : all the classes implementing this interface are available in the task IoC container. + * Just add a parameter to the constructor of your component. + * + * @since 3.5 + */ +public interface TaskComponent { +} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/TaskExtension.java b/sonar-plugin-api/src/main/java/org/sonar/api/TaskExtension.java index 147d7237d5e..4da612ec234 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/TaskExtension.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/TaskExtension.java @@ -24,5 +24,5 @@ package org.sonar.api; * * @since 3.5 */ -public interface TaskExtension extends Extension { +public interface TaskExtension extends Extension, TaskComponent { } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/RequiresProject.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/RequiresProject.java new file mode 100644 index 00000000000..010cd0f82e6 --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/RequiresProject.java @@ -0,0 +1,36 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.batch; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * The presence of this annotation on a task extension class indicates that the extension + * will be disabled when there is no project available. + * + * @since 3.5 + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) +public @interface RequiresProject { +} 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 7de741cc6c9..35a5b108983 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 @@ -19,11 +19,11 @@ */ package org.sonar.api.batch.bootstrap; -import org.sonar.api.BatchExtension; -import org.sonar.api.batch.InstantiationStrategy; +import org.sonar.api.TaskExtension; +import org.sonar.api.batch.RequiresProject; /** - * This extension point allows to change project structure at runtime. It is executed once during batch startup. + * This extension point allows to change project structure at runtime. It is executed once during task startup. * Some use-cases : * <ul> * <li>Add sub-projects which are not defined in batch bootstrapper. For example the C# plugin gets the hierarchy @@ -34,8 +34,8 @@ import org.sonar.api.batch.InstantiationStrategy; * * @since 2.9 */ -@InstantiationStrategy(InstantiationStrategy.PER_BATCH) -public abstract class ProjectBuilder implements BatchExtension { +@RequiresProject +public abstract class ProjectBuilder implements TaskExtension { private ProjectReactor reactor; |