1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.logging.log4j.core.appender.rolling.action;
19
20 import org.apache.logging.log4j.Logger;
21 import org.apache.logging.log4j.status.StatusLogger;
22
23 import java.io.IOException;
24 import java.nio.file.FileVisitResult;
25 import java.nio.file.NoSuchFileException;
26 import java.nio.file.Path;
27 import java.nio.file.SimpleFileVisitor;
28 import java.nio.file.attribute.BasicFileAttributes;
29 import java.util.ArrayList;
30 import java.util.Collections;
31 import java.util.List;
32 import java.util.Objects;
33
34
35
36
37 public class SortingVisitor extends SimpleFileVisitor<Path> {
38
39 private static final Logger LOGGER = StatusLogger.getLogger();
40 private final PathSorter sorter;
41 private final List<PathWithAttributes> collected = new ArrayList<>();
42
43
44
45
46
47
48
49 public SortingVisitor(final PathSorter sorter) {
50 this.sorter = Objects.requireNonNull(sorter, "sorter");
51 }
52
53 @Override
54 public FileVisitResult visitFile(final Path path, final BasicFileAttributes attrs) throws IOException {
55 collected.add(new PathWithAttributes(path, attrs));
56 return FileVisitResult.CONTINUE;
57 }
58
59 @Override
60 public FileVisitResult visitFileFailed(Path file, IOException ioException) throws IOException {
61
62
63 if (ioException instanceof NoSuchFileException) {
64 LOGGER.info("File {} could not be accessed, it has likely already been deleted", file, ioException);
65 return FileVisitResult.CONTINUE;
66 } else {
67 return super.visitFileFailed(file, ioException);
68 }
69 }
70
71 public List<PathWithAttributes> getSortedPaths() {
72 Collections.sort(collected, sorter);
73 return collected;
74 }
75 }