KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Calendar JavaDoc;
21 import java.util.GregorianCalendar JavaDoc;
22
23 /**
24  * Rotation stragety based on a specific time of day.
25  *
26  * @author <a HREF="mailto:leif@apache.org">Leif Mortenson</a>
27  */

28 public class RotateStrategyByTimeOfDay
29     implements RotateStrategy
30 {
31     /** Constant that stores the the number of ms in 24 hours. */
32     private static final long TIME_24_HOURS = 24 * 3600 * 1000;
33
34     /** Time in ms that the current rotation started. */
35     private long m_currentRotation;
36
37     /**
38      * Rotate logs at specific time of day.
39      * By default do log rotation at 00:00:00 every day.
40      */

41     public RotateStrategyByTimeOfDay()
42     {
43         this( 0 );
44     }
45
46     /**
47      * Rotate logs at specific time of day.
48      *
49      * @param time Offset in milliseconds into the day to perform the log rotation.
50      */

51     public RotateStrategyByTimeOfDay( final long time )
52     {
53         // Calculate the time at the beginning of the current day and add the time to that.
54
final GregorianCalendar JavaDoc cal = new GregorianCalendar JavaDoc();
55         cal.set( Calendar.MILLISECOND, 0 );
56         cal.set( Calendar.SECOND, 0 );
57         cal.set( Calendar.MINUTE, 0 );
58         cal.set( Calendar.HOUR_OF_DAY, 0 );
59         m_currentRotation = cal.getTime().getTime() + time;
60
61         // Make sure that the current rotation time is in the past.
62
if( m_currentRotation > System.currentTimeMillis() )
63         {
64             m_currentRotation -= TIME_24_HOURS;
65         }
66     }
67
68     /**
69      * reset interval history counters.
70      */

71     public void reset()
72     {
73         final long now = System.currentTimeMillis();
74
75         // Make sure the currentRotation time is set so that the current system
76
// time is within 24 hours.
77
while( m_currentRotation + TIME_24_HOURS < now )
78         {
79             m_currentRotation += TIME_24_HOURS;
80         }
81     }
82
83     /**
84      * Check if now a log rotation is neccessary.
85      * If the time of the current rotation + 24 hours is less than the current time.
86      * If not then a rotation is needed.
87      *
88      * @param data the last message written to the log system
89      * @param file not used
90      * @return boolean return true if log rotation is neccessary, else false
91      */

92     public boolean isRotationNeeded( final String JavaDoc data, final File JavaDoc file )
93     {
94         final long now = System.currentTimeMillis();
95         if( m_currentRotation + TIME_24_HOURS < now )
96         {
97             // Needs to be rotated.
98
return true;
99         }
100         else
101         {
102             return false;
103         }
104     }
105 }
106
107
Popular Tags