001: /*
002: * Copyright 2005 Joe Walker
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.directwebremoting.hibernate;
017:
018: import java.beans.BeanInfo;
019: import java.beans.IntrospectionException;
020: import java.beans.Introspector;
021: import java.beans.PropertyDescriptor;
022: import java.lang.reflect.Method;
023: import java.util.HashMap;
024: import java.util.Map;
025:
026: import net.sf.hibernate.Hibernate;
027:
028: import org.directwebremoting.convert.BeanConverter;
029: import org.directwebremoting.extend.Converter;
030: import org.directwebremoting.extend.MarshallException;
031: import org.directwebremoting.extend.Property;
032:
033: /**
034: * BeanConverter that works with Hibernate to get BeanInfo.
035: * @author Joe Walker [joe at getahead dot ltd dot uk]
036: */
037: public class H2BeanConverter extends BeanConverter implements Converter {
038: /* (non-Javadoc)
039: * @see org.directwebremoting.extend.NamedConverter#getPropertyMapFromObject(java.lang.Object, boolean, boolean)
040: */
041: @Override
042: public Map<String, Property> getPropertyMapFromObject(
043: Object example, boolean readRequired, boolean writeRequired)
044: throws MarshallException {
045: Class<?> clazz = Hibernate.getClass(example);
046: try {
047: BeanInfo info = Introspector.getBeanInfo(clazz);
048:
049: Map<String, Property> properties = new HashMap<String, Property>();
050: for (PropertyDescriptor descriptor : info
051: .getPropertyDescriptors()) {
052: String name = descriptor.getName();
053:
054: // We don't marshall getClass()
055: if ("class".equals(name)) {
056: continue;
057: }
058:
059: // Access rules mean we might not want to do this one
060: if (!isAllowedByIncludeExcludeRules(name)) {
061: continue;
062: }
063:
064: if (readRequired && descriptor.getReadMethod() == null) {
065: continue;
066: }
067:
068: if (writeRequired
069: && descriptor.getWriteMethod() == null) {
070: continue;
071: }
072:
073: properties.put(name, new H2PropertyDescriptorProperty(
074: descriptor));
075: }
076:
077: return properties;
078: } catch (Exception ex) {
079: throw new MarshallException(clazz, ex);
080: }
081: }
082:
083: /**
084: * Cache the method if possible, using the classname and property name to
085: * allow for similar named methods.
086: * @param data The bean to introspect
087: * @param property The property to get the accessor for
088: * @return The getter method
089: * @throws IntrospectionException
090: */
091: protected Method findGetter(Object data, String property)
092: throws IntrospectionException {
093: String key = data.getClass().getName() + ":" + property;
094:
095: Method method = methods.get(key);
096: if (method == null) {
097: PropertyDescriptor[] props = Introspector.getBeanInfo(
098: data.getClass()).getPropertyDescriptors();
099: for (PropertyDescriptor prop : props) {
100: if (prop.getName().equalsIgnoreCase(property)) {
101: method = prop.getReadMethod();
102: }
103: }
104:
105: methods.put(key, method);
106: }
107:
108: return method;
109: }
110:
111: /**
112: * The cache of method lookups that we've already done
113: */
114: protected final Map<String, Method> methods = new HashMap<String, Method>();
115: }
|