View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.core.rolling.helper;
15  
16  import java.io.File;
17  import java.util.Arrays;
18  import java.util.Comparator;
19  import java.util.Date;
20  import java.util.regex.Matcher;
21  import java.util.regex.Pattern;
22  
23  public class SizeAndTimeBasedArchiveRemover extends TimeBasedArchiveRemover {
24  
25      protected static final int NO_INDEX = -1;
26  
27      public SizeAndTimeBasedArchiveRemover(FileNamePattern fileNamePattern, RollingCalendar rc) {
28          super(fileNamePattern, rc);
29      }
30  
31      protected File[] getFilesInPeriod(Date dateOfPeriodToClean) {
32          File archive0 = new File(fileNamePattern.convertMultipleArguments(dateOfPeriodToClean, 0));
33          File parentDir = getParentDir(archive0);
34          String stemRegex = createStemRegex(dateOfPeriodToClean);
35          File[] matchingFileArray = FileFilterUtil.filesInFolderMatchingStemRegex(parentDir, stemRegex);
36          return matchingFileArray;
37      }
38  
39      private String createStemRegex(final Date dateOfPeriodToClean) {
40          String regex = fileNamePattern.toRegexForFixedDate(dateOfPeriodToClean);
41          return FileFilterUtil.afterLastSlash(regex);
42      }
43  
44      @Override
45      protected void descendingSort(File[] matchingFileArray, Date date) {
46  
47          String regexForIndexExtreaction = createStemRegex(date);
48          final Pattern pattern = Pattern.compile(regexForIndexExtreaction);
49  
50          Arrays.sort(matchingFileArray, new Comparator<File>() {
51              @Override
52              public int compare(final File f1, final File f2) {
53  
54                  int index1 = extractIndex(pattern, f1);
55                  int index2 = extractIndex(pattern, f2);
56  
57                  if (index1 == index2)
58                      return 0;
59                  // descending sort, i.e. newest files first
60                  if (index2 < index1)
61                      return -1;
62                  else
63                      return 1;
64              }
65  
66              private int extractIndex(Pattern pattern, File f1) {
67                  Matcher matcher = pattern.matcher(f1.getName());
68                  if (matcher.find()) {
69                      String indexAsStr = matcher.group(1);
70  
71                      if (indexAsStr == null || indexAsStr.isEmpty())
72                          return NO_INDEX; // unreachable code?
73                      else
74                          return Integer.parseInt(indexAsStr);
75                  } else
76                      return NO_INDEX;
77              }
78          });
79      }
80  
81  }