Skip to content

Commit c21ec13

Browse files
Bukamaslawekjaranowski
authored andcommitted
Loggerinterface Basic
1 parent c6b7d95 commit c21ec13

File tree

2 files changed

+77
-87
lines changed

2 files changed

+77
-87
lines changed

src/main/java/org/apache/maven/plugins/clean/CleanMojo.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,8 @@ public class CleanMojo extends AbstractMojo {
186186
* should usually reside on the same volume. The exact conditions are system dependant though, but if an atomic
187187
* move is not supported, the standard deletion mechanism will be used.
188188
*
189-
* @since 3.2
190189
* @see #fast
190+
* @since 3.2
191191
*/
192192
@Parameter(property = "maven.clean.fastDir")
193193
private File fastDir;
@@ -199,8 +199,8 @@ public class CleanMojo extends AbstractMojo {
199199
* the actual file deletion should be started in the background when the session ends (this should only be used
200200
* when maven is embedded in a long running process).
201201
*
202-
* @since 3.2
203202
* @see #fast
203+
* @since 3.2
204204
*/
205205
@Parameter(property = "maven.clean.fastMode", defaultValue = FAST_MODE_BACKGROUND)
206206
private String fastMode;

src/main/java/org/apache/maven/plugins/clean/Cleaner.java

+75-85
Original file line numberDiff line numberDiff line change
@@ -56,65 +56,60 @@ class Cleaner {
5656
*/
5757
private final MavenSession session;
5858

59-
private final Logger logDebug;
60-
61-
private final Logger logInfo;
62-
63-
private final Logger logVerbose;
64-
65-
private final Logger logWarn;
66-
6759
private final File fastDir;
6860

6961
private final String fastMode;
7062

63+
private final boolean verbose;
64+
65+
private Log log;
66+
7167
/**
7268
* 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.
7675
*/
7776
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-
8677
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;
8781
this.fastDir = fastDir;
8882
this.fastMode = fastMode;
83+
this.verbose = verbose;
8984
}
9085

9186
/**
9287
* Deletes the specified directories and its contents.
9388
*
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.
9893
* @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.
10196
* @throws IOException If a file/directory could not be deleted and <code>failOnError</code> is <code>true</code>.
10297
*/
10398
public void delete(
10499
File basedir, Selector selector, boolean followSymlinks, boolean failOnError, boolean retryOnError)
105100
throws IOException {
106101
if (!basedir.isDirectory()) {
107102
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);
110105
}
111106
return;
112107
}
113108
throw new IOException("Invalid base directory " + basedir);
114109
}
115110

116-
if (logInfo != null) {
117-
logInfo.log("Deleting " + basedir + (selector != null ? " (" + selector + ")" : ""));
111+
if (log.isInfoEnabled()) {
112+
log.info("Deleting " + basedir + (selector != null ? " (" + selector + ")" : ""));
118113
}
119114

120115
File file = followSymlinks ? basedir : basedir.getCanonicalFile();
@@ -148,9 +143,8 @@ private boolean fastDelete(File baseDirFile) {
148143
throw e;
149144
}
150145
} 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);
154148
}
155149
return false;
156150
}
@@ -161,10 +155,11 @@ private boolean fastDelete(File baseDirFile) {
161155
Files.createDirectories(fastDir);
162156
}
163157
} 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);
168163
}
169164
return false;
170165
}
@@ -180,9 +175,8 @@ private boolean fastDelete(File baseDirFile) {
180175
BackgroundCleaner.delete(this, tmpDir.toFile(), fastMode);
181176
return true;
182177
} 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);
186180
}
187181
return false;
188182
}
@@ -191,15 +185,15 @@ private boolean fastDelete(File baseDirFile) {
191185
/**
192186
* Deletes the specified file or directory.
193187
*
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.
200194
* @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.
203197
* @return The result of the cleaning, never <code>null</code>.
204198
* @throws IOException If a file/directory could not be deleted and <code>failOnError</code> is <code>true</code>.
205199
*/
@@ -229,24 +223,30 @@ private Result delete(
229223
child, prefix + filename, selector, followSymlinks, failOnError, retryOnError));
230224
}
231225
}
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);
234228
}
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);
237231
}
238232
}
239233

240234
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);
249248
}
249+
250250
result.failures += delete(file, failOnError, retryOnError);
251251
} else {
252252
result.excluded = true;
@@ -266,8 +266,8 @@ private boolean isSymbolicLink(Path path) throws IOException {
266266
* Deletes the specified file, directory. If the path denotes a symlink, only the link is removed, its target is
267267
* left untouched.
268268
*
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.
271271
* @param retryOnError Whether to undertake additional delete attempts in case the first attempt failed.
272272
* @return <code>0</code> if the file was deleted, <code>1</code> otherwise.
273273
* @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
299299
if (failOnError) {
300300
throw new IOException("Failed to delete " + file);
301301
} else {
302-
if (logWarn != null) {
303-
logWarn.log("Failed to delete " + file);
302+
if (log.isWarnEnabled()) {
303+
log.warn("Failed to delete " + file);
304304
}
305305
return 1;
306306
}
@@ -322,27 +322,24 @@ public void update(Result result) {
322322
}
323323
}
324324

325-
private interface Logger {
326-
327-
void log(CharSequence message);
328-
}
329-
330325
private static class BackgroundCleaner extends Thread {
331326

327+
private static final int NEW = 0;
328+
private static final int RUNNING = 1;
329+
private static final int STOPPED = 2;
332330
private static BackgroundCleaner instance;
333-
334331
private final Deque<File> filesToDelete = new ArrayDeque<>();
335-
336332
private final Cleaner cleaner;
337-
338333
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-
344334
private int status = NEW;
345335

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+
346343
public static void delete(Cleaner cleaner, File dir, String fastMode) {
347344
synchronized (BackgroundCleaner.class) {
348345
if (instance == null || !instance.doDelete(dir)) {
@@ -359,13 +356,6 @@ static void sessionEnd() {
359356
}
360357
}
361358

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-
369359
public void run() {
370360
while (true) {
371361
File basedir = pollNext();
@@ -450,8 +440,8 @@ synchronized void doSessionEnd() {
450440
}
451441
if (!FAST_MODE_DEFER.equals(fastMode)) {
452442
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");
455445
}
456446
while (status != STOPPED) {
457447
wait();

0 commit comments

Comments
 (0)