KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > gumby > tags > New


1 package org.sapia.gumby.tags;
2
3 import java.lang.reflect.Constructor JavaDoc;
4 import java.util.ArrayList JavaDoc;
5 import java.util.List JavaDoc;
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 /**
15  * @author Yanick Duchesne
16  *
17  * <dl>
18  * <dt><b>Copyright: </b>
19  * <dd>Copyright &#169; 2002-2005 <a HREF="http://www.sapia-oss.org">Sapia Open
20  * Source Software </a>. All Rights Reserved.</dd>
21  * </dt>
22  * <dt><b>License: </b>
23  * <dd>Read the license.txt file of the jar or visit the <a
24  * HREF="http://www.sapia-oss.org/license.html">license page </a> at the Sapia
25  * OSS web site</dd>
26  * </dt>
27  * </dl>
28  */

29 public class New implements ContextAware, ObjectCreationCallback,
30     ObjectWrapperIF {
31
32   private Class JavaDoc _class;
33   private List JavaDoc _args = new ArrayList JavaDoc();
34   private Object JavaDoc _created;
35   private RenderContext _context;
36
37   public void setClass(String JavaDoc name) throws Exception JavaDoc {
38     _class = _context.getSettings().resolveClass(name);
39     if(_class == null) {
40       throw new ClassNotFoundException JavaDoc(name);
41     }
42   }
43
44   public Arg createArg() {
45     Arg arg = new Arg();
46     _args.add(arg);
47     return arg;
48   }
49
50   /**
51    * @see org.sapia.gumby.factory.ContextAware#handleContext(org.sapia.gumby.RenderContext)
52    */

53   public void handleContext(RenderContext context) {
54     _context = context;
55   }
56
57   /**
58    * @see org.sapia.util.xml.confix.ObjectWrapperIF#getWrappedObject()
59    */

60   public Object JavaDoc getWrappedObject() {
61     try {
62       return onCreate();
63     } catch(ConfigurationException e) {
64       throw new RuntimeException JavaDoc("Could not instantiate object", e);
65     }
66   }
67
68   /**
69    * @see org.sapia.util.xml.confix.ObjectCreationCallback#onCreate()
70    */

71   public Object JavaDoc onCreate() throws ConfigurationException {
72     if(_created == null) {
73       if(_class == null) {
74         throw new ConfigurationException("'class' not specified");
75       }
76       Class JavaDoc[] types = new Class JavaDoc[_args.size()];
77       Object JavaDoc[] params = new Object JavaDoc[_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 JavaDoc cons = _class.getConstructor(types);
86         _created = cons.newInstance(params);
87       } catch(Exception JavaDoc 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