KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log > output > io > rotate > UniqueFileStrategy


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

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

38 public class UniqueFileStrategy
39     implements FileStrategy
40 {
41     private File JavaDoc m_baseFile;
42
43     private SimpleDateFormat JavaDoc m_formatter;
44
45     private String JavaDoc m_suffix;
46
47     /**
48      * Creation of a new Unique File Strategy ??
49      * @param baseFile the base file
50      */

51     public UniqueFileStrategy( final File JavaDoc baseFile )
52     {
53         m_baseFile = baseFile;
54     }
55
56     /**
57      * Creation of a new Unique File Strategy ??
58      * @param baseFile the base file
59      * @param pattern the format pattern
60      */

61     public UniqueFileStrategy( final File JavaDoc baseFile, String JavaDoc pattern )
62     {
63         this( baseFile );
64         m_formatter = new SimpleDateFormat JavaDoc( pattern );
65     }
66
67     /**
68      * Creation of a new Unique File Strategy ??
69      * @param baseFile the base file
70      * @param pattern the format pattern
71      * @param suffix the suffix ??
72      */

73     public UniqueFileStrategy( final File JavaDoc baseFile, String JavaDoc pattern, String JavaDoc suffix )
74     {
75         this( baseFile, pattern );
76         m_suffix = suffix;
77     }
78
79     /**
80      * Calculate the real file name from the base filename.
81      *
82      * @return File the calculated file name
83      */

84     public File JavaDoc nextFile()
85     {
86         final StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
87         sb.append( m_baseFile );
88         if( m_formatter == null )
89         {
90             sb.append( System.currentTimeMillis() );
91         }
92         else
93         {
94             final String JavaDoc dateString = m_formatter.format( new Date JavaDoc() );
95             sb.append( dateString );
96         }
97
98         if( m_suffix != null )
99         {
100             sb.append( m_suffix );
101         }
102
103         return new File JavaDoc( sb.toString() );
104     }
105 }
106
107
Popular Tags