KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > monitor > model > SchedulerTraker


1 /*
2  * (c) Rob Gordon 2005
3  */

4 package org.oddjob.monitor.model;
5
6 import java.util.ArrayList JavaDoc;
7 import java.util.List JavaDoc;
8
9 import org.apache.log4j.Logger;
10 import org.oddjob.framework.Transportable;
11 import org.oddjob.scheduling.OddjobScheduler;
12 import org.oddjob.scheduling.ScheduleSummary;
13
14 /**
15  * Tracks schedulers as they are added and removed from a job tree. Used
16  * by Explorer to build the schedule/unshedule actions.
17  *
18  * @author Rob Gordon.
19  */

20 public class SchedulerTraker {
21     private static final Logger logger = Logger.getLogger(SchedulerTraker.class);
22     
23     /** Available schedulers */
24     private List JavaDoc schedulers/*<OddjobScheduler>*/ = new ArrayList JavaDoc();
25
26     /**
27      * Add a scheduler to the list of those available.
28      *
29      * @param scheduler The scheduler.
30      */

31     synchronized public void addScheduler(OddjobScheduler scheduler) {
32         schedulers.add(scheduler);
33     }
34     
35     /**
36      * Remove a scheduler from the list of available schedulers.
37      *
38      * @param scheduler The scheduler.
39      */

40     synchronized public void removeSchedule(OddjobScheduler scheduler) {
41         schedulers.remove(scheduler);
42     }
43     
44     /**
45      * Get a the JobSchedulerSummary objects for the given jobs.
46      *
47      * @param component The job which might be scheduled.
48      * @return An array of scheduler summary objects. Might be empty
49      * but never null.
50      */

51     public JobSchedulerSummary[] getSchedulerSummaries(Object JavaDoc component) {
52         List JavaDoc results = new ArrayList JavaDoc();
53         OddjobScheduler[] sa = getSchedulers();
54         for (int i = 0; i < sa.length; ++i) {
55             ScheduleSummary[] summaries = null;
56             try {
57                 OddjobScheduler scheduler = sa[i];
58                 // if job isn't a proxy, then don't attempt to send
59
// accross network
60
if (scheduler instanceof Transportable
61                         && ! (component instanceof Transportable)) {
62                     continue;
63                 }
64                 summaries = scheduler.summariesFor(component);
65                 if (summaries == null) {
66                     throw new IllegalStateException JavaDoc("An OddjobScheduler should never provide null summaries.");
67                 }
68             } catch (Exception JavaDoc e) {
69                 // most likely a lost network connection.
70
logger.warn("Failed to retrieve scheduler summaries.", e);
71                 continue;
72             }
73             for (int j = 0; j < summaries.length; ++j) {
74                 results.add(new JobSchedulerSummary(sa[i], summaries[j]));
75             }
76         }
77         return (JobSchedulerSummary[]) results.toArray(new JobSchedulerSummary[0]);
78     }
79     
80     /**
81      * Get a list of the available schedulers.
82      *
83      * @return An array of available schedulers. Never null.
84      */

85     synchronized public OddjobScheduler[] getSchedulers() {
86         return (OddjobScheduler[]) schedulers.toArray(new OddjobScheduler[0]);
87     }
88 }
89
Popular Tags