KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright (c) 2004, Rob Gordon.
3  */

4 package org.oddjob.state;
5
6 import org.oddjob.Stateful;
7
8 /**
9  * A wrapper for a job that holds the state of the job
10  * after it's been executed.
11  *
12  * @author Rob Gordon.
13  */

14 public class StateMemory implements JobStateListener {
15     private volatile JobState jobState;
16     private volatile Throwable JavaDoc t;
17
18     public void jobStateChange(JobStateEvent event) {
19         // only save the first state change after the
20
// job finishes executing - stops the unlikely
21
// event that the job is reset before our listener
22
// is removed.
23
if (jobState == null || jobState == JobState.READY
24                 || jobState == JobState.EXECUTING) {
25             jobState = event.getJobState();
26             t = event.getException();
27         }
28     }
29             
30     public JobState getJobState() {
31         return jobState;
32     }
33             
34     public Throwable JavaDoc getThrowable() {
35         return t;
36     }
37     
38     public void run(Runnable JavaDoc job) {
39         if (job instanceof Stateful) {
40             ((Stateful)job).addJobStateListener(this);
41         } else {
42             jobState = JobState.COMPLETE;
43         }
44         try {
45             job.run();
46         }
47         finally {
48             if (job instanceof Stateful) {
49                 ((Stateful)job).removeJobStateListener(this);
50             }
51         }
52     }
53 }
54
55
Popular Tags