-
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathAgentFileVisitor.java
More file actions
291 lines (253 loc) · 10.5 KB
/
Copy pathAgentFileVisitor.java
File metadata and controls
291 lines (253 loc) · 10.5 KB
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
package io.jenkins.plugins.util;
import org.apache.commons.lang3.StringUtils;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.types.FileSet;
import org.apache.tools.ant.types.selectors.TypeSelector;
import org.apache.tools.ant.types.selectors.TypeSelector.FileType;
import edu.hm.hafner.util.FilteredLog;
import edu.hm.hafner.util.VisibleForTesting;
import java.io.File;
import java.io.IOException;
import java.io.Serial;
import java.io.Serializable;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import hudson.remoting.VirtualChannel;
import jenkins.MasterToSlaveFileCallable;
import io.jenkins.plugins.util.AgentFileVisitor.FileVisitorResult;
/**
* Finds all files that match a specified Ant file pattern and visits these files with the processing method
* {@link #processFile(Path, Charset, FilteredLog)}, that has to be implemented by concrete subclasses. This callable
* will be invoked on an agent so all fields and the returned list of results need to be {@link Serializable}.
*
* @param <T>
* the type of the results
*
* @author Ullrich Hafner
*/
public abstract class AgentFileVisitor<T extends Serializable>
extends MasterToSlaveFileCallable<FileVisitorResult<T>> {
@Serial
private static final long serialVersionUID = 2216842481400265078L;
private final String filePattern;
private final String encoding;
private final boolean followSymbolicLinks;
private final boolean errorOnEmptyFiles;
private final FileSystemFacade fileSystemFacade;
private static final String EMPTY_FILE = "Skipping file '%s' because it's empty";
/**
* Creates a new instance of {@link AgentFileVisitor}.
*
* @param filePattern
* ant file-set pattern to scan for files to parse
* @param encoding
* encoding of the files to parse
* @param followSymbolicLinks
* determines whether the visitor should traverse symbolic links
* @param errorOnEmptyFiles
* determines whether the visitor should log errors if a file is empty
*/
protected AgentFileVisitor(final String filePattern, final String encoding, final boolean followSymbolicLinks, final boolean errorOnEmptyFiles) {
this(filePattern, encoding, followSymbolicLinks, errorOnEmptyFiles, new FileSystemFacade());
}
@VisibleForTesting
AgentFileVisitor(final String filePattern, final String encoding, final boolean followSymbolicLinks, final boolean errorOnEmptyFiles, final FileSystemFacade fileSystemFacade) {
super();
this.filePattern = filePattern;
this.encoding = encoding;
this.followSymbolicLinks = followSymbolicLinks;
this.errorOnEmptyFiles = errorOnEmptyFiles;
this.fileSystemFacade = fileSystemFacade;
}
@Override
public final FileVisitorResult<T> invoke(final File workspace, final VirtualChannel channel) {
var log = new FilteredLog("Errors during parsing");
log.logInfo("Searching for all files in '%s' that match the pattern '%s'",
fileSystemFacade.getAbsolutePath(workspace), filePattern);
log.logInfo("Traversing of symbolic links: %s", followSymbolicLinks ? "enabled" : "disabled");
var fileNames = fileSystemFacade.find(filePattern, followSymbolicLinks, workspace);
if (fileNames.length == 0) {
log.logError("No files found for pattern '%s'. Configuration error?", filePattern);
return new FileVisitorResult<>(log);
}
else {
log.logInfo("-> found %s", plural(fileNames.length, "file"));
return new FileVisitorResult<>(log, scanFiles(workspace, fileNames, log));
}
}
private List<T> scanFiles(final File workspace, final String[] fileNames, final FilteredLog log) {
List<T> results = new ArrayList<>();
for (String fileName : fileNames) {
var file = fileSystemFacade.resolve(workspace, fileName);
if (fileSystemFacade.isNotReadable(file)) {
log.logError("Skipping file '%s' because Jenkins has no permission to read the file", fileName);
}
else if (fileSystemFacade.isEmpty(file)) {
if (errorOnEmptyFiles) {
log.logError(EMPTY_FILE, fileName);
}
else {
log.logInfo(EMPTY_FILE, fileName);
}
}
else {
Optional<T> result = processFile(file, new ValidationUtilities().getCharset(encoding), log);
if (result.isPresent()) {
results.add(result.get());
log.logInfo("Successfully processed file '%s'", fileName);
}
else {
log.logError("No result created for file '%s' due to some errors", fileName);
}
}
}
return results;
}
/**
* Creates the correct singular or plural form of the specified word depending on the size of the elements.
*
* @param count
* the count of elements
* @param itemName
* the name of the items (singular)
*
* @return the message
*/
protected String plural(final int count, @SuppressWarnings("SameParameterValue") final String itemName) {
return "%d %s%s".formatted(count, itemName, count == 1 ? "" : "s");
}
protected abstract Optional<T> processFile(Path file, Charset charset, FilteredLog log);
/**
* File system facade that can be replaced by a stub in unit tests.
*/
static class FileSystemFacade implements Serializable {
@Serial
private static final long serialVersionUID = 4052720703351280685L;
String getAbsolutePath(final File file) {
return file.getAbsolutePath();
}
String[] find(final String includesPattern, final boolean followSymbolicLinks, final File workspace) {
return new FileFinder(includesPattern, StringUtils.EMPTY, followSymbolicLinks).find(workspace);
}
Path resolve(final File folder, final String fileName) {
return folder.toPath().resolve(fileName);
}
boolean isNotReadable(final Path file) {
return !Files.isReadable(file);
}
boolean isEmpty(final Path file) {
try {
return Files.size(file) <= 0;
}
catch (IOException e) {
return true;
}
}
}
/**
* Scans the workspace and finds all files matching a given ant pattern.
*
* @author Ullrich Hafner
*/
static class FileFinder extends MasterToSlaveFileCallable<String[]> {
@Serial
private static final long serialVersionUID = 2970029366847565970L;
private final String includesPattern;
private final String excludesPattern;
private final boolean followSymbolicLinks;
FileFinder(final String includesPattern, final String excludesPattern) {
this(includesPattern, excludesPattern, false);
}
FileFinder(final String includesPattern, final String excludesPattern, final boolean followSymbolicLinks) {
super();
this.includesPattern = includesPattern;
this.excludesPattern = excludesPattern;
this.followSymbolicLinks = followSymbolicLinks;
}
/**
* Returns an array with the file names of the specified file pattern that have been found in the workspace.
*
* @param workspace
* root directory of the workspace
* @param channel
* not used
*
* @return the file names of all found files
*/
@Override
public String[] invoke(final File workspace, final VirtualChannel channel) {
return find(workspace);
}
/**
* Returns an array with the file names of the specified file pattern that have been found in the workspace.
*
* @param workspace
* root directory of the workspace
*
* @return the file names of all found files
*/
String[] find(final File workspace) {
try {
var fileSet = new FileSet();
var antProject = new Project();
fileSet.setProject(antProject);
fileSet.setDir(workspace);
fileSet.setIncludes(includesPattern);
var selector = new TypeSelector();
var fileType = new FileType();
fileType.setValue(FileType.FILE);
selector.setType(fileType);
fileSet.addType(selector);
if (StringUtils.isNotBlank(excludesPattern)) {
fileSet.setExcludes(excludesPattern);
}
fileSet.setFollowSymlinks(followSymbolicLinks);
return fileSet.getDirectoryScanner(antProject).getIncludedFiles();
}
catch (BuildException ignored) {
return new String[0]; // as fallback do not return any file
}
}
}
/**
* The results for all found files. Logging messages that have been recorded during the scanning process will be
* also available.
*
* @param <T>
* the type of the results
*/
public static class FileVisitorResult<T extends Serializable> implements Serializable {
@Serial
private static final long serialVersionUID = 5094277468158899325L;
private final FilteredLog log;
@SuppressWarnings("PMD.LooseCoupling")
private final ArrayList<T> results;
FileVisitorResult(final FilteredLog log) {
this(log, Collections.emptyList());
}
FileVisitorResult(final FilteredLog log, final List<T> results) {
this.log = log;
this.results = new ArrayList<>(results);
}
public FilteredLog getLog() {
return log;
}
public List<T> getResults() {
return Collections.unmodifiableList(results);
}
/**
* Returns whether there have been error messages recorded.
*
* @return {@code true} if error messages have been recorded, {@code false} otherwise
*/
public boolean hasErrors() {
return !getLog().getErrorMessages().isEmpty();
}
}
}