Line | Hits | Source |
---|---|---|
1 | /* | |
2 | Chrysalis Web Framework [http://chrysalis.sourceforge.net] | |
3 | Copyright (c) 2002, 2003, 2004, Paul Strack | |
4 | ||
5 | All rights reserved. | |
6 | ||
7 | Redistribution and use in source and binary forms, with or without | |
8 | modification, are permitted provided that the following conditions are met: | |
9 | ||
10 | 1. Redistributions of source code must retain the above copyright notice, this | |
11 | list of conditions and the following disclaimer. | |
12 | ||
13 | 2. Redistributions in binary form must reproduce the above copyright notice, | |
14 | this list of conditions and the following disclaimer in the documentation | |
15 | and/or other materials provided with the distribution. | |
16 | ||
17 | 3. Neither the name of the copyright holder nor the names of its contributors | |
18 | may be used to endorse or promote products derived from this software without | |
19 | specific prior written permission. | |
20 | ||
21 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | |
22 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
23 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
24 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR | |
25 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
26 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
27 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON | |
28 | ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
29 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
30 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
31 | */ | |
32 | ||
33 | package org.chwf.filter; | |
34 | ||
35 | import java.util.Arrays; | |
36 | import java.util.HashMap; | |
37 | import java.util.List; | |
38 | import java.util.Map; | |
39 | import java.util.WeakHashMap; | |
40 | ||
41 | import org.chwf.converter.ConversionException; | |
42 | ||
43 | /** | |
44 | * The <code>BeanFilter</code> class is the abstract superclass | |
45 | * of all bean filters. Bean filters serve as an abstraction layer | |
46 | * facilitating string-to-data value conversions for Java Bean business | |
47 | * objects. Unlike the classes in the <code>java.beans</code> packages, | |
48 | * the bean filter classes are designed for thread-safety, so that they | |
49 | * may be used in multi-thread application servers. | |
50 | * | |
51 | * @author <a href="mailto:pfstrack@users.sourceforge.net">Paul Strack</a> | |
52 | */ | |
53 | public abstract class BeanFilter { | |
54 | ||
55 | /** Standard filter class suffix. */ | |
56 | private static final String FILTER_SUFFIX = "Filter"; | |
57 | ||
58 | /** Standard name for the property's "label" attribute. */ | |
59 | public static final String ATTRIBUTE_LABEL = "label"; | |
60 | ||
61 | /** Standard name for the property's "required" attribute. */ | |
62 | public static final String ATTRIBUTE_REQUIRED = "required"; | |
63 | ||
64 | /** Standard name for the property's "datatype" attribute. */ | |
65 | public static final String ATTRIBUTE_DATATYPE = "datatype"; | |
66 | ||
67 | /** Standard name for the property's "readonly" attribute. */ | |
68 | public static final String ATTRIBUTE_READONLY = "readonly"; | |
69 | ||
70 | /** Standard name for the property's "min" attribute. */ | |
71 | public static final String ATTRIBUTE_MIN = "min"; | |
72 | ||
73 | /** Standard name for the property's "max" attribute. */ | |
74 | public static final String ATTRIBUTE_MAX = "max"; | |
75 | ||
76 | /** Standard name for the property's "minlength" attribute. */ | |
77 | public static final String ATTRIBUTE_MINLENGTH = "minlength"; | |
78 | ||
79 | /** Standard name for the property's "maxlength" attribute. */ | |
80 | public static final String ATTRIBUTE_MAXLENGTH = "maxlength"; | |
81 | ||
82 | /** Standard name for the property's "scale" attribute. */ | |
83 | public static final String ATTRIBUTE_SCALE = "scale"; | |
84 | ||
85 | /** Standard name for the property's "options" attribute. */ | |
86 | public static final String ATTRIBUTE_OPTIONS = "options"; | |
87 | ||
88 | /** | |
89 | * List of known attribute names. This list is useful for | |
90 | * filtering out unknown attributes, so that they can be | |
91 | * (a) ignored or (b) passed onto another layer without | |
92 | * any processing. | |
93 | */ | |
94 | 2 | public static final List KNOWN_ATTRIBUTES = |
95 | Arrays.asList( | |
96 | new String[] { | |
97 | ATTRIBUTE_LABEL, | |
98 | ATTRIBUTE_REQUIRED, | |
99 | ATTRIBUTE_DATATYPE, | |
100 | ATTRIBUTE_READONLY, | |
101 | ATTRIBUTE_MIN, | |
102 | ATTRIBUTE_MAX, | |
103 | ATTRIBUTE_MINLENGTH, | |
104 | ATTRIBUTE_MAXLENGTH, | |
105 | ATTRIBUTE_SCALE, | |
106 | ATTRIBUTE_OPTIONS }); | |
107 | ||
108 | /** Static cache of all bean filters used by the system. */ | |
109 | 1 | private static final Map BEAN_FILTERS = new WeakHashMap(); |
110 | ||
111 | /** The class of the filtered bean. */ | |
112 | private final Class beanClass; | |
113 | ||
114 | /** Property filters for a specific bean filter. */ | |
115 | 26 | private Map propertyFilters = new HashMap(); |
116 | ||
117 | /** Method filters for a specific bean filter. */ | |
118 | 26 | private Map methodFilters = new HashMap(); |
119 | ||
120 | /** | |
121 | * Constructor to specify the class of filtered bean. This constructor is | |
122 | * called by subclasses: <code>super(beanClass);</code><p> | |
123 | * | |
124 | * @param beanClass The class of the filtered bean. | |
125 | */ | |
126 | 26 | protected BeanFilter(Class beanClass) { |
127 | 26 | this.beanClass = beanClass; |
128 | 26 | } |
129 | ||
130 | /** | |
131 | * Find the filter for a given object.<p> | |
132 | * | |
133 | * @param object The object being filtered. | |
134 | * @return The filter. | |
135 | * @throws InitializationException If the filter cannot be initialized. | |
136 | */ | |
137 | public static BeanFilter findFilter(Object object) | |
138 | throws InitializationException { | |
139 | 73 | return findFilter(object.getClass()); |
140 | } | |
141 | ||
142 | /** | |
143 | * Find the filter for a given class.<p> | |
144 | * | |
145 | * @param beanClass The class of the filtered bean. | |
146 | * @return The filter. | |
147 | * @throws InitializationException If the filter cannot be initialized. | |
148 | */ | |
149 | public static BeanFilter findFilter(Class beanClass) | |
150 | throws InitializationException { | |
151 | 132 | return findFilter(beanClass.getName()); |
152 | } | |
153 | ||
154 | /** | |
155 | * Find the filter for the specified class name. This method first searches | |
156 | * for the filter class <code>beanClass + "Filter"</code>. If it cannot | |
157 | * create this object, it creates a {@link org.chwf.filter.GenericBeanFilter} | |
158 | * for the bean class using reflection.<p> | |
159 | * | |
160 | * @param className This must be the fully qualified name of the class. | |
161 | * @return The filter. | |
162 | * @exception InitializationException If the filter cannot be initialized. | |
163 | */ | |
164 | public static BeanFilter findFilter(String className) | |
165 | throws InitializationException { | |
166 | 135 | BeanFilter beanFilter = null; |
167 | 135 | synchronized (BEAN_FILTERS) { |
168 | 135 | beanFilter = (BeanFilter) BEAN_FILTERS.get(className); |
169 | 135 | if (beanFilter == null) { |
170 | try { | |
171 | 28 | beanFilter = |
172 | (BeanFilter) Class.forName(className + FILTER_SUFFIX).newInstance(); | |
173 | 1 | } catch (Exception ex) { |
174 | // Custom filter cannot be created. Construct generic filter: | |
175 | 27 | try { |
176 | 27 | beanFilter = new GenericBeanFilter(Class.forName(className)); |
177 | 25 | ((GenericBeanFilter) beanFilter).initializePropertyAttributes(); |
178 | 25 | } catch (ClassNotFoundException ex2) { |
179 | 2 | throw new InitializationException( |
180 | InitializationException.MESSAGE_CLASS_NOT_FOUND, | |
181 | new Object[] { className }); | |
182 | } | |
183 | } | |
184 | 26 | BEAN_FILTERS.put(className, beanFilter); |
185 | } | |
186 | 133 | } |
187 | 133 | return beanFilter; |
188 | } | |
189 | ||
190 | /** | |
191 | * Get the specified property value of the target object, as a string.<p> | |
192 | * | |
193 | * @param object The target object. | |
194 | * @param property The property being retrieved. | |
195 | * @return The property value. | |
196 | * @throws FilterException For read failure. | |
197 | */ | |
198 | public String get(Object object, String property) throws FilterException { | |
199 | 19 | return getPropertyFilter(property).get(object); |
200 | } | |
201 | ||
202 | /** | |
203 | * Get the specified property value of the target object, as an object.<p> | |
204 | * | |
205 | * @param object The target object. | |
206 | * @param property The property being retrieved. | |
207 | * @return The property value. | |
208 | * @throws FilterException For read failure. | |
209 | */ | |
210 | public Object getPropertyAsObject(Object object, String property) | |
211 | throws FilterException { | |
212 | 24 | return getPropertyFilter(property).getPropertyAsObject(object); |
213 | } | |
214 | ||
215 | /** | |
216 | * Set the specified property of the target object, using a string.<p> | |
217 | * | |
218 | * @param object The target object. | |
219 | * @param property The property being updated. | |
220 | * @param value The new property value. | |
221 | * @throws FilterException For write failure. | |
222 | * @throws ConversionException If the value cannot be converted to | |
223 | * the correct type. | |
224 | */ | |
225 | public void set(Object object, String property, String value) | |
226 | throws FilterException, ConversionException { | |
227 | 19 | getPropertyFilter(property).set(object, value); |
228 | 13 | } |
229 | ||
230 | /** | |
231 | * Create a new object of the bean class.<p> | |
232 | * | |
233 | * @return A new object. | |
234 | * @throws UnsupportedOperationException If create is not supported | |
235 | * for this bean. | |
236 | * @throws OperationException If the create operation itself | |
237 | * throws an exception. The underlying exception is wrapped | |
238 | * in this one. | |
239 | */ | |
240 | public Object create() | |
241 | throws UnsupportedOperationException, OperationException { | |
242 | try { | |
243 | 3 | return beanClass.newInstance(); |
244 | } catch (Exception ex) { | |
245 | 0 | throw new OperationException(ex); |
246 | } | |
247 | } | |
248 | ||
249 | /** | |
250 | * Get a property filter for the specified property.<p> | |
251 | * | |
252 | * @param property The property. | |
253 | * @return The property filter. | |
254 | * @throws PropertyNotFoundException If the bean does not have the property. | |
255 | */ | |
256 | public PropertyFilter getPropertyFilter(String property) | |
257 | throws PropertyNotFoundException { | |
258 | 143 | PropertyFilter filter = (PropertyFilter) propertyFilters.get(property); |
259 | 143 | if (filter == null) { |
260 | 3 | throw new PropertyNotFoundException(beanClass.getName(), property); |
261 | } | |
262 | 140 | return filter; |
263 | } | |
264 | ||
265 | /** | |
266 | * Get all property filters.<p> | |
267 | * | |
268 | * @return An array of property filters. | |
269 | */ | |
270 | public PropertyFilter[] getPropertyFilters() { | |
271 | 39 | PropertyFilter[] filters = new PropertyFilter[propertyFilters.size()]; |
272 | 39 | propertyFilters.values().toArray(filters); |
273 | 39 | return filters; |
274 | } | |
275 | ||
276 | /** | |
277 | * Get the bean class.<p> | |
278 | * | |
279 | * @return The bean class. | |
280 | */ | |
281 | public Class getBeanClass() { | |
282 | 133 | return beanClass; |
283 | } | |
284 | ||
285 | /** | |
286 | * Put a property filter into this bean filter. This overwrites any | |
287 | * existing filter.<p> | |
288 | * | |
289 | * @param name The property name. | |
290 | * @param filter The property filter. | |
291 | */ | |
292 | protected void putPropertyFilter(String name, PropertyFilter filter) { | |
293 | 104 | this.propertyFilters.put(name, filter); |
294 | 104 | } |
295 | ||
296 | /** | |
297 | * Put a property attribute value into a property filter.<p> | |
298 | * | |
299 | * @param property The property name. | |
300 | * @param attribute The attribute name. | |
301 | * @param value The attribute value. | |
302 | * @throws PropertyNotFoundException If the object does not have the property. | |
303 | */ | |
304 | protected void putPropertyAttribute( | |
305 | String property, | |
306 | String attribute, | |
307 | Object value) | |
308 | throws PropertyNotFoundException { | |
309 | 1 | getPropertyFilter(property).setAttribute(attribute, value); |
310 | 1 | } |
311 | ||
312 | /** | |
313 | * Returns property value as an object using its getter method.<p> | |
314 | * | |
315 | * @param owner The owning object. | |
316 | * @param property The bean property or null if it cannot be found. | |
317 | * @return The property value. | |
318 | */ | |
319 | public static Object getProperty(Object owner, String property) { | |
320 | try { | |
321 | 24 | return BeanFilter.findFilter(owner).getPropertyAsObject(owner, property); |
322 | } catch (Exception ex) { | |
323 | 2 | return null; |
324 | } | |
325 | } | |
326 | } |
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |