001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache license, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the license for the specific language governing permissions and 015 * limitations under the license. 016 */ 017 018package org.apache.logging.log4j.core.appender.rolling.action; 019 020import org.apache.logging.log4j.core.config.plugins.Plugin; 021import org.apache.logging.log4j.core.config.plugins.PluginAttribute; 022import org.apache.logging.log4j.core.config.plugins.PluginFactory; 023 024/** 025 * {@link PathSorter} that sorts path by their LastModified attribute. 026 */ 027@Plugin(name = "SortByModificationTime", category = "Core", printObject = true) 028public class PathSortByModificationTime implements PathSorter { 029 030 private final boolean recentFirst; 031 private final int multiplier; 032 033 /** 034 * Constructs a new SortByModificationTime sorter. 035 * 036 * @param recentFirst if true, most recently modified paths should come first 037 */ 038 public PathSortByModificationTime(final boolean recentFirst) { 039 this.recentFirst = recentFirst; 040 this.multiplier = recentFirst ? 1 : -1; 041 } 042 043 /** 044 * Create a PathSorter that sorts by lastModified time. 045 * 046 * @param recentFirst if true, most recently modified paths should come first. 047 * @return A PathSorter. 048 */ 049 @PluginFactory 050 public static PathSorter createSorter( // 051 @PluginAttribute(value = "recentFirst", defaultBoolean = true) final boolean recentFirst) { 052 return new PathSortByModificationTime(recentFirst); 053 } 054 055 /** 056 * Returns whether this sorter sorts recent files first. 057 * 058 * @return whether this sorter sorts recent files first 059 */ 060 public boolean isRecentFirst() { 061 return recentFirst; 062 } 063 064 /* 065 * (non-Javadoc) 066 * 067 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object) 068 */ 069 @Override 070 public int compare(final PathWithAttributes path1, final PathWithAttributes path2) { 071 final long lastModified1 = path1.getAttributes().lastModifiedTime().toMillis(); 072 final long lastModified2 = path2.getAttributes().lastModifiedTime().toMillis(); 073 int result = Long.signum(lastModified2 - lastModified1); 074 if (result == 0) { // if same time compare paths lexicographically 075 try { 076 // assuming paths contain counters and dates, use reverse lexicographical order: 077 // 20151129 before 20151128, path-2.log before path-1.log 078 result = path2.getPath().compareTo(path1.getPath()); 079 } catch (final ClassCastException ex) { 080 result = path2.getPath().toString().compareTo(path1.getPath().toString()); 081 } 082 } 083 return multiplier * result; 084 } 085}