1 package jfun.yan.xml.nuts; 2 3 4 import java.util.ArrayList ; 5 import java.util.List ; 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 25 public class Util { 26 private static void assertMethodName(Component c, String mname, 27 Location loc){ 28 final Class type = c.getType(); 29 if(type!=null){ 30 try{ 31 type.getMethod(mname, null); 32 } 33 catch(NoSuchMethodException e){ 34 throw new ConfigurationException("lifecycle method " 35 + mname + " not defined by "+ Misc.getTypeName(type), 36 loc); 37 } 38 } 39 } 40 50 private static Component wrapLifecycle( 51 Component c, Location loc, 52 DefaultLifecycleManager man, 53 String starter, String stopper, String disposer){ 54 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 84 public static Component wrapLifecycle( 85 Component c, Location loc, 86 DefaultLifecycleManager man, 87 String initializer, String starter, String stopper, String disposer){ 88 if(initializer != null){ 89 assertMethodName(c, initializer, loc); 90 Class 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 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 116 public static Component convert(final Nut nut, 117 final Class elem_type, final Component c){ 118 final Class 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 map(Object 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 toString(){ 133 return c.toString(); 134 } 135 }); 136 } 137 145 public static Component[] convert(Nut nut, 146 final Class 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 |