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.time.Instant;
18  import java.util.Arrays;
19  import java.util.Comparator;
20  import java.util.Date;
21  import java.util.regex.Matcher;
22  import java.util.regex.Pattern;
23  
24  public class SizeAndTimeBasedArchiveRemover extends TimeBasedArchiveRemover {
25  
26      protected static final int NO_INDEX = -1;
27  
28      public SizeAndTimeBasedArchiveRemover(FileNamePattern fileNamePattern, RollingCalendar rc) {
29          super(fileNamePattern, rc);
30      }
31  
32      protected File[] getFilesInPeriod(Instant instantOfPeriodToClean) {
33          File archive0 = new File(fileNamePattern.convertMultipleArguments(instantOfPeriodToClean, 0));
34          File parentDir = getParentDir(archive0);
35          String stemRegex = createStemRegex(instantOfPeriodToClean);
36          File[] matchingFileArray = FileFilterUtil.filesInFolderMatchingStemRegex(parentDir, stemRegex);
37          return matchingFileArray;
38      }
39  
40      private String createStemRegex(final Instant instantOfPeriodToClean) {
41          String regex = fileNamePattern.toRegexForFixedDate(instantOfPeriodToClean);
42          return FileFilterUtil.afterLastSlash(regex);
43      }
44  
45      @Override
46      protected void descendingSort(File[] matchingFileArray, Instant instant) {
47  
48          String regexForIndexExtreaction = createStemRegex(instant);
49          final Pattern pattern = Pattern.compile(regexForIndexExtreaction);
50  
51          Arrays.sort(matchingFileArray, new Comparator<File>() {
52              @Override
53              public int compare(final File f1, final File f2) {
54  
55                  int index1 = extractIndex(pattern, f1);
56                  int index2 = extractIndex(pattern, f2);
57  
58                  if (index1 == index2)
59                      return 0;
60                  // descending sort, i.e. newest files first
61                  if (index2 < index1)
62                      return -1;
63                  else
64                      return 1;
65              }
66  
67              private int extractIndex(Pattern pattern, File f1) {
68                  Matcher matcher = pattern.matcher(f1.getName());
69                  if (matcher.find()) {
70                      String indexAsStr = matcher.group(1);
71  
72                      if (indexAsStr == null || indexAsStr.isEmpty())
73                          return NO_INDEX; // unreachable code?
74                      else
75                          return Integer.parseInt(indexAsStr);
76                  } else
77                      return NO_INDEX;
78              }
79          });
80      }
81  
82  }