-
Notifications
You must be signed in to change notification settings - Fork 18
/
FileNameTrigger.java
548 lines (462 loc) · 20.4 KB
/
FileNameTrigger.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
package org.jenkinsci.plugins.fstrigger.triggers;
import antlr.ANTLRException;
import hudson.DescriptorExtensionList;
import hudson.Extension;
import hudson.FilePath;
import hudson.Util;
import hudson.console.AnnotatedLargeText;
import hudson.model.*;
import hudson.remoting.VirtualChannel;
import hudson.util.FormValidation;
import hudson.util.SequentialExecutionQueue;
import hudson.util.StreamTaskListener;
import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
import org.apache.commons.jelly.XMLOutput;
import org.jenkinsci.lib.xtrigger.AbstractTrigger;
import org.jenkinsci.lib.xtrigger.XTriggerDescriptor;
import org.jenkinsci.lib.xtrigger.XTriggerException;
import org.jenkinsci.lib.xtrigger.XTriggerLog;
import org.jenkinsci.plugins.fstrigger.core.FSTriggerAction;
import org.jenkinsci.plugins.fstrigger.core.FSTriggerContentFileType;
import org.jenkinsci.plugins.fstrigger.service.FSTriggerComputeFileService;
import org.jenkinsci.plugins.fstrigger.service.FSTriggerFileNameCheckedModifiedService;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;
import java.io.File;
import java.io.IOException;
import java.io.ObjectStreamException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* @author Gregory Boissinot
*/
public class FileNameTrigger extends AbstractTrigger {
public static final String STRATEGY_IGNORE = "IGNORE";
public static final String STRATEGY_LATEST = "LATEST";
private static Logger LOGGER = Logger.getLogger(FileNameTrigger.class.getName());
private static final String CAUSE = "Triggered by a change to a file";
private FileNameTriggerInfo[] fileInfo = new FileNameTriggerInfo[0];
public FileNameTrigger(String cronTabSpec, FileNameTriggerInfo[] fileInfo) throws ANTLRException {
super(cronTabSpec);
this.fileInfo = fileInfo;
}
@SuppressWarnings("unused")
public FileNameTriggerInfo[] getFileInfo() {
return fileInfo;
}
@Override
protected File getLogFile() {
return new File(job.getRootDir(), "trigger-polling-files.log");
}
@Override
protected Action[] getScheduledActions(Node node, XTriggerLog log) {
return new Action[0];
}
@Override
protected boolean requiresWorkspaceForPolling() {
return false;
}
@Override
public void start(Node pollingNode, BuildableItem project, boolean newInstance, XTriggerLog log) {
try {
FSTriggerComputeFileService service = new FSTriggerComputeFileService();
for (FileNameTriggerInfo info : fileInfo) {
FilePath resolvedFile = service.computedFile(pollingNode, (AbstractProject) project, info, new XTriggerLog((StreamTaskListener) TaskListener.NULL));
if (resolvedFile != null) {
info.setResolvedFile(resolvedFile);
info.setLastModifications(resolvedFile.lastModified());
// Initialize the memory information if whe introspect the content
initContentElementsIfNeed(info);
}
}
} catch (XTriggerException fse) {
LOGGER.log(Level.SEVERE, "Error on trigger startup " + fse.getMessage());
fse.printStackTrace();
} catch (Throwable t) {
LOGGER.log(Level.SEVERE, "Severe error on trigger startup " + t.getMessage());
t.printStackTrace();
}
}
private void initContentElementsIfNeed(FileNameTriggerInfo info) throws XTriggerException {
FilePath resolvedFile = info.getResolvedFile();
if (resolvedFile != null) {
boolean inspectingContentFile = info.isInspectingContentFile();
if (inspectingContentFile) {
FSTriggerContentFileType[] contentFileTypes = info.getContentFileTypes();
if (contentFileTypes != null) {
for (final FSTriggerContentFileType type : contentFileTypes) {
final String jobName = job.getName();
if (type != null) {
try {
Object memoryInfo = resolvedFile.act(new FilePath.FileCallable<Object>() {
public Object invoke(File f, VirtualChannel channel) throws IOException, InterruptedException {
try {
type.initMemoryFields(jobName, f);
} catch (XTriggerException fse) {
throw new RuntimeException(fse);
}
return type.getMemoryInfo();
}
});
type.setMemoryInfo(memoryInfo);
} catch (IOException ioe) {
throw new XTriggerException(ioe);
} catch (InterruptedException ie) {
throw new XTriggerException(ie);
} catch (Throwable t) {
throw new XTriggerException(t);
}
}
}
}
}
}
}
private void refreshMemoryInfo(FileNameTriggerInfo info, FilePath newComputedFile) throws XTriggerException {
try {
if (newComputedFile != null && newComputedFile.exists()) {
info.setResolvedFile(newComputedFile);
info.setLastModifications(newComputedFile.lastModified());
initContentElementsIfNeed(info);
} else {
info.setResolvedFile(null);
info.setLastModifications(0l);
}
} catch (IOException ioe) {
throw new XTriggerException(ioe);
} catch (InterruptedException ie) {
throw new XTriggerException(ie);
}
}
@Override
protected boolean checkIfModified(Node pollingNode, XTriggerLog log) throws XTriggerException {
//1-- Compute new resolved files
FilePath[] resolvedFiles = getNewResolvedFiles(pollingNode, log);
//2-- Check if there are at least one change
boolean changeResult = checkIfThereAreAtLeastOneChange(resolvedFiles, log);
//3-- Refresh new resolved files
refreshNewResolvedFiles(resolvedFiles);
//4-- Return change status
return changeResult;
}
private FilePath[] getNewResolvedFiles(Node pollingNode, XTriggerLog log) throws XTriggerException {
FilePath[] resolvedFiles = new FilePath[fileInfo.length];
for (int i = 0; i < fileInfo.length; i++) {
FileNameTriggerInfo info = fileInfo[i];
FilePath resolvedFile = new FSTriggerComputeFileService().computedFile(pollingNode, (AbstractProject) job, info, log);
resolvedFiles[i] = resolvedFile;
}
return resolvedFiles;
}
private boolean checkIfThereAreAtLeastOneChange(FilePath[] resolvedFiles, XTriggerLog log) throws XTriggerException {
for (int i = 0; i < resolvedFiles.length; i++) {
if (offlineSlaveOnStartup) {
log.info("No nodes were available at startup or at previous poll.");
offlineSlaveOnStartup = false;
return false;
}
FileNameTriggerInfo info = fileInfo[i];
FilePath resolvedFile = resolvedFiles[i];
boolean changed = checkIfModifiedFile(resolvedFile, info, log);
if (changed) {
return true;
}
}
return false;
}
private void refreshNewResolvedFiles(FilePath[] resolvedFiles) throws XTriggerException {
for (int i = 0; i < resolvedFiles.length; i++) {
refreshMemoryInfo(fileInfo[i], resolvedFiles[i]);
}
}
private boolean checkIfModifiedFile(FilePath newResolvedFile, final FileNameTriggerInfo info, final XTriggerLog log) throws XTriggerException {
// Do not trigger a build if the new computed file doesn't exist.
if (newResolvedFile == null) {
log.info("The computed file doesn't exist.");
return false;
}
try {
FilePath resolvedFile = info.getResolvedFile();
final Long lastModification = info.getLastModifications();
final String resolvedFilePath = (resolvedFile != null) ? resolvedFile.getRemote() : null;
boolean changedFileName = newResolvedFile.act(new FilePath.FileCallable<Boolean>() {
public Boolean invoke(File newResolvedFile, VirtualChannel channel) throws IOException, InterruptedException {
try {
FSTriggerFileNameCheckedModifiedService service = new FSTriggerFileNameCheckedModifiedService(log, info, resolvedFilePath, lastModification, newResolvedFile);
return service.checkFileName();
} catch (XTriggerException fse) {
throw new RuntimeException(fse);
}
}
});
if (changedFileName) {
return true;
}
boolean inspectingContentFile = info.isInspectingContentFile();
FSTriggerContentFileType[] contentFileTypes = info.getContentFileTypes();
if (inspectingContentFile) {
log.info("Inspecting the contents of '" + newResolvedFile + "'");
for (final FSTriggerContentFileType type : contentFileTypes) {
final Object memoryObject = type.getMemoryInfo();
if (memoryObject == null) {
log.info("No modifications according the given criteria.");
return false;
}
boolean isTriggered = newResolvedFile.act(new FilePath.FileCallable<Boolean>() {
public Boolean invoke(File newResolvedFile, VirtualChannel channel) throws IOException, InterruptedException {
boolean isTriggered;
try {
FSTriggerFileNameCheckedModifiedService service = new FSTriggerFileNameCheckedModifiedService(log, info, resolvedFilePath, lastModification, newResolvedFile);
type.setMemoryInfo(memoryObject);
isTriggered = service.checkContentType(type);
} catch (XTriggerException fse) {
throw new RuntimeException(fse);
}
return isTriggered;
}
});
if (isTriggered) {
return true;
}
}
return false;
}
} catch (IOException ioe) {
throw new XTriggerException(ioe);
} catch (InterruptedException ie) {
throw new XTriggerException(ie);
} catch (Throwable e) {
throw new XTriggerException(e);
}
return false;
}
@Override
protected String getName() {
return "FSTrigger";
}
@Override
public String getCause() {
return CAUSE;
}
@Override
public Collection<? extends Action> getProjectActions() {
return Collections.singleton(new FSTriggerFilesAction(this.getDescriptor().getDisplayName()));
}
@Override
public FileNameTriggerDescriptor getDescriptor() {
return (FileNameTriggerDescriptor) Hudson.getInstance().getDescriptorOrDie(getClass());
}
public final class FSTriggerFilesAction extends FSTriggerAction {
private transient String actionTitle;
public FSTriggerFilesAction(String actionTitle) {
this.actionTitle = actionTitle;
}
@Override
public String getDisplayName() {
return "FSTrigger Files Log";
}
@Override
public String getUrlName() {
return "triggerPollLogFiles";
}
@Override
public String getIconFileName() {
return "clipboard.gif";
}
@SuppressWarnings("unused")
public String getActionTitle() {
return actionTitle;
}
@SuppressWarnings("unused")
public String getLog() throws IOException {
return Util.loadFile(getLogFile());
}
@SuppressWarnings("unused")
public void writeLogTo(XMLOutput out) throws IOException {
new AnnotatedLargeText<FSTriggerFilesAction>(getLogFile(), Charset.defaultCharset(), true, this).writeHtmlTo(0, out.asWriter());
}
}
@Extension
@SuppressWarnings("unused")
public static class FileNameTriggerDescriptor extends XTriggerDescriptor {
private transient final SequentialExecutionQueue queue = new SequentialExecutionQueue(Executors.newSingleThreadExecutor());
public ExecutorService getExecutor() {
return queue.getExecutors();
}
@Override
public boolean isApplicable(Item item) {
return true;
}
@Override
public String getDisplayName() {
return org.jenkinsci.plugins.fstrigger.Messages.fstrigger_fileNameContent_displayName();
}
@Override
public String getHelpFile() {
return "/plugin/fstrigger/help-monitorFile.html";
}
@SuppressWarnings("unchecked")
public DescriptorExtensionList getListFSTriggerFileNameDescriptors() {
return DescriptorExtensionList.createDescriptorList(Hudson.getInstance(), FSTriggerContentFileType.class);
}
/**
* Performs syntax check.
*
* @param value the file pattern
* @return the form validation object
*/
public FormValidation doCheckFile(@QueryParameter String value) {
if (value == null || value.trim().isEmpty()) {
return FormValidation.error("You must provide a file to monitor");
}
value = value.replaceAll("[\t\r\n]+", " ");
value = value.trim();
if (value.length() < 2) {
return FormValidation.error("You must provide a folder.");
}
return FormValidation.ok();
}
public FormValidation doCheckContentNature() {
return FormValidation.ok();
}
@Override
public FileNameTrigger newInstance(StaplerRequest req, JSONObject formData) throws FormException {
FileNameTrigger fileNameTrigger;
String cronTab = formData.getString("cronTabSpec");
Object entryObject = formData.get("fileElement");
List<FileNameTriggerInfo> entries = new ArrayList<FileNameTriggerInfo>();
if (entryObject instanceof JSONObject) {
entries.add(fillAndGetEntry(req, (JSONObject) entryObject));
} else {
JSONArray jsonArray = (JSONArray) entryObject;
if (jsonArray != null) {
for (Object aJsonArray : jsonArray) {
entries.add(fillAndGetEntry(req, (JSONObject) aJsonArray));
}
}
}
try {
return new FileNameTrigger(cronTab, entries.toArray(new FileNameTriggerInfo[entries.size()]));
} catch (ANTLRException ae) {
throw new FormException(ae, "cronTabSpec");
}
}
private FileNameTriggerInfo fillAndGetEntry(StaplerRequest req, JSONObject entryObject) {
FileNameTriggerInfo info = new FileNameTriggerInfo();
info.setFilePathPattern(Util.fixEmpty(entryObject.getString("filePathPattern")));
info.setStrategy(entryObject.getString("strategy"));
setInfoContentType(req, entryObject, info);
return info;
}
private void setInfoContentType(StaplerRequest req, JSONObject entryObject, FileNameTriggerInfo info) {
Object inspectingFileContentObject = entryObject.get("inspectingContentFile");
if (inspectingFileContentObject == null) {
unsetContentType(info);
} else {
JSONObject inspectingFileContentJSONObject = entryObject.getJSONObject("inspectingContentFile");
if (isNoContentNatureSelected(inspectingFileContentJSONObject)) {
unsetContentType(info);
} else {
info.setInspectingContentFile(true);
info.setDoNotCheckLastModificationDate(inspectingFileContentJSONObject.getBoolean("doNotCheckLastModificationDate"));
setContentNature(req, info, inspectingFileContentJSONObject);
}
}
}
private void unsetContentType(FileNameTriggerInfo info) {
info.setInspectingContentFile(false);
info.setDoNotCheckLastModificationDate(false);
info.setContentFileTypes(new FSTriggerContentFileType[0]);
}
private boolean isNoContentNatureSelected(JSONObject inspectingFileContentJSONObject) {
return inspectingFileContentJSONObject.size() == 1;
}
private void setContentNature(StaplerRequest req, FileNameTriggerInfo info, JSONObject inspectingFileContentJSONObject) {
JSON contentFileTypesJsonElt;
try {
contentFileTypesJsonElt = inspectingFileContentJSONObject.getJSONArray("contentFileTypes");
} catch (JSONException jsone) {
contentFileTypesJsonElt = inspectingFileContentJSONObject.getJSONObject("contentFileTypes");
}
List<FSTriggerContentFileType> types = req.bindJSONToList(FSTriggerContentFileType.class, contentFileTypesJsonElt);
info.setContentFileTypes(types.toArray(new FSTriggerContentFileType[types.size()]));
}
}
/**
* Backward compatibility
*/
@SuppressWarnings("unused")
@Deprecated
private transient String folderPath;
@SuppressWarnings("unused")
@Deprecated
private transient String fileName;
@SuppressWarnings("unused")
@Deprecated
private transient String strategy;
@SuppressWarnings("unused")
@Deprecated
private transient boolean inspectingContentFile;
@SuppressWarnings("unused")
@Deprecated
private transient boolean doNotCheckLastModificationDate;
@SuppressWarnings("unused")
@Deprecated
private transient FSTriggerContentFileType[] contentFileTypes;
@SuppressWarnings({"unused", "deprecation"})
@Deprecated
public String getFolderPath() {
return folderPath;
}
@SuppressWarnings({"unused", "deprecation"})
@Deprecated
public String getFileName() {
return fileName;
}
@SuppressWarnings({"unused", "deprecation"})
@Deprecated
public String getStrategy() {
return strategy;
}
@SuppressWarnings({"unused", "deprecation"})
@Deprecated
public boolean isInspectingContentFile() {
return inspectingContentFile;
}
@SuppressWarnings({"unused", "deprecation"})
@Deprecated
public boolean isDoNotCheckLastModificationDate() {
return doNotCheckLastModificationDate;
}
@SuppressWarnings({"unused", "deprecation"})
@Deprecated
public FSTriggerContentFileType[] getContentFileTypes() {
return contentFileTypes;
}
@SuppressWarnings({"unused", "deprecation"})
protected Object readResolve() throws ObjectStreamException {
//Call Trigger readResolver to set tab field
super.readResolve();
//Previous version 0.11
if (folderPath != null) {
FileNameTriggerInfo info = new FileNameTriggerInfo();
info.setFilePathPattern(folderPath + File.separatorChar + fileName);
info.setStrategy(strategy);
info.setInspectingContentFile(inspectingContentFile);
info.setDoNotCheckLastModificationDate(doNotCheckLastModificationDate);
info.setContentFileTypes(contentFileTypes);
fileInfo = new FileNameTriggerInfo[]{info};
}
return this;
}
}