KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.text.SimpleDateFormat JavaDoc;
12 import java.util.Date JavaDoc;
13
14 /**
15  * Strategy for naming log files based on appending time suffix.
16  * A file name can be based on simply appending the number of miliseconds
17  * since (not really sure) 1/1/1970.
18  * Other constructors accept a pattern of a <code>SimpleDateFormat</code>
19  * to form the appended string to the base file name as well as a suffix
20  * which should be appended last.
21  * <p/>
22  * A <code>new UniqueFileStrategy( new File("foo.", "yyyy-MM-dd", ".log" )</code>
23  * object will return <code>File</code> objects with file names like
24  * <code>foo.2001-12-24.log</code>
25  *
26  * @author <a HREF="mailto:bh22351@i-one.at">Bernhard Huber</a>
27  * @author <a HREF="mailto:giacomo@apache.org">Giacomo Pati</a>
28  */

29 public class UniqueFileStrategy
30         implements FileStrategy {
31     private File JavaDoc m_baseFile;
32     private File JavaDoc m_currentFile;
33
34     private SimpleDateFormat JavaDoc m_formatter;
35
36     private String JavaDoc m_suffix;
37
38     public UniqueFileStrategy(final File JavaDoc baseFile) {
39         m_baseFile = baseFile;
40     }
41
42     public UniqueFileStrategy(final File JavaDoc baseFile, String JavaDoc pattern) {
43         this(baseFile);
44         m_formatter = new SimpleDateFormat JavaDoc(pattern);
45     }
46
47     public UniqueFileStrategy(final File JavaDoc baseFile, String JavaDoc pattern, String JavaDoc suffix) {
48         this(baseFile, pattern);
49         m_suffix = suffix;
50     }
51
52     public File JavaDoc currentFile() {
53         return m_currentFile;
54     }
55
56     /**
57      * Calculate the real file name from the base filename.
58      *
59      * @return File the calculated file name
60      */

61     public File JavaDoc nextFile() {
62         final StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
63         sb.append(m_baseFile);
64         if (m_formatter == null) {
65             sb.append(System.currentTimeMillis());
66         }
67         else {
68             final String JavaDoc dateString = m_formatter.format(new Date JavaDoc());
69             sb.append(dateString);
70         }
71
72         if (m_suffix != null) {
73             sb.append(m_suffix);
74         }
75
76         m_currentFile = new File JavaDoc(sb.toString());
77         return m_currentFile;
78     }
79 }
80
81
Popular Tags