-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathSCMNavigatorRequest.java
351 lines (326 loc) · 13.2 KB
/
SCMNavigatorRequest.java
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
* The MIT License
*
* Copyright (c) 2017 CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package jenkins.scm.api.trait;
import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.io.Closeable;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import jenkins.scm.api.SCMHead;
import jenkins.scm.api.SCMNavigator;
import jenkins.scm.api.SCMSource;
import jenkins.scm.api.SCMSourceObserver;
import jenkins.scm.impl.NoOpProjectObserver;
/**
* Represents the context of an individual request for a call to
* {@link SCMNavigator#visitSources(SCMSourceObserver)} or an equivalent method.
*
* @since 2.2.0
*/
public abstract class SCMNavigatorRequest implements Closeable {
/**
* The {@link SCMNavigator} to use when applying the {@link #prefilters}.
*/
@NonNull
private final SCMNavigator navigator;
/**
* The filters requiring context of the {@link SCMNavigatorRequest}, typically because the decision to filter may
* require making remote requests.
*/
@NonNull
private final List<SCMSourceFilter> filters;
/**
* The filters that do not require context of the {@link SCMNavigatorRequest} and only require the {@link SCMNavigator}
* and {@code projectName} to decide exclusion - typically filtering based on the name.
*/
@NonNull
private final List<SCMSourcePrefilter> prefilters;
/**
* The {@link SCMSourceObserver} for this request.
*/
@NonNull
private final SCMSourceObserver observer;
/**
* The {@link SCMSourceObserver#getIncludes()} of {@link #observer}.
*/
@CheckForNull
private final Set<String> observerIncludes;
/**
* The {@link SCMSourceTrait} instances to apply to {@link SCMSourceBuilder} instances.
*/
@NonNull
private final List<SCMSourceTrait> traits;
/**
* The {@link SCMSourceDecorator} instances to apply to {@link SCMSourceBuilder} instances.
*/
@NonNull
private final List<SCMSourceDecorator<?,?>> decorators;
/**
* Any {@link Closeable} objects that should be closed with the request.
*/
// TODO widen type to AutoClosable once Java 7+
@NonNull
private final List<Closeable> managedClosables = new ArrayList<Closeable>();
/**
* Constructor.
*
* @param source the source.
* @param context the context.
* @param observer the observer.
*/
protected SCMNavigatorRequest(@NonNull SCMNavigator source, @NonNull SCMNavigatorContext<?, ?> context,
@NonNull SCMSourceObserver observer) {
this.navigator = source;
this.filters = new ArrayList<SCMSourceFilter>(context.filters());
this.prefilters = new ArrayList<SCMSourcePrefilter>(context.prefilters());
this.observer = observer;
this.observerIncludes = this.observer.getIncludes();
this.traits = new ArrayList<SCMSourceTrait>(context.traits());
this.decorators = new ArrayList<SCMSourceDecorator<?, ?>>(context.decorators());
}
/**
* Returns the {@link SCMSourceTrait} instances to apply to every {@link SCMSource}.
*
* @return the {@link SCMSourceTrait} instances to apply to every {@link SCMSource}.
*/
@NonNull
public final List<SCMSourceTrait> traits() {
return Collections.unmodifiableList(traits);
}
/**
* Returns the {@link SCMSourceDecorator} instances to apply to {@link SCMSource} instances.
*
* @return the {@link SCMSourceDecorator} instances to apply to {@link SCMSource} instances.
*/
@NonNull
public final List<SCMSourceDecorator<?, ?>> decorators() {
return Collections.unmodifiableList(decorators);
}
/**
* Records a processing result to the {@linkplain Witness}es.
*
* @param projectName the project name.
* @param isMatch {@code true} if the projectName pair was sent to the {@link #observer}.
* @param witnesses the {@link Witness} instances to notify.
*/
@SuppressWarnings("unchecked")
private static void record(@NonNull String projectName, boolean isMatch,
@NonNull Witness... witnesses) {
if (witnesses.length > 0) {
for (Witness witness : witnesses) {
witness.record(projectName, isMatch);
}
}
}
/**
* Tests if the project name is excluded from the request.
*
* @param projectName the project name.
* @return {@code true} if the {@link SCMHead} is excluded.
* @throws IOException if there is an I/O error.
* @throws InterruptedException if the operation was interrupted.
*/
public final boolean isExcluded(@NonNull String projectName) throws IOException, InterruptedException {
if (observerIncludes != null && !observerIncludes.contains(projectName)) {
return true;
}
if (!prefilters.isEmpty()) {
for (SCMSourcePrefilter prefilter : prefilters) {
if (prefilter.isExcluded(navigator, projectName)) {
return true;
}
}
}
if (filters.isEmpty()) {
return false;
}
for (SCMSourceFilter filter : filters) {
if (filter.isExcluded(this, projectName)) {
return true;
}
}
return false;
}
/**
* Processes a named project in the scope of the current request.
*
* @param projectName the name of the project.
* @param sourceFactory the factory for instantiating a {@link SCMSource}.
* @param attributeFactory (optional) factory for instantiating the attribute map.
* @param witnesses the witnesses to record the processing result.
* @return {@code true} if and only if the request is completed, {@code false} if the request can process
* additional named projects.
* @throws IllegalArgumentException if the attribute factory provides attribute names that are unrecognized, or
* repeats already added attributes.
* @throws IOException if there is an I/O error.
* @throws InterruptedException if the operation was interrupted.
*/
public boolean process(@NonNull String projectName, @NonNull SourceLambda sourceFactory,
@CheckForNull AttributeLambda attributeFactory,
Witness... witnesses)
throws IllegalArgumentException, IOException, InterruptedException {
return process(
projectName,
Collections.singletonList(sourceFactory),
attributeFactory == null ? null : Collections.singletonList(attributeFactory),
witnesses
);
}
/**
* Processes a named project in the scope of the current request.
*
* @param projectName the name of the project.
* @param sourceFactories the factories for instantiating the {@link SCMSource} instances.
* @param attributeFactories (optional) factories for instantiating the attribute maps.
* @param witnesses the witnesses to record the processing result.
* @return {@code true} if and only if the request is completed, {@code false} if the request can process
* additional named projects.
* @throws IllegalArgumentException if an attribute factory provides attribute names that are unrecognized, or
* repeats already added attributes.
* @throws IOException if there is an I/O error.
* @throws InterruptedException if the operation was interrupted.
*/
public boolean process(@NonNull String projectName, @NonNull List<SourceLambda> sourceFactories,
@CheckForNull List<AttributeLambda> attributeFactories,
Witness... witnesses)
throws IllegalArgumentException, IOException, InterruptedException {
if (Thread.interrupted()) {
throw new InterruptedException();
}
if (isExcluded(projectName)) {
// not included
record(projectName, false, witnesses);
return !observer.isObserving();
}
record(projectName, true, witnesses);
SCMSourceObserver.ProjectObserver po = observer.observe(projectName);
if (po instanceof NoOpProjectObserver) {
// we know this is safe to break contract with
return !observer.isObserving();
}
for (SourceLambda s : sourceFactories) {
po.addSource(s.create(projectName));
}
if (attributeFactories != null) {
for (AttributeLambda attributeFactory : attributeFactories) {
for (Map.Entry<String, Object> entry : attributeFactory.create(projectName).entrySet()) {
po.addAttribute(entry.getKey(), entry.getValue());
}
}
}
po.complete();
return !observer.isObserving();
}
/**
* Adds managing a {@link Closeable} into the scope of the {@link SCMNavigatorRequest}
*
* @param closeable the {@link Closeable} to manage.
*/
public void manage(@CheckForNull Closeable closeable) {
if (closeable != null) {
managedClosables.add(closeable);
}
}
/**
* {@inheritDoc}
*/
@Override
public void close() throws IOException {
IOException ioe = null;
for (Closeable c : managedClosables) {
try {
c.close();
} catch (IOException e) {
if (ioe == null) {
ioe = e;
} else {
// TODO replace with direct call to addSuppressed once baseline Java is 1.7
try {
Method addSuppressed = Throwable.class.getMethod("addSuppressed", Throwable.class);
addSuppressed.invoke(ioe, e);
} catch (NoSuchMethodException e1) {
// ignore, best effort
} catch (IllegalAccessException e1) {
// ignore, best effort
} catch (InvocationTargetException e1) {
// ignore, best effort
}
}
}
}
if (ioe != null) {
throw ioe;
}
}
/**
* A lambda that will create the {@link SCMSource} instance for a specific project name.
*/
public interface SourceLambda {
/**
* Creates the {@link SCMSource} for the named project.
*
* @param projectName the named project.
* @return the {@link SCMSource} instance.
* @throws IOException if there is an I/O error.
* @throws InterruptedException if the operation was interrupted.
*/
@NonNull
SCMSource create(@NonNull String projectName) throws IOException, InterruptedException;
}
/**
* A lambda that will create the map of attributes for a specific project name.
*/
public interface AttributeLambda {
/**
* Creates the attributes map for the named project.
*
* @param projectName the named project.
* @return the attributes map.
* @throws IOException if there is an I/O error.
* @throws InterruptedException if the operation was interrupted.
*/
@NonNull
Map<String, Object> create(@NonNull String projectName) throws IOException, InterruptedException;
}
/**
* Callback lambda to track the results of
* {@link #process(String, SourceLambda, AttributeLambda, Witness...)} or
* {@link #process(String, List, List, Witness...)}
*/
public interface Witness {
/**
* Records the result of the named project.
*
* @param projectName the project name.
* @param isMatch {@code true} if the project matched.
*/
void record(@NonNull String projectName, boolean isMatch);
}
}