1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
/*
* Sonar, open source software quality management tool.
* Copyright (C) 2009 SonarSource SA
* 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;
import org.picocontainer.Characteristics;
import org.picocontainer.MutablePicoContainer;
import org.sonar.api.batch.BatchExtensionDictionnary;
import org.sonar.api.batch.FileFilter;
import org.sonar.api.batch.ProjectClasspath;
import org.sonar.api.batch.SensorContext;
import org.sonar.api.database.DatabaseSession;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.measures.Metric;
import org.sonar.api.measures.Metrics;
import org.sonar.api.resources.DefaultProjectFileSystem;
import org.sonar.api.resources.Languages;
import org.sonar.api.resources.Project;
import org.sonar.api.rules.DefaultRulesManager;
import org.sonar.api.utils.IocContainer;
import org.sonar.batch.indexer.DefaultSonarIndex;
import org.sonar.core.qualitymodel.DefaultModelFinder;
import org.sonar.core.rule.DefaultRuleFinder;
import org.sonar.jpa.dao.*;
import java.util.List;
public class ProjectBatch {
private MutablePicoContainer globalContainer;
private MutablePicoContainer batchContainer;
public ProjectBatch(MutablePicoContainer container) {
this.globalContainer = container;
}
public void execute(DefaultSonarIndex index, Project project) {
try {
startChildContainer(index, project);
SensorContext sensorContext = batchContainer.getComponent(SensorContext.class);
for (Class<? extends CoreJob> clazz : CoreJobs.allJobs()) {
CoreJob job = getComponent(clazz);
job.execute(project, sensorContext);
commit();
}
} finally {
index.clear();
stop();
}
}
public void startChildContainer(DefaultSonarIndex index, Project project) {
batchContainer = globalContainer.makeChildContainer();
batchContainer.as(Characteristics.CACHE).addComponent(project);
batchContainer.as(Characteristics.CACHE).addComponent(project.getPom());
batchContainer.as(Characteristics.CACHE).addComponent(ProjectClasspath.class);
batchContainer.as(Characteristics.CACHE).addComponent(index.getBucket(project).getSnapshot());
batchContainer.as(Characteristics.CACHE).addComponent(project.getConfiguration());
//need to be registered after the Configuration
batchContainer.getComponent(BatchPluginRepository.class).registerPlugins(batchContainer);
batchContainer.as(Characteristics.CACHE).addComponent(DaoFacade.class);
batchContainer.as(Characteristics.CACHE).addComponent(RulesDao.class);
batchContainer.as(Characteristics.CACHE).addComponent(org.sonar.api.database.daos.RulesDao.class);
batchContainer.as(Characteristics.CACHE).addComponent(MeasuresDao.class);
batchContainer.as(Characteristics.CACHE).addComponent(org.sonar.api.database.daos.MeasuresDao.class);
batchContainer.as(Characteristics.CACHE).addComponent(ProfilesDao.class);
batchContainer.as(Characteristics.CACHE).addComponent(AsyncMeasuresDao.class);
batchContainer.as(Characteristics.CACHE).addComponent(AsyncMeasuresService.class);
batchContainer.as(Characteristics.CACHE).addComponent(DefaultRulesManager.class);
batchContainer.as(Characteristics.CACHE).addComponent(DefaultSensorContext.class);
batchContainer.as(Characteristics.CACHE).addComponent(Languages.class);
batchContainer.as(Characteristics.CACHE).addComponent(BatchExtensionDictionnary.class);
batchContainer.as(Characteristics.CACHE).addComponent(DefaultTimeMachine.class);
batchContainer.as(Characteristics.CACHE).addComponent(ViolationsDao.class);
batchContainer.as(Characteristics.CACHE).addComponent(ViolationFilters.class);
batchContainer.as(Characteristics.CACHE).addComponent(ResourceFilters.class);
batchContainer.as(Characteristics.CACHE).addComponent(DefaultModelFinder.class);
batchContainer.as(Characteristics.CACHE).addComponent(DefaultRuleFinder.class);
batchContainer.addAdapter(new ProfileProvider());
batchContainer.addAdapter(new CheckProfileProvider());
loadCoreComponents(batchContainer);
batchContainer.as(Characteristics.CACHE).addComponent(new IocContainer(batchContainer));
batchContainer.start();
// post-initializations
prepareProject(project, index);
}
private void prepareProject(Project project, DefaultSonarIndex index) {
project.setLanguage(getComponent(Languages.class).get(project.getLanguageKey()));
index.selectProject(project, getComponent(ResourceFilters.class), getComponent(ViolationFilters.class), getComponent(MeasuresDao.class), getComponent(ViolationsDao.class));
List<FileFilter> fileFilters = batchContainer.getComponents(FileFilter.class);
((DefaultProjectFileSystem)project.getFileSystem()).addFileFilters(fileFilters);
}
private void loadCoreComponents(MutablePicoContainer container) {
for (Class<?> clazz : CoreJobs.allJobs()) {
container.as(Characteristics.CACHE).addComponent(clazz);
}
for (Metric metric : CoreMetrics.getMetrics()) {
container.as(Characteristics.CACHE).addComponent(metric.getKey(), metric);
}
for (Metrics metricRepo : container.getComponents(Metrics.class)) {
for (Metric metric : metricRepo.getMetrics()) {
container.as(Characteristics.CACHE).addComponent(metric.getKey(), metric);
}
}
}
private void stop() {
if (batchContainer != null) {
commit();
try {
globalContainer.removeChildContainer(batchContainer);
batchContainer.stop();
batchContainer = null;
} catch (Exception e) {
// do not log
}
}
}
public void commit() {
getComponent(DatabaseSession.class).commit();
}
public <T> T getComponent(Class<T> clazz) {
if (batchContainer != null) {
return batchContainer.getComponent(clazz);
}
return globalContainer.getComponent(clazz);
}
}
|