KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jfun > yan > xml > nuts > LifecycleNut


1 package jfun.yan.xml.nuts;
2
3 import java.util.Locale JavaDoc;
4
5 import jfun.yan.Component;
6 import jfun.yan.lifecycle.DefaultLifecycleDescriptor;
7 import jfun.yan.lifecycle.Lifecycle;
8 import jfun.yan.lifecycle.LifecycleManager;
9
10
11 /**
12  * The <lifecycle> tag.
13  * For example:
14  * <pre>
15  * &lt;lifecycle name="disposer" reentrant="true"&gt;
16  * &lt;method class="..." name="..." args="..."/&gt;
17  * &lt;lifecycle&gt;
18  * </pre>
19  * <p>
20  * @author Ben Yu
21  * Nov 28, 2005 10:27:58 AM
22  */

23 public class LifecycleNut extends DelegatingNut {
24   private String JavaDoc name;
25   private boolean reentrant;
26   private boolean reentrant_set = false;
27   public String JavaDoc getName() {
28     return name;
29   }
30
31   public void setName(String JavaDoc name) {
32     this.name = name;
33   }
34
35   public boolean isReentrant() {
36     return reentrant;
37   }
38
39   public void setReentrant(boolean reentrant) {
40     this.reentrant = reentrant;
41     this.reentrant_set = true;
42   }
43
44   public Component eval(){
45     checkMandatory("name", name);
46     final Component target = getMandatory();
47     final String JavaDoc phase = name.toLowerCase(Locale.US)
48       .trim();
49     if(DefaultLifecycleDescriptor
50         .DEFAULT_INITIALIZER_NAME.equals(phase)){
51       //initializer runs right away.
52
return target;
53     }
54     else{
55       //The object identity of the factory determines if this factory
56
//can be called multiple times by the lifecycle manager.
57
//so cannot use singleton even if it carries no state.
58
final Component factory =
59         target.factory();
60       final LifecycleManager man = super.getNutEnvironment()
61       .getLifecycleManager();
62       final Lifecycle lifecycle = new Lifecycle();
63       lifecycle.put(phase, FactoryProcedure.instance(), isReentrant(phase));
64       return man.withLifecycle(factory, lifecycle);
65     }
66   }
67   private boolean isReentrant(String JavaDoc phase){
68     if(reentrant_set){
69       return reentrant;
70     }
71     return !DefaultLifecycleDescriptor.DEFAULT_DISPOSER_NAME
72         .equals(phase);
73   }
74 }
75
Popular Tags