3 * Copyright (C) 2009-2023 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.ce.task.projectexport.taskprocessor;
23 import org.sonar.ce.task.CeTask;
24 import org.sonar.ce.task.CeTaskResult;
25 import org.sonar.ce.task.container.TaskContainer;
26 import org.sonar.ce.task.container.TaskContainerImpl;
27 import org.sonar.ce.task.projectexport.ProjectExportContainerPopulator;
28 import org.sonar.ce.task.projectexport.ProjectExportProcessor;
29 import org.sonar.ce.task.taskprocessor.CeTaskProcessor;
30 import org.sonar.core.platform.SpringComponentContainer;
32 import static org.sonar.db.ce.CeTaskTypes.PROJECT_EXPORT;
34 public class ProjectExportTaskProcessor implements CeTaskProcessor {
36 private final SpringComponentContainer componentContainer;
38 public ProjectExportTaskProcessor(SpringComponentContainer componentContainer) {
39 this.componentContainer = componentContainer;
43 public Set<String> getHandledCeTaskTypes() {
44 return Set.of(PROJECT_EXPORT);
48 public CeTaskResult process(CeTask task) {
49 processProjectExport(task);
53 private void processProjectExport(CeTask task) {
54 CeTask.Component exportComponent = mandatoryComponent(task, PROJECT_EXPORT);
55 failIfNotMain(exportComponent, task);
56 ProjectDescriptor projectExportDescriptor = new ProjectDescriptor(exportComponent.getUuid(),
57 mandatoryKey(exportComponent), mandatoryName(exportComponent));
59 try (TaskContainer taskContainer = new TaskContainerImpl(componentContainer,
60 new ProjectExportContainerPopulator(projectExportDescriptor))) {
61 taskContainer.bootup();
62 taskContainer.getComponentByType(ProjectExportProcessor.class).process();
67 private static void failIfNotMain(CeTask.Component exportComponent, CeTask task) {
68 task.getEntity().filter(entity -> entity.equals(exportComponent))
69 .orElseThrow(() -> new IllegalStateException("Component of task must be the same as entity"));
72 private static CeTask.Component mandatoryComponent(CeTask task, String type) {
73 return task.getComponent().orElseThrow(() -> new IllegalStateException(String.format("Task with type %s must have a component", type)));
76 private static String mandatoryKey(CeTask.Component component) {
77 return component.getKey().orElseThrow(() -> new IllegalStateException("Task component must have a key"));
80 private static String mandatoryName(CeTask.Component component) {
81 return component.getName().orElseThrow(() -> new IllegalStateException("Task component must have a name"));