KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejbca > appserver > jboss > RollingCalendar


1 package org.ejbca.appserver.jboss;
2
3 import java.util.Calendar JavaDoc;
4 import java.util.Date JavaDoc;
5 import java.util.GregorianCalendar JavaDoc;
6 import java.util.Locale JavaDoc;
7 import java.util.TimeZone JavaDoc;
8
9 /**
10  * RollingCalendar is a helper class to DailyRollingFileAppender.
11  * Given a periodicity type and the current time, it computes the
12  * start of the next interval.
13  * */

14 public class RollingCalendar extends GregorianCalendar JavaDoc {
15
16     // The code assumes that the following constants are in a increasing
17
// sequence.
18
static public final int TOP_OF_TROUBLE=-1;
19     static public final int TOP_OF_MINUTE = 0;
20     static public final int TOP_OF_HOUR = 1;
21     static public final int HALF_DAY = 2;
22     static public final int TOP_OF_DAY = 3;
23     static public final int TOP_OF_WEEK = 4;
24     static public final int TOP_OF_MONTH = 5;
25
26
27     int type = ScriptrunningDailyRollingFileAppender.TOP_OF_TROUBLE;
28
29     RollingCalendar() {
30         super();
31     }
32
33     RollingCalendar(TimeZone JavaDoc tz, Locale JavaDoc locale) {
34         super(tz, locale);
35     }
36
37     void setType(int type) {
38         this.type = type;
39     }
40
41     public long getNextCheckMillis(Date JavaDoc now) {
42         return getNextCheckDate(now).getTime();
43     }
44
45     public Date JavaDoc getNextCheckDate(Date JavaDoc now) {
46         this.setTime(now);
47
48         switch(type) {
49         case ScriptrunningDailyRollingFileAppender.TOP_OF_MINUTE:
50             this.set(Calendar.SECOND, 0);
51             this.set(Calendar.MILLISECOND, 0);
52             this.add(Calendar.MINUTE, 1);
53             break;
54         case ScriptrunningDailyRollingFileAppender.TOP_OF_HOUR:
55             this.set(Calendar.MINUTE, 0);
56             this.set(Calendar.SECOND, 0);
57             this.set(Calendar.MILLISECOND, 0);
58             this.add(Calendar.HOUR_OF_DAY, 1);
59             break;
60         case ScriptrunningDailyRollingFileAppender.HALF_DAY:
61             this.set(Calendar.MINUTE, 0);
62             this.set(Calendar.SECOND, 0);
63             this.set(Calendar.MILLISECOND, 0);
64             int hour = get(Calendar.HOUR_OF_DAY);
65             if(hour < 12) {
66                 this.set(Calendar.HOUR_OF_DAY, 12);
67             } else {
68                 this.set(Calendar.HOUR_OF_DAY, 0);
69                 this.add(Calendar.DAY_OF_MONTH, 1);
70             }
71             break;
72         case ScriptrunningDailyRollingFileAppender.TOP_OF_DAY:
73             this.set(Calendar.HOUR_OF_DAY, 0);
74             this.set(Calendar.MINUTE, 0);
75             this.set(Calendar.SECOND, 0);
76             this.set(Calendar.MILLISECOND, 0);
77             this.add(Calendar.DATE, 1);
78             break;
79         case ScriptrunningDailyRollingFileAppender.TOP_OF_WEEK:
80             this.set(Calendar.DAY_OF_WEEK, getFirstDayOfWeek());
81             this.set(Calendar.HOUR_OF_DAY, 0);
82             this.set(Calendar.SECOND, 0);
83             this.set(Calendar.MILLISECOND, 0);
84             this.add(Calendar.WEEK_OF_YEAR, 1);
85             break;
86         case ScriptrunningDailyRollingFileAppender.TOP_OF_MONTH:
87             this.set(Calendar.DATE, 1);
88             this.set(Calendar.HOUR_OF_DAY, 0);
89             this.set(Calendar.SECOND, 0);
90             this.set(Calendar.MILLISECOND, 0);
91             this.add(Calendar.MONTH, 1);
92             break;
93         default:
94             throw new IllegalStateException JavaDoc("Unknown periodicity type.");
95         }
96         return getTime();
97     }
98 }
99
Popular Tags