diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2013-02-26 16:53:29 +0100 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@gmail.com> | 2013-02-27 14:37:01 +0100 |
commit | b0fe2aae234489d21be964a9e8a1ffd699be4fbb (patch) | |
tree | 6e127ce10f3d1c4eb8724c43447cf29cbf849baf /sonar-batch | |
parent | b71ab81e064d952eb398700dba3dbe6ee8261afd (diff) | |
download | sonarqube-b0fe2aae234489d21be964a9e8a1ffd699be4fbb.tar.gz sonarqube-b0fe2aae234489d21be964a9e8a1ffd699be4fbb.zip |
Refactor ioc containers to allow views to work as a task.
Signed-off-by: Julien Lancelot <julien.lancelot@gmail.com>
Diffstat (limited to 'sonar-batch')
12 files changed, 328 insertions, 188 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BootstrapContainer.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BootstrapContainer.java index 1ded5571244..80f2c1f48bd 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BootstrapContainer.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BootstrapContainer.java @@ -58,9 +58,6 @@ public class BootstrapContainer extends Container { @Override protected void configure() { container.addSingleton(globalProperties); - if (reactor != null) { - container.addSingleton(reactor); - } container.addSingleton(new PropertiesConfiguration()); container.addSingleton(BootstrapSettings.class); container.addSingleton(ServerClient.class); @@ -98,7 +95,13 @@ public class BootstrapContainer extends Container { @Override protected void doStart() { - Container taskBootstrap = installChild(new TaskBootstrapContainer(taskCommand)); - taskBootstrap.start(); + Container childModule = new TaskBootstrapContainer(taskCommand, reactor); + try { + installChild(childModule); + childModule.start(); + } finally { + childModule.stop(); + uninstallChild(); + } } } 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 211128bde2f..3a3ab35e85d 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 @@ -79,7 +79,7 @@ public class ExtensionInstaller implements BatchComponent { return installed; } - public void installTaskExtensions(ComponentContainer container, boolean projectPresent) { + public void installTaskExtensions(ComponentContainer container, boolean requiringProject) { boolean dryRun = settings.getBoolean(CoreProperties.DRY_RUN); for (Map.Entry<PluginMetadata, Plugin> entry : pluginRepository.getPluginsByMetadata().entrySet()) { PluginMetadata metadata = entry.getKey(); @@ -87,8 +87,8 @@ public class ExtensionInstaller implements BatchComponent { container.addExtension(metadata, plugin); for (Object extension : plugin.getExtensions()) { - installTaskExtension(container, metadata, extension, projectPresent); - if (projectPresent) { + installTaskExtension(container, metadata, extension, requiringProject); + if (requiringProject) { installBatchExtension(container, metadata, extension, dryRun, InstantiationStrategy.PER_BATCH); } } @@ -96,8 +96,8 @@ public class ExtensionInstaller implements BatchComponent { List<ExtensionProvider> providers = container.getComponentsByType(ExtensionProvider.class); for (ExtensionProvider provider : providers) { - executeTaskExtensionProvider(container, provider, projectPresent); - if (projectPresent) { + executeTaskExtensionProvider(container, provider, requiringProject); + if (requiringProject) { executeBatchExtensionProvider(container, InstantiationStrategy.PER_BATCH, dryRun, provider); } } @@ -114,10 +114,10 @@ public class ExtensionInstaller implements BatchComponent { } } - boolean installTaskExtension(ComponentContainer container, @Nullable PluginMetadata plugin, Object extension, boolean projectPresent) { + boolean installTaskExtension(ComponentContainer container, @Nullable PluginMetadata plugin, Object extension, boolean requiringProject) { boolean installed; if (ExtensionUtils.isTaskExtension(extension) && - (projectPresent || !ExtensionUtils.requiresProject(extension)) && + (requiringProject == ExtensionUtils.requiresProject(extension)) && ExtensionUtils.supportsEnvironment(extension, environment)) { logInstallExtension("task", plugin, extension); container.addExtension(plugin, extension); diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/MetricProvider.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/MetricProvider.java index 9df02c89559..05eed0b6fd0 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/MetricProvider.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/MetricProvider.java @@ -21,17 +21,15 @@ package org.sonar.batch.bootstrap; import com.google.common.collect.Lists; import org.slf4j.LoggerFactory; -import org.sonar.api.BatchExtension; import org.sonar.api.ExtensionProvider; -import org.sonar.api.batch.InstantiationStrategy; import org.sonar.api.measures.CoreMetrics; import org.sonar.api.measures.Metric; import org.sonar.api.measures.Metrics; +import org.sonar.api.task.TaskExtension; import java.util.List; -@InstantiationStrategy(InstantiationStrategy.PER_BATCH) -public class MetricProvider extends ExtensionProvider implements BatchExtension { +public class MetricProvider extends ExtensionProvider implements TaskExtension { private Metrics[] factories; diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ModuleContainer.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ModuleContainer.java new file mode 100644 index 00000000000..530a4ce0527 --- /dev/null +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ModuleContainer.java @@ -0,0 +1,119 @@ +/* + * 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.batch.bootstrap; + +import org.sonar.api.batch.BatchExtensionDictionnary; +import org.sonar.api.batch.bootstrap.ProjectDefinition; +import org.sonar.api.database.model.Snapshot; +import org.sonar.api.resources.Project; +import org.sonar.api.scan.filesystem.FileExclusions; +import org.sonar.api.scan.filesystem.PathResolver; +import org.sonar.batch.DefaultProfileLoader; +import org.sonar.batch.DefaultProjectClasspath; +import org.sonar.batch.DefaultSensorContext; +import org.sonar.batch.DefaultTimeMachine; +import org.sonar.batch.ResourceFilters; +import org.sonar.batch.ViolationFilters; +import org.sonar.batch.components.TimeMachineConfiguration; +import org.sonar.batch.events.EventBus; +import org.sonar.batch.local.DryRunExporter; +import org.sonar.batch.phases.Phases; +import org.sonar.batch.phases.PhasesTimeProfiler; +import org.sonar.batch.scan.LastSnapshots; +import org.sonar.batch.scan.filesystem.DeprecatedFileSystemAdapter; +import org.sonar.batch.scan.filesystem.ExclusionFilters; +import org.sonar.batch.scan.filesystem.FileSystemLogger; +import org.sonar.batch.scan.filesystem.LanguageFilters; +import org.sonar.batch.scan.filesystem.ModuleFileSystemProvider; +import org.sonar.jpa.dao.ProfilesDao; +import org.sonar.jpa.dao.RulesDao; + +public abstract class ModuleContainer extends Container { + + private Project project; + private ProjectDefinition projectDefinition; + private Snapshot snapshot; + + public ModuleContainer(Project project, ProjectDefinition projectDefinition, Snapshot snapshot) { + this.project = project; + this.projectDefinition = projectDefinition; + this.snapshot = snapshot; + } + + @Override + protected void configure() { + addCoreComponents(); + addPluginExtensions(); + } + + private void addCoreComponents() { + container.addSingleton(projectDefinition); + container.addSingleton(project.getConfiguration()); + container.addSingleton(project); + for (Object component : projectDefinition.getContainerExtensions()) { + container.addSingleton(component); + } + // the Snapshot component will be removed when asynchronous measures are improved (required for AsynchronousMeasureSensor) + container.addSingleton(snapshot); + + container.addSingleton(ProjectSettings.class); + + // hack to initialize commons-configuration before ExtensionProviders + container.getComponentByType(ProjectSettings.class); + + container.addSingleton(EventBus.class); + container.addSingleton(Phases.class); + container.addSingleton(PhasesTimeProfiler.class); + for (Class clazz : Phases.getPhaseClasses()) { + container.addSingleton(clazz); + } + container.addSingleton(UnsupportedProperties.class); + + container.addSingleton(RulesDao.class); + container.addSingleton(LastSnapshots.class); + + // file system + container.addSingleton(PathResolver.class); + container.addSingleton(FileExclusions.class); + container.addSingleton(LanguageFilters.class); + container.addSingleton(ExclusionFilters.class); + container.addSingleton(DefaultProjectClasspath.class); + container.addPicoAdapter(new ModuleFileSystemProvider()); + container.addSingleton(DeprecatedFileSystemAdapter.class); + container.addSingleton(FileSystemLogger.class); + + container.addSingleton(TimeMachineConfiguration.class); + container.addSingleton(org.sonar.api.database.daos.MeasuresDao.class); + container.addSingleton(ProfilesDao.class); + container.addSingleton(DefaultSensorContext.class); + container.addSingleton(BatchExtensionDictionnary.class); + container.addSingleton(DefaultTimeMachine.class); + container.addSingleton(ViolationFilters.class); + container.addSingleton(ResourceFilters.class); + container.addSingleton(DefaultProfileLoader.class); + container.addSingleton(DryRunExporter.class); + } + + private void addPluginExtensions() { + ExtensionInstaller installer = container.getComponentByType(ExtensionInstaller.class); + installer.installInspectionExtensions(container); + } + +} diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/TaskContainer.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ProjectLessTaskContainer.java index 9c4e23c0a22..2fa80eac3a3 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/TaskContainer.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ProjectLessTaskContainer.java @@ -21,15 +21,14 @@ package org.sonar.batch.bootstrap; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.sonar.api.batch.bootstrap.ProjectReactor; import org.sonar.api.config.EmailSettings; +import org.sonar.api.resources.Languages; import org.sonar.api.resources.ResourceTypes; import org.sonar.api.task.Task; import org.sonar.api.task.TaskDefinition; import org.sonar.api.utils.SonarException; -import org.sonar.batch.DefaultFileLinesContextFactory; import org.sonar.batch.DefaultResourceCreationLock; -import org.sonar.batch.ProjectConfigurator; -import org.sonar.batch.ProjectTree; import org.sonar.batch.components.PastMeasuresLoader; import org.sonar.batch.components.PastSnapshotFinder; import org.sonar.batch.components.PastSnapshotFinderByDate; @@ -37,7 +36,6 @@ import org.sonar.batch.components.PastSnapshotFinderByDays; import org.sonar.batch.components.PastSnapshotFinderByPreviousAnalysis; import org.sonar.batch.components.PastSnapshotFinderByPreviousVersion; import org.sonar.batch.components.PastSnapshotFinderByVersion; -import org.sonar.batch.index.DefaultIndex; import org.sonar.batch.index.DefaultPersistenceManager; import org.sonar.batch.index.DefaultResourcePersister; import org.sonar.batch.index.DependencyPersister; @@ -46,11 +44,7 @@ import org.sonar.batch.index.LinkPersister; import org.sonar.batch.index.MeasurePersister; import org.sonar.batch.index.MemoryOptimizer; import org.sonar.batch.index.SourcePersister; -import org.sonar.batch.scan.ScanTask; import org.sonar.batch.tasks.ListTasksTask; -import org.sonar.core.component.ScanGraph; -import org.sonar.core.component.ScanGraphStore; -import org.sonar.core.component.ScanPerspectives; import org.sonar.core.i18n.I18nManager; import org.sonar.core.i18n.RuleI18nManager; import org.sonar.core.metric.CacheMetricFinder; @@ -60,40 +54,38 @@ import org.sonar.core.persistence.DatabaseVersion; import org.sonar.core.persistence.MyBatis; import org.sonar.core.persistence.SemaphoreUpdater; import org.sonar.core.persistence.SemaphoresImpl; +import org.sonar.core.qualitymodel.DefaultModelFinder; import org.sonar.core.resource.DefaultResourcePermissions; import org.sonar.core.rule.CacheRuleFinder; -import org.sonar.core.test.TestPlanBuilder; -import org.sonar.core.test.TestableBuilder; import org.sonar.core.user.DefaultUserFinder; import org.sonar.jpa.dao.MeasuresDao; import org.sonar.jpa.session.DefaultDatabaseConnector; import org.sonar.jpa.session.JpaDatabaseSession; +import javax.annotation.Nullable; + /** * Level-3 components. Task-level components that don't depends on project. */ -public class TaskContainer extends Container { +public class ProjectLessTaskContainer extends Container { - private static final Logger LOG = LoggerFactory.getLogger(TaskContainer.class); + private static final Logger LOG = LoggerFactory.getLogger(ProjectLessTaskContainer.class); private TaskDefinition taskDefinition; - private boolean projectPresent; - public TaskContainer(TaskDefinition task, boolean projectPresent) { + private ProjectReactor reactor; + + public ProjectLessTaskContainer(TaskDefinition task, @Nullable ProjectReactor reactor) { this.taskDefinition = task; - this.projectPresent = projectPresent; + this.reactor = reactor; } @Override protected void configure() { - logSettings(); registerCoreComponents(); registerDatabaseComponents(); - registerCoreTasks(); - if (projectPresent) { - registerCoreComponentsRequiringProject(); - } - registerTaskExtensions(); + registerCoreProjectLessTasks(); + registerProjectLessTaskExtensions(); } private void registerCoreComponents() { @@ -114,6 +106,21 @@ public class TaskContainer extends Container { container.addSingleton(PastSnapshotFinderByPreviousVersion.class); container.addSingleton(PastMeasuresLoader.class); container.addSingleton(PastSnapshotFinder.class); + container.addSingleton(Languages.class); + container.addSingleton(DefaultModelFinder.class); + container.addSingleton(MetricProvider.class); + container.addSingleton(DefaultResourceCreationLock.class); + container.addSingleton(DefaultPersistenceManager.class); + container.addSingleton(DependencyPersister.class); + container.addSingleton(EventPersister.class); + container.addSingleton(LinkPersister.class); + container.addSingleton(MeasurePersister.class); + container.addSingleton(MemoryOptimizer.class); + container.addSingleton(DefaultResourcePermissions.class); + container.addSingleton(DefaultResourcePersister.class); + container.addSingleton(SourcePersister.class); + container.addSingleton(DefaultNotificationManager.class); + } private void registerDatabaseComponents() { @@ -132,46 +139,13 @@ public class TaskContainer extends Container { container.addSingleton(BatchDatabaseSessionFactory.class); } - private void registerCoreTasks() { + private void registerCoreProjectLessTasks() { container.addSingleton(ListTasksTask.class); - if (projectPresent) { - container.addSingleton(ScanTask.class); - } } - private void registerTaskExtensions() { + private void registerProjectLessTaskExtensions() { ExtensionInstaller installer = container.getComponentByType(ExtensionInstaller.class); - installer.installTaskExtensions(container, projectPresent); - } - - private void registerCoreComponentsRequiringProject() { - container.addSingleton(DefaultResourceCreationLock.class); - container.addSingleton(DefaultPersistenceManager.class); - container.addSingleton(DependencyPersister.class); - container.addSingleton(EventPersister.class); - container.addSingleton(LinkPersister.class); - container.addSingleton(MeasurePersister.class); - container.addSingleton(MemoryOptimizer.class); - container.addSingleton(DefaultResourcePermissions.class); - container.addSingleton(DefaultResourcePersister.class); - container.addSingleton(SourcePersister.class); - container.addSingleton(DefaultNotificationManager.class); - container.addSingleton(MetricProvider.class); - container.addSingleton(ProjectExclusions.class); - container.addSingleton(ProjectReactorReady.class); - container.addSingleton(ProjectTree.class); - container.addSingleton(ProjectConfigurator.class); - container.addSingleton(DefaultIndex.class); - container.addSingleton(DefaultFileLinesContextFactory.class); - container.addSingleton(ProjectLock.class); - container.addSingleton(DryRunDatabase.class); - - // graphs - container.addSingleton(ScanGraph.create()); - container.addSingleton(TestPlanBuilder.class); - container.addSingleton(TestableBuilder.class); - container.addSingleton(ScanPerspectives.class); - container.addSingleton(ScanGraphStore.class); + installer.installTaskExtensions(container, false); } private void logSettings() { @@ -183,11 +157,29 @@ public class TaskContainer extends Container { */ @Override protected void doStart() { - Task task = container.getComponentByType(taskDefinition.getTask()); - if (task != null) { - task.execute(); - } else { - throw new SonarException("Extension " + taskDefinition.getTask() + " was not found in declared extensions."); + boolean projectPresent = (reactor != null); + if (ExtensionUtils.requiresProject(taskDefinition.getTask())) { + if (!projectPresent) { + throw new SonarException("Task '" + taskDefinition.getName() + "' requires to be run on a project"); + } + // Create a new child container to put project specific components + Container childModule = new ProjectTaskContainer(taskDefinition, reactor); + try { + installChild(childModule); + childModule.start(); + } finally { + childModule.stop(); + uninstallChild(); + } + } + else { + Task task = container.getComponentByType(taskDefinition.getTask()); + if (task != null) { + logSettings(); + task.execute(); + } else { + throw new SonarException("Extension " + taskDefinition.getTask() + " was not found in declared extensions."); + } } } diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ProjectTaskContainer.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ProjectTaskContainer.java new file mode 100644 index 00000000000..1dccf54a284 --- /dev/null +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ProjectTaskContainer.java @@ -0,0 +1,111 @@ +/* + * 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.batch.bootstrap; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.sonar.api.batch.bootstrap.ProjectReactor; +import org.sonar.api.task.Task; +import org.sonar.api.task.TaskDefinition; +import org.sonar.api.utils.SonarException; +import org.sonar.batch.DefaultFileLinesContextFactory; +import org.sonar.batch.ProjectConfigurator; +import org.sonar.batch.ProjectTree; +import org.sonar.batch.index.DefaultIndex; +import org.sonar.batch.scan.ScanTask; +import org.sonar.core.component.ScanGraph; +import org.sonar.core.component.ScanGraphStore; +import org.sonar.core.component.ScanPerspectives; +import org.sonar.core.test.TestPlanBuilder; +import org.sonar.core.test.TestableBuilder; + +/** + * Level-4 components. Task-level components that depends on project. + */ +public class ProjectTaskContainer extends Container { + + private static final Logger LOG = LoggerFactory.getLogger(ProjectTaskContainer.class); + + private TaskDefinition taskDefinition; + + private ProjectReactor reactor; + + public ProjectTaskContainer(TaskDefinition task, ProjectReactor reactor) { + this.taskDefinition = task; + this.reactor = reactor; + } + + @Override + protected void configure() { + container.addSingleton(reactor); + registerCoreProjectTasks(); + registerCoreComponentsRequiringProject(); + registerProjectTaskExtensions(); + } + + private void registerCoreProjectTasks() { + container.addSingleton(ScanTask.class); + } + + private void registerProjectTaskExtensions() { + ExtensionInstaller installer = container.getComponentByType(ExtensionInstaller.class); + installer.installTaskExtensions(container, true); + } + + /** + * Used by views plugin to emulate a project + */ + private void registerCoreComponentsRequiringProject() { + container.addSingleton(ProjectExclusions.class); + container.addSingleton(ProjectReactorReady.class); + container.addSingleton(ProjectTree.class); + container.addSingleton(ProjectConfigurator.class); + container.addSingleton(DefaultIndex.class); + container.addSingleton(DefaultFileLinesContextFactory.class); + container.addSingleton(ProjectLock.class); + container.addSingleton(DryRunDatabase.class); + + // graphs + container.addSingleton(ScanGraph.create()); + container.addSingleton(TestPlanBuilder.class); + container.addSingleton(TestableBuilder.class); + container.addSingleton(ScanPerspectives.class); + container.addSingleton(ScanGraphStore.class); + } + + private void logSettings() { + LOG.info("------------- Executing {}", taskDefinition.getName()); + } + + /** + * Execute task + */ + @Override + protected void doStart() { + Task task = container.getComponentByType(taskDefinition.getTask()); + if (task != null) { + logSettings(); + task.execute(); + } else { + throw new SonarException("Extension " + taskDefinition.getTask() + " was not found in declared extensions."); + } + } + +} diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/TaskBootstrapContainer.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/TaskBootstrapContainer.java index 39a81ebffdb..8bc1d412bbe 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/TaskBootstrapContainer.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/TaskBootstrapContainer.java @@ -34,9 +34,11 @@ import javax.annotation.Nullable; public class TaskBootstrapContainer extends Container { private String taskCommand; + private ProjectReactor reactor; - public TaskBootstrapContainer(@Nullable String taskCommand) { + public TaskBootstrapContainer(@Nullable String taskCommand, @Nullable ProjectReactor reactor) { this.taskCommand = taskCommand; + this.reactor = reactor; } @Override @@ -63,11 +65,11 @@ public class TaskBootstrapContainer extends Container { } private void executeTask(TaskDefinition taskDefinition) { - boolean projectPresent = container.getComponentByType(ProjectReactor.class) != null; + boolean projectPresent = (reactor != null); if (ExtensionUtils.requiresProject(taskDefinition.getTask()) && !projectPresent) { throw new SonarException("Task '" + taskDefinition.getName() + "' requires to be run on a project"); } - Container childModule = new TaskContainer(taskDefinition, projectPresent); + Container childModule = new ProjectLessTaskContainer(taskDefinition, reactor); try { installChild(childModule); childModule.start(); diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/ScanContainer.java b/sonar-batch/src/main/java/org/sonar/batch/scan/ScanContainer.java index e898e87d53f..4bc78585fd8 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/ScanContainer.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/ScanContainer.java @@ -21,114 +21,32 @@ package org.sonar.batch.scan; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.sonar.api.batch.BatchExtensionDictionnary; import org.sonar.api.batch.bootstrap.ProjectDefinition; +import org.sonar.api.database.model.Snapshot; import org.sonar.api.profiles.RulesProfile; -import org.sonar.api.resources.Languages; import org.sonar.api.resources.Project; -import org.sonar.api.scan.filesystem.FileExclusions; -import org.sonar.api.scan.filesystem.PathResolver; -import org.sonar.batch.DefaultProfileLoader; -import org.sonar.batch.DefaultProjectClasspath; -import org.sonar.batch.DefaultSensorContext; -import org.sonar.batch.DefaultTimeMachine; import org.sonar.batch.ProfileProvider; -import org.sonar.batch.ProjectTree; import org.sonar.batch.ResourceFilters; import org.sonar.batch.ViolationFilters; -import org.sonar.batch.bootstrap.Container; -import org.sonar.batch.bootstrap.ExtensionInstaller; -import org.sonar.batch.bootstrap.ProjectSettings; -import org.sonar.batch.bootstrap.UnsupportedProperties; -import org.sonar.batch.components.TimeMachineConfiguration; -import org.sonar.batch.events.EventBus; +import org.sonar.batch.bootstrap.ModuleContainer; import org.sonar.batch.index.DefaultIndex; -import org.sonar.batch.index.ResourcePersister; -import org.sonar.batch.local.DryRunExporter; import org.sonar.batch.phases.Phases; -import org.sonar.batch.phases.PhasesTimeProfiler; -import org.sonar.batch.scan.filesystem.DeprecatedFileSystemAdapter; -import org.sonar.batch.scan.filesystem.ExclusionFilters; -import org.sonar.batch.scan.filesystem.FileSystemLogger; -import org.sonar.batch.scan.filesystem.LanguageFilters; -import org.sonar.batch.scan.filesystem.ModuleFileSystemProvider; -import org.sonar.core.qualitymodel.DefaultModelFinder; -import org.sonar.jpa.dao.ProfilesDao; -import org.sonar.jpa.dao.RulesDao; -public class ScanContainer extends Container { +public class ScanContainer extends ModuleContainer { private static final Logger LOG = LoggerFactory.getLogger(ScanContainer.class); private Project project; - public ScanContainer(Project project) { + public ScanContainer(Project project, ProjectDefinition projectDefinition, Snapshot snapshot) { + super(project, projectDefinition, snapshot); this.project = project; } @Override protected void configure() { - logSettings(); - addCoreComponents(); - addPluginExtensions(); - } - - private void addCoreComponents() { - ProjectDefinition projectDefinition = container.getComponentByType(ProjectTree.class).getProjectDefinition(project); - container.addSingleton(projectDefinition); - container.addSingleton(project.getConfiguration()); - container.addSingleton(project); - container.addSingleton(ProjectSettings.class); - - // hack to initialize commons-configuration before ExtensionProviders - container.getComponentByType(ProjectSettings.class); - - container.addSingleton(EventBus.class); - container.addSingleton(Phases.class); - container.addSingleton(PhasesTimeProfiler.class); - for (Class clazz : Phases.getPhaseClasses()) { - container.addSingleton(clazz); - } - container.addSingleton(UnsupportedProperties.class); - - for (Object component : projectDefinition.getContainerExtensions()) { - container.addSingleton(component); - } - container.addSingleton(Languages.class); - container.addSingleton(RulesDao.class); - container.addSingleton(LastSnapshots.class); - - // file system - container.addSingleton(PathResolver.class); - container.addSingleton(FileExclusions.class); - container.addSingleton(LanguageFilters.class); - container.addSingleton(ExclusionFilters.class); - container.addSingleton(DefaultProjectClasspath.class); - container.addPicoAdapter(new ModuleFileSystemProvider()); - container.addSingleton(DeprecatedFileSystemAdapter.class); - container.addSingleton(FileSystemLogger.class); - - - // the Snapshot component will be removed when asynchronous measures are improved (required for AsynchronousMeasureSensor) - container.addSingleton(container.getComponentByType(ResourcePersister.class).getSnapshot(project)); - - container.addSingleton(TimeMachineConfiguration.class); - container.addSingleton(org.sonar.api.database.daos.MeasuresDao.class); - container.addSingleton(ProfilesDao.class); - container.addSingleton(DefaultSensorContext.class); - container.addSingleton(BatchExtensionDictionnary.class); - container.addSingleton(DefaultTimeMachine.class); - container.addSingleton(ViolationFilters.class); - container.addSingleton(ResourceFilters.class); - container.addSingleton(DefaultModelFinder.class); - container.addSingleton(DefaultProfileLoader.class); - container.addSingleton(DryRunExporter.class); + super.configure(); container.addPicoAdapter(new ProfileProvider()); } - private void addPluginExtensions() { - ExtensionInstaller installer = container.getComponentByType(ExtensionInstaller.class); - installer.installInspectionExtensions(container); - } - private void logSettings() { LOG.info("------------- Inspecting {}", project.getName()); } @@ -140,10 +58,11 @@ public class ScanContainer extends Container { protected void doStart() { DefaultIndex index = container.getComponentByType(DefaultIndex.class); index.setCurrentProject(project, - container.getComponentByType(ResourceFilters.class), - container.getComponentByType(ViolationFilters.class), - container.getComponentByType(RulesProfile.class)); + container.getComponentByType(ResourceFilters.class), + container.getComponentByType(ViolationFilters.class), + container.getComponentByType(RulesProfile.class)); + logSettings(); container.getComponentByType(Phases.class).execute(project); } } diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/ScanTask.java b/sonar-batch/src/main/java/org/sonar/batch/scan/ScanTask.java index f007e92749d..38ee09abe6d 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/ScanTask.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/ScanTask.java @@ -20,11 +20,14 @@ package org.sonar.batch.scan; import com.google.common.annotations.VisibleForTesting; +import org.sonar.api.batch.bootstrap.ProjectDefinition; +import org.sonar.api.database.model.Snapshot; import org.sonar.api.platform.ComponentContainer; import org.sonar.api.resources.Project; import org.sonar.api.task.Task; import org.sonar.api.task.TaskDefinition; import org.sonar.batch.ProjectTree; +import org.sonar.batch.index.ResourcePersister; import org.sonar.batch.tasks.RequiresProject; @RequiresProject @@ -58,7 +61,9 @@ public class ScanTask implements Task { @VisibleForTesting void scan(Project project) { - ScanContainer projectModule = new ScanContainer(project); + ProjectDefinition projectDefinition = container.getComponentByType(ProjectTree.class).getProjectDefinition(project); + Snapshot snapshot = container.getComponentByType(ResourcePersister.class).getSnapshot(project); + ScanContainer projectModule = new ScanContainer(project, projectDefinition, snapshot); try { ComponentContainer childContainer = container.createChild(); projectModule.init(childContainer); diff --git a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/TaskBootstrapContainerTest.java b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/TaskBootstrapContainerTest.java index 9e6b7320a2a..148449bfce7 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/TaskBootstrapContainerTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/TaskBootstrapContainerTest.java @@ -44,7 +44,7 @@ public class TaskBootstrapContainerTest { } }; bootstrapModule.init(); - TaskBootstrapContainer module = new TaskBootstrapContainer("inspect"); + TaskBootstrapContainer module = new TaskBootstrapContainer("inspect", null); bootstrapModule.installChild(module); thrown.expect(SonarException.class); diff --git a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/TaskContainerTest.java b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/TaskContainerTest.java index 03944ea0d77..ce956d6bd84 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/TaskContainerTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/TaskContainerTest.java @@ -20,6 +20,8 @@ package org.sonar.batch.bootstrap; import org.junit.Test; +import org.sonar.api.batch.bootstrap.ProjectDefinition; +import org.sonar.api.batch.bootstrap.ProjectReactor; import org.sonar.api.platform.ComponentContainer; import org.sonar.api.task.TaskDefinition; @@ -40,7 +42,7 @@ public class TaskContainerTest { } }; bootstrapModule.init(); - TaskContainer module = new TaskContainer(TaskDefinition.create(), true); + ProjectTaskContainer module = new ProjectTaskContainer(TaskDefinition.create(), new ProjectReactor(ProjectDefinition.create())); bootstrapModule.installChild(module); verify(extensionInstaller).installTaskExtensions(any(ComponentContainer.class), eq(true)); @@ -57,7 +59,7 @@ public class TaskContainerTest { } }; bootstrapModule.init(); - TaskContainer module = new TaskContainer(TaskDefinition.create(), false); + ProjectLessTaskContainer module = new ProjectLessTaskContainer(TaskDefinition.create(), null); bootstrapModule.installChild(module); verify(extensionInstaller).installTaskExtensions(any(ComponentContainer.class), eq(false)); diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/ScanContainerTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/ScanContainerTest.java index b8ecbdc8b7a..b31627bd2c2 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/scan/ScanContainerTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/scan/ScanContainerTest.java @@ -22,23 +22,18 @@ package org.sonar.batch.scan; import org.apache.commons.configuration.PropertiesConfiguration; import org.fest.assertions.Assertions; import org.junit.Test; -import org.mockito.Matchers; import org.sonar.api.batch.bootstrap.ProjectDefinition; import org.sonar.api.database.model.Snapshot; import org.sonar.api.platform.ComponentContainer; import org.sonar.api.resources.Project; -import org.sonar.api.resources.Resource; -import org.sonar.batch.ProjectTree; import org.sonar.batch.bootstrap.BatchSettings; import org.sonar.batch.bootstrap.Container; import org.sonar.batch.bootstrap.ExtensionInstaller; import org.sonar.batch.bootstrap.ProjectSettings; -import org.sonar.batch.index.ResourcePersister; import static org.mockito.Matchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; public class ScanContainerTest { @Test @@ -46,24 +41,18 @@ public class ScanContainerTest { // components injected in the parent container final Project project = new Project("foo"); project.setConfiguration(new PropertiesConfiguration()); - final ProjectTree projectTree = mock(ProjectTree.class); - when(projectTree.getProjectDefinition(project)).thenReturn(ProjectDefinition.create()); - final ResourcePersister resourcePersister = mock(ResourcePersister.class); - when(resourcePersister.getSnapshot(Matchers.<Resource>any())).thenReturn(new Snapshot()); final ExtensionInstaller extensionInstaller = mock(ExtensionInstaller.class); Container batchModule = new Container() { @Override protected void configure() { container.addSingleton(extensionInstaller); - container.addSingleton(projectTree); - container.addSingleton(resourcePersister); container.addSingleton(new BatchSettings()); } }; batchModule.init(); - ScanContainer projectModule = new ScanContainer(project); + ScanContainer projectModule = new ScanContainer(project, ProjectDefinition.create(), new Snapshot()); batchModule.installChild(projectModule); verify(extensionInstaller).installInspectionExtensions(any(ComponentContainer.class)); |