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 org.jivesoftware.util.FastDateFormat;
11:
12: import java.io.File;
13: import java.util.Date;
14:
15: /**
16: * Strategy for naming log files based on appending time suffix.
17: * A file name can be based on simply appending the number of miliseconds
18: * since (not really sure) 1/1/1970.
19: * Other constructors accept a pattern of a <code>SimpleDateFormat</code>
20: * to form the appended string to the base file name as well as a suffix
21: * which should be appended last.
22: * <p/>
23: * A <code>new UniqueFileStrategy( new File("foo.", "yyyy-MM-dd", ".log" )</code>
24: * object will return <code>File</code> objects with file names like
25: * <code>foo.2001-12-24.log</code>
26: *
27: * @author <a href="mailto:bh22351@i-one.at">Bernhard Huber</a>
28: * @author <a href="mailto:giacomo@apache.org">Giacomo Pati</a>
29: */
30: public class UniqueFileStrategy implements FileStrategy {
31: private File m_baseFile;
32: private File m_currentFile;
33:
34: private FastDateFormat m_formatter;
35:
36: private String m_suffix;
37:
38: public UniqueFileStrategy(final File baseFile) {
39: m_baseFile = baseFile;
40: }
41:
42: public UniqueFileStrategy(final File baseFile, String pattern) {
43: this (baseFile);
44: m_formatter = FastDateFormat.getInstance(pattern);
45: }
46:
47: public UniqueFileStrategy(final File baseFile, String pattern,
48: String suffix) {
49: this (baseFile, pattern);
50: m_suffix = suffix;
51: }
52:
53: public File currentFile() {
54: return m_currentFile;
55: }
56:
57: /**
58: * Calculate the real file name from the base filename.
59: *
60: * @return File the calculated file name
61: */
62: public File nextFile() {
63: final StringBuilder sb = new StringBuilder();
64: sb.append(m_baseFile);
65: if (m_formatter == null) {
66: sb.append(System.currentTimeMillis());
67: } else {
68: final String dateString = m_formatter.format(new Date());
69: sb.append(dateString);
70: }
71:
72: if (m_suffix != null) {
73: sb.append(m_suffix);
74: }
75:
76: m_currentFile = new File(sb.toString());
77: return m_currentFile;
78: }
79: }
|