001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.core.rolling.helper;
015
016import java.io.File;
017import java.time.Instant;
018import java.util.Arrays;
019import java.util.Comparator;
020import java.util.Date;
021import java.util.regex.Matcher;
022import java.util.regex.Pattern;
023
024public class SizeAndTimeBasedArchiveRemover extends TimeBasedArchiveRemover {
025
026    protected static final int NO_INDEX = -1;
027
028    public SizeAndTimeBasedArchiveRemover(FileNamePattern fileNamePattern, RollingCalendar rc) {
029        super(fileNamePattern, rc);
030    }
031
032    protected File[] getFilesInPeriod(Instant instantOfPeriodToClean) {
033        File archive0 = new File(fileNamePattern.convertMultipleArguments(instantOfPeriodToClean, 0));
034        File parentDir = getParentDir(archive0);
035        String stemRegex = createStemRegex(instantOfPeriodToClean);
036        File[] matchingFileArray = FileFilterUtil.filesInFolderMatchingStemRegex(parentDir, stemRegex);
037        return matchingFileArray;
038    }
039
040    private String createStemRegex(final Instant instantOfPeriodToClean) {
041        String regex = fileNamePattern.toRegexForFixedDate(instantOfPeriodToClean);
042        return FileFilterUtil.afterLastSlash(regex);
043    }
044
045    @Override
046    protected void descendingSort(File[] matchingFileArray, Instant instant) {
047
048        String regexForIndexExtreaction = createStemRegex(instant);
049        final Pattern pattern = Pattern.compile(regexForIndexExtreaction);
050
051        Arrays.sort(matchingFileArray, new Comparator<File>() {
052            @Override
053            public int compare(final File f1, final File f2) {
054
055                int index1 = extractIndex(pattern, f1);
056                int index2 = extractIndex(pattern, f2);
057
058                if (index1 == index2)
059                    return 0;
060                // descending sort, i.e. newest files first
061                if (index2 < index1)
062                    return -1;
063                else
064                    return 1;
065            }
066
067            private int extractIndex(Pattern pattern, File f1) {
068                Matcher matcher = pattern.matcher(f1.getName());
069                if (matcher.find()) {
070                    String indexAsStr = matcher.group(1);
071
072                    if (indexAsStr == null || indexAsStr.isEmpty())
073                        return NO_INDEX; // unreachable code?
074                    else
075                        return Integer.parseInt(indexAsStr);
076                } else
077                    return NO_INDEX;
078            }
079        });
080    }
081
082}