001    /*
002     * Copyright 2001-2004 The Apache Software Foundation.
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    
017    package org.apache.axis.encoding.ser;
018    
019    import java.io.IOException;
020    import java.util.HashMap;
021    import java.util.Map;
022    
023    import javax.xml.namespace.QName;
024    
025    import org.apache.axis.description.TypeDesc;
026    import org.apache.axis.encoding.Deserializer;
027    import org.apache.axis.utils.BeanPropertyDescriptor;
028    import org.apache.axis.utils.BeanUtils;
029    import org.apache.axis.utils.JavaUtils;
030    
031    /**
032     * DeserializerFactory for Bean
033     *
034     * @author Rich Scheuerle <scheu@us.ibm.com>
035     * @author Sam Ruby <rubys@us.ibm.com>
036     */
037    public class BeanDeserializerFactory extends BaseDeserializerFactory {
038    
039        /** Type metadata about this class for XML deserialization */
040        protected transient TypeDesc typeDesc = null;
041        protected transient Map propertyMap = null;
042    
043        public BeanDeserializerFactory(Class javaType, QName xmlType) {
044            super(BeanDeserializer.class, xmlType, javaType);
045            
046            // Sometimes an Enumeration class is registered as a Bean.
047            // If this is the case, silently switch to the EnumDeserializer
048            if (JavaUtils.isEnumClass(javaType)) {
049                deserClass = EnumDeserializer.class;
050            }
051    
052            typeDesc = TypeDesc.getTypeDescForClass(javaType);
053            propertyMap = getProperties(javaType, typeDesc);
054        }
055    
056       /**
057         * Get a list of the bean properties
058         */
059        public static Map getProperties(Class javaType, TypeDesc typeDesc ) {
060            Map propertyMap = null;
061    
062            if (typeDesc != null) {
063                propertyMap = typeDesc.getPropertyDescriptorMap();
064            } else {
065                BeanPropertyDescriptor[] pd = BeanUtils.getPd(javaType, null);
066                propertyMap = new HashMap();
067                // loop through properties and grab the names for later
068                for (int i = 0; i < pd.length; i++) {
069                    BeanPropertyDescriptor descriptor = pd[i];
070                    propertyMap.put(descriptor.getName(), descriptor);
071                }
072            }
073    
074            return propertyMap;
075        }
076    
077       /**
078         * Optimize construction of a BeanDeserializer by caching the
079         * type descriptor and property map.
080         */
081        protected Deserializer getGeneralPurpose(String mechanismType) {
082            if (javaType == null || xmlType == null) {
083               return super.getGeneralPurpose(mechanismType);
084            }
085    
086            if (deserClass == EnumDeserializer.class) {
087               return super.getGeneralPurpose(mechanismType);
088            }
089            return new BeanDeserializer(javaType, xmlType, typeDesc, propertyMap);
090        }
091    
092    
093        private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
094            in.defaultReadObject();
095            typeDesc = TypeDesc.getTypeDescForClass(javaType);
096            propertyMap = getProperties(javaType, typeDesc);
097        }
098    }