KickJava   Java API By Example, From Geeks To Geeks.

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


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.Date JavaDoc;
8 import java.util.EventObject JavaDoc;
9
10 import org.oddjob.util.IO;
11
12 /**
13  * An instance of this class is produced when a job state changes. It is
14  * passed to all JobStateListeners.
15  *
16  * @author Rob Gordon
17  */

18
19 public class JobStateEvent extends EventObject JavaDoc implements Serializable JavaDoc {
20
21     private static final long serialVersionUID = 20051026;
22
23     static final String JavaDoc REPLACEMENT_EXCEPTION_TEXT = "Exception is not serializable, message is: ";
24     
25     private JobState jobState;
26     private Date JavaDoc time;
27     private Throwable JavaDoc exception;
28     
29     /**
30      * Used to replace a non serializable exception.
31      *
32      */

33     class ExceptionReplacement extends Exception JavaDoc {
34         private static final long serialVersionUID = 20051217;
35         public ExceptionReplacement(Throwable JavaDoc replacing) {
36             super(REPLACEMENT_EXCEPTION_TEXT + replacing.getMessage());
37             super.setStackTrace(exception.getStackTrace());
38         }
39     }
40     
41     /**
42      * Constructor.
43      *
44      * @param job The source of the event.
45      * @param jobState The state.
46      * @param time the Time of the event.
47      * @param exception The exception if applicable, or null otherwise.
48      */

49     public JobStateEvent(Object JavaDoc job, JobState jobState, Date JavaDoc time, Throwable JavaDoc exception) {
50         super(job);
51         if (jobState == null) {
52             throw new NullPointerException JavaDoc("JobState can not be null!");
53         }
54         this.jobState = jobState;
55         this.time = time;
56         this.exception = exception;
57     }
58
59     /**
60      * Constructor.
61      *
62      * @param job The source of the event.
63      * @param jobState The state.
64      * @param exception The exception if applicable, or null otherwise.
65      */

66     public JobStateEvent(Object JavaDoc job, JobState jobState, Throwable JavaDoc exception) {
67         this(job, jobState, new Date JavaDoc(), exception);
68     }
69
70     /**
71      * Constructor.
72      *
73      * @param job The source of the event.
74      * @param jobState The state.
75      */

76     public JobStateEvent(Object JavaDoc job, JobState jobState) {
77         this(job, jobState, null);
78     }
79
80     /**
81      * Get the job state.
82      *
83      * @return The job state.
84      */

85     public JobState getJobState() {
86         return jobState;
87     }
88
89     /**
90      * Get the exception if applicable, null otherwise.
91      *
92      * @return The excpetion of null.
93      */

94     public Throwable JavaDoc getException() {
95         return exception;
96     }
97
98     /**
99      * Get the time of the event..
100      *
101      * @return The time.
102      */

103     public Date JavaDoc getTime() {
104         return time;
105     }
106
107     /**
108      * Override toString.
109      */

110     public String JavaDoc toString() {
111         return "JobStateEvent, source=" + getSource() + ", " + jobState;
112     }
113     
114     /*
115      * Custom serialization.
116      */

117     private void writeObject(ObjectOutputStream JavaDoc s)
118     throws IOException JavaDoc {
119         s.writeObject(jobState);
120         s.writeObject(time);
121         if (IO.canSerialize(exception)) {
122             s.writeObject(exception);
123         }
124         else {
125             s.writeObject(new ExceptionReplacement(exception));
126         }
127     }
128     
129     /*
130      * Custom serialization.
131      */

132     private void readObject(ObjectInputStream JavaDoc s)
133     throws IOException JavaDoc, ClassNotFoundException JavaDoc {
134         jobState = (JobState) s.readObject();
135         time = (Date JavaDoc) s.readObject();
136         exception = (Exception JavaDoc) s.readObject();
137     }
138     
139 }
140
Popular Tags