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