KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.Collections JavaDoc;
18 import java.util.LinkedHashSet JavaDoc;
19 import java.util.Set JavaDoc;
20
21 import jfun.yan.Component;
22
23
24 /**
25  * <p>
26  * This class manages life-cycle for component objects.
27  * </p>
28  * Zephyr Business Solution
29  *
30  * @author Michelle Lei
31  *
32  */

33 public class LifecycleManager {
34   //a set of LiveObject in the order of creation.
35
private final Set JavaDoc history = Collections.synchronizedSet(new LinkedHashSet JavaDoc());
36   /**
37    * Add a LiveObject into the lifecycle manager.
38    * @param lo the LifeObject instance.
39    */

40   public void addLiveObject(LiveObject lo){
41     history.add(lo);
42   }
43   /**
44    * To create a Component object that enables life-cycle support.
45    * @param c the Component to enable life-cycle support.
46    * @param life the Life object.
47    * @return a new Component object that supports life-cycle.
48    */

49   public Component withLifecycle(Component c, Life life){
50     return c.mutate(new InstanceTracker(life, history));
51   }
52   /**
53    * To get the managed {@link LiveObject} instances.
54    */

55   public Set JavaDoc getManagedInstances(){
56     return history;
57   }
58   /**
59    * Invoke a certain phase for all managed instances
60    * in the creation order.
61    * @param key the key of the phase.
62    * @param args the arguments passed to the phase procedure.
63    * @param handler the exception handler.
64    * @throws Throwable when any error happens.
65    */

66   public void fifo(Object JavaDoc key, Object JavaDoc[] args, ExceptionHandler handler)
67   throws Throwable JavaDoc{
68     final LiveObject[] los = new LiveObject[history.size()];
69     history.toArray(los);
70     for(int i=0; i<los.length; i++){
71       final LiveObject lo = los[i];
72       invokeOne(lo, key, args, handler);
73     }
74   }
75   /**
76    * Invoke a certain phase for all managed instances
77    * in the reverse order of creation.
78    * @param key the key of the phase.
79    * @param args the arguments passed to the phase procedure.
80    * @param handler the exception handler.
81    * @throws Throwable when any error happens.
82    */

83   public void filo(Object JavaDoc key, Object JavaDoc[] args, ExceptionHandler handler)
84   throws Throwable JavaDoc{
85     final LiveObject[] los = new LiveObject[history.size()];
86     history.toArray(los);
87     for(int i=0; i<los.length; i++){
88       final LiveObject lo = los[los.length-i-1];
89       invokeOne(lo, key, args, handler);
90     }
91   }
92   private static void invokeOne(LiveObject lo, Object JavaDoc key, Object JavaDoc[] args,
93       ExceptionHandler handler)
94   throws Throwable JavaDoc{
95     if(!lo.containsPhase(key)) return;
96     try{
97       lo.invoke(key, args);
98     }
99     catch(java.lang.reflect.InvocationTargetException JavaDoc e){
100       handleException(e.getTargetException(), handler);
101     }
102     catch(Error JavaDoc e){
103       throw e;
104     }
105     catch(Throwable JavaDoc e){
106       handleException(e, handler);
107     }
108   }
109   private static void handleException(Throwable JavaDoc e, ExceptionHandler handler)
110   throws Throwable JavaDoc{
111     handler.handle(e);
112   }
113 }
114
Popular Tags