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 import java.util.concurrent.Future;
19
20 import ch.qos.logback.core.CoreConstants;
21 import ch.qos.logback.core.rolling.helper.ArchiveRemover;
22 import ch.qos.logback.core.rolling.helper.AsynchronousCompressor;
23 import ch.qos.logback.core.rolling.helper.CompressionMode;
24 import ch.qos.logback.core.rolling.helper.Compressor;
25 import ch.qos.logback.core.rolling.helper.FileNamePattern;
26 import ch.qos.logback.core.rolling.helper.RenameUtil;
27
28
29
30
31
32
33
34
35
36
37
38 public class TimeBasedRollingPolicy<E> extends RollingPolicyBase implements
39 TriggeringPolicy<E> {
40 static final String FNP_NOT_SET = "The FileNamePattern option must be set before using TimeBasedRollingPolicy. ";
41 static final int NO_DELETE_HISTORY = 0;
42
43
44 FileNamePattern fileNamePatternWCS;
45
46 private Compressor compressor;
47 private RenameUtil renameUtil = new RenameUtil();
48 Future<?> future;
49
50 private int maxHistory = NO_DELETE_HISTORY;
51 private ArchiveRemover archiveRemover;
52
53 TimeBasedFileNamingAndTriggeringPolicy<E> timeBasedTriggering;
54
55 public void start() {
56
57 renameUtil.setContext(this.context);
58
59
60 if (fileNamePatternStr != null) {
61 fileNamePattern = new FileNamePattern(fileNamePatternStr, this.context);
62 determineCompressionMode();
63 } else {
64 addWarn(FNP_NOT_SET);
65 addWarn(CoreConstants.SEE_FNP_NOT_SET);
66 throw new IllegalStateException(FNP_NOT_SET
67 + CoreConstants.SEE_FNP_NOT_SET);
68 }
69
70 compressor = new Compressor(compressionMode);
71 compressor.setContext(context);
72
73 fileNamePatternWCS = new FileNamePattern(computeFileNameStr_WCS(
74 fileNamePatternStr, compressionMode), this.context);
75
76 addInfo("Will use the pattern " + fileNamePatternWCS
77 + " for the active file");
78
79 if (timeBasedTriggering == null) {
80 timeBasedTriggering = new DefaultTimeBasedFileNamingAndTriggeringPolicy<E>();
81 }
82 timeBasedTriggering.setContext(context);
83 timeBasedTriggering.setTimeBasedRollingPolicy(this);
84 timeBasedTriggering.start();
85
86
87
88
89 if (maxHistory != NO_DELETE_HISTORY) {
90 archiveRemover = timeBasedTriggering.getArchiveRemover();
91 archiveRemover.setMaxHistory(maxHistory);
92 }
93 }
94
95 public void setTimeBasedFileNamingAndTriggeringPolicy(
96 TimeBasedFileNamingAndTriggeringPolicy<E> timeBasedTriggering) {
97 this.timeBasedTriggering = timeBasedTriggering;
98 }
99
100 public TimeBasedFileNamingAndTriggeringPolicy<E> getTimeBasedFileNamingAndTriggeringPolicy() {
101 return timeBasedTriggering;
102 }
103
104 static public String computeFileNameStr_WCS(String fileNamePatternStr,
105 CompressionMode compressionMode) {
106 int len = fileNamePatternStr.length();
107 switch (compressionMode) {
108 case GZ:
109 return fileNamePatternStr.substring(0, len - 3);
110 case ZIP:
111 return fileNamePatternStr.substring(0, len - 4);
112 case NONE:
113 return fileNamePatternStr;
114 }
115 throw new IllegalStateException("Execution should not reach this point");
116 }
117
118 public void rollover() throws RolloverFailure {
119
120
121
122
123 String elapsedPeriodsFileName = timeBasedTriggering
124 .getElapsedPeriodsFileName();
125
126 if (compressionMode == CompressionMode.NONE) {
127 if (getParentsRawFileProperty() != null) {
128 renameUtil.rename(getParentsRawFileProperty(), elapsedPeriodsFileName);
129 }
130 } else {
131 if (getParentsRawFileProperty() == null) {
132 future = asyncCompress(elapsedPeriodsFileName, elapsedPeriodsFileName);
133 } else {
134 future = renamedRawAndAsyncCompress(elapsedPeriodsFileName);
135 }
136 }
137
138 if (archiveRemover != null) {
139 archiveRemover.clean(new Date(timeBasedTriggering.getCurrentTime()));
140 }
141 }
142
143 Future asyncCompress(String nameOfFile2Compress, String nameOfCompressedFile)
144 throws RolloverFailure {
145 AsynchronousCompressor ac = new AsynchronousCompressor(compressor);
146 return ac.compressAsynchronously(nameOfFile2Compress, nameOfCompressedFile);
147 }
148
149 Future renamedRawAndAsyncCompress(String nameOfCompressedFile)
150 throws RolloverFailure {
151 String parentsRawFile = getParentsRawFileProperty();
152 String tmpTarget = parentsRawFile + System.nanoTime() + ".tmp";
153 renameUtil.rename(parentsRawFile, tmpTarget);
154 return asyncCompress(tmpTarget, nameOfCompressedFile);
155 }
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177 public String getActiveFileName() {
178 String parentsRawFileProperty = getParentsRawFileProperty();
179 if (parentsRawFileProperty != null) {
180 return parentsRawFileProperty;
181 } else {
182 return timeBasedTriggering
183 .getCurrentPeriodsFileNameWithoutCompressionSuffix();
184 }
185 }
186
187 public boolean isTriggeringEvent(File activeFile, final E event) {
188 return timeBasedTriggering.isTriggeringEvent(activeFile, event);
189 }
190
191
192
193
194
195
196 public int getMaxHistory() {
197 return maxHistory;
198 }
199
200
201
202
203
204
205
206 public void setMaxHistory(int maxHistory) {
207 this.maxHistory = maxHistory;
208 }
209
210 @Override
211 public String toString() {
212 return "c.q.l.core.rolling.TimeBasedRollingPolicy";
213 }
214 }