1 17 18 package org.sape.carbon.services.scheduler; 19 20 import java.util.Calendar ; 21 import java.util.Date ; 22 import java.util.GregorianCalendar ; 23 import java.util.Timer ; 24 25 import org.sape.carbon.core.config.InvalidConfigurationException; 26 import org.sape.carbon.core.util.calendar.DayOfWeekEnum; 27 import org.sape.carbon.core.util.calendar.MonthEnum; 28 29 import org.sape.carbon.services.threadpool.ThreadPool; 30 31 import org.apache.commons.logging.Log; 32 import org.apache.commons.logging.LogFactory; 33 34 43 class FixedRateTask extends AbstractTask { 44 45 48 private Log log = LogFactory.getLog(this.getClass()); 49 50 52 private static final int MIN_MINUTE = 0; 53 54 private static final int MAX_MINUTE = 59; 55 56 57 private static final int MIN_HOUR = 0; 58 59 private static final int MAX_HOUR = 23; 60 61 62 private static final int MIN_DAY_OF_MONTH = 1; 63 64 private static final int MAX_DAY_OF_MONTH = 31; 65 66 68 private static final long EVERY_MINUTE = 1000 * 60; 69 70 private static final long EVERY_HOUR = EVERY_MINUTE * 60; 71 72 private static final long EVERY_DAY = EVERY_HOUR * 24; 73 74 private static final long EVERY_WEEK = EVERY_DAY * 7; 75 76 private static final long EVERY_MONTH = EVERY_DAY * 30; 77 78 private static final long EVERY_YEAR = EVERY_DAY * 365; 79 80 81 private long period; 82 83 84 private Date time; 85 86 95 protected FixedRateTask( 96 FixedRateTaskConfiguration taskConfiguration, 97 ThreadPool threadPool) { 98 99 super(taskConfiguration, threadPool); 100 101 Integer minute = taskConfiguration.getMinute(); 102 Integer hour = taskConfiguration.getHour(); 103 Integer dayOfMonth = taskConfiguration.getDayOfMonth(); 104 MonthEnum month = taskConfiguration.getMonth(); 105 DayOfWeekEnum dayOfWeek = taskConfiguration.getDayOfWeek(); 106 107 validate( 109 taskConfiguration.getConfigurationName(), 110 "Minute", 111 minute, 112 MIN_MINUTE, 113 MAX_MINUTE); 114 validate( 115 taskConfiguration.getConfigurationName(), 116 "Hour", 117 hour, 118 MIN_HOUR, 119 MAX_HOUR); 120 validate( 121 taskConfiguration.getConfigurationName(), 122 "DayOfMonth", 123 dayOfMonth, 124 MIN_DAY_OF_MONTH, 125 MAX_DAY_OF_MONTH); 126 127 Calendar firstRunTime = new GregorianCalendar (); 128 firstRunTime.set(Calendar.MILLISECOND, 0); 129 firstRunTime.set(Calendar.SECOND, 0); 130 131 if (minute == null) { 134 136 if (!(hour == null && dayOfMonth == null 138 && month == null && dayOfWeek == null)) { 139 140 throw new InvalidConfigurationException( 141 this.getClass(), 142 taskConfiguration.getConfigurationName(), 143 "Minute", 144 "Minute was not specifed, but other configuration " 145 + "information exists. Either specify Minute " 146 + "or remove all other configuration information"); 147 } 148 149 this.period = EVERY_MINUTE; 151 152 firstRunTime.add(Calendar.MINUTE, 1); 154 155 } else if (hour == null) { 156 159 if (!(dayOfMonth == null && month == null && dayOfWeek == null)) { 161 throw new InvalidConfigurationException( 162 this.getClass(), 163 taskConfiguration.getConfigurationName(), 164 "Hour", 165 "Hour was not specifed, but day and/or month " 166 + "configuration information exists. Either " 167 + "specify Hour or remove day and month " 168 + "configuration information"); 169 } 170 171 this.period = EVERY_HOUR; 173 174 firstRunTime.set(Calendar.MINUTE, minute.intValue()); 175 176 Date now = new Date (); 177 if (firstRunTime.getTime().compareTo(now) <= 0) { 178 firstRunTime.add(Calendar.HOUR_OF_DAY, 1); 179 } 180 181 } else if (dayOfMonth == null && dayOfWeek == null) { 182 185 if (month != null) { 187 throw new InvalidConfigurationException( 188 this.getClass(), 189 taskConfiguration.getConfigurationName(), 190 "Month", 191 "Month was supplied without DayOfMonth, either remove " 192 + "Month or add DayOfMonth"); 193 } 194 195 this.period = EVERY_DAY; 196 197 firstRunTime.set(Calendar.MINUTE, minute.intValue()); 198 firstRunTime.set(Calendar.HOUR_OF_DAY, hour.intValue()); 199 200 Date now = new Date (); 201 if (firstRunTime.getTime().compareTo(now) <= 0) { 202 firstRunTime.add(Calendar.DAY_OF_MONTH, 1); 205 } 206 207 } else if (dayOfMonth != null) { 208 if (dayOfWeek != null) { 210 throw new InvalidConfigurationException( 211 this.getClass(), 212 taskConfiguration.getConfigurationName(), 213 "DayOfWeek", 214 "Both DayOfWeek and DayOfMonth were specified, only one " 215 + "can be used"); 216 } 217 218 if (month == null) { 219 222 this.period = EVERY_MONTH; 223 224 firstRunTime.set(Calendar.MINUTE, minute.intValue()); 225 firstRunTime.set(Calendar.HOUR_OF_DAY, hour.intValue()); 226 firstRunTime.set(Calendar.DAY_OF_MONTH, dayOfMonth.intValue()); 227 228 Date now = new Date (); 229 if (firstRunTime.getTime().compareTo(now) <= 0) { 230 firstRunTime.add(Calendar.MONTH, 1); 231 } 232 233 } else { 234 237 this.period = EVERY_YEAR; 238 239 firstRunTime.set(Calendar.MINUTE, minute.intValue()); 240 firstRunTime.set(Calendar.HOUR_OF_DAY, hour.intValue()); 241 firstRunTime.set(Calendar.DAY_OF_MONTH, dayOfMonth.intValue()); 242 firstRunTime.set(Calendar.MONTH, month.getOrdinal()); 243 244 Date now = new Date (); 245 if (firstRunTime.getTime().compareTo(now) <= 0) { 246 firstRunTime.add(Calendar.YEAR, 1); 247 } 248 } 249 250 } else if (dayOfWeek != null) { 251 254 if (month != null) { 256 throw new InvalidConfigurationException( 257 this.getClass(), 258 taskConfiguration.getConfigurationName(), 259 "Month", 260 "Month cannot be used with DayOfWeek, " 261 + "it can only be used in conjunction with " 262 + "DayOfMonth"); 263 } 264 265 this.period = EVERY_WEEK; 266 267 firstRunTime.set(Calendar.MINUTE, minute.intValue()); 268 firstRunTime.set(Calendar.HOUR_OF_DAY, hour.intValue()); 269 firstRunTime.set(Calendar.DAY_OF_WEEK, dayOfWeek.getOrdinal()); 270 271 Date now = new Date (); 272 if (firstRunTime.getTime().compareTo(now) <= 0) { 273 firstRunTime.add(Calendar.WEEK_OF_MONTH, 1); 274 } 275 276 } else { 277 throw new InvalidConfigurationException( 279 this.getClass(), 280 taskConfiguration.getConfigurationName(), 281 "all attributes", 282 "Could not determine first run time or repeat period"); 283 } 284 285 this.time = firstRunTime.getTime(); 286 287 if (log.isTraceEnabled()) { 288 log.trace("Creating task defined by configuration [" 289 + taskConfiguration.getConfigurationName() 290 + "] to run starting at [" 291 + this.time 292 + "] with period [" 293 + this.period 294 + "] ms"); 295 } 296 297 } 298 299 302 public void schedule(Timer timer) { 303 timer.scheduleAtFixedRate(getTask(), this.time, this.period); 304 } 305 306 315 private void validate(String configName, 316 String attributeName, 317 Integer value, 318 int minValue, 319 int maxValue) { 320 321 if (value != null 322 && !isWithinRange(value.intValue(), minValue, maxValue)) { 323 324 throw new InvalidConfigurationException( 325 this.getClass(), 326 configName, 327 attributeName, 328 "Value [" 329 + value 330 + "] out of range: min [" 331 + minValue 332 + "], max [" 333 + maxValue 334 + "] inclusive"); 335 } 336 } 337 338 346 private boolean isWithinRange(int value, int min, int max) { 347 if (value < min || value > max) { 348 return false; 349 } else { 350 return true; 351 } 352 } 353 } 354 | Popular Tags |