Skip to content

Commit

Permalink
Fixed JENKINS-12676.
Browse files Browse the repository at this point in the history
Get the next builds through Java reflection.

Thanks to OHTAKE Tomohiro for patch.
  • Loading branch information
ialbors-pfc committed Oct 31, 2012
1 parent 6a4dbfd commit 51fa266
Showing 1 changed file with 23 additions and 23 deletions.
@@ -1,5 +1,6 @@
package hudson.plugins.nextexecutions.utils;

import java.lang.reflect.Field;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
Expand All @@ -10,51 +11,50 @@
import hudson.model.AbstractProject;
import hudson.plugins.nextexecutions.NextBuilds;
import hudson.scheduler.CronTab;
import hudson.scheduler.CronTabList;
import hudson.triggers.TimerTrigger;
import hudson.triggers.Trigger;

public class NextExecutionsUtils {

/**
* Returns the {@link NextBuild} for the project.
* Returns the {@link NextBuild} for the project.
*
* @return The {@link NextBuild} object with the associated
* next execution date or null.
*/
public static NextBuilds getNextBuild(AbstractProject project){
if(!project.isDisabled()){
Trigger trigger = project.getTrigger(TimerTrigger.class);
if(trigger != null){
List<CronTab> crons = parseSpec(trigger.getSpec());
if(trigger != null){
try{
Field triggerTabsField = Trigger.class.getDeclaredField("tabs");
triggerTabsField.setAccessible(true);

CronTabList cronTabList = (CronTabList)triggerTabsField.get(trigger);

Field crontablistTabsField = CronTabList.class.getDeclaredField("tabs");
crontablistTabsField.setAccessible(true);

List<CronTab> crons = (Vector<CronTab>)crontablistTabsField.get(cronTabList);

Calendar cal = null;
for (CronTab cronTab : crons) {
Date d = new Date();
cal = (cal == null || cal.compareTo(cronTab.ceil(d.getTime())) > 0)? cronTab.ceil(d.getTime()) : cal;
}
if(cal != null)
return new NextBuilds(project, cal);
}
catch(NoSuchFieldException e){
e.printStackTrace();
}
catch(IllegalAccessException e){
e.printStackTrace();
}

}
}
return null;
}

/**
* Pretty much the same as {@link CronTabList#create(String)}
*/
private static List<CronTab> parseSpec(String format) {
Vector<CronTab> r = new Vector<CronTab>();
int lineNumber = 0;
for (String line : format.split("\\r?\\n")) {
lineNumber++;
line = line.trim();
if(line.length()==0 || line.startsWith("#"))
continue; // ignorable line
try {
r.add(new CronTab(line,lineNumber));
} catch (ANTLRException e) {
e.printStackTrace();
}
}
return r;
}
}

0 comments on commit 51fa266

Please sign in to comment.