KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > tags > databinding > invoke > CallMethod


1 /*
2  * Copyright 2004 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  * $Header:$
17  */

18 package org.apache.beehive.netui.tags.databinding.invoke;
19
20 import org.apache.beehive.netui.util.Bundle;
21 import org.apache.beehive.netui.util.internal.cache.MethodCache;
22
23 import javax.servlet.jsp.JspException JavaDoc;
24 import java.lang.reflect.Method JavaDoc;
25 import java.util.List JavaDoc;
26
27 /**
28  * <p/>
29  * An abstract base class for tags that are capable of reflectively
30  * invoking methods. Specializations of this tag provide method
31  * implementations that locate the object on which to invoke the
32  * method and that handle any return value from the invoked
33  * method.
34  * </p>
35  * <p/>
36  * The <code>CallMethod</code> tag can have child tags of type
37  * {@link MethodParameter}; these tags must be in the same
38  * order as the parameter list in the method signature of the
39  * method that will be invoked. To invoke an overloaded method, the
40  * {@link MethodParameter#setType(String)} property must be set to the String
41  * name of the type to pass to the method. If the type attribute values
42  * on nested {@link MethodParameter} tags do not match any method signature,
43  * an error will be reported in the page.
44  * </p>
45  *
46  * @jsptagref.tagdescription Calls methods on any Java classes.
47  * <p>The <code>controlId</code> attribute is used to specify the cclass to be called.
48  * The value returned is stored in the
49  * <code>{pageContext...}</code> data binding context object under the
50  * attribute specified by the <code>resultId</code>
51  * attribute.
52  * <p/>
53  * <p>For example, if you call a Java class with the following &lt;netui-data:callMethod> tag...
54  * <p/>
55  * <pre>
56  * &lt;netui-data:callMethod object="${pageFlow}" method="hello" resultId="helloMessage"/>
57  * </pre>
58  * <p/>
59  * <p>...the result of the call is stored in the <code>pageScope</code> data binding context under the
60  * attribute <code>helloMessage</code>.
61  * <p/>
62  * <p>The result can be retrieved with the data binding expression
63  * <code>${pageScope.helloMessage}</code>
64  * <p/>
65  * <pre> &lt;netui:span value="<b>${pageScope.helloMessage}</b>"/></pre>
66  * <p/>
67  * In a scriptlet, the result can be retrieved by calling the <code>getAttribute()</code>
68  * method on the
69  * {@link javax.servlet.jsp.PageContext javax.servlet.jsp.PageContext} object:
70  * <p/>
71  * <pre> &lt;%= pageContext.getAttribute("helloMessage") %></pre>
72  * <p/>
73  * @netui:tag name="callMethod" description="Use this tag to call a method on an object."
74  * @see MethodParameter
75  * @see CallPageFlow
76  * @see javax.servlet.jsp.PageContext
77  */

78 public class CallMethod
79     extends AbstractCallMethod {
80
81     private static final String JavaDoc DEFAULT_OBJECT_NAME = Bundle.getString("Tags_CallMethod_defaultObjectName");
82     private static final MethodCache _cache = new MethodCache();
83
84     private Object JavaDoc _object = null;
85
86     /**
87      * Get the name of this tag. This is used to identify the type of this tag
88      * for reporting tag errors.
89      *
90      * @return a constant String representing the name of this tag.
91      */

92     public String JavaDoc getTagName() {
93         return "CallMethod";
94     }
95
96     /**
97      * Set the object on which to invoke a method.
98      *
99      * @param object the object on which to invoke a method
100      * @jsptagref.attributedescription A string or data binding expression that names the class on which to call a method.
101      * @jsptagref.attributesyntaxvalue <i>string_or_expression_object</i>
102      * @netui:attribute required="false" rtexprvalue="true"
103      */

104     public void setObject(Object JavaDoc object) {
105         _object = object;
106     }
107
108     /**
109      * Reset all of the fields of this tag.
110      */

111     protected void localRelease() {
112         super.localRelease();
113         _object = null;
114     }
115
116     protected Object JavaDoc resolveObject()
117             throws ObjectNotFoundException, JspException JavaDoc {
118         return _object;
119     }
120
121     protected final Method JavaDoc findMethod(Object JavaDoc target, String JavaDoc methodName, boolean verifyTypes) {
122         List JavaDoc params = getParameterNodes();
123
124         if(verifyTypes) {
125             String JavaDoc[] argTypes = null;
126             argTypes = new String JavaDoc[params.size()];
127             for(int i = 0; i < argTypes.length; i++) {
128                 argTypes[i] = ((ParamNode)params.get(i)).typeName;
129             }
130             return _cache.getMethod(target.getClass(), methodName, argTypes);
131         }
132         else
133             return _cache.getMethod(target.getClass(), methodName, params.size());
134     }
135
136     protected String JavaDoc getObjectName() {
137         return DEFAULT_OBJECT_NAME;
138     }
139 }
140
Popular Tags