KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > util > log > output > io > rotate > ExpandingFileStrategy


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE file.
7  */

8 package org.jivesoftware.util.log.output.io.rotate;
9
10 import java.io.File JavaDoc;
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 ExpandingFileStrategy implements FileStrategy {
26
27     ///the base file name.
28
private String JavaDoc baseFileName;
29
30     public ExpandingFileStrategy(final String JavaDoc baseFileName) {
31
32         this.baseFileName = baseFileName;
33     }
34
35     public File JavaDoc currentFile() {
36         return new File JavaDoc(baseFileName);
37     }
38
39     /**
40      * Calculate the real file name from the base filename.
41      *
42      * @return File the calculated file name
43      */

44     public File JavaDoc nextFile() {
45         // go through all the possible filenames and delete/rename as necessary
46
for (int i = 0; true; i++) {
47             File JavaDoc test = new File JavaDoc(baseFileName.substring(0, baseFileName.lastIndexOf('.')) +
48                     "_" + i + baseFileName.substring(baseFileName.lastIndexOf('.')));
49
50             if (test.exists()) {
51                 continue;
52             }
53             else {
54                 return test;
55             }
56         }
57     }
58 }
59
60
Popular Tags