KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > state > JobStateHandler


1 package org.oddjob.state;
2
3 import java.io.IOException JavaDoc;
4 import java.io.ObjectInputStream JavaDoc;
5 import java.io.ObjectOutputStream JavaDoc;
6 import java.io.Serializable JavaDoc;
7 import java.util.ArrayList JavaDoc;
8
9 import org.apache.log4j.Logger;
10 import org.oddjob.util.OddjobConstantException;
11
12
13 /**
14  * An interface to define a state handler which helps an object
15  * to implement the stateful interface.
16  *
17  * @author Rob Gordon
18  */

19
20 public class JobStateHandler implements Serializable JavaDoc {
21     private static final long serialVersionUID = 20050926;
22     
23     private static final Logger logger = Logger.getLogger(JobStateHandler.class);
24     
25     /** The source. */
26     private transient Object JavaDoc source;
27     
28     /** State listeners */
29     private transient ArrayList JavaDoc listeners = new ArrayList JavaDoc();
30
31     /** The last event */
32     private JobStateEvent lastEvent;
33
34     /**
35      * Constrctor.
36      *
37      * @param source The source for events.
38      */

39     public JobStateHandler(Object JavaDoc source) {
40         this.source = source;
41         lastEvent = new JobStateEvent(source, JobState.READY, null);
42     }
43     
44     /**
45      * Get the last event.
46      *
47      * @return The last event.
48      */

49     public JobStateEvent getLastEvent() {
50         return lastEvent;
51     }
52     
53     /**
54      * Typically only called after restoring a jobstate handler after deserialisation.
55      *
56      * @param source
57      */

58     public void setSource(Object JavaDoc source) {
59         if (this.source != null) {
60             throw new OddjobConstantException("Source can not be changed for a JobStateHandler.");
61         }
62         this.source = source;
63         lastEvent = new JobStateEvent(source, lastEvent.getJobState(), lastEvent.getTime(), lastEvent.getException());
64     }
65     
66
67     /**
68      * Set the job state to pending.
69      */

70     public void setJobStateReady() {
71         fireEvent(new JobStateEvent(source, JobState.READY, null));
72     }
73     
74     /**
75      * Request the job state be set to executing. A
76      * job can only be set to executing if it is
77      * ready.
78      *
79      * @return true if it succeeds.
80      */

81     public boolean requestJobStateExecuting() {
82         if (getJobState() != JobState.READY) {
83             return false;
84         }
85         fireEvent(new JobStateEvent(source, JobState.EXECUTING, null));
86         return true;
87     }
88
89     /**
90      * Set the job state be set to complete.
91      *
92      * @return true if it succeeds.
93      */

94     public void setJobStateComplete() {
95         fireEvent(new JobStateEvent(source, JobState.COMPLETE, null));
96     }
97
98     /**
99      * Request the job state be set to not complete.
100      *
101      * @return true if it succeeds.
102      */

103     public void setJobStateNotComplete() {
104         fireEvent(new JobStateEvent(source, JobState.NOT_COMPLETE, null));
105     }
106
107     /**
108      * Set state exception.
109      */

110     public void setJobStateException(Throwable JavaDoc ex) {
111         fireEvent(new JobStateEvent(source, JobState.EXCEPTION, ex));
112     }
113     
114     /**
115      * Return the current state of the job.
116      */

117     public JobState getJobState() {
118             return lastEvent.getJobState();
119     }
120
121     /**
122      * Add a job state listener. This method will send the last event
123      * to the new listener. It is possible that the listener may get the
124      * notification twice.
125      *
126      * @param listener The listener.
127      */

128     public void addJobStateListener(JobStateListener listener) {
129         JobStateEvent event;
130         synchronized (listeners) {
131             listeners.add(listener);
132             event = lastEvent;
133         }
134         listener.jobStateChange(event);
135     }
136
137
138     /**
139      * Remove a job state listener.
140      *
141      * @param listener The listener.
142      */

143     public void removeJobStateListener(JobStateListener listener) {
144         synchronized (listeners) {
145             listeners.remove(listener);
146         }
147     }
148
149     /**
150      * Override toString.
151      */

152     public String JavaDoc toString() {
153         return "JobStateHandler( " + getJobState().toString() + " )";
154     }
155
156     /**
157      * Fire the event, update last event.
158      *
159      * @param event The event.
160      */

161     public void fireEvent(JobStateEvent event) {
162         JobStateListener[] copy = null;
163         synchronized (listeners) {
164             lastEvent = event;
165             copy = (JobStateListener[]) listeners.toArray(new JobStateListener[0]);
166         }
167         
168         for (int i = 0; i < copy.length; ++i) {
169             try {
170                 copy[i].jobStateChange(event);
171             }
172             catch (Throwable JavaDoc t) {
173                 logger.error("Failed notifiying listener [" + copy[i]
174                         + "] of event [" + event + "]", t);
175             }
176         }
177     }
178     
179     /**
180      * Implement custom serialization.
181      *
182      * @param s The stream.
183      * @throws IOException If serialisation fails.
184      */

185     private void writeObject(ObjectOutputStream JavaDoc s)
186     throws IOException JavaDoc {
187         s.defaultWriteObject();
188     }
189     
190     /**
191      * Implement custom serialization.
192      *
193      * @param s The stream
194      * @throws IOException If serialisation fails.
195      * @throws ClassNotFoundException If class can't be found.
196      */

197     private void readObject(ObjectInputStream JavaDoc s)
198     throws IOException JavaDoc, ClassNotFoundException JavaDoc {
199         s.defaultReadObject();
200         listeners = new ArrayList JavaDoc();
201     }
202 }
203
204
Popular Tags