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.core.config.plugins.Plugin;
21  import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
22  import org.apache.logging.log4j.core.config.plugins.PluginFactory;
23  
24  /**
25   * {@link PathSorter} that sorts path by their LastModified attribute.
26   */
27  @Plugin(name = "SortByModificationTime", category = "Core", printObject = true)
28  public class PathSortByModificationTime implements PathSorter {
29  
30      private final boolean recentFirst;
31      private final int multiplier;
32  
33      /**
34       * Constructs a new SortByModificationTime sorter.
35       * 
36       * @param recentFirst if true, most recently modified paths should come first
37       */
38      public PathSortByModificationTime(final boolean recentFirst) {
39          this.recentFirst = recentFirst;
40          this.multiplier = recentFirst ? 1 : -1;
41      }
42  
43      /**
44       * Create a PathSorter that sorts by lastModified time.
45       * 
46       * @param recentFirst if true, most recently modified paths should come first.
47       * @return A PathSorter.
48       */
49      @PluginFactory
50      public static PathSorter createSorter( //
51              @PluginAttribute(value = "recentFirst", defaultBoolean = true) final boolean recentFirst) {
52          return new PathSortByModificationTime(recentFirst);
53      }
54  
55      /**
56       * Returns whether this sorter sorts recent files first.
57       * 
58       * @return whether this sorter sorts recent files first
59       */
60      public boolean isRecentFirst() {
61          return recentFirst;
62      }
63  
64      /*
65       * (non-Javadoc)
66       * 
67       * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
68       */
69      @Override
70      public int compare(final PathWithAttributes path1, final PathWithAttributes path2) {
71          final long lastModified1 = path1.getAttributes().lastModifiedTime().toMillis();
72          final long lastModified2 = path2.getAttributes().lastModifiedTime().toMillis();
73          int result = Long.signum(lastModified2 - lastModified1);
74          if (result == 0) { // if same time compare paths lexicographically
75              try {
76                  // assuming paths contain counters and dates, use reverse lexicographical order:
77                  // 20151129 before 20151128, path-2.log before path-1.log
78                  result = path2.getPath().compareTo(path1.getPath());
79              } catch (final ClassCastException ex) {
80                  result = path2.getPath().toString().compareTo(path1.getPath().toString());
81              }
82          }
83          return multiplier * result;
84      }
85  }