001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.jsp.cfg;
030:
031: import com.caucho.config.ConfigException;
032: import com.caucho.config.types.Signature;
033: import com.caucho.util.L10N;
034:
035: import javax.annotation.PostConstruct;
036: import javax.servlet.jsp.tagext.FunctionInfo;
037: import java.lang.reflect.Method;
038: import java.lang.reflect.Modifier;
039:
040: /**
041: * Configuration for the taglib tag in the .tld
042: */
043: public class TldFunction {
044: private static L10N L = new L10N(TldFunction.class);
045:
046: private String _name;
047: private Class _functionClass;
048: private Signature _signature;
049: private String _displayName;
050: private String _description;
051: private String _example;
052:
053: private Method _method;
054:
055: /**
056: * Sets the function name.
057: */
058: public void setName(String name) {
059: _name = name;
060: }
061:
062: /**
063: * Gets the function name.
064: */
065: public String getName() {
066: return _name;
067: }
068:
069: /**
070: * Sets the function class.
071: */
072: public void setFunctionClass(Class functionClass) {
073: _functionClass = functionClass;
074: }
075:
076: /**
077: * Gets the function class.
078: */
079: public Class getFunctionClass() {
080: return _functionClass;
081: }
082:
083: /**
084: * Sets the function signature.
085: */
086: public void setFunctionSignature(Signature signature) {
087: _signature = signature;
088: }
089:
090: /**
091: * Gets the function signature.
092: */
093: public Signature getFunctionSignature() {
094: return _signature;
095: }
096:
097: /**
098: * Sets the display-name
099: */
100: public void setDisplayName(String displayName) {
101: _displayName = displayName;
102: }
103:
104: /**
105: * Gets the display-name
106: */
107: public String getDisplayName() {
108: return _displayName;
109: }
110:
111: /**
112: * Sets the description
113: */
114: public void setDescription(String description) {
115: _description = description;
116: }
117:
118: /**
119: * Gets the description
120: */
121: public String getDescription() {
122: return _description;
123: }
124:
125: /**
126: * Sets the example
127: */
128: public void setExample(String example) {
129: _example = example;
130: }
131:
132: /**
133: * Gets the example
134: */
135: public String getExample() {
136: return _example;
137: }
138:
139: /**
140: * Returns the underlying method.
141: */
142: public Method getMethod() {
143: return _method;
144: }
145:
146: /**
147: * Initialize
148: */
149: @PostConstruct
150: public void init() throws ConfigException {
151: if (_name == null)
152: throw new ConfigException(L.l("function needs <name>"));
153:
154: if (_signature == null)
155: throw new ConfigException(L
156: .l("function requires <signature>"));
157:
158: Method[] methods = _functionClass.getMethods();
159:
160: loop: for (int i = 0; i < methods.length; i++) {
161: Method method = methods[i];
162:
163: if (!Modifier.isPublic(method.getModifiers()))
164: continue;
165:
166: if (Modifier.isAbstract(method.getModifiers()))
167: continue;
168:
169: if (_signature.matches(method)) {
170: _method = method;
171:
172: if (!Modifier.isPublic(_method.getModifiers()))
173: throw new ConfigException(L.l(
174: "function `{0}' must be public", _method));
175: if (!Modifier.isStatic(_method.getModifiers()))
176: throw new ConfigException(L.l(
177: "function `{0}' must be static", _method));
178: if (Modifier.isAbstract(_method.getModifiers()))
179: throw new ConfigException(L.l(
180: "function `{0}' must be concrete", _method));
181: return;
182: }
183: }
184:
185: throw new ConfigException(
186: L
187: .l(
188: "No public method in `{0}' matches the signature `{1}'",
189: _functionClass.getName(), _signature
190: .getSignature()));
191: }
192:
193: public FunctionInfo toFunctionInfo() {
194: return new FunctionInfo(_name, _functionClass.getName(),
195: _signature.getSignature());
196: }
197:
198: public String toString() {
199: return "TldFunction[" + _signature.getSignature() + "]";
200: }
201: }
|