KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.jivesoftware.util.log.format.Formatter;
11 import org.jivesoftware.util.log.output.io.FileTarget;
12 import java.io.File JavaDoc;
13 import java.io.IOException JavaDoc;
14
15 /**
16  * This is a basic Output log target that writes to rotating files.
17  *
18  * @author <a HREF="mailto:peter@apache.org">Peter Donald</a>
19  * @author <a HREF="mailto:mcconnell@osm.net">Stephen McConnell</a>
20  * @author <a HREF="mailto:bh22351@i-one.at">Bernhard Huber</a>
21  */

22 public class RotatingFileTarget extends FileTarget {
23
24     ///The rotation strategy to be used.
25
private RotateStrategy m_rotateStrategy;
26
27     ///The file strategy to be used.
28
private FileStrategy m_fileStrategy;
29
30     /**
31      * Construct RotatingFileTarget object.
32      *
33      * @param formatter Formatter to be used
34      */

35     public RotatingFileTarget(final Formatter formatter,
36                               final RotateStrategy rotateStrategy,
37                               final FileStrategy fileStrategy)
38             throws IOException JavaDoc {
39         super(null, false, formatter);
40
41         m_rotateStrategy = rotateStrategy;
42         m_fileStrategy = fileStrategy;
43
44         getInitialFile();
45     }
46
47     public synchronized void rotate()
48             throws IOException JavaDoc {
49         close();
50
51         final File JavaDoc file = m_fileStrategy.nextFile();
52         setFile(file, false);
53         openFile();
54     }
55
56     /**
57      * Output the log message, and check if rotation is needed.
58      */

59     public synchronized void write(final String JavaDoc data) {
60         // send the log message
61
super.write(data);
62
63         // if rotation is needed, close old File, create new File
64
final boolean rotate =
65                 m_rotateStrategy.isRotationNeeded(data, getFile());
66         if (rotate) {
67             try {
68                 rotate();
69             }
70             catch (final IOException JavaDoc ioe) {
71                 getErrorHandler().error("Error rotating file", ioe, null);
72             }
73         }
74     }
75
76     private void getInitialFile() throws IOException JavaDoc {
77         close();
78
79         boolean rotate = m_rotateStrategy.isRotationNeeded("", m_fileStrategy.currentFile());
80
81         if (rotate) {
82             setFile(m_fileStrategy.nextFile(), false);
83         }
84         else {
85             setFile(m_fileStrategy.currentFile(), true);
86         }
87
88         openFile();
89     }
90 }
Popular Tags