KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.oddjob.framework;
2
3
4 import java.beans.PropertyChangeListener JavaDoc;
5 import java.beans.PropertyChangeSupport JavaDoc;
6
7 import org.apache.log4j.Logger;
8 import org.oddjob.Iconic;
9 import org.oddjob.arooa.ArooaRuntime;
10 import org.oddjob.arooa.Location;
11 import org.oddjob.arooa.RuntimeConfiguration;
12 import org.oddjob.images.IconHelper;
13 import org.oddjob.images.IconListener;
14 import org.oddjob.images.IconTip;
15 import org.oddjob.state.JobStateEvent;
16 import org.oddjob.state.JobStateHandler;
17 import org.oddjob.state.JobStateListener;
18 import org.oddjob.state.StateTransform;
19 import org.oddjob.util.Lock;
20
21 /**
22  * An abstract implementation of a component which provides commen functionality to
23  * concrete sub classes.
24  *
25  * @author Rob Gordon
26  */

27
28 public abstract class BaseComponent implements Iconic {
29     
30     /** The location. */
31     protected Location location;
32     
33     /** RuntimeConfigurable. */
34     private ArooaRuntime arooaRuntime;
35     
36     /**
37      * Implement property change support which sub classes can take advantage of.
38      */

39     protected PropertyChangeSupport JavaDoc changes = new PropertyChangeSupport JavaDoc(this);
40
41     /**
42      * Used to notify clients of an icon change.
43      */

44     protected IconHelper iconHelper = new IconHelper(this);
45     
46     /**
47      * A state handler to delagate state change functionality to.
48      * */

49     protected JobStateHandler stateHandler = new JobStateHandler(this);
50     
51     /**
52      * This flag is set once the object is destroyed
53      * Methods in subclass should check this flag.
54      */

55     protected volatile boolean destroyed;
56     
57     /**
58      * This locks the component. It can be used
59      * by methods in subclasses to ensure the component
60      * is locked before proceeding.
61      */

62     protected Lock lock = new Lock();
63     
64     protected abstract Logger logger();
65     
66     public void arooaRuntime(ArooaRuntime arooaRuntime) {
67         this.arooaRuntime = arooaRuntime;
68     }
69     
70     public Location getLocation() {
71         return this.location;
72     }
73     
74     protected RuntimeConfiguration arooaRuntime() {
75         return arooaRuntime;
76     }
77     
78     protected void setJobStateReady() {
79         iconHelper.changeIcon(IconHelper.READY);
80         stateHandler.setJobStateReady();
81     }
82     
83     protected void setJobStateComplete() {
84         iconHelper.changeIcon(IconHelper.COMPLETE);
85         stateHandler.setJobStateComplete();
86     }
87     
88     protected void setJobStateNotComplete() {
89         iconHelper.changeIcon(IconHelper.NOT_COMPLETE);
90         stateHandler.setJobStateNotComplete();
91     }
92     
93     /**
94      * Utility method to set the job state to exception.
95      *
96      * @param err An error message.
97      * @param ex The exception.
98      */

99     protected void setJobStateException(Throwable JavaDoc ex) {
100         stateHandler.setJobStateException(ex);
101         iconHelper.changeIcon(IconHelper.EXCEPTION);
102     }
103     
104     /**
105      * Configure the runtime. If there is no runtime for this
106      * component then true is returned.
107      *
108      * @return true if sucessful. False if not.
109      */

110     protected boolean configure() {
111         // runtime configuration.
112
if (arooaRuntime != null) {
113             try {
114                 logger().debug("Configuring job [" + this + "]");
115                 arooaRuntime.configure();
116             } catch (Exception JavaDoc e) {
117                 logger().error("Configuration exception for job: "
118                     + this + ", location: " + getLocation(), e);
119                 setJobStateException(e);
120                 return false;
121             }
122         }
123         return true;
124     }
125     
126     protected boolean canSoftReset() {
127         return StateTransform.SOFT_RESET.canReset(stateHandler.getJobState());
128     }
129     
130     protected boolean canHardReset() {
131         return StateTransform.HARD_RESET.canReset(stateHandler.getJobState());
132     }
133     
134     /**
135      * Returns the last JobState event. This is useful when Jobs are being
136      * used directly in code, and only one thread is using the job. Otherwise
137      * a JobStateListener should always be used.
138      * <p>
139      * This is not a property so that it can't be accessed directly in scripts.
140      *
141      * @return The last JobStateEvent. Will never be null.
142      */

143     public JobStateEvent lastJobStateEvent() {
144         if (destroyed) {
145             throw new IllegalStateException JavaDoc("[" + this + "] destroyed");
146         }
147         return stateHandler.getLastEvent();
148     }
149     
150     /**
151      * Add a job state listener.
152      */

153     public void addJobStateListener(JobStateListener listener) {
154         if (destroyed) {
155             throw new IllegalStateException JavaDoc("[" + this + "] destroyed");
156         }
157         stateHandler.addJobStateListener(listener);
158     }
159
160     /**
161      * Remove a job state listener.
162      */

163     public void removeJobStateListener(JobStateListener listener){
164         stateHandler.removeJobStateListener(listener);
165     }
166     
167     /**
168      * Add a property change listener.
169      *
170      * @param l The property change listener.
171      */

172     public void addPropertyChangeListener(PropertyChangeListener JavaDoc l) {
173         if (destroyed) {
174             throw new IllegalStateException JavaDoc("[" + this + "] destroyed");
175         }
176         changes.addPropertyChangeListener(l);
177     }
178
179     /**
180      * Remove a property change listener.
181      *
182      * @param l The property change listener.
183      */

184     public void removePropertyChangeListener(PropertyChangeListener JavaDoc l) {
185         changes.removePropertyChangeListener(l);
186     }
187
188     /**
189      * Return an icon tip for a given id. Part
190      * of the Iconic interface.
191      */

192     public IconTip iconForId(String JavaDoc iconId) {
193         return iconHelper.iconForId(iconId);
194     }
195
196     /**
197      * Add an icon listener. Part of the Iconic
198      * interface.
199      *
200      * @param listener The listener.
201      */

202     public void addIconListener(IconListener listener) {
203         if (destroyed) {
204             throw new IllegalStateException JavaDoc("[" + this + "] destroyed");
205         }
206         iconHelper.addIconListener(listener);
207     }
208
209     /**
210      * Remove an icon listener. Part of the Iconic
211      * interface.
212      *
213      * @param listener The listener.
214      */

215     public void removeIconListener(IconListener listener) {
216         iconHelper.removeIconListener(listener);
217     }
218         
219     /**
220      * Destroy resources. Override onDestroy to do specific resourece
221      * clear up.
222      */

223     public final void destroy() {
224         lock.accquire("Destruction in progress.");
225         try {
226             if (destroyed) {
227                 throw new IllegalStateException JavaDoc("[" + this + "] destroyed");
228             }
229             logger().debug("Destroying [" + this + "].");
230             onDestroy();
231             destroyed = true;
232             logger().debug("Destroyed [" + this + "]");
233         }
234         finally {
235             lock.release();
236         }
237     }
238     
239     /**
240      * Subclasses override this method to clear up resources.
241      *
242      */

243     public void onDestroy() { }
244 }
245
Popular Tags