01: /*
02: * Copyright (C) The Apache Software Foundation. All rights reserved.
03: *
04: * This software is published under the terms of the Apache Software License
05: * version 1.1, a copy of which has been included with this distribution in
06: * the LICENSE file.
07: */
08: package org.jivesoftware.util.log.output.io.rotate;
09:
10: import java.io.File;
11:
12: /**
13: * strategy for naming log files based on appending revolving suffix.
14: * <p/>
15: * Heavily odified by Bruce Ritchie (Jive Software) to rotate along
16: * the following strategy:
17: * <p/>
18: * current log file will always be the base File name
19: * the next oldest file will be the _1 file
20: * the next oldest file will be the _2 file
21: * etc.
22: *
23: * @author <a href="mailto:bh22351@i-one.at">Bernhard Huber</a>
24: */
25: public class RevolvingFileStrategy implements FileStrategy {
26:
27: ///max file prefix count
28: private int maxCount;
29:
30: ///the base file name.
31: private String baseFileName;
32:
33: public RevolvingFileStrategy(final String baseFileName,
34: final int maxCount) {
35:
36: this .baseFileName = baseFileName;
37: this .maxCount = maxCount;
38:
39: if (-1 == this .maxCount) {
40: this .maxCount = 5;
41: }
42: }
43:
44: public File currentFile() {
45: return new File(baseFileName);
46: }
47:
48: /**
49: * Calculate the real file name from the base filename.
50: *
51: * @return File the calculated file name
52: */
53: public File nextFile() {
54: // go through all the possible filenames and delete/rename as necessary
55: for (int i = maxCount; i > 0; i--) {
56: File test = new File(baseFileName.substring(0, baseFileName
57: .lastIndexOf('.'))
58: + "_"
59: + i
60: + baseFileName.substring(baseFileName
61: .lastIndexOf('.')));
62:
63: if (i == maxCount && test.exists()) {
64: test.delete();
65: }
66:
67: if (test.exists()) {
68: File r = new File(baseFileName.substring(0,
69: baseFileName.lastIndexOf('.'))
70: + "_"
71: + (i + 1)
72: + baseFileName.substring(baseFileName
73: .lastIndexOf('.')));
74: test.renameTo(r);
75: }
76: }
77:
78: // rename the current file
79: File current = new File(baseFileName);
80: File first = new File(baseFileName.substring(0, baseFileName
81: .lastIndexOf('.'))
82: + "_1"
83: + baseFileName.substring(baseFileName.lastIndexOf('.')));
84: current.renameTo(first);
85:
86: // return the base filename
87: return new File(baseFileName);
88: }
89: }
|