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.convert;
017:
018: import java.beans.BeanInfo;
019: import java.beans.IntrospectionException;
020: import java.beans.Introspector;
021: import java.beans.PropertyDescriptor;
022: import java.util.HashMap;
023: import java.util.Map;
024:
025: import org.directwebremoting.extend.Converter;
026: import org.directwebremoting.extend.InboundContext;
027: import org.directwebremoting.extend.MarshallException;
028: import org.directwebremoting.extend.Property;
029: import org.directwebremoting.extend.TypeHintContext;
030: import org.directwebremoting.impl.PropertyDescriptorProperty;
031:
032: /**
033: * Convert a Javascript associative array into a JavaBean
034: * @author Joe Walker [joe at getahead dot ltd dot uk]
035: */
036: public class BeanConverter extends BasicObjectConverter implements
037: Converter {
038: /* (non-Javadoc)
039: * @see org.directwebremoting.extend.NamedConverter#getPropertyMapFromObject(java.lang.Object, boolean, boolean)
040: */
041: public Map<String, Property> getPropertyMapFromObject(
042: Object example, boolean readRequired, boolean writeRequired)
043: throws MarshallException {
044: return getPropertyMapFromClass(example.getClass(),
045: readRequired, writeRequired);
046: }
047:
048: /* (non-Javadoc)
049: * @see org.directwebremoting.extend.NamedConverter#getPropertyMap(java.lang.Class, boolean, boolean)
050: */
051: public Map<String, Property> getPropertyMapFromClass(Class<?> type,
052: boolean readRequired, boolean writeRequired)
053: throws MarshallException {
054: try {
055: BeanInfo info = Introspector.getBeanInfo(type);
056: PropertyDescriptor[] descriptors = info
057: .getPropertyDescriptors();
058:
059: Map<String, Property> properties = new HashMap<String, Property>();
060: for (PropertyDescriptor descriptor : descriptors) {
061: String name = descriptor.getName();
062:
063: // We don't marshall getClass()
064: if ("class".equals(name)) {
065: continue;
066: }
067:
068: // Access rules mean we might not want to do this one
069: if (!isAllowedByIncludeExcludeRules(name)) {
070: continue;
071: }
072:
073: if (readRequired && descriptor.getReadMethod() == null) {
074: continue;
075: }
076:
077: if (writeRequired
078: && descriptor.getWriteMethod() == null) {
079: continue;
080: }
081:
082: properties.put(name, new PropertyDescriptorProperty(
083: descriptor));
084: }
085:
086: return properties;
087: } catch (IntrospectionException ex) {
088: throw new MarshallException(type, ex);
089: }
090: }
091:
092: /* (non-Javadoc)
093: * @see org.directwebremoting.convert.BasicObjectConverter#createTypeHintContext(org.directwebremoting.extend.InboundContext, org.directwebremoting.extend.Property)
094: */
095: @Override
096: protected TypeHintContext createTypeHintContext(
097: InboundContext inctx, Property property) {
098: return new TypeHintContext(converterManager, property
099: .getSetter(), 0);
100: }
101: }
|