1
2
3
4
5
6
7
8
9
10
11
12
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
83
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
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 }