View Javadoc

1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2011, 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;
15  
16  import java.io.File;
17  import java.util.Date;
18  
19  import ch.qos.logback.core.rolling.helper.ArchiveRemover;
20  import ch.qos.logback.core.rolling.helper.DateTokenConverter;
21  import ch.qos.logback.core.rolling.helper.RollingCalendar;
22  import ch.qos.logback.core.spi.ContextAwareBase;
23  
24  abstract public class TimeBasedFileNamingAndTriggeringPolicyBase<E> extends
25          ContextAwareBase implements TimeBasedFileNamingAndTriggeringPolicy<E> {
26  
27    protected TimeBasedRollingPolicy<E> tbrp;
28  
29    protected ArchiveRemover archiveRemover = null;
30    protected String elapsedPeriodsFileName;
31    protected RollingCalendar rc;
32  
33    protected long artificialCurrentTime = -1;
34    protected Date dateInCurrentPeriod = null;
35  
36    protected long nextCheck;
37    protected boolean started = false;
38  
39    public boolean isStarted() {
40      return started;
41    }
42  
43    public void start() {
44      DateTokenConverter dtc = tbrp.fileNamePattern.getPrimaryDateTokenConverter();
45      if (dtc == null) {
46        throw new IllegalStateException("FileNamePattern ["
47                + tbrp.fileNamePattern.getPattern()
48                + "] does not contain a valid DateToken");
49      }
50  
51      rc = new RollingCalendar();
52      rc.init(dtc.getDatePattern());
53      addInfo("The date pattern is '" + dtc.getDatePattern()
54              + "' from file name pattern '" + tbrp.fileNamePattern.getPattern()
55              + "'.");
56      rc.printPeriodicity(this);
57  
58      setDateInCurrentPeriod(new Date(getCurrentTime()));
59      if (tbrp.getParentsRawFileProperty() != null) {
60        File currentFile = new File(tbrp.getParentsRawFileProperty());
61        if (currentFile.exists() && currentFile.canRead()) {
62          setDateInCurrentPeriod(new Date(currentFile.lastModified()));
63        }
64      }
65  
66      addInfo("Setting initial period to " + dateInCurrentPeriod);
67      computeNextCheck();
68    }
69  
70    public void stop() {
71      started = false;
72    }
73  
74    protected void computeNextCheck() {
75      nextCheck = rc.getNextTriggeringMillis(dateInCurrentPeriod);
76    }
77  
78    protected void setDateInCurrentPeriod(long now) {
79      dateInCurrentPeriod.setTime(now);
80    }
81  
82    // allow Test classes to act on the dateInCurrentPeriod field to simulate old
83    // log files needing rollover
84    public void setDateInCurrentPeriod(Date _dateInCurrentPeriod) {
85      this.dateInCurrentPeriod = _dateInCurrentPeriod;
86    }
87  
88    public String getElapsedPeriodsFileName() {
89      return elapsedPeriodsFileName;
90    }
91  
92    public String getCurrentPeriodsFileNameWithoutCompressionSuffix() {
93      return tbrp.fileNamePatternWCS.convert(dateInCurrentPeriod);
94    }
95  
96    public void setCurrentTime(long timeInMillis) {
97      artificialCurrentTime = timeInMillis;
98    }
99  
100   public long getCurrentTime() {
101     // if time is forced return the time set by user
102     if (artificialCurrentTime >= 0) {
103       return artificialCurrentTime;
104     } else {
105       return System.currentTimeMillis();
106     }
107   }
108 
109   public void setTimeBasedRollingPolicy(TimeBasedRollingPolicy<E> _tbrp) {
110     this.tbrp = _tbrp;
111 
112   }
113 
114   public ArchiveRemover getArchiveRemover() {
115     return archiveRemover;
116   }
117 
118 }