77import hudson .scheduler .CronTab ;
88import hudson .scheduler .Hash ;
99
10- import java .lang .reflect .Method ;
1110import java .util .Calendar ;
12- import java .util .HashMap ;
1311import java .util .Map ;
1412
15- import com .google .common .collect .Maps ;
16-
1713import antlr .ANTLRException ;
1814
15+ import com .google .common .collect .Maps ;
16+
1917/**
2018 * this is a copy of {@link CronTab} with added parameters map support
2119 *
2422public class ParameterizedCronTab {
2523
2624 private final Map <String , String > parameterValues ;
27- // private final CronTab cronTab ;
28- private long [] bits ;
29- private Integer dayOfWeek ;
25+ private final long [] bits ;
26+ private final Integer dayOfWeek ;
27+ private final CronTab cronTab ;
3028
29+ /**
30+ * look at this fragile happy-crappy! Is this better than duplicating all of the Crontab source-tree? you decide.
31+ *
32+ * @param cronTab the crontab to use as a template
33+ * @param parameters the parameters in name=value key pairings
34+ */
3135 public ParameterizedCronTab (CronTab cronTab , Map <String , String > parameters ) {
32- // this.cronTab = cronTab;
33- bits = (long [])FieldAccessor .access (cronTab , "bits" );
34- dayOfWeek = (Integer )FieldAccessor .access (cronTab , "dayOfWeek" );
35-
36+ this .cronTab = cronTab ;
37+ FieldAccessor fieldAccessor = new FieldAccessor ();
38+ bits = fieldAccessor .access (cronTab , "bits" );
39+ dayOfWeek = fieldAccessor .access (cronTab , "dayOfWeek" );
40+
3641 parameterValues = parameters ;
3742 }
3843
@@ -55,54 +60,29 @@ public Map<String, String> getParameterValues() {
5560 return parameterValues ;
5661 }
5762
58- /**
59- * Returns true if n-th bit is on.
60- */
61- private boolean checkBits (long bitMask , int n ) {
62- return (bitMask | (1L << n ))== bitMask ;
63- }
63+ /**
64+ * Returns true if n-th bit is on.
65+ */
66+ private boolean checkBits (long bitMask , int n ) {
67+ return (bitMask | (1L << n )) == bitMask ;
68+ }
6469
6570 public boolean check (Calendar calendar ) {
66- if (!checkBits (bits [0 ],calendar .get (MINUTE )))
67- return false ;
68- if (!checkBits (bits [1 ],calendar .get (HOUR_OF_DAY )))
69- return false ;
70- if (!checkBits (bits [2 ],calendar .get (DAY_OF_MONTH )))
71- return false ;
72- if (!checkBits (bits [3 ],calendar .get (MONTH )+1 ))
73- return false ;
74- if (!checkBits (dayOfWeek ,calendar .get (Calendar .DAY_OF_WEEK )-1 ))
75- return false ;
76-
77- return true ;
71+ if (!checkBits (bits [0 ], calendar .get (MINUTE )))
72+ return false ;
73+ if (!checkBits (bits [1 ], calendar .get (HOUR_OF_DAY )))
74+ return false ;
75+ if (!checkBits (bits [2 ], calendar .get (DAY_OF_MONTH )))
76+ return false ;
77+ if (!checkBits (bits [3 ], calendar .get (MONTH ) + 1 ))
78+ return false ;
79+ if (!checkBits (dayOfWeek , calendar .get (Calendar .DAY_OF_WEEK ) - 1 ))
80+ return false ;
7881
79- // Object result;
80- // try {
81- // Method method = CronTab.class.getMethod("check", Calendar.class);
82- // result = method.invoke(cronTab, calendar);
83- // } catch (Exception e) {
84- // e.printStackTrace();
85- // return false;
86- // }
87- // return (Boolean) result;
88- // return cronTab.check(calendar);
82+ return true ;
8983 }
9084
9185 public String checkSanity () {
92- // for( int i=0; i<5; i++ ) {
93- // long bitMask = (i<4)?bits[i]:(long)dayOfWeek;
94- // for( int j=BaseParser.LOWER_BOUNDS[i]; j<=BaseParser.UPPER_BOUNDS[i]; j++ ) {
95- // if(!checkBits(bitMask,j)) {
96- // // this rank has a sparse entry.
97- // // if we have a sparse rank, one of them better be the left-most.
98- // if(i>0)
99- // return Messages.CronTab_do_you_really_mean_every_minute_when_you(spec, "0 " + spec.substring(spec.indexOf(' ')+1));
100- // // once we find a sparse rank, upper ranks don't matter
101- // return null;
102- // }
103- // }
104- // }
105-
106- return null ;
86+ return cronTab .checkSanity ();
10787 }
10888}
0 commit comments