KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > io > BeanHelper


1 /*
2  * Copyright 1999,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
17 package org.apache.taglibs.io;
18
19 import java.beans.BeanInfo JavaDoc;
20 import java.beans.Introspector JavaDoc;
21 import java.beans.IntrospectionException JavaDoc;
22 import java.beans.PropertyDescriptor JavaDoc;
23 import java.lang.reflect.Method JavaDoc;
24 import java.io.BufferedReader JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.io.InputStreamReader JavaDoc;
28 import java.io.OutputStream JavaDoc;
29 import java.io.OutputStreamWriter JavaDoc;
30 import java.io.StringReader JavaDoc;
31 import java.io.Reader JavaDoc;
32 import java.io.Writer JavaDoc;
33
34 import javax.servlet.jsp.JspException JavaDoc;
35 import javax.servlet.jsp.tagext.Tag JavaDoc;
36 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
37
38 /** A collection of bean and introspection helper methods.
39   *
40   * @author <a HREF="mailto:james.strachan@metastuff.com">James Strachan</a>
41   * @version $Revision: 1.2 $
42   */

43 public class BeanHelper {
44
45     protected static final Object JavaDoc[] NULL_ARGUMENTS = {};
46     
47     /** Gets the value of the given property
48       *
49       * @param bean is the JavaBean which contains the property
50       * @param propertyName is the name of the property to set
51       *
52       * @return the value of the given property
53       */

54     public static Object JavaDoc getProperty( Object JavaDoc bean, String JavaDoc propertyName ) throws JspException JavaDoc {
55         try {
56             PropertyDescriptor JavaDoc descriptor
57                 = getPropertyDescriptor( bean, propertyName );
58             Method JavaDoc method = descriptor.getReadMethod();
59             return method.invoke( bean, NULL_ARGUMENTS );
60         }
61         catch (Exception JavaDoc e) {
62             throw new JspException JavaDoc(
63                 "Failed to get property: " + propertyName
64                 + " on bean: " + bean + ". Exception: " + e );
65         }
66     }
67     
68     /** Sets the value of the given property
69       *
70       * @param bean is the JavaBean which contains the property
71       * @param propertyName is the name of the property to set
72       * @param value is the value of the property to set
73       *
74       * @return true if the property was set else false if it could not be set
75       */

76     public static void setProperty( Object JavaDoc bean, String JavaDoc propertyName, Object JavaDoc value ) throws JspException JavaDoc {
77         try {
78             PropertyDescriptor JavaDoc descriptor
79                 = getPropertyDescriptor( bean, propertyName );
80             Method JavaDoc method = descriptor.getWriteMethod();
81             Object JavaDoc[] arguments = { value };
82             method.invoke( bean, arguments );
83         }
84         catch (Exception JavaDoc e) {
85             throw new JspException JavaDoc(
86                 "Failed to set property: " + propertyName
87                 + " on bean: " + bean + ". Exception: " + e );
88         }
89     }
90     
91     /** @return a PropertyDescriptor for the given bean and property name
92       */

93     protected static PropertyDescriptor JavaDoc getPropertyDescriptor(
94         Object JavaDoc bean,
95         String JavaDoc propertyName
96     ) throws JspException JavaDoc {
97         try {
98             Class JavaDoc beanClass = bean.getClass();
99             BeanInfo JavaDoc beanInfo = Introspector.getBeanInfo( beanClass );
100
101             // Its a shame the JDK doesn't have a helper method on BeanInfo for this
102
PropertyDescriptor JavaDoc[] descriptors = beanInfo.getPropertyDescriptors();
103             if ( descriptors != null ) {
104                 int size = descriptors.length;
105                 for ( int i = 0; i < size; i++ ) {
106                     PropertyDescriptor JavaDoc descriptor = descriptors[i];
107                     String JavaDoc name = descriptor.getName();
108                     if ( propertyName.equals( name ) ) {
109                         return descriptor;
110                     }
111                 }
112             }
113             throw new JspException JavaDoc(
114                 "No such property: " + propertyName + " on bean: " + bean
115             );
116         }
117         catch (IntrospectionException JavaDoc e) {
118             throw new JspException JavaDoc(
119                 "Failed to instrospect bean: " + bean + ". Exception: " + e
120             );
121         }
122     }
123 }
124
Popular Tags