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