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.util.Arrays;
018import java.util.Comparator;
019import java.util.Date;
020import java.util.regex.Matcher;
021import java.util.regex.Pattern;
022
023public class SizeAndTimeBasedArchiveRemover extends TimeBasedArchiveRemover {
024
025    protected static final int NO_INDEX = -1;
026
027    public SizeAndTimeBasedArchiveRemover(FileNamePattern fileNamePattern, RollingCalendar rc) {
028        super(fileNamePattern, rc);
029    }
030
031    protected File[] getFilesInPeriod(Date dateOfPeriodToClean) {
032        File archive0 = new File(fileNamePattern.convertMultipleArguments(dateOfPeriodToClean, 0));
033        File parentDir = getParentDir(archive0);
034        String stemRegex = createStemRegex(dateOfPeriodToClean);
035        File[] matchingFileArray = FileFilterUtil.filesInFolderMatchingStemRegex(parentDir, stemRegex);
036        return matchingFileArray;
037    }
038
039    private String createStemRegex(final Date dateOfPeriodToClean) {
040        String regex = fileNamePattern.toRegexForFixedDate(dateOfPeriodToClean);
041        return FileFilterUtil.afterLastSlash(regex);
042    }
043
044    @Override
045    protected void descendingSort(File[] matchingFileArray, Date date) {
046
047        String regexForIndexExtreaction = createStemRegex(date);
048        final Pattern pattern = Pattern.compile(regexForIndexExtreaction);
049
050        Arrays.sort(matchingFileArray, new Comparator<File>() {
051            @Override
052            public int compare(final File f1, final File f2) {
053
054                int index1 = extractIndex(pattern, f1);
055                int index2 = extractIndex(pattern, f2);
056
057                if (index1 == index2)
058                    return 0;
059                // descending sort, i.e. newest files first
060                if (index2 < index1)
061                    return -1;
062                else
063                    return 1;
064            }
065
066            private int extractIndex(Pattern pattern, File f1) {
067                Matcher matcher = pattern.matcher(f1.getName());
068                if (matcher.find()) {
069                    String indexAsStr = matcher.group(1);
070
071                    if (indexAsStr == null || indexAsStr.isEmpty())
072                        return NO_INDEX; // unreachable code?
073                    else
074                        return Integer.parseInt(indexAsStr);
075                } else
076                    return NO_INDEX;
077            }
078        });
079    }
080
081}