KickJava   Java API By Example, From Geeks To Geeks.

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


1 package jfun.yan.xml.nuts;
2
3
4 import java.util.ArrayList JavaDoc;
5 import java.util.List JavaDoc;
6
7 import jfun.util.Misc;
8 import jfun.yan.Component;
9 import jfun.yan.Components;
10 import jfun.yan.Map;
11 import jfun.yan.lifecycle.DefaultLifecycleManager;
12 import jfun.yan.util.ReflectionUtil;
13 import jfun.yan.xml.ConfigurationException;
14 import jfun.yan.xml.Location;
15 import jfun.yan.xml.nut.Nut;
16
17
18 /**
19  * Common utility function that may worth being reused
20  * by various Nut implementations.
21  * <p>
22  * @author Ben Yu
23  * Nov 10, 2005 12:31:52 AM
24  */

25 public class Util {
26   private static void assertMethodName(Component c, String JavaDoc mname,
27       Location loc){
28     final Class JavaDoc type = c.getType();
29     if(type!=null){
30       try{
31         type.getMethod(mname, null);
32       }
33       catch(NoSuchMethodException JavaDoc e){
34         throw new ConfigurationException("lifecycle method "
35             + mname + " not defined by "+ Misc.getTypeName(type),
36             loc);
37       }
38     }
39   }
40   /**
41    * Create a Component that adds lifecycle support.
42    * @param c the Component to add lifecycle to.
43    * @param loc the location of the Component in the source file.
44    * @param man the lifecycle manager.
45    * @param starter the starter method name.
46    * @param stopper the stopper method name.
47    * @param disposer the disposer method name.
48    * @return the Component with lifecycle.
49    */

50   private static Component wrapLifecycle(
51       Component c, Location loc,
52       DefaultLifecycleManager man,
53       String JavaDoc starter, String JavaDoc stopper, String JavaDoc disposer){
54     //we do initialization before wiring. So skip the default lifecycle manager
55
//for initializers.
56
final DefaultLifecycleManager.DefaultLifecycle lifecycle =
57       man.newLifecycle();
58     if(disposer != null){
59       assertMethodName(c, disposer, loc);
60       lifecycle.disposer(disposer);
61     }
62     if(starter != null){
63       assertMethodName(c, starter, loc);
64       lifecycle.starter(starter);
65     }
66     if(stopper != null){
67       assertMethodName(c, stopper, loc);
68       lifecycle.stopper(stopper);
69     }
70     c = lifecycle.manage(c);
71     return c;
72   }
73   /**
74    * Create a Component that adds lifecycle support.
75    * @param c the Component to add lifecycle to.
76    * @param loc the location of the Component in the source file.
77    * @param man the lifecycle manager.
78    * @param initializer the initializer method name. Null if not available.
79    * @param starter the starter method name. Null if not available.
80    * @param stopper the stopper method name. Null if not available.
81    * @param disposer the disposer method name. Null if not available.
82    * @return the Component with lifecycle.
83    */

84   public static Component wrapLifecycle(
85       Component c, Location loc,
86       DefaultLifecycleManager man,
87       String JavaDoc initializer, String JavaDoc starter, String JavaDoc stopper, String JavaDoc disposer){
88     if(initializer != null){
89       assertMethodName(c, initializer, loc);
90       Class JavaDoc realtype = c.isConcrete()?c.getType():null;
91       c = c.followedBy(
92           Components.invokingMethod(realtype, initializer, null, false));
93     }
94     return wrapLifecycle(c, loc, man, starter, stopper, disposer);
95   }
96   /**
97    * Create a Component that adds lifecycle support.
98    * @param c the Component to add lifecycle to.
99    * @param ln the LifecycleNut object.
100    * @return the Component with life cycle.
101    */

102   public static Component wrapLifecycle(
103       Component c, LifecycleDeclaration ln){
104     return wrapLifecycle(c, ln.getTagLocation(),
105         ln.getNutEnvironment().getLifecycleManager(),
106         ln.getInitializer(), ln.getStarter(), ln.getStopper(), ln.getDisposer()
107         );
108   }
109   /**
110    * Convert a Component so that the instantiation result is of a certain type.
111    * @param nut the Nut object that does the conversion.
112    * @param elem_type the element type.
113    * @param c the Component to convert.
114    * @return the new Component object.
115    */

116   public static Component convert(final Nut nut,
117       final Class JavaDoc elem_type, final Component c){
118     final Class JavaDoc type = c.getType();
119     if(type!=null && ReflectionUtil.isAssignableFrom(elem_type, type)){
120       return c;
121     }
122     return c.map(new Map(){
123       public Object JavaDoc map(Object JavaDoc obj){
124         try{
125           return nut.convert(elem_type, obj);
126         }
127         catch(ConfigurationException e){
128           throw new ConfigurationException(
129               e.getMessage() + ": "+ c, e, e.getLocation());
130         }
131       }
132       public String JavaDoc toString(){
133         return c.toString();
134       }
135     });
136   }
137   /**
138    * Convert an array of Component object so that the instantiation results
139    * are of a certain type.
140    * @param nut the Nut object to perform the conversion.
141    * @param elem_type the element type.
142    * @param elems the array of Components.
143    * @return the new array of Components.
144    */

145   public static Component[] convert(Nut nut,
146       final Class JavaDoc elem_type, Component[] elems){
147     final Component[] ret = new Component[elems.length];
148     for(int i=0; i<ret.length; i++){
149       ret[i] = convert(nut, elem_type, elems[i]);
150     }
151     return ret;
152   }
153
154 }
155
Popular Tags