1 package org.apache.turbine.services.schedule; 2 3 18 19 import java.util.Calendar ; 20 import java.util.Date ; 21 22 import org.apache.commons.lang.StringUtils; 23 24 import org.apache.commons.logging.Log; 25 import org.apache.commons.logging.LogFactory; 26 27 import org.apache.turbine.util.TurbineException; 28 29 38 public class JobEntry 39 extends BaseJobEntry 40 implements Comparable 41 { 42 43 private static Log log = LogFactory.getLog(ScheduleService.LOGGER_NAME); 44 45 46 private boolean jobIsActive = false; 47 48 49 private long runtime = 0; 50 51 52 private static final int SECOND = 0; 53 private static final int MINUTE = 1; 54 private static final int WEEK_DAY = 2; 55 private static final int DAY_OF_MONTH = 3; 56 private static final int DAILY = 4; 57 58 61 public JobEntry() 62 { 63 } 64 65 96 public JobEntry(int sec, 97 int min, 98 int hour, 99 int wd, 100 int day_mo, 101 String task) 102 throws TurbineException 103 { 104 if (StringUtils.isEmpty(task)) 105 { 106 throw new TurbineException("Error in JobEntry. " + 107 "Bad Job parameter. Task not set."); 108 } 109 110 setSecond(sec); 111 setMinute(min); 112 setHour(hour); 113 setWeekDay(wd); 114 setDayOfMonth(day_mo); 115 setTask(task); 116 117 calcRunTime(); 118 } 119 120 128 public int compareTo(Object je) 129 { 130 int result = -1; 131 if (je instanceof JobEntry) 132 { 133 result = getJobId() - ((JobEntry) je).getJobId(); 134 } 135 return result; 136 } 137 138 143 public void setActive(boolean isActive) 144 { 145 jobIsActive = isActive; 146 } 147 148 154 public boolean isActive() 155 { 156 return jobIsActive; 157 } 158 159 164 public long getNextRuntime() 165 { 166 return runtime; 167 } 168 169 174 public Date getNextRunDate() 175 { 176 return new Date (runtime); 177 } 178 179 184 public String getNextRunAsString() 185 { 186 return getNextRunDate().toString(); 187 } 188 189 205 public void calcRunTime() 206 throws TurbineException 207 { 208 Calendar schedrun = Calendar.getInstance(); 209 Calendar now = Calendar.getInstance(); 210 211 switch (evaluateJobType()) 212 { 213 case SECOND: 214 schedrun.add(Calendar.SECOND, getSecond()); 216 runtime = schedrun.getTime().getTime(); 217 break; 218 219 case MINUTE: 220 schedrun.add(Calendar.SECOND, getSecond()); 222 schedrun.add(Calendar.MINUTE, getMinute()); 223 runtime = schedrun.getTime().getTime(); 224 break; 225 226 case WEEK_DAY: 227 schedrun.set(Calendar.SECOND, getSecond()); 229 schedrun.set(Calendar.MINUTE, getMinute()); 230 schedrun.set(Calendar.HOUR_OF_DAY, getHour()); 231 schedrun.set(Calendar.DAY_OF_WEEK, getWeekDay()); 232 233 if (now.before(schedrun)) 234 { 235 runtime = schedrun.getTime().getTime(); 237 } 238 else 239 { 240 schedrun.add(Calendar.DAY_OF_WEEK, 7); 242 runtime = schedrun.getTime().getTime(); 243 } 244 break; 245 246 case DAY_OF_MONTH: 247 schedrun.set(Calendar.SECOND, getSecond()); 249 schedrun.set(Calendar.MINUTE, getMinute()); 250 schedrun.set(Calendar.HOUR_OF_DAY, getHour()); 251 schedrun.set(Calendar.DAY_OF_MONTH, getDayOfMonth()); 252 253 if (now.before(schedrun)) 254 { 255 runtime = schedrun.getTime().getTime(); 257 } 258 else 259 { 260 schedrun.add(Calendar.MONTH, 1); 262 runtime = schedrun.getTime().getTime(); 263 } 264 break; 265 266 case DAILY: 267 schedrun.set(Calendar.SECOND, getSecond()); 269 schedrun.set(Calendar.MINUTE, getMinute()); 270 schedrun.set(Calendar.HOUR_OF_DAY, getHour()); 271 272 if (now.before(schedrun)) 274 { 275 runtime = schedrun.getTime().getTime(); 276 } 277 else 278 { 279 schedrun.add(Calendar.HOUR_OF_DAY, 24); 281 runtime = schedrun.getTime().getTime(); 282 } 283 break; 284 285 default: 286 } 288 289 log.info("Next runtime for task " + this.getTask() + " is " + this.getNextRunDate()); 290 } 291 292 302 private int evaluateJobType() 303 throws TurbineException 304 { 305 306 if (getDayOfMonth() < 0) 308 { 309 if (getWeekDay() < 0) 311 { 312 if (getHour() < 0) 314 { 315 if (getMinute() < 0) 317 { 318 if (getSecond() < 0) 320 throw new TurbineException("Error in JobEntry. Bad Job parameter."); 321 322 return SECOND; 323 } 324 else 325 { 326 if (getMinute() < 0 || getSecond() < 0) 329 throw new TurbineException("Error in JobEntry. Bad Job parameter."); 330 331 return MINUTE; 332 } 333 } 334 else 335 { 336 if (getMinute() < 0 || getHour() < 0 || getSecond() < 0) 339 throw new TurbineException("Error in JobEntry. Bad Job parameter."); 340 341 return DAILY; 342 } 343 } 344 else 345 { 346 if (getMinute() < 0 || getHour() < 0 || getSecond() < 0) 349 throw new TurbineException("Error in JobEntry. Bad Job parameter."); 350 351 return WEEK_DAY; 352 } 353 } 354 else 355 { 356 if (getMinute() < 0 || getHour() < 0) 359 throw new TurbineException("Error in JobEntry. Bad Job parameter."); 360 361 return DAY_OF_MONTH; 362 } 363 } 364 365 } 366 | Popular Tags |