001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019/*
020 * Copyright 2001-2004 The Apache Software Foundation.
021 * 
022 * Licensed under the Apache License, Version 2.0 (the "License");
023 * you may not use this file except in compliance with the License.
024 * You may obtain a copy of the License at
025 * 
026 *      http://www.apache.org/licenses/LICENSE-2.0
027 * 
028 * Unless required by applicable law or agreed to in writing, software
029 * distributed under the License is distributed on an "AS IS" BASIS,
030 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
031 * See the License for the specific language governing permissions and
032 * limitations under the License.
033 */
034
035package org.apache.axis.encoding.ser;
036
037import java.io.IOException;
038import java.util.Map;
039
040import javax.xml.namespace.QName;
041
042import lucee.runtime.type.StructImpl;
043
044import org.apache.axis.description.TypeDesc;
045import org.apache.axis.encoding.Deserializer;
046import org.apache.axis.utils.BeanPropertyDescriptor;
047import org.apache.axis.utils.BeanUtils;
048import org.apache.axis.utils.JavaUtils;
049
050/**
051 * DeserializerFactory for Bean
052 *
053 * @author Rich Scheuerle <scheu@us.ibm.com>
054 * @author Sam Ruby <rubys@us.ibm.com>
055 */
056public class BeanDeserializerFactory extends BaseDeserializerFactory {
057
058    /** Type metadata about this class for XML deserialization */
059    protected transient TypeDesc typeDesc = null;
060    protected transient Map propertyMap = null;
061
062    public BeanDeserializerFactory(Class javaType, QName xmlType) {
063        super(BeanDeserializer.class, xmlType, javaType);
064        
065        // Sometimes an Enumeration class is registered as a Bean.
066        // If this is the case, silently switch to the EnumDeserializer
067        if (JavaUtils.isEnumClass(javaType)) {
068            deserClass = EnumDeserializer.class;
069        }
070
071        typeDesc = TypeDesc.getTypeDescForClass(javaType);
072        propertyMap = getProperties(javaType, typeDesc);
073    }
074
075   /**
076     * Get a list of the bean properties
077     */
078    public static Map getProperties(Class javaType, TypeDesc typeDesc ) {
079        Map propertyMap = null;
080
081        if (typeDesc != null) {
082            propertyMap = typeDesc.getPropertyDescriptorMap();
083        } else {
084            BeanPropertyDescriptor[] pd = BeanUtils.getPd(javaType, null);
085            propertyMap = new StructImpl();// use this to get rid of case sensitivity
086            
087            // loop through properties and grab the names for later
088            for (int i = 0; i < pd.length; i++) {
089                BeanPropertyDescriptor descriptor = pd[i];
090                propertyMap.put(descriptor.getName(), descriptor);
091            }
092        }
093
094        return propertyMap;
095    }
096
097   /**
098     * Optimize construction of a BeanDeserializer by caching the
099     * type descriptor and property map.
100     */
101    protected Deserializer getGeneralPurpose(String mechanismType) {
102        if (javaType == null || xmlType == null) {
103           return super.getGeneralPurpose(mechanismType);
104        }
105
106        if (deserClass == EnumDeserializer.class) {
107           return super.getGeneralPurpose(mechanismType);
108        }
109        return new BeanDeserializer(javaType, xmlType, typeDesc, propertyMap);
110    }
111
112
113    private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
114        in.defaultReadObject();
115        typeDesc = TypeDesc.getTypeDescForClass(javaType);
116        propertyMap = getProperties(javaType, typeDesc);
117    }
118}