KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jfun > yan > lifecycle > DefaultLifecycleDescriptor


1 /*****************************************************************************
2  * Copyright (C) Zephyr Business Solution. All rights reserved. *
3  * ------------------------------------------------------------------------- *
4  * The software in this package is published under the terms of the BSD *
5  * style license a copy of which has been included with this distribution in *
6  * the LICENSE.txt file. *
7  *****************************************************************************/

8
9 /*
10  * Created on Oct 13, 2005
11  *
12  * Author Michelle Lei
13  * ZBS
14  */

15 package jfun.yan.lifecycle;
16
17 /**
18  * <p>
19  * The descriptor for a typically used life-cycle scheme
20  * with "init", "verify", "dispose", "start", "stop", "open", "close" supported.
21  * </p>
22  * Zephyr Business Solution
23  *
24  * @author Michelle Lei
25  *
26  */

27 public class DefaultLifecycleDescriptor {
28   private Phase initializer;
29   private Phase disposer;
30   private Phase verifier;
31   private Phase opener;
32   private Phase closer;
33   private Phase starter;
34   private Phase stopper;
35   /**
36    * The default key of the <i>open</i> phase.
37    */

38   public static final String JavaDoc DEFAULT_OPENER_NAME = "opener";
39   /**
40    * The default key of the <i>close</i> phase.
41    */

42   public static final String JavaDoc DEFAULT_CLOSER_NAME = "closer";
43   /**
44    * The default key of the <i>init</i> phase.
45    */

46   public static final String JavaDoc DEFAULT_INITIALIZER_NAME = "initializer";
47   /**
48    * The default key of the <i>verify</i> phase.
49    */

50   public static final String JavaDoc DEFAULT_VERIFIER_NAME = "verifier";
51   /**
52    * The default key of the <i>dispose</i> phase.
53    */

54   public static final String JavaDoc DEFAULT_DISPOSER_NAME = "disposer";
55   /**
56    * The default key of the <i>start</i> phase.
57    */

58   public static final String JavaDoc DEFAULT_STARTER_NAME = "starter";
59   /**
60    * The default key of the <i>stop</i> phase.
61    */

62   public static final String JavaDoc DEFAULT_STOPPER_NAME = "stopper";
63   private static final ExceptionHandler thrower = ExceptionHandlers.thrower();
64   private static final ExceptionHandler printer = ExceptionHandlers.printer();
65   static final Phase DEFAULT_OPENER =
66     new Phase(DEFAULT_OPENER_NAME, thrower);
67   static final Phase DEFAULT_CLOSER =
68     new Phase(DEFAULT_CLOSER_NAME, printer);
69   static final Phase DEFAULT_INITIALIZER =
70     new Phase(DEFAULT_INITIALIZER_NAME, thrower);
71   static final Phase DEFAULT_VERIFIER =
72     new Phase(DEFAULT_VERIFIER_NAME, thrower);
73   static final Phase DEFAULT_DISPOSER =
74     new Phase(DEFAULT_DISPOSER_NAME, printer);
75   static final Phase DEFAULT_STARTER =
76     new Phase(DEFAULT_STARTER_NAME, thrower);
77   static final Phase DEFAULT_STOPPER =
78     new Phase(DEFAULT_STOPPER_NAME, printer);
79   /**
80    * <p>
81    * To create a LifecycleDescriptor object.
82    * </p>
83    * <p>
84    * By default,
85    * <i>init</i>, <i>open</i>, <i>start</i> phases are initialized with the default key and a exception printer
86    * that prints exception thrown out of the procedure to the <i><code>System.err</code></i>. <br>
87    * <i>dispose</i>, <i>close</i>, <i>stop</i> phases are initialized with the default key and a exception thrower
88    * that will stop the execution when any phase of any object fails.<br>
89    * </p>
90    */

91   public DefaultLifecycleDescriptor(){
92     this.initializer = DEFAULT_INITIALIZER;
93     this.disposer = DEFAULT_DISPOSER;
94     this.verifier = DEFAULT_VERIFIER;
95     this.opener = DEFAULT_OPENER;
96     this.closer = DEFAULT_CLOSER;
97     this.starter = DEFAULT_STARTER;
98     this.stopper = DEFAULT_STOPPER;
99   }
100   /**
101    * To invoke the "open" phase for the objects stored in the LifecycleManager object.
102    * @param lm the LifecycleManager object.
103    * @throws Throwable when any exception happens.
104    */

105   public void open(LifecycleManager lm)
106   throws Throwable JavaDoc{
107     fifo(lm, opener, null);
108   }
109   /**
110    * To invoke the "close" phase for the objects stored in the LifecycleManager object.
111    * @param lm the LifecycleManager object.
112    * @throws Throwable when any exception happens.
113    */

114   public void close(LifecycleManager lm)
115   throws Throwable JavaDoc{
116     filo(lm, closer, null);
117   }
118   /**
119    * To invoke the "init" phase for the objects stored in the LifecycleManager object.
120    * @param lm the LifecycleManager object.
121    * @throws Throwable when any exception happens.
122    */

123   public void init(LifecycleManager lm)
124   throws Throwable JavaDoc{
125     fifo(lm, initializer, null);
126   }
127   /**
128    * To invoke the "dispose" phase for the objects stored in the LifecycleManager object.
129    * @param lm the LifecycleManager object.
130    * @throws Throwable when any exception happens.
131    */

132   public void dispose(LifecycleManager lm)
133   throws Throwable JavaDoc{
134     filo(lm, disposer, null);
135   }
136   /**
137    * To invoke the "verify" phase for the objects stored in the LifecycleManager object.
138    * @param lm the LifecycleManager object.
139    * @throws Throwable when any exception happens.
140    */

141   public void verify(LifecycleManager lm)
142   throws Throwable JavaDoc{
143     fifo(lm, verifier, null);
144   }
145   /**
146    * To invoke the "start" phase for the objects stored in the LifecycleManager object.
147    * @param lm the LifecycleManager object.
148    * @throws Throwable when any exception happens.
149    */

150   public void start(LifecycleManager lm)
151   throws Throwable JavaDoc{
152     fifo(lm, starter, null);
153   }
154   /**
155    * To invoke the "stop" phase for the objects stored in the LifecycleManager object.
156    * @param lm the LifecycleManager object.
157    * @throws Throwable when any exception happens.
158    */

159   public void stop(LifecycleManager lm)
160   throws Throwable JavaDoc{
161     filo(lm, stopper, null);
162   }
163   
164   /**
165    * To register the "open" phase into a Lifecycle object.
166    * @param lc the Lifecycle object.
167    * @param proc the Procedure for the phase.
168    * @param reentrant whether the phase is reentrant.
169    * @return this LifecycleDescriptor object itself.
170    */

171   public DefaultLifecycleDescriptor opener(Lifecycle lc, Procedure proc, boolean reentrant){
172     return addLifeProc(lc, opener, proc, reentrant);
173   }
174   /**
175    * To register the "close" phase into a Lifecycle object.
176    * @param lc the Lifecycle object.
177    * @param proc the Procedure for the phase.
178    * @param reentrant whether the phase is reentrant.
179    * @return this LifecycleDescriptor object itself.
180    */

181   public DefaultLifecycleDescriptor closer(Lifecycle lc, Procedure proc, boolean reentrant){
182     return addLifeProc(lc, closer, proc, reentrant);
183   }
184   /**
185    * To register the "init" phase into a Lifecycle object.
186    * @param lc the Lifecycle object.
187    * @param proc the Procedure for the phase.
188    * @param reentrant whether the phase is reentrant.
189    * @return this LifecycleDescriptor object itself.
190    */

191   public DefaultLifecycleDescriptor initializer(Lifecycle lc, Procedure proc, boolean reentrant){
192     return addLifeProc(lc, initializer, proc, reentrant);
193   }
194   /**
195    * To register the "dispose" phase into a Lifecycle object.
196    * @param lc the Lifecycle object.
197    * @param proc the Procedure for the phase.
198    * @param reentrant whether the phase is reentrant.
199    * @return this LifecycleDescriptor object itself.
200    */

201   public DefaultLifecycleDescriptor disposer(Lifecycle lc, Procedure proc, boolean reentrant){
202     return addLifeProc(lc, disposer, proc, reentrant);
203   }
204   /**
205    * To register the "verify" phase into a Lifecycle object.
206    * @param lc the Lifecycle object.
207    * @param proc the Procedure for the phase.
208    * @param reentrant whether the phase is reentrant.
209    * @return this LifecycleDescriptor object itself.
210    */

211   public DefaultLifecycleDescriptor verifier(Lifecycle lc, Procedure proc, boolean reentrant){
212     return addLifeProc(lc, verifier, proc, reentrant);
213   }
214   /**
215    * To register the "start" phase into a Lifecycle object.
216    * @param lc the Lifecycle object.
217    * @param proc the Procedure for the phase.
218    * @param reentrant whether the phase is reentrant.
219    * @return this LifecycleDescriptor object itself.
220    */

221   public DefaultLifecycleDescriptor starter(Lifecycle lc, Procedure proc, boolean reentrant){
222     return addLifeProc(lc, starter, proc, reentrant);
223   }
224   /**
225    * To register the "stop" phase into a Lifecycle object.
226    * @param lc the Lifecycle object.
227    * @param proc the Procedure for the phase.
228    * @param reentrant whether the phase is reentrant.
229    * @return this LifecycleDescriptor object itself.
230    */

231   public DefaultLifecycleDescriptor stopper(Lifecycle lc, Procedure proc, boolean reentrant){
232     return addLifeProc(lc, stopper, proc, reentrant);
233   }
234   /**
235    * To register the "open" phase into a Lifecycle object.<br>
236    * The phase allows re-entry.
237    * @param lc the Lifecycle object.
238    * @param proc the Procedure for the phase.
239    * @return this LifecycleDescriptor object itself.
240    */

241   public DefaultLifecycleDescriptor opener(Lifecycle lc, Procedure proc){
242     return opener(lc, proc, true);
243   }
244   /**
245    * To register the "close" phase into a Lifecycle object.<br>
246    * The phase allows re-entry.
247    * @param lc the Lifecycle object.
248    * @param proc the Procedure for the phase.
249    * @return this LifecycleDescriptor object itself.
250    */

251   public DefaultLifecycleDescriptor closer(Lifecycle lc, Procedure proc){
252     return closer(lc, proc, true);
253   }
254   /**
255    * To register the "init" phase into a Lifecycle object.<br>
256    * The phase ignores re-entry.
257    * @param lc the Lifecycle object.
258    * @param proc the Procedure for the phase.
259    * @return this LifecycleDescriptor object itself.
260    */

261   public DefaultLifecycleDescriptor initializer(Lifecycle lc, Procedure proc){
262     return initializer(lc, proc, false);
263   }
264   /**
265    * To register the "dispose" phase into a Lifecycle object.<br>
266    * The phase ignores re-entry.
267    * @param lc the Lifecycle object.
268    * @param proc the Procedure for the phase.
269    * @return this LifecycleDescriptor object itself.
270    */

271   public DefaultLifecycleDescriptor disposer(Lifecycle lc, Procedure proc){
272     return disposer(lc, proc, false);
273   }
274   /**
275    * To register the "verify" phase into a Lifecycle object.<br>
276    * The phase allows re-entry.
277    * @param lc the Lifecycle object.
278    * @param proc the Procedure for the phase.
279    * @return this LifecycleDescriptor object itself.
280    */

281   public DefaultLifecycleDescriptor verifier(Lifecycle lc, Procedure proc){
282     return verifier(lc, proc, true);
283   }
284   /**
285    * To register the "start" phase into a Lifecycle object.<br>
286    * The phase allows re-entry.
287    * @param lc the Lifecycle object.
288    * @param proc the Procedure for the phase.
289    * @return this LifecycleDescriptor object itself.
290    */

291   public DefaultLifecycleDescriptor starter(Lifecycle lc, Procedure proc){
292     return starter(lc, proc, true);
293   }
294   /**
295    * To register the "stop" phase into a Lifecycle object.<br>
296    * The phase allows re-entry.
297    * @param lc the Lifecycle object.
298    * @param proc the Procedure for the phase.
299    * @return this LifecycleDescriptor object itself.
300    */

301   public DefaultLifecycleDescriptor stopper(Lifecycle lc, Procedure proc){
302     return stopper(lc, proc, true);
303   }
304
305   private DefaultLifecycleDescriptor addLifeProc(Lifecycle lc, Phase act, Procedure proc, boolean reentrant){
306     lc.put(act.getPhaseKey(), proc, reentrant);
307     return this;
308   }
309   
310   
311   private static void fifo(LifecycleManager lm, Phase action, Object JavaDoc[] args)
312   throws Throwable JavaDoc{
313     lm.fifo(action.getPhaseKey(), args, action.getExceptionHandler());
314   }
315   private static void filo(LifecycleManager lm, Phase action, Object JavaDoc[] args)
316   throws Throwable JavaDoc{
317     lm.filo(action.getPhaseKey(), args, action.getExceptionHandler());
318   }
319   /**
320    * To get the "closer" phase.
321    */

322   public Phase getCloser() {
323     return closer;
324   }
325   /**
326    * To set the "close" phase.
327    * @param closer the phase.
328    */

329   public void setCloser(Phase closer) {
330     this.closer = closer;
331   }
332   /**
333    * To get the "disposer" phase.
334    */

335   public Phase getDisposer() {
336     return disposer;
337   }
338   /**
339    * To set the "dispose" phase.
340    * @param disposer the phase.
341    */

342   public void setDisposer(Phase disposer) {
343     this.disposer = disposer;
344   }
345   /**
346    * To get the "init" phase.
347    */

348   public Phase getInitializer() {
349     return initializer;
350   }
351   /**
352    * To set the "init" phase.
353    * @param initializer the phase.
354    */

355   public void setInitializer(Phase initializer) {
356     this.initializer = initializer;
357   }
358   /**
359    * To get the "open" phase.
360    */

361   public Phase getOpener() {
362     return opener;
363   }
364   /**
365    * To set the "open" phase.
366    * @param opener the phase.
367    */

368   public void setOpener(Phase opener) {
369     this.opener = opener;
370   }
371   /**
372    * To get the "start" phase.
373    */

374   public Phase getStarter() {
375     return starter;
376   }
377   /**
378    * To set the "start" phase.
379    * @param starter the phase.
380    */

381   public void setStarter(Phase starter) {
382     this.starter = starter;
383   }
384   /**
385    * To get the "stop" phase.
386    */

387   public Phase getStopper() {
388     return stopper;
389   }
390   /**
391    * To set the "stop" phase.
392    * @param stopper the phase.
393    */

394   public void setStopper(Phase stopper) {
395     this.stopper = stopper;
396   }
397   /**
398    * To get the "verify" phase.
399    */

400   public Phase getVerifier() {
401     return verifier;
402   }
403   /**
404    * To set the "verify" phase.
405    * @param verifier the phase.
406    */

407   public void setVerifier(Phase verifier) {
408     this.verifier = verifier;
409   }
410   
411 }
412
Popular Tags