KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > mvc > controller > BeanHandle


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.verge.mvc.controller;
8
9
10 import java.lang.reflect.Method JavaDoc;
11
12 import com.inversoft.beans.BeanException;
13 import com.inversoft.beans.JavaBeanTools;
14 import com.inversoft.util.ReflectionException;
15 import com.inversoft.util.ReflectionTools;
16
17
18 /**
19  * <p>
20  * This class allows reflective access to the handle methods
21  * used by the Inversoft controller components. This is like
22  * BeanProperty in that it only models a single method in a
23  * single class.
24  * </p>
25  *
26  * @author Brian Pontarelli
27  * @since 2.0
28  * @version 2.0
29  */

30 public class BeanHandle {
31
32     String JavaDoc handleName;
33     Class JavaDoc beanClass;
34     Class JavaDoc[] paramTypes;
35     Method JavaDoc method;
36
37
38     /**
39      * Constructs a new BeanHandle object for the given bean class and handle
40      * name.
41      *
42      * @param handleName The name of the handle method
43      * @param beanClass The bean class object
44      * @throws BeanException If there was any problem creating the BeanHandle
45      */

46     public BeanHandle(String JavaDoc handleName, Class JavaDoc beanClass, Class JavaDoc[] paramTypes)
47     throws BeanException {
48         this.handleName = handleName;
49         this.beanClass = beanClass;
50         this.paramTypes = paramTypes;
51
52         initialize();
53     }
54
55
56     /**
57      * Locates the the BeanHandle method.
58      *
59      * @throws BeanException If the method can not be found
60      */

61     protected void initialize() throws BeanException {
62         String JavaDoc handleMethodName = JavaBeanTools.makeHandle(handleName);
63         method = ReflectionTools.findMethod(beanClass, handleMethodName,
64             paramTypes, 0);
65         if (method == null) {
66             throw new BeanException("Invalid handle method: " + handleMethodName);
67         }
68     }
69
70     /**
71      * Returns the type of object(s) that are passed to the handle method.
72      *
73      * @return The parameter types as an array of Class objects
74      */

75     protected Class JavaDoc [] getParamTypes() {
76         return paramTypes;
77     }
78
79     /**
80      * Returns the method for this handle reference
81      *
82      * @return The handle method
83      */

84     public Method JavaDoc getHandleMethod() {
85         return method;
86     }
87
88     /**
89      * Returns the name of the handle method of the bean
90      *
91      * @return The name of the handle method (JavaBean property style)
92      */

93     public String JavaDoc getHandleName() {
94         return handleName;
95     }
96
97     /**
98      * Invokes the handle method for this handle reference with the given
99      * parameters
100      *
101      * @param bean The bean instance to invoke the handle method on
102      * @param params The parameters to pass to the handle method
103      * @return The return value from the handle method if there was any. This
104      * could be any Object include primitive wrappers and arrays
105      * @throws BeanException If the handle method threw any other exception or
106      * if there was a problem invoking the method
107      */

108     public Object JavaDoc invokeHandle(Object JavaDoc bean, Object JavaDoc[] params) throws BeanException {
109
110         try {
111             return ReflectionTools.invokeMethod(method, bean, params);
112         } catch (ReflectionException re) {
113
114             Throwable JavaDoc target = re.getTarget();
115             if (target == null) {
116                 throw new BeanException(re.getMessage());
117             } else if (target instanceof RuntimeException JavaDoc) {
118                 throw (RuntimeException JavaDoc) target;
119             } else if (target instanceof Error JavaDoc) {
120                 throw (Error JavaDoc) target;
121             } else {
122                 throw new BeanException(target);
123             }
124         }
125     }
126 }
Popular Tags