KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.Serializable JavaDoc;
18 import java.util.Collection JavaDoc;
19 import java.util.Collections JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.LinkedHashMap JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.Set JavaDoc;
24
25 /**
26  * <p>
27  * This class represents the entire life-cycle for a component.
28  * </p>
29  * Zephyr Business Solution
30  *
31  * @author Michelle Lei
32  *
33  */

34 public class Lifecycle implements Life, Serializable JavaDoc{
35   private final Map JavaDoc entries = new LinkedHashMap JavaDoc();
36   /**
37    * This class encapsulates the "what-to-do" information
38    * for a life-cycle phase.
39    */

40   public static class Entry implements Serializable JavaDoc{
41     private final Procedure proc;
42     private final boolean reentrant;
43     /**
44      * To create a Entry object.
45      * @param proc the Procedure object to invoke for the phase.
46      * @param reentrant whether the Procedure is re-entrant.
47      * If not re-entrant, the second time the procedure is called
48      * for the same object, it does nothing.
49      */

50     public Entry(Procedure proc, boolean reentrant) {
51       this.proc = proc;
52       this.reentrant = reentrant;
53     }
54     /**
55      * To get the procedure to invoke.
56      */

57     public Procedure getProcedure() {
58       return proc;
59     }
60     /**
61      * Is it re-entrant?
62      */

63     public boolean isReentrant() {
64       return reentrant;
65     }
66     public boolean equals(Object JavaDoc obj) {
67       if(obj instanceof Entry){
68         Entry other = (Entry)obj;
69         return reentrant==other.reentrant && proc.equals(other.proc);
70       }
71       else return false;
72     }
73     public int hashCode() {
74       return reentrant?1:0+proc.hashCode()*31;
75     }
76     public String JavaDoc toString() {
77       return proc.toString();
78     }
79   }
80   /**
81    * Is any life-cycle phase defined at all?
82    */

83   public boolean isEmpty(){
84     return entries.isEmpty();
85   }
86   /**
87    * To get the number of phases defined.
88    */

89   public int size(){
90     return entries.size();
91   }
92   /**
93    * To add a phase.
94    * @param key the key of the phase.
95    * @param proc the procedure to invoke for the phase.
96    * @param reentrant is the procedure re-entrant?
97    */

98   public void put(Object JavaDoc key, Procedure proc, boolean reentrant){
99     entries.put(key, new Entry(proc, reentrant));
100   }
101   /**
102    * To remove a phase definition.
103    * @param key the key of the phase.
104    */

105   public void remove(Object JavaDoc key){
106     entries.remove(key);
107   }
108   /**
109    * To get a phase entry.
110    * @param key the key of the phase.
111    * @return the Entry object.
112    */

113   public Entry get(Object JavaDoc key){
114     return (Entry)entries.get(key);
115   }
116   /**
117    * To get all the phase keys.
118    */

119   public Set JavaDoc keys(){
120     return entries.keySet();
121   }
122   /**
123    * To get all the entries.
124    */

125   public Collection JavaDoc entries(){
126     return Collections.unmodifiableCollection(entries.values());
127   }
128   public LiveObject bear(Object JavaDoc obj){
129     final Map JavaDoc invokables = new LinkedHashMap JavaDoc(entries.size());
130     for(Iterator JavaDoc it=entries.keySet().iterator(); it.hasNext();){
131       final Object JavaDoc key = it.next();
132       final Entry entry = get(key);
133       final Procedure proc = entry.getProcedure();
134       invokables.put(key, entry.isReentrant()?proc:new NonReentrantProc(proc));
135     }
136     return new LiveObject(obj, invokables);
137   }
138   private static class NonReentrantProc implements Procedure{
139     public synchronized void invoke(Object JavaDoc self, Object JavaDoc[] args) throws Throwable JavaDoc {
140       if(visited)
141         return;
142       visited = true;
143       proc.invoke(self, args);
144     }
145     public String JavaDoc toString() {
146       return proc.toString();
147     }
148     private final Procedure proc;
149     private boolean visited = false;
150     NonReentrantProc(Procedure proc) {
151       this.proc = proc;
152     }
153   }
154 }
155
Popular Tags