KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > velocity > tools > view > ViewToolInfo


1 /*
2  * Copyright 2003 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.velocity.tools.view;
18
19 import org.apache.velocity.tools.view.tools.ViewTool;
20 import org.apache.velocity.app.Velocity;
21
22 /**
23  * ToolInfo implementation for view tools. New instances
24  * are returned for every call to getInstance(obj), and tools
25  * that implement {@link ViewTool} are initialized with the
26  * given object before being returned.
27  *
28  * @author <a HREF="mailto:nathan@esha.com">Nathan Bubna</a>
29  *
30  * @version $Id: ViewToolInfo.java,v 1.7.2.1 2004/03/12 20:16:28 nbubna Exp $
31  */

32 public class ViewToolInfo implements ToolInfo
33 {
34
35     private String JavaDoc key;
36     private Class JavaDoc clazz;
37     private boolean initializable = false;
38
39
40     public ViewToolInfo() {}
41
42
43     //TODO: if classloading becomes needed elsewhere, move this to a utils class
44
/**
45      * Return the <code>Class</code> object for the specified fully qualified
46      * class name, from this web application's class loader. If no
47      * class loader is set for the current thread, then the class loader
48      * that loaded this class will be used.
49      *
50      * @param name Fully qualified class name to be loaded
51      * @return Class object
52      * @exception ClassNotFoundException if the class cannot be found
53      * @since VelocityTools 1.1
54      */

55     protected Class JavaDoc getApplicationClass(String JavaDoc name) throws ClassNotFoundException JavaDoc
56     {
57         ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
58         if (loader == null)
59         {
60             loader = ViewToolInfo.class.getClassLoader();
61         }
62         return loader.loadClass(name);
63     }
64
65
66     /*********************** Mutators *************************/
67
68     public void setKey(String JavaDoc key)
69     {
70         this.key = key;
71     }
72
73
74     /**
75      * If an instance of the tool cannot be created from
76      * the classname passed to this method, it will throw an exception.
77      *
78      * @param classname the fully qualified java.lang.Class of the tool
79      */

80     public void setClassname(String JavaDoc classname) throws Exception JavaDoc
81     {
82         this.clazz = getApplicationClass(classname);
83         /* create an instance and see if it is initializable */
84         if (clazz.newInstance() instanceof ViewTool)
85         {
86             this.initializable = true;
87         }
88     }
89
90
91     /*********************** Accessors *************************/
92
93     public String JavaDoc getKey()
94     {
95         return key;
96     }
97
98
99     public String JavaDoc getClassname()
100     {
101         return clazz.getName();
102     }
103
104
105     /**
106      * Returns a new instance of the tool. If the tool
107      * implements {@link ViewTool}, the new instance
108      * will be initialized using the given data.
109      */

110     public Object JavaDoc getInstance(Object JavaDoc initData)
111     {
112         Object JavaDoc tool = null;
113         try
114         {
115             tool = clazz.newInstance();
116         }
117         /* we shouldn't get exceptions here because we already
118          * got an instance of this class during setClassname().
119          * but to be safe, let's catch the declared ones and give
120          * notice of them, and let other exceptions slip by. */

121         catch (IllegalAccessException JavaDoc e)
122         {
123             Velocity.error("Exception while instantiating instance of \"" +
124                            getClassname() + "\": " + e);
125         }
126         catch (InstantiationException JavaDoc e)
127         {
128             Velocity.error("Exception while instantiating instance of \"" +
129                            getClassname() + "\": " + e);
130         }
131         
132         if (initializable) {
133             ((ViewTool)tool).init(initData);
134         }
135         return tool;
136     }
137
138 }
139
Popular Tags