]> source.dussan.org Git - sonarqube.git/blob
aa1668150a64255952882159380415a544ceca33
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2016 SonarSource SA
4  * mailto:contact AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.server.computation.taskprocessor.report;
21
22 import java.util.Collections;
23 import java.util.Set;
24 import javax.annotation.CheckForNull;
25 import org.sonar.ce.queue.CeTask;
26 import org.sonar.ce.queue.CeTaskResult;
27 import org.sonar.ce.settings.SettingsLoader;
28 import org.sonar.ce.settings.ThreadLocalSettings;
29 import org.sonar.ce.taskprocessor.CeTaskProcessor;
30 import org.sonar.core.platform.ComponentContainer;
31 import org.sonar.db.ce.CeTaskTypes;
32 import org.sonar.plugin.ce.ReportAnalysisComponentProvider;
33 import org.sonar.server.computation.container.ComputeEngineContainer;
34 import org.sonar.server.computation.container.ContainerFactory;
35 import org.sonar.server.computation.step.ComputationStepExecutor;
36 import org.sonar.server.computation.taskprocessor.TaskResultHolder;
37
38 public class ReportTaskProcessor implements CeTaskProcessor {
39
40   private static final Set<String> HANDLED_TYPES = Collections.singleton(CeTaskTypes.REPORT);
41
42   private final ContainerFactory containerFactory;
43   private final ComponentContainer serverContainer;
44   @CheckForNull
45   private final ReportAnalysisComponentProvider[] componentProviders;
46
47   /**
48    * Used when at least one Privileged plugin is installed
49    */
50   public ReportTaskProcessor(ContainerFactory containerFactory, ComponentContainer serverContainer, ReportAnalysisComponentProvider[] componentProviders) {
51     this.containerFactory = containerFactory;
52     this.serverContainer = serverContainer;
53     this.componentProviders = componentProviders;
54   }
55
56   /**
57    * Used when no privileged plugin is installed
58    */
59   public ReportTaskProcessor(ContainerFactory containerFactory, ComponentContainer serverContainer) {
60     this.containerFactory = containerFactory;
61     this.serverContainer = serverContainer;
62     this.componentProviders = null;
63   }
64
65   @Override
66   public Set<String> getHandledCeTaskTypes() {
67     return HANDLED_TYPES;
68   }
69
70   @Override
71   public CeTaskResult process(CeTask task) {
72     ComputeEngineContainer ceContainer = containerFactory.create(serverContainer, task, componentProviders);
73
74     try {
75       ceContainer.getComponentByType(ComputationStepExecutor.class).execute();
76       return ceContainer.getComponentByType(TaskResultHolder.class).getResult();
77     } finally {
78       ensureThreadLocalIsClean(ceContainer);
79
80       ceContainer.cleanup();
81     }
82   }
83
84   /** safety call to clear ThreadLocal even if Pico container fails to call {@link SettingsLoader#stop()}) */
85   private static void ensureThreadLocalIsClean(ComputeEngineContainer ceContainer) {
86     ceContainer.getComponentByType(ThreadLocalSettings.class).remove();
87   }
88 }