defaultconvertermanager.java

来自「反向的AJAX。最大的特性是我们成为反向的Ajax。DWR1.x允许你用java」· Java 代码 · 共 447 行 · 第 1/2 页

JAVA
447
字号
/* * Copyright 2005 Joe Walker * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * *     http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.directwebremoting.dwrp;import java.util.Arrays;import java.util.Collection;import java.util.Collections;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import org.directwebremoting.extend.Converter;import org.directwebremoting.extend.ConverterManager;import org.directwebremoting.extend.InboundContext;import org.directwebremoting.extend.InboundVariable;import org.directwebremoting.extend.MarshallException;import org.directwebremoting.extend.NamedConverter;import org.directwebremoting.extend.OutboundContext;import org.directwebremoting.extend.OutboundVariable;import org.directwebremoting.extend.TypeHintContext;import org.directwebremoting.util.LocalUtil;import org.directwebremoting.util.Logger;import org.directwebremoting.util.Messages;/** * A class to manage the converter types and the instansiated class name matches. * @author Joe Walker [joe at getahead dot ltd dot uk] */public class DefaultConverterManager implements ConverterManager{    /* (non-Javadoc)     * @see org.directwebremoting.ConverterManager#addConverterType(java.lang.String, java.lang.String)     */    public void addConverterType(String id, String className)    {        if (!LocalUtil.isJavaIdentifier(id))        {            log.error("Illegal identifier: '" + id + "'");            return;        }        Class clazz = LocalUtil.classForName(id, className, Converter.class);        if (clazz != null)        {            log.debug("- adding converter type: " + id + " = " + clazz.getName());            converterTypes.put(id, clazz);        }    }    /* (non-Javadoc)     * @see org.directwebremoting.ConverterManager#addConverter(java.lang.String, java.lang.String, java.util.Map)     */    public void addConverter(String match, String type, Map params) throws IllegalArgumentException, InstantiationException, IllegalAccessException    {        Class clazz = (Class) converterTypes.get(type);        if (clazz == null)        {            log.info("Probably not an issue: " + match + " is not available so the " + type + " converter will not load. This is only an problem if you wanted to use it.");            return;        }        Converter converter = (Converter) clazz.newInstance();        converter.setConverterManager(this);        LocalUtil.setParams(converter, params, ignore);        // add the converter for the specified match        addConverter(match, converter);    }    /* (non-Javadoc)     * @see org.directwebremoting.ConverterManager#addConverter(java.lang.String, org.directwebremoting.Converter)     */    public void addConverter(String match, Converter converter) throws IllegalArgumentException    {        // Check that we don't have this one already        Converter other = (Converter) converters.get(match);        if (other != null)        {            log.warn("Clash of converters for " + match + ". Using " + converter.getClass().getName() + " in place of " + other.getClass().getName());        }        log.debug("- adding converter: " + LocalUtil.getShortClassName(converter.getClass()) + " for " + match);        converters.put(match, converter);    }    /* (non-Javadoc)     * @see org.directwebremoting.ConverterManager#getConverterMatchStrings()     */    public Collection getConverterMatchStrings()    {        return Collections.unmodifiableSet(converters.keySet());    }    /* (non-Javadoc)     * @see org.directwebremoting.ConverterManager#getConverterByMatchString(java.lang.String)     */    public Converter getConverterByMatchString(String match)    {        return (Converter) converters.get(match);    }    /* (non-Javadoc)     * @see org.directwebremoting.ConverterManager#isConvertable(java.lang.Class)     */    public boolean isConvertable(Class paramType)    {        return getConverter(paramType) != null;    }    /* (non-Javadoc)     * @see org.directwebremoting.ConverterManager#convertInbound(java.lang.Class, org.directwebremoting.InboundVariable, org.directwebremoting.InboundContext, org.directwebremoting.TypeHintContext)     */    public Object convertInbound(Class paramType, InboundVariable iv, InboundContext inctx, TypeHintContext incc) throws MarshallException    {        Object converted = inctx.getConverted(iv, paramType);        if (converted == null)        {            // Was the inbound variable marshalled as an Object in the client            // (could mean that this is an instance of one of our generated            // JavaScript classes)            Converter converter = getNamedConverter(paramType, iv.getType());            // Fall back to the standard way of locating a converter if we            // didn't find anything above            if (converter == null)            {                converter = getConverter(paramType);            }            if (converter == null)            {                throw new MarshallException(paramType, Messages.getString("DefaultConverterManager.MissingConverter", paramType));            }            // We only think about doing a null conversion ourselves once we are            // sure that there is a converter available. This prevents hackers            // from passing null to things they are not allowed to convert            if (iv.isNull())            {                return null;            }            inctx.pushContext(incc);            converted = converter.convertInbound(paramType, iv, inctx);            inctx.popContext();        }        return converted;    }    /* (non-Javadoc)     * @see org.directwebremoting.ConverterManager#convertOutbound(java.lang.Object, org.directwebremoting.OutboundContext)     */    public OutboundVariable convertOutbound(Object object, OutboundContext outctx) throws MarshallException    {        if (object == null)        {            return new SimpleOutboundVariable("null", outctx, true);        }        // Check to see if we have done this one already        OutboundVariable ov = outctx.get(object);        if (ov != null)        {            // So the object as been converted already, we just need to refer to it.            return ov.getReferenceVariable();        }        // So we will have to do the conversion        Converter converter = getConverter(object);        if (converter == null)        {            log.error(Messages.getString("DefaultConverterManager.MissingConverter", object.getClass().getName()));            return new SimpleOutboundVariable("null", outctx, true);        }        return converter.convertOutbound(object, outctx);    }    /* (non-Javadoc)     * @see org.directwebremoting.ConverterManager#setExtraTypeInfo(org.directwebremoting.TypeHintContext, java.lang.Class)     */    public void setExtraTypeInfo(TypeHintContext thc, Class type)    {        extraTypeInfoMap.put(thc, type);    }    /* (non-Javadoc)     * @see org.directwebremoting.ConverterManager#getExtraTypeInfo(org.directwebremoting.TypeHintContext)     */    public Class getExtraTypeInfo(TypeHintContext thc)    {        Class type = (Class) extraTypeInfoMap.get(thc);        return type;    }    /* (non-Javadoc)     * @see org.directwebremoting.ConverterManager#setConverters(java.util.Map)     */    public void setConverters(Map converters)    {        this.converters = converters;    }    /**     * Like <code>getConverter(object.getClass());</code> except that since the     * object can be null we check for that fist and then do a lookup against     * the <code>Void.TYPE</code> converter     * @param object The object to find a converter for

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?