1 package org.oddjob.state; 2 3 import java.io.IOException ; 4 import java.io.ObjectInputStream ; 5 import java.io.ObjectOutputStream ; 6 import java.io.Serializable ; 7 import java.util.Date ; 8 import java.util.EventObject ; 9 10 import org.oddjob.util.IO; 11 12 18 19 public class JobStateEvent extends EventObject implements Serializable { 20 21 private static final long serialVersionUID = 20051026; 22 23 static final String REPLACEMENT_EXCEPTION_TEXT = "Exception is not serializable, message is: "; 24 25 private JobState jobState; 26 private Date time; 27 private Throwable exception; 28 29 33 class ExceptionReplacement extends Exception { 34 private static final long serialVersionUID = 20051217; 35 public ExceptionReplacement(Throwable replacing) { 36 super(REPLACEMENT_EXCEPTION_TEXT + replacing.getMessage()); 37 super.setStackTrace(exception.getStackTrace()); 38 } 39 } 40 41 49 public JobStateEvent(Object job, JobState jobState, Date time, Throwable exception) { 50 super(job); 51 if (jobState == null) { 52 throw new NullPointerException ("JobState can not be null!"); 53 } 54 this.jobState = jobState; 55 this.time = time; 56 this.exception = exception; 57 } 58 59 66 public JobStateEvent(Object job, JobState jobState, Throwable exception) { 67 this(job, jobState, new Date (), exception); 68 } 69 70 76 public JobStateEvent(Object job, JobState jobState) { 77 this(job, jobState, null); 78 } 79 80 85 public JobState getJobState() { 86 return jobState; 87 } 88 89 94 public Throwable getException() { 95 return exception; 96 } 97 98 103 public Date getTime() { 104 return time; 105 } 106 107 110 public String toString() { 111 return "JobStateEvent, source=" + getSource() + ", " + jobState; 112 } 113 114 117 private void writeObject(ObjectOutputStream s) 118 throws IOException { 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 132 private void readObject(ObjectInputStream s) 133 throws IOException , ClassNotFoundException { 134 jobState = (JobState) s.readObject(); 135 time = (Date ) s.readObject(); 136 exception = (Exception ) s.readObject(); 137 } 138 139 } 140 | Popular Tags |