KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jsp > cfg > TldFunction


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.jsp.cfg;
30
31 import com.caucho.config.ConfigException;
32 import com.caucho.config.types.Signature;
33 import com.caucho.util.L10N;
34
35 import javax.annotation.PostConstruct;
36 import javax.servlet.jsp.tagext.FunctionInfo JavaDoc;
37 import java.lang.reflect.Method JavaDoc;
38 import java.lang.reflect.Modifier JavaDoc;
39
40 /**
41  * Configuration for the taglib tag in the .tld
42  */

43 public class TldFunction {
44   private static L10N L = new L10N(TldFunction.class);
45   
46   private String JavaDoc _name;
47   private Class JavaDoc _functionClass;
48   private Signature _signature;
49   private String JavaDoc _displayName;
50   private String JavaDoc _description;
51   private String JavaDoc _example;
52
53   private Method JavaDoc _method;
54
55   /**
56    * Sets the function name.
57    */

58   public void setName(String JavaDoc name)
59   {
60     _name = name;
61   }
62
63   /**
64    * Gets the function name.
65    */

66   public String JavaDoc getName()
67   {
68     return _name;
69   }
70
71   /**
72    * Sets the function class.
73    */

74   public void setFunctionClass(Class JavaDoc functionClass)
75   {
76     _functionClass = functionClass;
77   }
78
79   /**
80    * Gets the function class.
81    */

82   public Class JavaDoc getFunctionClass()
83   {
84     return _functionClass;
85   }
86
87   /**
88    * Sets the function signature.
89    */

90   public void setFunctionSignature(Signature signature)
91   {
92     _signature = signature;
93   }
94
95   /**
96    * Gets the function signature.
97    */

98   public Signature getFunctionSignature()
99   {
100     return _signature;
101   }
102
103   /**
104    * Sets the display-name
105    */

106   public void setDisplayName(String JavaDoc displayName)
107   {
108     _displayName = displayName;
109   }
110
111   /**
112    * Gets the display-name
113    */

114   public String JavaDoc getDisplayName()
115   {
116     return _displayName;
117   }
118
119   /**
120    * Sets the description
121    */

122   public void setDescription(String JavaDoc description)
123   {
124     _description = description;
125   }
126
127   /**
128    * Gets the description
129    */

130   public String JavaDoc getDescription()
131   {
132     return _description;
133   }
134
135   /**
136    * Sets the example
137    */

138   public void setExample(String JavaDoc example)
139   {
140     _example = example;
141   }
142
143   /**
144    * Gets the example
145    */

146   public String JavaDoc getExample()
147   {
148     return _example;
149   }
150
151   /**
152    * Returns the underlying method.
153    */

154   public Method JavaDoc getMethod()
155   {
156     return _method;
157   }
158
159   /**
160    * Initialize
161    */

162   @PostConstruct
163   public void init()
164     throws ConfigException
165   {
166     if (_name == null)
167       throw new ConfigException(L.l("function needs <name>"));
168     
169     if (_signature == null)
170       throw new ConfigException(L.l("function requires <signature>"));
171     
172     Method JavaDoc []methods = _functionClass.getMethods();
173
174     loop:
175     for (int i = 0; i < methods.length; i++) {
176       Method JavaDoc method = methods[i];
177
178       if (! Modifier.isPublic(method.getModifiers()))
179         continue;
180       
181       if (Modifier.isAbstract(method.getModifiers()))
182         continue;
183
184       if (_signature.matches(method)) {
185         _method = method;
186
187         if (! Modifier.isPublic(_method.getModifiers()))
188           throw new ConfigException(L.l("function `{0}' must be public",
189                                           _method));
190         if (! Modifier.isStatic(_method.getModifiers()))
191           throw new ConfigException(L.l("function `{0}' must be static",
192                                           _method));
193         if (Modifier.isAbstract(_method.getModifiers()))
194           throw new ConfigException(L.l("function `{0}' must be concrete",
195                                           _method));
196         return;
197       }
198     }
199
200     throw new ConfigException(L.l("No public method in `{0}' matches the signature `{1}'",
201                                     _functionClass.getName(),
202                                     _signature.getSignature()));
203   }
204
205   public FunctionInfo JavaDoc toFunctionInfo()
206   {
207     return new FunctionInfo JavaDoc(_name,
208                 _functionClass.getName(),
209                 _signature.getSignature());
210   }
211
212   public String JavaDoc toString()
213   {
214     return "TldFunction[" + _signature.getSignature() + "]";
215   }
216 }
217
Popular Tags