1 package org.oddjob.schedules.schedules; 2 3 import java.io.Serializable ; 4 import java.util.Date ; 5 6 import org.apache.log4j.Logger; 7 import org.oddjob.schedules.AbstractSchedule; 8 import org.oddjob.schedules.Interval; 9 import org.oddjob.schedules.Schedule; 10 import org.oddjob.schedules.ScheduleContext; 11 12 40 41 final public class CountSchedule extends AbstractSchedule 42 implements Serializable { 43 private static final long serialVersionUID = 20050226; 44 45 private static final Logger logger = Logger.getLogger(CountSchedule.class); 46 47 private static final String COUNT_KEY = "countschedulecount"; 48 private static final String LAST_KEY = "countschedulelast"; 49 50 55 private int countTo; 56 57 62 public void setCount(String count) { 63 this.countTo = Integer.parseInt(count); 64 } 65 66 71 public String getCount() { 72 return Integer.toString(countTo); 73 } 74 75 class ImmediateSchedule implements Schedule, Serializable { 76 private static final long serialVersionUID = 20060113; 77 public Interval nextDue(ScheduleContext context) { 78 Date date = context.getDate(); 79 return new Interval(date, date); 80 } 81 public void setLimits(Interval limits) { 82 } 83 } 84 85 89 public Interval nextDue(ScheduleContext context) { 90 Date now = context.getDate(); 91 if (now == null) { 92 return null; 93 } 94 if (getChildSchedule() == null) { 95 setChildSchedule(new ImmediateSchedule()); 96 } 97 98 int counted = 0; 99 Integer storedCount = (Integer ) context.getData(COUNT_KEY); 100 if (storedCount != null) { 101 counted = storedCount.intValue(); 102 } 103 104 logger.debug(this + ": in date is " + now + ", count is " + counted); 105 106 Date last = (Date ) context.getData(LAST_KEY); 107 108 if (!now.equals(last)) { 109 ++counted; 110 last = now; 111 } 112 113 context.putData(COUNT_KEY, new Integer (counted)); 114 context.putData(LAST_KEY, last); 115 116 if ( counted <= countTo) { 117 Schedule child = getChildSchedule(); 118 child.setLimits(getLimits()); 119 return child.nextDue(context); 120 } 121 else { 122 return null; 123 } 124 } 125 126 129 130 public String toString() { 131 return "Count Schedule, count to " + countTo; 132 } 133 } 134 | Popular Tags |