1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 package org.objectledge.parameters;
30
31 import java.beans.IndexedPropertyDescriptor;
32 import java.beans.Introspector;
33 import java.beans.PropertyDescriptor;
34 import java.lang.reflect.Method;
35 import java.math.BigDecimal;
36 import java.util.Date;
37
38 /***
39 * Uses bean introspection to set writable properties of bean from
40 * the parameters.
41 *
42 * <p>This class includes code from Jakarta Turbine project and parts of it are subject to
43 * Apache Software Foundation License included in ObjectLedge distribution
44 * (file /ASFLicense.txt).</p>
45 *
46 * @author <a href="maito:mgolebsk@elka.pw.edu.pl">Marcin Golebski</a>
47 * @created 2005-07-26
48 * @version $Id: RequestParametersUtils.java,v 1.1 2005/08/26 07:14:15 zwierzem Exp $
49 */
50 public class RequestParametersUtils
51 {
52 /***
53 * Uses bean introspection to set writable properties of bean from
54 * the parameters, where a (case-insensitive) name match between
55 * the bean property and the parameter is looked for.
56 *
57 * @param bean An Object.
58 * @throws Exception
59 * @exception Exception a generic exception.
60 */
61 public static void setProperties(RequestParameters params, Object bean)
62 throws Exception
63 {
64
65 Class beanClass = bean.getClass();
66 PropertyDescriptor[] props
67 = Introspector.getBeanInfo(beanClass).getPropertyDescriptors();
68
69 for (int i = 0; i < props.length; i++)
70 {
71 String propname = props[i].getName();
72 Method setter = props[i].getWriteMethod();
73 if (setter != null &&
74 (containsKey(params, propname) ))
75
76
77
78
79 {
80 setProperty(params, bean, props[i]);
81 }
82 }
83 }
84
85
86 /***
87 * Uses bean introspection to set writable properties of bean from
88 * the parameters, where a (case-insensitive) name match between
89 * the bean property and the parameter is looked for.
90 *
91 * @param bean An Object.
92 * @throws Exception
93 * @exception Exception a generic exception.
94 */
95
96 private static void setPropertiesForSearch(RequestParameters params, Object bean)
97 throws Exception
98 {
99
100 Class beanClass = bean.getClass();
101 PropertyDescriptor[] props
102 = Introspector.getBeanInfo(beanClass).getPropertyDescriptors();
103
104 for (int i = 0; i < props.length; i++)
105 {
106 String propname = props[i].getName();
107 Method setter = props[i].getWriteMethod();
108 if (setter != null &&
109 containsKey(params, propname) )
110 {
111 setProperty(params, bean, props[i]);
112 }
113 }
114 }
115
116
117 /***
118 * Determine whether a given key has been inserted. All keys are
119 * stored in lowercase strings, so override method to account for
120 * this.
121 *
122 * @param key An Object with the key to search for.
123 * @return True if the object is found.
124 */
125 private static boolean containsKey(RequestParameters params, Object key)
126 {
127 String tmp = convert((String)key);
128 return params.isDefined(tmp) && !params.get(tmp).equals("");
129 }
130
131 /***
132 * Trims the string data and applies the conversion specified in
133 * the property given by URL_CASE_FOLDING. It returns a new
134 * string so that it does not destroy the value data.
135 *
136 * @param value A String to be processed.
137 * @return A new String converted to lowercase and trimmed.
138 */
139 private static String convert(String value)
140 {
141 return value.trim();
142
143 }
144
145
146 /***
147 * Set the property 'prop' in the bean to the value of the
148 * corresponding parameters. Supports all types supported by
149 * getXXX methods plus a few more that come for free because
150 * primitives have to be wrapped before being passed to invoke
151 * anyway.
152 *
153 * @param bean An Object.
154 * @param prop A PropertyDescriptor.
155 * @exception Exception a generic exception.
156 */
157 private static void setProperty(RequestParameters params, Object bean,
158 PropertyDescriptor prop)
159 throws Exception
160 {
161 if (prop instanceof IndexedPropertyDescriptor)
162 {
163 throw new Exception(prop.getName() +
164 " is an indexed property (not supported)");
165 }
166
167 Method setter = prop.getWriteMethod();
168 if (setter == null)
169 {
170 throw new Exception(prop.getName() +
171 " is a read only property");
172 }
173
174 Class propclass = prop.getPropertyType();
175 Object[] args = {null};
176
177 if (propclass == String.class)
178 {
179 args[0] = params.get(prop.getName());
180 }
181 else if (propclass == Integer.class || propclass == Integer.TYPE)
182 {
183 args[0] = params.getInt(prop.getName());
184 }
185 else if (propclass == Long.class || propclass == Long.TYPE)
186 {
187 args[0] = new Long(params.getLong(prop.getName()));
188 }
189 else if (propclass == Short.class || propclass == Short.TYPE)
190 {
191 args[0] = new Short((short)params.getLong(prop.getName()));
192 }
193 else if (propclass == Byte.class || propclass == Byte.TYPE)
194 {
195 args[0] = new Byte((byte)params.getLong(prop.getName()));
196 }
197 else if (propclass == Character.class || propclass == Character.TYPE)
198 {
199 args[0] = new Character(params.get(prop.getName()).charAt(0));
200 }
201 else if (propclass == Boolean.class || propclass == Boolean.TYPE)
202 {
203 args[0] = params.getBoolean(prop.getName());
204 }
205 else if (propclass == Double.class || propclass == Double.TYPE)
206 {
207
208
209 args[0] = new Double(params.getFloat(prop.getName()));
210 }
211 else if (propclass == BigDecimal.class)
212 {
213
214
215 args[0] = new BigDecimal(params.getFloat(prop.getName()));
216 }
217 else if (propclass == String[].class)
218 {
219 args[0] = params.getStrings(prop.getName());
220 }
221
222
223
224
225 else if (propclass == int[].class)
226 {
227 args[0] = params.getInts(prop.getName());
228 }
229 else if (propclass == Integer[].class)
230 {
231
232 args[0] = new Integer(params.getInt(prop.getName()));
233 }
234 else if (propclass == Date.class)
235 {
236 args[0] = params.getDate(prop.getName());
237 }
238
239
240
241
242
243
244
245
246 else
247 {
248 throw new Exception("property "
249 + prop.getName()
250 + " is of unsupported type "
251 + propclass.toString());
252 }
253
254 setter.invoke(bean, args);
255 }
256
257
258 }
259