KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > grlea > log > rollover > TimeOfDayRolloverStrategy


1 package org.grlea.log.rollover;
2
3 // $Id: TimeOfDayRolloverStrategy.java,v 1.4 2006/07/13 12:39:15 grlea Exp $
4
// Copyright (c) 2004-2006 Graham Lea. All rights reserved.
5

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

18 import java.io.IOException JavaDoc;
19 import java.util.Calendar JavaDoc;
20 import java.util.Date JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.TimeZone JavaDoc;
23 import java.util.regex.Matcher JavaDoc;
24 import java.util.regex.Pattern JavaDoc;
25
26 /**
27  * <p>A {@link RolloverStrategy} that requests rollover at a specific time each day.
28  * Note that, because the strategy is only consulted at intervals, log files will usually contain
29  * records that are slightly past the rollover time (depending on the rollover interval).</p>
30  *
31  * @author Graham Lea
32  * @version $Revision: 1.4 $
33  */

34 class
35 TimeOfDayRolloverStrategy
36 implements RolloverStrategy
37 {
38    /** The key of the property specifying the time of day at which to roll over. */
39    private static final String JavaDoc KEY_ROLLOVER_TIME = "simplelog.rollover.timeOfDay.time";
40
41    /** The default value for the rollover time. */
42    private static final String JavaDoc ROLLOVER_TIME_DEFAULT = "0:00";
43
44    /** The key of the property specifying the timezone in which the rollover time is specified. */
45    private static final String JavaDoc KEY_TIMEZONE = "simplelog.rollover.timeOfDay.timezone";
46
47    /**
48     * The time zone in which the time is being specified
49     */

50    private TimeZone JavaDoc timeZone;
51
52    /**
53     * The hour of the day at which to roll over (in 24 hour time, where 0 = midnight)
54     */

55    private int hour;
56
57    /**
58     * The minute of the hour to roll over
59     */

60    private int minute;
61
62    /**
63     * Whether the current rollover time was set programatically, rather than by calling configure.
64     */

65    private boolean timeSetProgramatically = false;
66
67    /**
68     * <p>Creates a new <code>SetTimeRolloverStrategy</code>.</p>
69     *
70     * <p>This constructor is only intended to be used by {@link RolloverManager}.</p>
71     */

72    TimeOfDayRolloverStrategy()
73    {
74       timeZone = null;
75       hour = -1;
76       minute = -1;
77    }
78
79    /**
80     * Creates a new <code>SetTimeRolloverStrategy</code> that will request log files be rolled at
81     * the specified time.
82     *
83     * @param timeZone the time zone in which the time is being specified
84     * @param hour the hour of the day at which to roll over (in 24 hour time, where 0 = midnight)
85     * @param minute the minute of the hour to roll over
86     */

87    public
88    TimeOfDayRolloverStrategy(TimeZone JavaDoc timeZone, int hour, int minute)
89    {
90       setRolloverTime(timeZone, hour, minute);
91    }
92
93    public void
94    configure(Map JavaDoc properties)
95    throws IOException JavaDoc
96    {
97       if (timeSetProgramatically)
98          return;
99       
100       String JavaDoc rolloverTime = (String JavaDoc) properties.get(KEY_ROLLOVER_TIME);
101       if (rolloverTime == null)
102          rolloverTime = ROLLOVER_TIME_DEFAULT;
103       rolloverTime = rolloverTime.trim();
104       if (rolloverTime.length() == 0)
105          rolloverTime = ROLLOVER_TIME_DEFAULT;
106
107       // Nice pattern this, huh? It handles 0:00 (or 00:00) through to 23:59, and nothing else.
108
Pattern JavaDoc pattern = Pattern.compile("([01]?\\d|2[0123]):([0-5]\\d)");
109       Matcher JavaDoc matcher = pattern.matcher(rolloverTime);
110       if (!matcher.matches())
111       {
112          throw new IOException JavaDoc(
113             "The specified rollover time does not conform to the required pattern: " + rolloverTime);
114       }
115
116       String JavaDoc hourString = matcher.group(1);
117       String JavaDoc minuteString = matcher.group(2);
118
119       int hour = Integer.parseInt(hourString);
120       int minute = Integer.parseInt(minuteString);
121
122       String JavaDoc timezoneString = (String JavaDoc) properties.get(KEY_TIMEZONE);
123       TimeZone JavaDoc timeZone;
124       if (timezoneString != null)
125       {
126          timeZone = TimeZone.getTimeZone(timezoneString);
127          if (timeZone.getID().equals("GMT") && !timezoneString.equals("GMT"))
128             throw new IOException JavaDoc("A TimeZone with the specified ID could not be created.");
129       }
130       else
131       {
132          timeZone = TimeZone.getDefault();
133       }
134
135       setRolloverTimeInternal(timeZone, hour, minute);
136    }
137
138    /**
139     * Sets the time at which a rollover should be requested. Once the time has been set by this
140     * method, it will not be changed by calls to {@link #configure}.
141     *
142     * @param timeZone the time zone in which the time is being specified
143     * @param hour the hour of the day at which to roll over (in 24 hour time, where 0 = midnight)
144     * @param minute the minute of the hour to roll over
145     */

146    public void
147    setRolloverTime(TimeZone JavaDoc timeZone, int hour, int minute)
148    {
149       setRolloverTimeInternal(timeZone, hour, minute);
150       this.timeSetProgramatically = true;
151    }
152
153    /**
154     * Sets the time at which a rollover should be requested. This method does not set
155     * {@link #timeSetProgramatically}.
156     *
157     * @param timeZone the time zone in which the time is being specified
158     * @param hour the hour of the day at which to roll over (in 24 hour time, where 0 = midnight)
159     * @param minute the minute of the hour to roll over
160     */

161    private void
162    setRolloverTimeInternal(TimeZone JavaDoc timeZone, int hour, int minute)
163    {
164       if (timeZone == null)
165       {
166          throw new IllegalArgumentException JavaDoc("timeZone cannot be null.");
167       }
168
169       if (hour < 0 || hour > 23)
170       {
171          throw new IllegalArgumentException JavaDoc("hour must be between 0 and 23, inclusive.");
172       }
173
174       if (minute < 0 || minute > 59)
175       {
176          throw new IllegalArgumentException JavaDoc("minute must be between 0 and 59, inclusive.");
177       }
178
179       this.timeZone = timeZone;
180       this.hour = hour;
181       this.minute = minute;
182    }
183
184    public boolean
185    rolloverNow(Date JavaDoc fileCreated, long fileLength)
186    {
187       if (timeZone == null)
188       {
189          throw new IllegalStateException JavaDoc("TimeOfDayRolloverStrategy has not been configured.");
190       }
191
192       // Calculate the next rollover time
193
Calendar JavaDoc rolloverCalendar = Calendar.getInstance(timeZone);
194       rolloverCalendar.setTime(fileCreated);
195       rolloverCalendar.set(Calendar.HOUR_OF_DAY, hour);
196       rolloverCalendar.set(Calendar.MINUTE, minute);
197       rolloverCalendar.set(Calendar.SECOND, 0);
198       rolloverCalendar.set(Calendar.MILLISECOND, 0);
199       long nextRolloverTime = rolloverCalendar.getTime().getTime();
200
201       while (nextRolloverTime <= fileCreated.getTime())
202       {
203          int day = rolloverCalendar.get(Calendar.DAY_OF_MONTH);
204          rolloverCalendar.set(Calendar.DAY_OF_MONTH, day + 1);
205          nextRolloverTime = rolloverCalendar.getTime().getTime();
206       }
207
208 // System.out.println("nextRolloverTime = " +
209
// new SimpleDateFormat("dd/MM HH:mm").format(new Date(nextRolloverTime)));
210

211       // If now < rolloverTime, not ready to rollover yet
212
long now = System.currentTimeMillis();
213       if (now < nextRolloverTime)
214       {
215          return false;
216       }
217       else
218       {
219          return true;
220       }
221    }
222 }
Popular Tags