KickJava   Java API By Example, From Geeks To Geeks.

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


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  * rotation stragety based when log writting started.
14  *
15  * @author <a HREF="mailto:bh22351@i-one.at">Bernhard Huber</a>
16  */

17 public class RotateStrategyByTime
18         implements RotateStrategy {
19     ///time interval when rotation is triggered.
20
private long m_timeInterval;
21
22     ///time when logging started.
23
private long m_startingTime;
24
25     ///rotation count.
26
private long m_currentRotation;
27
28     /**
29      * Rotate logs by time.
30      * By default do log rotation every 24 hours
31      */

32     public RotateStrategyByTime() {
33         this(1000 * 60 * 60 * 24);
34     }
35
36     /**
37      * Rotate logs by time.
38      *
39      * @param timeInterval rotate after time-interval [ms] has expired
40      */

41     public RotateStrategyByTime(final long timeInterval) {
42         m_startingTime = System.currentTimeMillis();
43         m_currentRotation = 0;
44         m_timeInterval = timeInterval;
45     }
46
47     /**
48      * reset interval history counters.
49      */

50     public void reset() {
51         m_startingTime = System.currentTimeMillis();
52         m_currentRotation = 0;
53     }
54
55     /**
56      * Check if now a log rotation is neccessary.
57      * If
58      * <code>(current_time - m_startingTime) / m_timeInterval &gt; m_currentRotation </code>
59      * rotation is needed.
60      *
61      * @param data the last message written to the log system
62      * @return boolean return true if log rotation is neccessary, else false
63      */

64     public boolean isRotationNeeded(final String JavaDoc data, final File JavaDoc file) {
65         final long newRotation =
66                 (System.currentTimeMillis() - m_startingTime) / m_timeInterval;
67
68         if (newRotation > m_currentRotation) {
69             m_currentRotation = newRotation;
70             return true;
71         }
72         else {
73             return false;
74         }
75     }
76 }
77
78
79
Popular Tags