|
| 1 | +package org.jenkinsci.plugins.parameterizedschedular; |
| 2 | + |
| 3 | +import static java.util.Calendar.DAY_OF_MONTH; |
| 4 | +import static java.util.Calendar.HOUR_OF_DAY; |
| 5 | +import static java.util.Calendar.MINUTE; |
| 6 | +import static java.util.Calendar.MONTH; |
| 7 | +import hudson.scheduler.CronTab; |
| 8 | +import hudson.scheduler.Hash; |
| 9 | + |
| 10 | +import java.lang.reflect.Method; |
| 11 | +import java.util.Calendar; |
| 12 | +import java.util.HashMap; |
| 13 | +import java.util.Map; |
| 14 | + |
| 15 | +import com.google.common.collect.Maps; |
| 16 | + |
| 17 | +import antlr.ANTLRException; |
| 18 | + |
| 19 | +/** |
| 20 | + * this is a copy of {@link CronTab} with added parameters map support |
| 21 | + * |
| 22 | + * @author jameswilson |
| 23 | + */ |
| 24 | +public class ParameterizedCronTab { |
| 25 | + |
| 26 | + private final Map<String, String> parameterValues; |
| 27 | +// private final CronTab cronTab; |
| 28 | + private long[] bits; |
| 29 | + private Integer dayOfWeek; |
| 30 | + |
| 31 | + 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 | + parameterValues = parameters; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * @param hash |
| 41 | + * Used to spread out token like "@daily". Null to preserve the legacy behaviour |
| 42 | + * of not spreading it out at all. |
| 43 | + */ |
| 44 | + public static ParameterizedCronTab create(String line, int lineNumber, Hash hash) throws ANTLRException { |
| 45 | + String[] lineParts = line.split("%"); |
| 46 | + CronTab cronTab = new CronTab(lineParts[0].trim(), lineNumber, hash); |
| 47 | + Map<String, String> parameters = Maps.newHashMap(); |
| 48 | + if (lineParts.length == 2) { |
| 49 | + parameters = new ParameterParser().parse(lineParts[1]); |
| 50 | + } |
| 51 | + return new ParameterizedCronTab(cronTab, parameters); |
| 52 | + } |
| 53 | + |
| 54 | + public Map<String, String> getParameterValues() { |
| 55 | + return parameterValues; |
| 56 | + } |
| 57 | + |
| 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 | + } |
| 64 | + |
| 65 | + 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; |
| 78 | + |
| 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); |
| 89 | + } |
| 90 | + |
| 91 | + 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; |
| 107 | + } |
| 108 | +} |
0 commit comments