1 package org.sapia.gumby.tags; 2 3 import java.lang.reflect.Constructor ; 4 import java.util.ArrayList ; 5 import java.util.List ; 6 7 import org.sapia.gumby.RenderContext; 8 import org.sapia.gumby.factory.ContextAware; 9 import org.sapia.gumby.tags.support.Arg; 10 import org.sapia.util.xml.confix.ConfigurationException; 11 import org.sapia.util.xml.confix.ObjectCreationCallback; 12 import org.sapia.util.xml.confix.ObjectWrapperIF; 13 14 29 public class New implements ContextAware, ObjectCreationCallback, 30 ObjectWrapperIF { 31 32 private Class _class; 33 private List _args = new ArrayList (); 34 private Object _created; 35 private RenderContext _context; 36 37 public void setClass(String name) throws Exception { 38 _class = _context.getSettings().resolveClass(name); 39 if(_class == null) { 40 throw new ClassNotFoundException (name); 41 } 42 } 43 44 public Arg createArg() { 45 Arg arg = new Arg(); 46 _args.add(arg); 47 return arg; 48 } 49 50 53 public void handleContext(RenderContext context) { 54 _context = context; 55 } 56 57 60 public Object getWrappedObject() { 61 try { 62 return onCreate(); 63 } catch(ConfigurationException e) { 64 throw new RuntimeException ("Could not instantiate object", e); 65 } 66 } 67 68 71 public Object onCreate() throws ConfigurationException { 72 if(_created == null) { 73 if(_class == null) { 74 throw new ConfigurationException("'class' not specified"); 75 } 76 Class [] types = new Class [_args.size()]; 77 Object [] params = new Object [_args.size()]; 78 Arg arg; 79 for(int i = 0; i < _args.size(); i++) { 80 arg = (Arg) _args.get(i); 81 types[i] = arg.getType(); 82 params[i] = arg.getValue(); 83 } 84 try { 85 Constructor cons = _class.getConstructor(types); 86 _created = cons.newInstance(params); 87 } catch(Exception e) { 88 e.printStackTrace(); 89 throw new ConfigurationException("Could not instantiate object from: " 90 + _class); 91 } 92 93 } 94 return _created; 95 } 96 97 } 98 | Popular Tags |