View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
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   * FileVisitor that sorts files.
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       * Constructs a new DeletingVisitor.
45       *
46       * @param basePath used to relativize paths
47       * @param pathFilters objects that need to confirm whether a file can be deleted
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          // LOG4J2-2677: Appenders may rollover and purge in parallel. SimpleVisitor rethrows exceptions from
62          // failed attempts to load file attributes.
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  }