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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
|
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2014 SonarSource
* mailto:contact AT sonarsource DOT com
*
* SonarQube 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.
*
* SonarQube 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 this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.batch;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.batch.Event;
import org.sonar.api.batch.SensorContext;
import org.sonar.api.batch.SonarIndex;
import org.sonar.api.batch.fs.InputDir;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.InputPath;
import org.sonar.api.design.Dependency;
import org.sonar.api.measures.Measure;
import org.sonar.api.measures.MeasuresFilter;
import org.sonar.api.measures.Metric;
import org.sonar.api.resources.Directory;
import org.sonar.api.resources.File;
import org.sonar.api.resources.Project;
import org.sonar.api.resources.ProjectLink;
import org.sonar.api.resources.Qualifiers;
import org.sonar.api.resources.Resource;
import org.sonar.api.rules.Violation;
import org.sonar.api.utils.SonarException;
import org.sonar.core.measure.MeasurementFilters;
import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Set;
public class DefaultSensorContext implements SensorContext {
private static final Logger LOG = LoggerFactory.getLogger(DefaultSensorContext.class);
private SonarIndex index;
private Project project;
private MeasurementFilters filters;
public DefaultSensorContext(SonarIndex index, Project project, MeasurementFilters filters) {
this.index = index;
this.project = project;
this.filters = filters;
}
public Project getProject() {
return project;
}
@Override
public boolean index(Resource resource) {
// SONAR-5006
if (indexedByCore(resource)) {
logWarning();
return true;
}
return index.index(resource);
}
private boolean indexedByCore(Resource resource) {
return StringUtils.equals(Qualifiers.DIRECTORY, resource.getQualifier()) ||
StringUtils.equals(Qualifiers.FILE, resource.getQualifier());
}
@Override
public boolean index(Resource resource, Resource parentReference) {
// SONAR-5006
if (indexedByCore(resource)) {
logWarning();
return true;
}
return index.index(resource, parentReference);
}
private void logWarning() {
if (LOG.isDebugEnabled()) {
LOG.debug("Plugins are no more responsible for indexing physical resources like directories and files. This is now handled by the platform.", new SonarException(
"Plugin should not index physical resources"));
}
}
@Override
public boolean isExcluded(Resource reference) {
return index.isExcluded(reference);
}
@Override
public boolean isIndexed(Resource reference, boolean acceptExcluded) {
return index.isIndexed(reference, acceptExcluded);
}
@Override
public Resource getParent(Resource reference) {
return index.getParent(reference);
}
@Override
public Collection<Resource> getChildren(Resource reference) {
return index.getChildren(reference);
}
@Override
public <G extends Serializable> Measure<G> getMeasure(Metric<G> metric) {
return index.getMeasure(project, metric);
}
@Override
public <M> M getMeasures(MeasuresFilter<M> filter) {
return index.getMeasures(project, filter);
}
@Override
public Measure saveMeasure(Measure measure) {
return index.addMeasure(project, measure);
}
@Override
public Measure saveMeasure(Metric metric, Double value) {
return index.addMeasure(project, new Measure(metric, value));
}
@Override
public <G extends Serializable> Measure<G> getMeasure(Resource resource, Metric<G> metric) {
return index.getMeasure(resource, metric);
}
@Override
public String saveResource(Resource resource) {
Resource persistedResource = index.addResource(resource);
if (persistedResource != null) {
return persistedResource.getEffectiveKey();
}
return null;
}
public boolean saveResource(Resource resource, Resource parentReference) {
return index.index(resource, parentReference);
}
@Override
public Resource getResource(Resource resource) {
return index.getResource(resource);
}
@Override
public <M> M getMeasures(Resource resource, MeasuresFilter<M> filter) {
return index.getMeasures(resource, filter);
}
@Override
public Measure saveMeasure(Resource resource, Metric metric, Double value) {
return saveMeasure(resource, new Measure(metric, value));
}
@Override
public Measure saveMeasure(Resource resource, Measure measure) {
if (filters.accept(resource, measure)) {
return index.addMeasure(resourceOrProject(resource), measure);
} else {
return measure;
}
}
@Override
public void saveViolation(Violation violation, boolean force) {
if (violation.getResource() == null) {
violation.setResource(resourceOrProject(violation.getResource()));
}
index.addViolation(violation, force);
}
@Override
public void saveViolation(Violation violation) {
saveViolation(violation, false);
}
@Override
public void saveViolations(Collection<Violation> violations) {
if (violations != null) {
for (Violation violation : violations) {
saveViolation(violation);
}
}
}
@Override
public Dependency saveDependency(Dependency dependency) {
return index.addDependency(dependency);
}
@Override
public Set<Dependency> getDependencies() {
return index.getDependencies();
}
@Override
public Collection<Dependency> getIncomingDependencies(Resource to) {
return index.getIncomingEdges(resourceOrProject(to));
}
@Override
public Collection<Dependency> getOutgoingDependencies(Resource from) {
return index.getOutgoingEdges(resourceOrProject(from));
}
@Override
public void saveSource(Resource reference, String source) {
// useless since 4.2.
}
@Override
public void saveLink(ProjectLink link) {
index.addLink(link);
}
@Override
public void deleteLink(String key) {
index.deleteLink(key);
}
@Override
public List<Event> getEvents(Resource resource) {
return index.getEvents(resource);
}
@Override
public Event createEvent(Resource resource, String name, String description, String category, Date date) {
return index.addEvent(resource, name, description, category, date);
}
@Override
public void deleteEvent(Event event) {
index.deleteEvent(event);
}
private Resource resourceOrProject(Resource resource) {
if (resource == null) {
return project;
}
Resource indexedResource = getResource(resource);
return indexedResource != null ? indexedResource : resource;
}
@Override
public Measure saveMeasure(InputFile inputFile, Metric metric, Double value) {
return saveMeasure(getResource(inputFile), metric, value);
}
@Override
public Measure saveMeasure(InputFile inputFile, Measure measure) {
return saveMeasure(getResource(inputFile), measure);
}
@Override
public Resource getResource(InputPath inputPath) {
Resource r;
if (inputPath instanceof InputDir) {
r = Directory.create(((InputDir) inputPath).relativePath());
} else if (inputPath instanceof InputFile) {
r = File.create(((InputFile) inputPath).relativePath());
} else {
throw new IllegalArgumentException("Unknow input path type: " + inputPath);
}
return getResource(r);
}
}
|