KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > framework > SimpleJob


1 package org.oddjob.framework;
2
3
4 import org.apache.log4j.MDC;
5 import org.oddjob.Resetable;
6 import org.oddjob.Stateful;
7 import org.oddjob.images.IconHelper;
8 import org.oddjob.logging.Log4jArchiver;
9 import org.oddjob.state.JobState;
10 import org.oddjob.util.OddjobLockedException;
11
12 /**
13  * An abstract implementation of a job which provides commen functionality to
14  * concrete sub classes.
15  *
16  * @author Rob Gordon
17  */

18
19 public abstract class SimpleJob extends BasePrimary
20 implements Runnable JavaDoc, Resetable, Stateful {
21
22     /*
23      * (non-Javadoc)
24      * @see org.oddjob.framework.BaseComponent#independant()
25      */

26     protected boolean independant() {
27         return true;
28     }
29     
30     /**
31      * Execute this job.
32      *
33      * @return 0 if the job is complete, anything else otherwise.
34      * @throws Exception If the unexpected occurs.
35      */

36     abstract protected int execute() throws Throwable JavaDoc;
37
38     /**
39      * Implement the main execute method for a job. This surrounds the
40      * doExecute method of the sub class and sets state for the job.
41      */

42     public final void run() throws OddjobLockedException {
43         lock.accquire("Job executing.");
44         String JavaDoc oldMDC = (String JavaDoc)MDC.get(Log4jArchiver.MDC);
45         try {
46             MDC.put(Log4jArchiver.MDC, getLogger());
47             if (!stateHandler.requestJobStateExecuting()) {
48                 logger().debug("Can't execute job [" + this + "] because state is ["
49                         + stateHandler.getJobState() + "]");
50                 return;
51             }
52             iconHelper.changeIcon(IconHelper.EXECUTING);
53     
54             // runtime configuration.
55
if (!configure()) {
56                 return;
57             }
58             logger().info("Executing job [" + this + "]");
59         
60             int result = -1;
61             try {
62                 result = execute();
63             }
64             catch (Throwable JavaDoc e) {
65                 logger().warn("Job Exception in [" + this + "]", e);
66                 setJobStateException(e);
67                 return;
68             }
69     
70             logger().info("Finished job [" + this + "], result " + result);
71         
72             if (result == 0) {
73                 setJobStateComplete();
74             }
75             else {
76                 setJobStateNotComplete();
77             }
78         }
79         finally {
80             if (oldMDC != null) {
81                 MDC.put(Log4jArchiver.MDC, oldMDC);
82             }
83             else {
84                 MDC.remove(Log4jArchiver.MDC);
85             }
86             lock.release();
87         }
88     }
89     
90     /**
91      * Allow subclasses to indicate they are
92      * stopping. The subclass must still implement
93      * Stoppable.
94      */

95     public final void stop() {
96         if (destroyed) {
97             throw new IllegalStateException JavaDoc("[" + this + "] destroyed");
98         }
99         if (stateHandler.getJobState() != JobState.EXECUTING) {
100             return;
101         }
102         logger().debug("Thread [" + Thread.currentThread().getName()
103                 + "] requested [" + getName() + "] stop.");
104         iconHelper.changeIcon(IconHelper.STOPPING);
105         try {
106             onStop();
107         } catch (RuntimeException JavaDoc e) {
108             iconHelper.changeIcon(IconHelper.EXECUTING);
109             throw e;
110         }
111         stop = true;
112         synchronized (this) {
113             notifyAll();
114         }
115     }
116     
117     public void onStop() { }
118     
119     /**
120      * Perform a soft reset on the job.
121      */

122     public void softReset() {
123         lock.accquire("Soft reset in progress.");
124         try {
125             logger().debug("Thread [" + Thread.currentThread().getName()
126                     + "] soft reset for [" + this + "].");
127             if (canSoftReset()) {
128                 setJobStateReady();
129                 logger().info("Reset job [" + this + "]");
130             }
131         }
132         finally {
133             lock.release();
134         }
135     }
136     
137     /**
138      * Perform a hard reset on the job.
139      */

140     public void hardReset() {
141         lock.accquire("Hard reset in progress.");
142         try {
143             logger().debug("Thread [" + Thread.currentThread().getName()
144                     + "] hard reset for [" + this + "].");
145             if (canHardReset()) {
146                 setJobStateReady();
147                 logger().info("Reset job [" + this + "]");
148             }
149         }
150         finally {
151             lock.release();
152         }
153     }
154
155 }
156
Popular Tags