@@ -56,65 +56,60 @@ class Cleaner {
56
56
*/
57
57
private final MavenSession session ;
58
58
59
- private final Logger logDebug ;
60
-
61
- private final Logger logInfo ;
62
-
63
- private final Logger logVerbose ;
64
-
65
- private final Logger logWarn ;
66
-
67
59
private final File fastDir ;
68
60
69
61
private final String fastMode ;
70
62
63
+ private final boolean verbose ;
64
+
65
+ private Log log ;
66
+
71
67
/**
72
68
* Creates a new cleaner.
73
- * @param log The logger to use, may be <code>null</code> to disable logging.
74
- * @param verbose Whether to perform verbose logging.
75
- * @param fastMode The fast deletion mode
69
+ *
70
+ * @param session The Maven session to be used.
71
+ * @param log The logger to use.
72
+ * @param verbose Whether to perform verbose logging.
73
+ * @param fastDir The explicit configured directory or to be deleted in fast mode.
74
+ * @param fastMode The fast deletion mode.
76
75
*/
77
76
Cleaner (MavenSession session , final Log log , boolean verbose , File fastDir , String fastMode ) {
78
- logDebug = (log == null || !log .isDebugEnabled ()) ? null : log ::debug ;
79
-
80
- logInfo = (log == null || !log .isInfoEnabled ()) ? null : log ::info ;
81
-
82
- logWarn = (log == null || !log .isWarnEnabled ()) ? null : log ::warn ;
83
-
84
- logVerbose = verbose ? logInfo : logDebug ;
85
-
86
77
this .session = session ;
78
+ // This can't be null as the Cleaner gets it from the CleanMojo which gets it from AbstractMojo class, where it
79
+ // is never null.
80
+ this .log = log ;
87
81
this .fastDir = fastDir ;
88
82
this .fastMode = fastMode ;
83
+ this .verbose = verbose ;
89
84
}
90
85
91
86
/**
92
87
* Deletes the specified directories and its contents.
93
88
*
94
- * @param basedir The directory to delete, must not be <code>null</code>. Non-existing directories will be silently
95
- * ignored.
96
- * @param selector The selector used to determine what contents to delete, may be <code>null</code> to delete
97
- * everything.
89
+ * @param basedir The directory to delete, must not be <code>null</code>. Non-existing directories will be silently
90
+ * ignored.
91
+ * @param selector The selector used to determine what contents to delete, may be <code>null</code> to delete
92
+ * everything.
98
93
* @param followSymlinks Whether to follow symlinks.
99
- * @param failOnError Whether to abort with an exception in case a selected file/directory could not be deleted.
100
- * @param retryOnError Whether to undertake additional delete attempts in case the first attempt failed.
94
+ * @param failOnError Whether to abort with an exception in case a selected file/directory could not be deleted.
95
+ * @param retryOnError Whether to undertake additional delete attempts in case the first attempt failed.
101
96
* @throws IOException If a file/directory could not be deleted and <code>failOnError</code> is <code>true</code>.
102
97
*/
103
98
public void delete (
104
99
File basedir , Selector selector , boolean followSymlinks , boolean failOnError , boolean retryOnError )
105
100
throws IOException {
106
101
if (!basedir .isDirectory ()) {
107
102
if (!basedir .exists ()) {
108
- if (logDebug != null ) {
109
- logDebug . log ("Skipping non-existing directory " + basedir );
103
+ if (log . isDebugEnabled () ) {
104
+ log . debug ("Skipping non-existing directory " + basedir );
110
105
}
111
106
return ;
112
107
}
113
108
throw new IOException ("Invalid base directory " + basedir );
114
109
}
115
110
116
- if (logInfo != null ) {
117
- logInfo . log ("Deleting " + basedir + (selector != null ? " (" + selector + ")" : "" ));
111
+ if (log . isInfoEnabled () ) {
112
+ log . info ("Deleting " + basedir + (selector != null ? " (" + selector + ")" : "" ));
118
113
}
119
114
120
115
File file = followSymlinks ? basedir : basedir .getCanonicalFile ();
@@ -148,9 +143,8 @@ private boolean fastDelete(File baseDirFile) {
148
143
throw e ;
149
144
}
150
145
} catch (IOException e ) {
151
- if (logDebug != null ) {
152
- // TODO: this Logger interface cannot log exceptions and needs refactoring
153
- logDebug .log ("Unable to fast delete directory: " + e );
146
+ if (log .isDebugEnabled ()) {
147
+ log .debug ("Unable to fast delete directory: " , e );
154
148
}
155
149
return false ;
156
150
}
@@ -161,10 +155,11 @@ private boolean fastDelete(File baseDirFile) {
161
155
Files .createDirectories (fastDir );
162
156
}
163
157
} catch (IOException e ) {
164
- if (logDebug != null ) {
165
- // TODO: this Logger interface cannot log exceptions and needs refactoring
166
- logDebug .log ("Unable to fast delete directory as the path " + fastDir
167
- + " does not point to a directory or cannot be created: " + e );
158
+ if (log .isDebugEnabled ()) {
159
+ log .debug (
160
+ "Unable to fast delete directory as the path " + fastDir
161
+ + " does not point to a directory or cannot be created: " ,
162
+ e );
168
163
}
169
164
return false ;
170
165
}
@@ -180,9 +175,8 @@ private boolean fastDelete(File baseDirFile) {
180
175
BackgroundCleaner .delete (this , tmpDir .toFile (), fastMode );
181
176
return true ;
182
177
} catch (IOException e ) {
183
- if (logDebug != null ) {
184
- // TODO: this Logger interface cannot log exceptions and needs refactoring
185
- logDebug .log ("Unable to fast delete directory: " + e );
178
+ if (log .isDebugEnabled ()) {
179
+ log .debug ("Unable to fast delete directory: " , e );
186
180
}
187
181
return false ;
188
182
}
@@ -191,15 +185,15 @@ private boolean fastDelete(File baseDirFile) {
191
185
/**
192
186
* Deletes the specified file or directory.
193
187
*
194
- * @param file The file/directory to delete, must not be <code>null</code>. If <code>followSymlinks</code> is
195
- * <code>false</code>, it is assumed that the parent file is canonical.
196
- * @param pathname The relative pathname of the file, using {@link File#separatorChar}, must not be
197
- * <code>null</code>.
198
- * @param selector The selector used to determine what contents to delete, may be <code>null</code> to delete
199
- * everything.
188
+ * @param file The file/directory to delete, must not be <code>null</code>. If <code>followSymlinks</code> is
189
+ * <code>false</code>, it is assumed that the parent file is canonical.
190
+ * @param pathname The relative pathname of the file, using {@link File#separatorChar}, must not be
191
+ * <code>null</code>.
192
+ * @param selector The selector used to determine what contents to delete, may be <code>null</code> to delete
193
+ * everything.
200
194
* @param followSymlinks Whether to follow symlinks.
201
- * @param failOnError Whether to abort with an exception in case a selected file/directory could not be deleted.
202
- * @param retryOnError Whether to undertake additional delete attempts in case the first attempt failed.
195
+ * @param failOnError Whether to abort with an exception in case a selected file/directory could not be deleted.
196
+ * @param retryOnError Whether to undertake additional delete attempts in case the first attempt failed.
203
197
* @return The result of the cleaning, never <code>null</code>.
204
198
* @throws IOException If a file/directory could not be deleted and <code>failOnError</code> is <code>true</code>.
205
199
*/
@@ -229,24 +223,30 @@ private Result delete(
229
223
child , prefix + filename , selector , followSymlinks , failOnError , retryOnError ));
230
224
}
231
225
}
232
- } else if (logDebug != null ) {
233
- logDebug . log ("Not recursing into symlink " + file );
226
+ } else if (log . isDebugEnabled () ) {
227
+ log . debug ("Not recursing into symlink " + file );
234
228
}
235
- } else if (logDebug != null ) {
236
- logDebug . log ("Not recursing into directory without included files " + file );
229
+ } else if (log . isDebugEnabled () ) {
230
+ log . debug ("Not recursing into directory without included files " + file );
237
231
}
238
232
}
239
233
240
234
if (!result .excluded && (selector == null || selector .isSelected (pathname ))) {
241
- if (logVerbose != null ) {
242
- if (isDirectory ) {
243
- logVerbose .log ("Deleting directory " + file );
244
- } else if (file .exists ()) {
245
- logVerbose .log ("Deleting file " + file );
246
- } else {
247
- logVerbose .log ("Deleting dangling symlink " + file );
248
- }
235
+ String logmessage ;
236
+ if (isDirectory ) {
237
+ logmessage = "Deleting directory " + file ;
238
+ } else if (file .exists ()) {
239
+ logmessage = "Deleting file " + file ;
240
+ } else {
241
+ logmessage = "Deleting dangling symlink " + file ;
242
+ }
243
+
244
+ if (verbose && log .isInfoEnabled ()) {
245
+ log .info (logmessage );
246
+ } else if (log .isDebugEnabled ()) {
247
+ log .debug (logmessage );
249
248
}
249
+
250
250
result .failures += delete (file , failOnError , retryOnError );
251
251
} else {
252
252
result .excluded = true ;
@@ -266,8 +266,8 @@ private boolean isSymbolicLink(Path path) throws IOException {
266
266
* Deletes the specified file, directory. If the path denotes a symlink, only the link is removed, its target is
267
267
* left untouched.
268
268
*
269
- * @param file The file/directory to delete, must not be <code>null</code>.
270
- * @param failOnError Whether to abort with an exception in case the file/directory could not be deleted.
269
+ * @param file The file/directory to delete, must not be <code>null</code>.
270
+ * @param failOnError Whether to abort with an exception in case the file/directory could not be deleted.
271
271
* @param retryOnError Whether to undertake additional delete attempts in case the first attempt failed.
272
272
* @return <code>0</code> if the file was deleted, <code>1</code> otherwise.
273
273
* @throws IOException If a file/directory could not be deleted and <code>failOnError</code> is <code>true</code>.
@@ -299,8 +299,8 @@ private int delete(File file, boolean failOnError, boolean retryOnError) throws
299
299
if (failOnError ) {
300
300
throw new IOException ("Failed to delete " + file );
301
301
} else {
302
- if (logWarn != null ) {
303
- logWarn . log ("Failed to delete " + file );
302
+ if (log . isWarnEnabled () ) {
303
+ log . warn ("Failed to delete " + file );
304
304
}
305
305
return 1 ;
306
306
}
@@ -322,27 +322,24 @@ public void update(Result result) {
322
322
}
323
323
}
324
324
325
- private interface Logger {
326
-
327
- void log (CharSequence message );
328
- }
329
-
330
325
private static class BackgroundCleaner extends Thread {
331
326
327
+ private static final int NEW = 0 ;
328
+ private static final int RUNNING = 1 ;
329
+ private static final int STOPPED = 2 ;
332
330
private static BackgroundCleaner instance ;
333
-
334
331
private final Deque <File > filesToDelete = new ArrayDeque <>();
335
-
336
332
private final Cleaner cleaner ;
337
-
338
333
private final String fastMode ;
339
-
340
- private static final int NEW = 0 ;
341
- private static final int RUNNING = 1 ;
342
- private static final int STOPPED = 2 ;
343
-
344
334
private int status = NEW ;
345
335
336
+ private BackgroundCleaner (Cleaner cleaner , File dir , String fastMode ) {
337
+ super ("mvn-background-cleaner" );
338
+ this .cleaner = cleaner ;
339
+ this .fastMode = fastMode ;
340
+ init (cleaner .fastDir , dir );
341
+ }
342
+
346
343
public static void delete (Cleaner cleaner , File dir , String fastMode ) {
347
344
synchronized (BackgroundCleaner .class ) {
348
345
if (instance == null || !instance .doDelete (dir )) {
@@ -359,13 +356,6 @@ static void sessionEnd() {
359
356
}
360
357
}
361
358
362
- private BackgroundCleaner (Cleaner cleaner , File dir , String fastMode ) {
363
- super ("mvn-background-cleaner" );
364
- this .cleaner = cleaner ;
365
- this .fastMode = fastMode ;
366
- init (cleaner .fastDir , dir );
367
- }
368
-
369
359
public void run () {
370
360
while (true ) {
371
361
File basedir = pollNext ();
@@ -450,8 +440,8 @@ synchronized void doSessionEnd() {
450
440
}
451
441
if (!FAST_MODE_DEFER .equals (fastMode )) {
452
442
try {
453
- if (cleaner .logInfo != null ) {
454
- cleaner .logInfo . log ("Waiting for background file deletion" );
443
+ if (cleaner .log . isInfoEnabled () ) {
444
+ cleaner .log . info ("Waiting for background file deletion" );
455
445
}
456
446
while (status != STOPPED ) {
457
447
wait ();
0 commit comments