1 package org.grlea.log.rollover; 2 3 6 18 import java.io.IOException ; 19 import java.util.Calendar ; 20 import java.util.Date ; 21 import java.util.Map ; 22 import java.util.TimeZone ; 23 import java.util.regex.Matcher ; 24 import java.util.regex.Pattern ; 25 26 34 class 35 TimeOfDayRolloverStrategy 36 implements RolloverStrategy 37 { 38 39 private static final String KEY_ROLLOVER_TIME = "simplelog.rollover.timeOfDay.time"; 40 41 42 private static final String ROLLOVER_TIME_DEFAULT = "0:00"; 43 44 45 private static final String KEY_TIMEZONE = "simplelog.rollover.timeOfDay.timezone"; 46 47 50 private TimeZone timeZone; 51 52 55 private int hour; 56 57 60 private int minute; 61 62 65 private boolean timeSetProgramatically = false; 66 67 72 TimeOfDayRolloverStrategy() 73 { 74 timeZone = null; 75 hour = -1; 76 minute = -1; 77 } 78 79 87 public 88 TimeOfDayRolloverStrategy(TimeZone timeZone, int hour, int minute) 89 { 90 setRolloverTime(timeZone, hour, minute); 91 } 92 93 public void 94 configure(Map properties) 95 throws IOException 96 { 97 if (timeSetProgramatically) 98 return; 99 100 String rolloverTime = (String ) 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 Pattern pattern = Pattern.compile("([01]?\\d|2[0123]):([0-5]\\d)"); 109 Matcher matcher = pattern.matcher(rolloverTime); 110 if (!matcher.matches()) 111 { 112 throw new IOException ( 113 "The specified rollover time does not conform to the required pattern: " + rolloverTime); 114 } 115 116 String hourString = matcher.group(1); 117 String minuteString = matcher.group(2); 118 119 int hour = Integer.parseInt(hourString); 120 int minute = Integer.parseInt(minuteString); 121 122 String timezoneString = (String ) properties.get(KEY_TIMEZONE); 123 TimeZone timeZone; 124 if (timezoneString != null) 125 { 126 timeZone = TimeZone.getTimeZone(timezoneString); 127 if (timeZone.getID().equals("GMT") && !timezoneString.equals("GMT")) 128 throw new IOException ("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 146 public void 147 setRolloverTime(TimeZone timeZone, int hour, int minute) 148 { 149 setRolloverTimeInternal(timeZone, hour, minute); 150 this.timeSetProgramatically = true; 151 } 152 153 161 private void 162 setRolloverTimeInternal(TimeZone timeZone, int hour, int minute) 163 { 164 if (timeZone == null) 165 { 166 throw new IllegalArgumentException ("timeZone cannot be null."); 167 } 168 169 if (hour < 0 || hour > 23) 170 { 171 throw new IllegalArgumentException ("hour must be between 0 and 23, inclusive."); 172 } 173 174 if (minute < 0 || minute > 59) 175 { 176 throw new IllegalArgumentException ("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 fileCreated, long fileLength) 186 { 187 if (timeZone == null) 188 { 189 throw new IllegalStateException ("TimeOfDayRolloverStrategy has not been configured."); 190 } 191 192 Calendar 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 211 long now = System.currentTimeMillis(); 213 if (now < nextRolloverTime) 214 { 215 return false; 216 } 217 else 218 { 219 return true; 220 } 221 } 222 } | Popular Tags |