dwrxmlconfigurator.java

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

JAVA
652
字号
/* * 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.impl;import java.io.IOException;import java.io.InputStream;import java.lang.reflect.Method;import java.util.Arrays;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.StringTokenizer;import javax.servlet.ServletContext;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.parsers.ParserConfigurationException;import org.directwebremoting.AjaxFilter;import org.directwebremoting.Container;import org.directwebremoting.WebContextFactory;import org.directwebremoting.extend.AccessControl;import org.directwebremoting.extend.AjaxFilterManager;import org.directwebremoting.extend.Configurator;import org.directwebremoting.extend.ConverterManager;import org.directwebremoting.extend.Creator;import org.directwebremoting.extend.CreatorManager;import org.directwebremoting.extend.TypeHintContext;import org.directwebremoting.util.LocalUtil;import org.directwebremoting.util.LogErrorHandler;import org.directwebremoting.util.Logger;import org.directwebremoting.util.Messages;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.NamedNodeMap;import org.w3c.dom.Node;import org.w3c.dom.NodeList;import org.xml.sax.SAXException;/** * A configurator that gets its configuration by reading a dwr.xml file. * @author Joe Walker [joe at getahead dot ltd dot uk] */public class DwrXmlConfigurator implements Configurator{    /**     * Setter for the resource name that we can use to read a file from the     * servlet context     * @param servletResourceName The name to lookup     * @throws IOException On file read failure     * @throws ParserConfigurationException On XML setup failure     * @throws SAXException On XML parse failure     */    public void setServletResourceName(String servletResourceName) throws IOException, ParserConfigurationException, SAXException    {        this.servletResourceName = servletResourceName;        ServletContext servletContext = WebContextFactory.get().getServletContext();        if (servletContext == null)        {            throw new IOException(Messages.getString("DwrXmlConfigurator.MissingServletContext"));        }        InputStream in = null;        try        {            in = servletContext.getResourceAsStream(servletResourceName);            if (in == null)            {                throw new IOException(Messages.getString("DwrXmlConfigurator.MissingConfigFile", servletResourceName));            }            log.debug("Configuring from servlet resource: " + servletResourceName);            setInputStream(in);        }        finally        {            LocalUtil.close(in);        }    }    /**     * Setter for a classpath based lookup     * @param classResourceName The resource to lookup in the classpath     * @throws IOException On file read failure     * @throws ParserConfigurationException On XML setup failure     * @throws SAXException On XML parse failure     */    public void setClassResourceName(String classResourceName) throws IOException, ParserConfigurationException, SAXException    {        this.classResourceName = classResourceName;        InputStream in = getClass().getResourceAsStream(classResourceName);        if (in == null)        {            throw new IOException(Messages.getString("DwrXmlConfigurator.MissingConfigFile", classResourceName));        }        log.debug("Configuring from class resource: " + classResourceName);        setInputStream(in);    }    /**     * Setter for a direct input stream to configure from     * @param in The input stream to read from.     * @throws IOException On file read failure     * @throws ParserConfigurationException On XML setup failure     * @throws SAXException On XML parse failure     */    public void setInputStream(InputStream in) throws ParserConfigurationException, SAXException, IOException    {        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();        dbf.setValidating(true);        DocumentBuilder db = dbf.newDocumentBuilder();        db.setEntityResolver(new DTDEntityResolver());        db.setErrorHandler(new LogErrorHandler());        document = db.parse(in);    }    /**     * To set the configuration document directly     * @param document The new configuration document     */    public void setDocument(Document document)    {        this.document = document;    }    /* (non-Javadoc)     * @see org.directwebremoting.Configurator#configure(org.directwebremoting.Container)     */    public void configure(Container container)    {        accessControl = (AccessControl) container.getBean(AccessControl.class.getName());        ajaxFilterManager = (AjaxFilterManager) container.getBean(AjaxFilterManager.class.getName());        converterManager = (ConverterManager) container.getBean(ConverterManager.class.getName());        creatorManager = (CreatorManager) container.getBean(CreatorManager.class.getName());        Element root = document.getDocumentElement();        NodeList rootChildren = root.getChildNodes();        for (int i = 0; i < rootChildren.getLength(); i++)        {            Node node = rootChildren.item(i);            if (node.getNodeType() == Node.ELEMENT_NODE)            {                Element child = (Element) node;                if (child.getNodeName().equals(ELEMENT_INIT))                {                    loadInits(child);                }                else if (child.getNodeName().equals(ELEMENT_ALLOW))                {                    loadAllows(child);                }                else if (child.getNodeName().equals(ELEMENT_SIGNATURES))                {                    loadSignature(child);                }            }        }    }    /**     * Internal method to load the inits element     * @param child The element to read     */    private void loadInits(Element child)    {        NodeList inits = child.getChildNodes();        for (int j = 0; j < inits.getLength(); j++)        {            if (inits.item(j).getNodeType() == Node.ELEMENT_NODE)            {                Element initer = (Element) inits.item(j);                if (initer.getNodeName().equals(ATTRIBUTE_CREATOR))                {                    String id = initer.getAttribute(ATTRIBUTE_ID);                    String className = initer.getAttribute(ATTRIBUTE_CLASS);                    creatorManager.addCreatorType(id, className);                }                else if (initer.getNodeName().equals(ATTRIBUTE_CONVERTER))                {                    String id = initer.getAttribute(ATTRIBUTE_ID);                    String className = initer.getAttribute(ATTRIBUTE_CLASS);                    converterManager.addConverterType(id, className);                }            }        }    }    /**     * Internal method to load the create/convert elements     * @param child The element to read     */    private void loadAllows(Element child)    {        NodeList allows = child.getChildNodes();        for (int j = 0; j < allows.getLength(); j++)        {            if (allows.item(j).getNodeType() == Node.ELEMENT_NODE)            {                Element allower = (Element) allows.item(j);                if (allower.getNodeName().equals(ELEMENT_CREATE))                {                    loadCreate(allower);                }                else if (allower.getNodeName().equals(ELEMENT_CONVERT))                {                    loadConvert(allower);                }                else if (allower.getNodeName().equals(ELEMENT_FILTER))                {                    loadFilter(allower);                }            }        }    }    /**     * Internal method to load the convert element     * @param allower The element to read     */    private void loadConvert(Element allower)    {        String match = allower.getAttribute(ATTRIBUTE_MATCH);        String type = allower.getAttribute(ATTRIBUTE_CONVERTER);        try        {            Map params = createSettingMap(allower);            converterManager.addConverter(match, type, params);        }        catch (NoClassDefFoundError ex)        {            log.info("Convertor '" + type + "' not loaded due to NoClassDefFoundError. (match='" + match + "'). Cause: " + ex.getMessage());        }        catch (Exception ex)        {            log.error("Failed to add convertor: match=" + match + ", type=" + type, ex);        }    }    /**     * Internal method to load the create element     * @param allower The element to read     */    private void loadCreate(Element allower)    {        String type = allower.getAttribute(ATTRIBUTE_CREATOR);        String javascript = allower.getAttribute(ATTRIBUTE_JAVASCRIPT);        try        {            Map params = createSettingMap(allower);            creatorManager.addCreator(javascript, type, params);            processPermissions(javascript, allower);            processAuth(javascript, allower);            processParameters(javascript, allower);            processAjaxFilters(javascript, allower);        }        catch (NoClassDefFoundError ex)        {            log.info("Creator '" + type + "' not loaded due to NoClassDefFoundError. (javascript='" + javascript + "'). Cause: " + ex.getMessage());        }        catch (Exception ex)        {            log.error("Failed to add creator: type=" + type + ", javascript=" + javascript, ex);        }    }    /**     * Internal method to load the convert element     * @param allower The element to read     */    private void loadFilter(Element allower)    {        String type = allower.getAttribute(ATTRIBUTE_CLASS);        try        {            Class impl = LocalUtil.classForName(type);            AjaxFilter object = (AjaxFilter) impl.newInstance();            LocalUtil.setParams(object, createSettingMap(allower), ignore);            ajaxFilterManager.addAjaxFilter(object);        }        catch (ClassCastException ex)        {            log.error(type + " does not implement " + AjaxFilter.class.getName(), ex);        }        catch (NoClassDefFoundError ex)        {            log.info("Missing class for filter (class='" + type + "'). Cause: " + ex.getMessage());        }        catch (Exception ex)        {            log.error("Failed to add filter: class=" + type, ex);        }    }    /**     * Create a parameter map from nested <param name="foo" value="blah"/>     * elements     * @param parent The parent element     * @return A map of parameters

⌨️ 快捷键说明

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