KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > jobs > WaitJob


1 package org.oddjob.jobs;
2
3 import org.oddjob.Stateful;
4 import org.oddjob.Stoppable;
5 import org.oddjob.arooa.ArooaConstants;
6 import org.oddjob.arooa.ArooaContext;
7 import org.oddjob.arooa.RuntimeConfiguration;
8 import org.oddjob.framework.SimpleJob;
9 import org.oddjob.state.JobState;
10 import org.oddjob.state.JobStateEvent;
11 import org.oddjob.state.JobStateListener;
12
13 /**
14  * @oddjob.description This Job will either wait a given number of milliseconds
15  * or will wait for a property or job to become available.
16  * <p>
17  * If the for property is provided, then the delay is used as the number of
18  * milliseconds between checking if the property is available.
19  *
20  * @example
21  *
22  * This example waits for a variable 'text' to be set. The value could be set
23  * accross the network or by a another job running in parallel.
24  *
25  * &lt;sequential name="Waiting For a Property"&gt;
26  * &lt;variables name="Variable Set Elsewhere" id="waitvars"/&gt;
27  * &lt;wait for="${waitvars.text}" pause="5000" name="Wait for Variable"/&gt;
28  * &lt;echo text="${waitvars.text}" name="Echo Text"/&gt;
29  * &lt;/sequential&gt;
30  *
31  * @author Rob Gordon
32  *
33  */

34
35 public class WaitJob extends SimpleJob
36         implements Stoppable {
37     private static final long serialVersionUID = 20051130;
38     
39     private static final long DEFAULT_WAIT_SLEEP = 1000;
40     
41     /**
42      * @oddjob.property
43      * @oddjob.description The wait delay in milliseconds.
44      * @oddjob.required No if for property is set, otherwise yes.
45      */

46     private long pause;
47
48     /**
49      * @oddjob.property for
50      * @oddjob.description The property to wait for.
51      * @oddjob.required No.
52      */

53     private Object JavaDoc forProperty;
54     
55     /** The runtime to get the waited for property from */
56     private RuntimeConfiguration rtc;
57     
58     private JobState state;
59     
60     private boolean not;
61     
62     /**
63      * Set the delay time in milli seconds.
64      *
65      * @param delay The delay time.
66      */

67     public void setPause(long delay) {
68         this.pause = delay;
69     }
70     
71     /**
72      * Get the delay time in milli seconds.
73      *
74      * @return The delay time.
75      */

76     public long getPause() {
77         return pause;
78     }
79     
80     public boolean setContext(ArooaContext arooaContext) {
81         rtc = (RuntimeConfiguration) arooaContext.get(
82                 ArooaConstants.CURRENTLY_CONFIGURING);
83         return super.setContext(arooaContext);
84     }
85     
86     /*
87      * (non-Javadoc)
88      * @see org.oddjob.jobs.AbstractJob#execute()
89      */

90     public int execute() throws Exception JavaDoc {
91         String JavaDoc forAttribute = null;
92         if (rtc != null) {
93             forAttribute = rtc.getAttribute("for");
94         }
95         
96         if (state != null) {
97             if (forProperty == null) {
98                 throw new IllegalStateException JavaDoc("'for' property must set.");
99             }
100             if (!(forProperty instanceof Stateful)) {
101                 throw new IllegalStateException JavaDoc("'for' property must Stateful.");
102             }
103             waitForState();
104         }
105         else if (forAttribute != null) {
106             logger().debug("Waiting for property [" + forAttribute + "]");
107             waitFor();
108         }
109         else {
110             simpleWait();
111         }
112         return 0;
113     }
114
115     protected void simpleWait() {
116         sleep(pause);
117     }
118
119     protected void waitFor() {
120         long sleep = pause;
121         if (sleep == 0) {
122             sleep = DEFAULT_WAIT_SLEEP;
123         }
124         while (!stop && forProperty == null) {
125             sleep(sleep);
126             rtc.configure();
127         }
128     }
129
130     protected void waitForState() {
131         class Listener implements JobStateListener {
132             JobState state;
133             synchronized public void jobStateChange(JobStateEvent event) {
134                 state = event.getJobState();
135                 logger().debug("State " + state);
136                 synchronized (WaitJob.this) {
137                     WaitJob.this.notifyAll();
138                 }
139             }
140             synchronized JobState getState() {
141                 return state;
142             }
143             
144         }
145         Listener l = new Listener();
146         ((Stateful) forProperty).addJobStateListener(l);
147         
148         while (true) {
149             JobState now = l.getState();
150             if (now == state && !not) {
151                 break;
152             }
153             if (now != state && not) {
154                 break;
155             }
156             logger().debug("Waiting for state " +
157                     (not ? "!" : "") + state + ", currently "
158                     + now);
159             sleep(1000);
160         }
161         ((Stateful) forProperty).removeJobStateListener(l);
162     }
163     
164     public Object JavaDoc getFor() {
165         return forProperty;
166     }
167
168     public void setFor(Object JavaDoc forProperty) {
169         this.forProperty = forProperty;
170     }
171     
172     public String JavaDoc getState() {
173         if (state == null) {
174             return null;
175         }
176         return state.toString();
177     }
178     
179     public void setState(String JavaDoc state) {
180         if (state.startsWith("!")) {
181             not = true;
182             state = state.substring(state.indexOf("!") + 1);
183         }
184         this.state = JobState.stateFor(state);
185         if (this.state == null) {
186             throw new IllegalArgumentException JavaDoc("Unknown state requested [" + state + "]");
187         }
188     }
189 }
190
Popular Tags