containerutil.java

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

JAVA
601
字号
/* * 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.util.ArrayList;import java.util.Collection;import java.util.Enumeration;import java.util.Iterator;import java.util.List;import java.util.StringTokenizer;import javax.servlet.ServletConfig;import javax.servlet.ServletContext;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.xml.parsers.ParserConfigurationException;import org.directwebremoting.Container;import org.directwebremoting.ServerContextFactory.ServerContextBuilder;import org.directwebremoting.WebContextFactory.WebContextBuilder;import org.directwebremoting.dwrp.DefaultConverterManager;import org.directwebremoting.dwrp.HtmlCallMarshaller;import org.directwebremoting.dwrp.PlainCallMarshaller;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.DebugPageGenerator;import org.directwebremoting.extend.DwrConstants;import org.directwebremoting.extend.Handler;import org.directwebremoting.extend.PageNormalizer;import org.directwebremoting.extend.Remoter;import org.directwebremoting.extend.ScriptSessionManager;import org.directwebremoting.extend.ServerLoadMonitor;import org.directwebremoting.servlet.AboutHandler;import org.directwebremoting.servlet.AuthHandler;import org.directwebremoting.servlet.DwrWebContextFilter;import org.directwebremoting.servlet.EngineHandler;import org.directwebremoting.servlet.HtmlCallHandler;import org.directwebremoting.servlet.HtmlPollHandler;import org.directwebremoting.servlet.IndexHandler;import org.directwebremoting.servlet.InterfaceHandler;import org.directwebremoting.servlet.PathConstants;import org.directwebremoting.servlet.PlainCallHandler;import org.directwebremoting.servlet.PlainPollHandler;import org.directwebremoting.servlet.TestHandler;import org.directwebremoting.servlet.UrlProcessor;import org.directwebremoting.servlet.UtilHandler;import org.directwebremoting.servlet.WebworkUtilHandler;import org.directwebremoting.util.LocalUtil;import org.directwebremoting.util.Logger;import org.xml.sax.SAXException;/** * An abstraction of all the common servlet operations that are required to host * a DWR service that depends on the servlet spec. * It would be good to have a base class for all servlet operations, however * lack of MI prevents us from doing this. * @author Joe Walker [joe at getahead dot ltd dot uk] */public class ContainerUtil{    /**     * Create a {@link DefaultContainer}, allowing users to upgrade to a child     * of DefaultContainer using an {@link ServletConfig} init parameter of     * <code>org.directwebremoting.Container</code>. Note that while the     * parameter name is the classname of {@link Container}, currently the only     * this can only be used to create children that inherit from     * {@link DefaultContainer}. This restriction may be relaxed in the future.     * Unlike {@link #setupDefaultContainer(DefaultContainer, ServletConfig)},     * this method does not call any setup methods.     * @param servletConfig The source of init parameters     * @return An unsetup implementaion of DefaultContainer     * @throws ServletException If the specified class could not be found     * @see ServletConfig#getInitParameter(String)     */    public static DefaultContainer createDefaultContainer(ServletConfig servletConfig) throws ServletException    {        try        {            String typeName = servletConfig.getInitParameter(Container.class.getName());            if (typeName == null)            {                return new DefaultContainer();            }            log.debug("Using alternate Container implementation: " + typeName);            Class type = LocalUtil.classForName(typeName);            return (DefaultContainer) type.newInstance();        }        catch (Exception ex)        {            throw new ServletException(ex);        }    }    /**     * Setup a {@link DefaultContainer}.     * Using {@link #createDefaultContainer(ServletConfig)} followed by     * {@link #setupFromServletConfig(DefaultContainer, ServletConfig)} before     * calling {@link DefaultContainer#setupFinished()}.     * @param container The container to configure     * @param servletConfig The source of init parameters     * @throws InstantiationException If we can't instantiate a bean     * @throws IllegalAccessException If we have access problems creating a bean     */    public static void setupDefaultContainer(DefaultContainer container, ServletConfig servletConfig) throws InstantiationException, IllegalAccessException    {        setupDefaults(container, servletConfig);        setupFromServletConfig(container, servletConfig);        container.setupFinished();    }    /**     * Take a DefaultContainer and setup the default beans     * @param container The container to configure     * @param servletConfig The source of init parameters     * @throws InstantiationException If we can't instantiate a bean     * @throws IllegalAccessException If we have access problems creating a bean     */    public static void setupDefaults(DefaultContainer container, ServletConfig servletConfig) throws InstantiationException, IllegalAccessException    {        container.addParameter(AccessControl.class.getName(), DefaultAccessControl.class.getName());        container.addParameter(ConverterManager.class.getName(), DefaultConverterManager.class.getName());        container.addParameter(CreatorManager.class.getName(), DefaultCreatorManager.class.getName());        container.addParameter(UrlProcessor.class.getName(), UrlProcessor.class.getName());        container.addParameter(WebContextBuilder.class.getName(), DefaultWebContextBuilder.class.getName());        container.addParameter(ServerContextBuilder.class.getName(), DefaultServerContextBuilder.class.getName());        container.addParameter(AjaxFilterManager.class.getName(), DefaultAjaxFilterManager.class.getName());        container.addParameter(Remoter.class.getName(), DefaultRemoter.class.getName());        container.addParameter(DebugPageGenerator.class.getName(), DefaultDebugPageGenerator.class.getName());        container.addParameter(PlainCallMarshaller.class.getName(), PlainCallMarshaller.class.getName());        container.addParameter(HtmlCallMarshaller.class.getName(), HtmlCallMarshaller.class.getName());        container.addParameter(PlainPollHandler.class.getName(), PlainPollHandler.class.getName());        container.addParameter(HtmlPollHandler.class.getName(), HtmlPollHandler.class.getName());        container.addParameter(ScriptSessionManager.class.getName(), DefaultScriptSessionManager.class.getName());        container.addParameter(PageNormalizer.class.getName(), DefaultPageNormalizer.class.getName());        if (servletConfig.getServletContext().getServerInfo().startsWith("jetty-6"))        {            container.addParameter(ServerLoadMonitor.class.getName(), ThreadDroppingServerLoadMonitor.class.getName());        }        else        {            container.addParameter(ServerLoadMonitor.class.getName(), DefaultServerLoadMonitor.class.getName());        }        // Mapping handlers to URLs        createUrlMapping(container, "/index.html", "indexHandlerUrl", IndexHandler.class);        createUrlMapping(container, "/engine.js", "engineHandlerUrl", EngineHandler.class);        createUrlMapping(container, "/util.js", "utilHandlerUrl", UtilHandler.class);        createUrlMapping(container, "/auth.js", "authHandlerUrl", AuthHandler.class);        createUrlMapping(container, "/webwork/DWRActionUtil.js", "webworkUtilHandlerUrl", WebworkUtilHandler.class);        createUrlMapping(container, "/about", "aboutHandlerUrl", AboutHandler.class);        createUrlMapping(container, "/test/", "testHandlerUrl", TestHandler.class);        createUrlMapping(container, "/interface/", "interfaceHandlerUrl", InterfaceHandler.class);        // The Poll and Call URLs can not be changed easily because they are        // referenced from engine.js. Maybe one day this would be a good        // extension        createUrlMapping(container, "/call/plaincall/", "plainCallHandlerUrl", PlainCallHandler.class);        createUrlMapping(container, "/call/plainpoll/", "plainPollHandlerUrl", PlainPollHandler.class);        createUrlMapping(container, "/call/htmlcall/", "htmlCallHandlerUrl", HtmlCallHandler.class);        createUrlMapping(container, "/call/htmlpoll/", "htmlPollHandlerUrl", HtmlPollHandler.class);        container.addParameter("maxWaitAfterWrite", "500");    }    /**     * Creates entries in the {@link Container} so 2 lookups are possible.     * <ul>     * <li>You can find a {@link Handler} for a URL. Used by {@link UrlProcessor}     * <li>You can inject (or lookup) the URL assigned to a {@link Handler}     * </ul>     * @param container The container to create the entries in     * @param url The URL of the new {@link Handler}     * @param propertyName The property name (for injection and lookup)     * @param handler The class of Handler     * @throws InstantiationException From {@link DefaultContainer#addParameter(Object, Object)}     * @throws IllegalAccessException From {@link DefaultContainer#addParameter(Object, Object)}     */    public static void createUrlMapping(DefaultContainer container, String url, String propertyName, Class handler) throws InstantiationException, IllegalAccessException    {        container.addParameter(PathConstants.URL_PREFIX + url, handler.getName());        container.addParameter(propertyName, url);    }    /**     * Take a DefaultContainer and setup the default beans     * @param container The container to configure     * @param servletConfig The servlet configuration (null to ignore)     * @throws InstantiationException If we can't instantiate a bean     * @throws IllegalAccessException If we have access problems creating a bean     */    public static void setupFromServletConfig(DefaultContainer container, ServletConfig servletConfig) throws InstantiationException, IllegalAccessException    {        Enumeration en = servletConfig.getInitParameterNames();        while (en.hasMoreElements())        {            String name = (String) en.nextElement();            String value = servletConfig.getInitParameter(name);            container.addParameter(name, value);        }    }    /**     * Make some changes to the ServletContext so {@link DwrWebContextFilter}     * can find the Container etc.     * @param context The servlet context     * @param config The servlet configuration     * @param container The container to save in the ServletContext     * @param webContextBuilder The WebContextBuilder to save     * @param servlet The Servlet to save     */    public static void prepareForWebContextFilter(ServletContext context, ServletConfig config, Container container, WebContextBuilder webContextBuilder, HttpServlet servlet)    {        context.setAttribute(Container.class.getName(), container);        context.setAttribute(WebContextBuilder.class.getName(), webContextBuilder);        context.setAttribute(ServletConfig.class.getName(), config);        context.setAttribute(HttpServlet.class.getName(), servlet);    }    /**     * Configure using the system dwr.xml from within the JAR file.     * @param container The container to configure     * @throws ParserConfigurationException If the config file parse fails     * @throws IOException If the config file read fails     * @throws SAXException If the config file parse fails     */    public static void configureFromSystemDwrXml(Container container) throws IOException, ParserConfigurationException, SAXException    {        DwrXmlConfigurator system = new DwrXmlConfigurator();        system.setClassResourceName(DwrConstants.FILE_DWR_XML);        system.configure(container);    }    /**     * Configure using the users dwr.xml that sits next in WEB-INF     * @param container The container to configure     * @throws ParserConfigurationException If the config file parse fails     * @throws IOException If the config file read fails     * @throws SAXException If the config file parse fails     */    public static void configureFromDefaultDwrXml(Container container) throws IOException, ParserConfigurationException, SAXException    {        DwrXmlConfigurator local = new DwrXmlConfigurator();        local.setServletResourceName(DwrConstants.DEFAULT_DWR_XML);        local.configure(container);    }    /**     * Add configurators from init params to the end of the list of     * configurators.     * @param container The container to configure     * @param servletConfig The source of init parameters     * @return true if any Configurators were read     * @throws SAXException If the config file parse fails     * @throws ParserConfigurationException If the config file parse fails     * @throws IOException If the config file read fails     */    public static boolean configureFromInitParams(Container container, ServletConfig servletConfig) throws IOException, ParserConfigurationException, SAXException    {        Enumeration en = servletConfig.getInitParameterNames();        boolean foundConfig = false;        while (en.hasMoreElements())        {            String name = (String) en.nextElement();            String value = servletConfig.getInitParameter(name);            // if the init param starts with "config" then try to load it            if (name.startsWith(INIT_CONFIG))            {                foundConfig = true;                StringTokenizer st = new StringTokenizer(value, "\n,");                while (st.hasMoreTokens())                {                    String fileName = st.nextToken().trim();                    DwrXmlConfigurator local = new DwrXmlConfigurator();                    local.setServletResourceName(fileName);                    local.configure(container);                }            }            else if (name.equals(INIT_CUSTOM_CONFIGURATOR))            {

⌨️ 快捷键说明

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