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.ArrayList ; 8 9 import org.apache.log4j.Logger; 10 import org.oddjob.util.OddjobConstantException; 11 12 13 19 20 public class JobStateHandler implements Serializable { 21 private static final long serialVersionUID = 20050926; 22 23 private static final Logger logger = Logger.getLogger(JobStateHandler.class); 24 25 26 private transient Object source; 27 28 29 private transient ArrayList listeners = new ArrayList (); 30 31 32 private JobStateEvent lastEvent; 33 34 39 public JobStateHandler(Object source) { 40 this.source = source; 41 lastEvent = new JobStateEvent(source, JobState.READY, null); 42 } 43 44 49 public JobStateEvent getLastEvent() { 50 return lastEvent; 51 } 52 53 58 public void setSource(Object 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 70 public void setJobStateReady() { 71 fireEvent(new JobStateEvent(source, JobState.READY, null)); 72 } 73 74 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 94 public void setJobStateComplete() { 95 fireEvent(new JobStateEvent(source, JobState.COMPLETE, null)); 96 } 97 98 103 public void setJobStateNotComplete() { 104 fireEvent(new JobStateEvent(source, JobState.NOT_COMPLETE, null)); 105 } 106 107 110 public void setJobStateException(Throwable ex) { 111 fireEvent(new JobStateEvent(source, JobState.EXCEPTION, ex)); 112 } 113 114 117 public JobState getJobState() { 118 return lastEvent.getJobState(); 119 } 120 121 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 143 public void removeJobStateListener(JobStateListener listener) { 144 synchronized (listeners) { 145 listeners.remove(listener); 146 } 147 } 148 149 152 public String toString() { 153 return "JobStateHandler( " + getJobState().toString() + " )"; 154 } 155 156 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 t) { 173 logger.error("Failed notifiying listener [" + copy[i] 174 + "] of event [" + event + "]", t); 175 } 176 } 177 } 178 179 185 private void writeObject(ObjectOutputStream s) 186 throws IOException { 187 s.defaultWriteObject(); 188 } 189 190 197 private void readObject(ObjectInputStream s) 198 throws IOException , ClassNotFoundException { 199 s.defaultReadObject(); 200 listeners = new ArrayList (); 201 } 202 } 203 204 | Popular Tags |