01: /*
02: * Copyright 2002-2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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.springframework.jdbc.core.namedparam;
18:
19: import org.springframework.beans.BeanWrapper;
20: import org.springframework.beans.BeanWrapperImpl;
21: import org.springframework.beans.NotReadablePropertyException;
22: import org.springframework.beans.PropertyAccessor;
23:
24: import java.util.List;
25: import java.util.ArrayList;
26: import java.beans.PropertyDescriptor;
27:
28: /**
29: * SqlParameterSource implementation that obtains parameter values
30: * from bean properties of a given JavaBean object. The names of
31: * the bean properties have to match the parameter names.
32: *
33: * <p>Uses a Spring BeanWrapper for bean property access underneath.
34: *
35: * @author Thomas Risberg
36: * @author Juergen Hoeller
37: * @since 2.0
38: * @see NamedParameterJdbcTemplate
39: * @see org.springframework.beans.BeanWrapper
40: */
41: public class BeanPropertySqlParameterSource extends
42: AbstractSqlParameterSource {
43:
44: private final BeanWrapper beanWrapper;
45:
46: private String[] propertyNames = null;
47:
48: /**
49: * Create a new BeanPropertySqlParameterSource for the given bean.
50: * @param object the bean instance to wrap
51: */
52: public BeanPropertySqlParameterSource(Object object) {
53: this .beanWrapper = new BeanWrapperImpl(object);
54: }
55:
56: public boolean hasValue(String paramName) {
57: return this .beanWrapper.isReadableProperty(paramName);
58: }
59:
60: public Object getValue(String paramName)
61: throws IllegalArgumentException {
62: try {
63: return this .beanWrapper.getPropertyValue(paramName);
64: } catch (NotReadablePropertyException ex) {
65: throw new IllegalArgumentException(ex.getMessage());
66: }
67: }
68:
69: /**
70: * Provide access to the property names of the wrapped bean.
71: * Uses support provided in the {@link PropertyAccessor} interface.
72: * @return an array containing all the known property names
73: */
74: public String[] getReadablePropertyNames() {
75:
76: synchronized (this ) {
77: if (propertyNames == null) {
78: List names = new ArrayList();
79: PropertyDescriptor[] props = beanWrapper
80: .getPropertyDescriptors();
81: for (int i = 0; i < props.length; i++) {
82: if (beanWrapper.isReadableProperty(props[i]
83: .getName())) {
84: names.add(props[i].getName());
85: }
86: }
87: propertyNames = (String[]) names
88: .toArray(new String[names.size()]);
89: }
90:
91: }
92:
93: return propertyNames;
94: }
95:
96: }
|