PageRenderTime 7ms CodeModel.GetById 2ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/hudson-core/src/main/java/hudson/model/queue/QueueSorter.java

http://github.com/hudson/hudson
Java | 57 lines | 27 code | 9 blank | 21 comment | 3 complexity | 2680e1f550b14ce95abaa57c6e9158f3 MD5 | raw file
 1package hudson.model.queue;
 2
 3import hudson.ExtensionList;
 4import hudson.ExtensionPoint;
 5import hudson.init.Initializer;
 6import hudson.model.Hudson;
 7import hudson.model.Queue;
 8import hudson.model.Queue.BuildableItem;
 9
10import java.util.List;
11import java.util.logging.Logger;
12
13import static hudson.init.InitMilestone.JOB_LOADED;
14
15/**
16 * Singleton extension point for sorting buildable items
17 *
18 * @since 1.343
19 */
20public abstract class QueueSorter implements ExtensionPoint {
21    /**
22     * Sorts the buildable items list. The items at the beginning will be executed
23     * before the items at the end of the list.
24     *
25     * @param buildables
26     *      List of buildable items in the queue. Never null.
27     */
28    public abstract void sortBuildableItems(List<BuildableItem> buildables);
29
30    /**
31     * All registered {@link QueueSorter}s. Only the first one will be picked up,
32     * unless explicitly overridden by {@link Queue#setSorter(QueueSorter)}.
33     */
34    public static ExtensionList<QueueSorter> all() {
35        return Hudson.getInstance().getExtensionList(QueueSorter.class);
36    }
37
38    /**
39     * Installs the default queue sorter.
40     *
41     * {@link Queue#Queue(hudson.model.LoadBalancer)} is too early to do this
42     */
43    @Initializer(after=JOB_LOADED)
44    public static void installDefaultQueueSorter() {
45        ExtensionList<QueueSorter> all = all();
46        if (all.isEmpty())  return;
47
48        Queue q = Hudson.getInstance().getQueue();
49        if (q.getSorter()!=null)        return; // someone has already installed something. leave that alone.
50
51        q.setSorter(all.get(0));
52        if (all.size()>1)
53            LOGGER.warning("Multiple QueueSorters are registered. Only the first one is used and the rest are ignored: "+all);
54    }
55
56    private static final Logger LOGGER = Logger.getLogger(QueueSorter.class.getName());
57}