KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > jobs > job > TriggerJob


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

4 package org.oddjob.jobs.job;
5
6 import org.oddjob.Stateful;
7 import org.oddjob.framework.BasePrimary;
8 import org.oddjob.images.IconHelper;
9 import org.oddjob.state.JobState;
10 import org.oddjob.state.JobStateEvent;
11 import org.oddjob.state.JobStateListener;
12 import org.oddjob.util.OddjobConfigException;
13
14 /**
15  * @oddjob.description Used to fire state dependent events such as
16  * a schedule.
17  * <p>
18  * The trigger will fire on the given state, and will be reset when the object
19  * being triggered on goes to any other state.
20  * <p>
21  * The trigger will fire to the COMPLETE state regardless of the state being
22  * triggered on.
23  * <p>
24  * A trigger job is most useful when added
25  * to a {@link org.oddjob.jobs.structural.SequentialJob}
26  * which will then COMPLETE when all it's child jobs/triggers complete.
27  *
28  * @oddjob.example
29  *
30  * <pre>
31  * &lt;sequential name="Trigger on Two Things"&gt;
32  * &lt;scheduler&gt;
33  * &lt;schedules&gt;
34  * &lt;trigger id="trigger" job="${notify}" on="${watch-both}" /&gt;
35  * &lt;/schedules&gt;
36  * &lt;/scheduler&gt;
37  * &lt;folder&gt;
38  * &lt;sequential id="watch-both" name="Run me first!" &gt;
39  * &lt;trigger on="${thing1}" /&gt;
40  * &lt;trigger on="${thing2}" /&gt;
41  * &lt;/sequential&gt;
42  * &lt;echo id="thing1" name="Run me!" text="Thank you" /&gt;
43  * &lt;echo id="thing2" name="Run me!" text="Thank you" /&gt;
44  * &lt;echo id="notify" name="Don't run me" text="You ran two things!" /&gt;
45  * &lt;/folder&gt;
46  * &lt;/sequential&gt;
47  *
48  * @author Rob Gordon.
49  */

50 public class TriggerJob extends BasePrimary
51 implements Stateful, JobStateListener, Runnable JavaDoc {
52     private static final long serialVersionUID = 20051206;
53     
54     /**
55      * @oddjob.property
56      * @oddjob.description The state to trigger on.
57      * @oddjob.required No, defaults to COMPLETE.
58      */

59     private String JavaDoc state;
60     
61     /** Monitors state */
62     private JobState jobState;
63     
64     /**
65      * @oddjob.property
66      * @oddjob.description The job to trigger on.
67      * @oddjob.required Yes
68      */

69     private Stateful on;
70
71     public boolean isTransient() {
72         return true;
73     }
74     
75     public boolean independant() {
76         return true;
77     }
78     
79     public void run() {
80         if (!configure()) {
81             return;
82         }
83         
84         if (jobState == null) {
85             if (state == null) {
86                 jobState = JobState.COMPLETE;
87                 logger().debug("Defaulting to complete.");
88             }
89             else {
90                 jobState = JobState.stateFor(state);
91             }
92         }
93         if (jobState == null) {
94             throw new OddjobConfigException("State is invalid.");
95         }
96         on.addJobStateListener(this);
97     }
98     
99     /* (non-Javadoc)
100      * @see org.oddjob.state.JobStateListener#jobStateChange(org.oddjob.state.JobStateEvent)
101      */

102     public void jobStateChange(JobStateEvent event) {
103         // use the time for what we're listening to incase this is a restart
104
// and we've just got the initial state from a persisted source.
105
if (event.getJobState() == jobState) {
106             stateHandler.fireEvent(
107                     new JobStateEvent(this, JobState.COMPLETE,
108                     event.getTime(), event.getException()));
109             logger().debug("[" + this.toString() + "] firing, event [" + event + "]");
110             iconHelper.changeIcon(IconHelper.COMPLETE);
111         }
112         else {
113             stateHandler.fireEvent(new JobStateEvent(this,
114                     JobState.READY, event.getTime(), null));
115             iconHelper.changeIcon(IconHelper.READY);
116         }
117     }
118     
119     public Stateful getOn() {
120         return on;
121     }
122     
123     public void setOn(Stateful on) {
124         this.on = on;
125     }
126     
127     public String JavaDoc getState() {
128         return state;
129     }
130     public void setState(String JavaDoc state) {
131         this.state = state;
132     }
133     
134     public void onDestroy() {
135         on.removeJobStateListener(this);
136     }
137 }
138
Popular Tags