⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 servletexternalcontextfake.java

📁 Please read your package and describe it at least 40 bytes in English. System will automatically de
💻 JAVA
字号:
/* * Copyright 2002-2004 the original author or authors. *  * 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 de.mindmatters.faces.context;import java.io.IOException;import java.io.InputStream;import java.net.MalformedURLException;import java.net.URL;import java.security.Principal;import java.util.AbstractMap;import java.util.AbstractSet;import java.util.Collections;import java.util.Enumeration;import java.util.HashMap;import java.util.Iterator;import java.util.Locale;import java.util.Map;import java.util.NoSuchElementException;import java.util.Set;import javax.faces.context.ExternalContext;import javax.servlet.ServletContext;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.springframework.beans.factory.DisposableBean;import org.springframework.util.Assert;import org.springframework.web.context.request.RequestAttributes;import org.springframework.web.context.request.RequestContextHolder;/** * <p> * This class allows the Faces API to be unaware of the nature of its containing * application environment. * </p> *  * <p> * <i>Note: documentation copied from specification</i> * </p> *  * <p> * This class implements the nature of a servlet applicaton environment. As it's * name says this class fakes the behavior described in the specifications. This * class implements the specified behavior which delegates to the * ServletContext. Specified behavior which delegates to the ServletRequest or * ServletResponse is <strong>not</strong> implemented (In fact a * {@link java.lang.UnsupportedOperationException} will be thrown). * </p> *  * <p> * This class is used on startup time to preinstantiate singletons declared in * any <i>Spring</i>-based configuration context which needs a * <strong>FacesContext</strong> for instantiation. * </p> *  * @author andreas.kuhrwahl *  */public class ServletExternalContextFake extends ExternalContext implements        DisposableBean {    /**     * ServletExternalContextFake-based implementation of the     * {@link RequestAttributes} interface.     *      * @author Andreas Kuhrwahl     *      */    private class ServletRequestAttributesFake implements RequestAttributes {        /**         * {@inheritDoc}         */        public void setAttribute(final String name, final Object value,                final int scope) {            if (scope == SCOPE_REQUEST) {                getRequestMap().put(name, value);            } else {                getSessionMap().put(name, value);            }        }        /**         * {@inheritDoc}         */        public Object getAttribute(final String name, final int scope) {            if (scope == SCOPE_REQUEST) {                return getRequestMap().get(name);            } else {                return getSessionMap().get(name);            }        }        /**         * {@inheritDoc}         */        public void removeAttribute(final String name, final int scope) {            if (scope == SCOPE_REQUEST) {                getRequestMap().remove(name);            } else {                getSessionMap().remove(name);            }        }        /**         * {@inheritDoc}         */        public String getSessionId() {            return getSessionMutex().getClass().getName();        }        /**         * {@inheritDoc}         */        public Object getSessionMutex() {            return getSessionMap();        }        /**         * {@inheritDoc}         */        public void registerDestructionCallback(final String name,                final Runnable callback, final int scope) {            throw new UnsupportedOperationException("not supported");        }    }    /** For logging. */    protected final Log logger = LogFactory.getLog(getClass());    /**     * The application-wide used scope.     *      * @author Andreas Kuhrwahl     *      */    private class ApplicationMap extends AbstractMap {        /**         * {@inheritDoc}         */        public Set entrySet() {            return new AbstractSet() {                public Iterator iterator() {                    return new Iterator() {                        private Enumeration e = servletContext                                .getAttributeNames();                        private String currentName;                        public boolean hasNext() {                            return e.hasMoreElements();                        }                        public Object next() {                            currentName = (String) e.nextElement();                            return new Map.Entry() {                                public Object getKey() {                                    return currentName;                                }                                public Object getValue() {                                    return servletContext                                            .getAttribute(currentName);                                }                                public Object setValue(final Object value) {                                    final Object previousValue = servletContext                                            .getAttribute(currentName);                                    servletContext.setAttribute(currentName,                                            value);                                    return previousValue;                                }                            };                        }                        public void remove() {                            if (currentName == null) {                                throw new NoSuchElementException(                                        "You must call next() at least once");                            }                            servletContext.removeAttribute(currentName);                        }                    };                }                public int size() {                    int size = 0;                    for (final Enumeration e = servletContext                            .getAttributeNames(); e.hasMoreElements(); e                            .nextElement()) {                        size++;                    }                    return size;                }            };        }        /**         * {@inheritDoc}         */        public Object put(final Object key, final Object value) {            Object previousValue = servletContext.getAttribute((String) key);            servletContext.setAttribute((String) key, value);            return previousValue;        }    }    /** The context of the application environment. */    private final ServletContext servletContext;    /**     * The container for initialization parameters of the application     * environment.     */    private Map initParameterMap;    /** The application-wide used scope. */    private Map applicationMap;    /** The request-wide used scope. */    private Map requestMap;    /**     * Constructs an ExternalContext with the given ServletContext     * <code>servletContext</code>.     *      * @param servletContext     *            the context of the application environment     */    public ServletExternalContextFake(final ServletContext servletContext) {        super();        Assert.notNull(servletContext);        this.servletContext = servletContext;        RequestContextHolder                .setRequestAttributes(new ServletRequestAttributesFake());    }    /**     * {@inheritDoc}     */    public void dispatch(final String path) throws IOException {        throw new UnsupportedOperationException();    }    /**     * {@inheritDoc}     */    public String encodeActionURL(final String url) {        throw new UnsupportedOperationException();    }    /**     * {@inheritDoc}     */    public String encodeNamespace(final String name) {        return name;    }    /**     * {@inheritDoc}     */    public String encodeResourceURL(final String url) {        return encodeActionURL(url);    }    /**     * {@inheritDoc}     */    public Map getApplicationMap() {        if (applicationMap == null) {            applicationMap = new ApplicationMap();        }        return applicationMap;    }    /**     * {@inheritDoc}     */    public String getAuthType() {        throw new UnsupportedOperationException();    }    /**     * {@inheritDoc}     */    public Object getContext() {        return servletContext;    }    /**     * {@inheritDoc}     */    public String getInitParameter(final String name) {        return servletContext.getInitParameter(name);    }    /**     * {@inheritDoc}     */    public Map getInitParameterMap() {        if (initParameterMap == null) {            initParameterMap = new HashMap();            for (final Enumeration e = servletContext.getInitParameterNames(); e                    .hasMoreElements();) {                final String name = (String) e.nextElement();                initParameterMap.put(name, servletContext                        .getInitParameter(name));            }            initParameterMap = Collections.unmodifiableMap(initParameterMap);        }        return initParameterMap;    }    /**     * {@inheritDoc}     */    public String getRemoteUser() {        throw new UnsupportedOperationException();    }    /**     * {@inheritDoc}     */    public Object getRequest() {        throw new UnsupportedOperationException();    }    /**     * {@inheritDoc}     */    public String getRequestContextPath() {        throw new UnsupportedOperationException();    }    /**     * {@inheritDoc}     */    public Map getRequestCookieMap() {        throw new UnsupportedOperationException();    }    /**     * {@inheritDoc}     */    public Map getRequestHeaderMap() {        throw new UnsupportedOperationException();    }    /**     * {@inheritDoc}     */    public Map getRequestHeaderValuesMap() {        throw new UnsupportedOperationException();    }    /**     * {@inheritDoc}     */    public Locale getRequestLocale() {        throw new UnsupportedOperationException();    }    /**     * {@inheritDoc}     */    public Iterator getRequestLocales() {        throw new UnsupportedOperationException();    }    /**     * {@inheritDoc}     */    public Map getRequestMap() {        if (requestMap == null) {            this.requestMap = new HashMap();        }        return requestMap;    }    /**     * {@inheritDoc}     */    public Map getRequestParameterMap() {        throw new UnsupportedOperationException();    }    /**     * {@inheritDoc}     */    public Iterator getRequestParameterNames() {        throw new UnsupportedOperationException();    }    /**     * {@inheritDoc}     */    public Map getRequestParameterValuesMap() {        throw new UnsupportedOperationException();    }    /**     * {@inheritDoc}     */    public String getRequestPathInfo() {        throw new UnsupportedOperationException();    }    /**     * {@inheritDoc}     */    public String getRequestServletPath() {        throw new UnsupportedOperationException();    }    /**     * {@inheritDoc}     */    public URL getResource(final String path) throws MalformedURLException {        return servletContext.getResource(path);    }    /**     * {@inheritDoc}     */    public InputStream getResourceAsStream(final String path) {        return servletContext.getResourceAsStream(path);    }    /**     * {@inheritDoc}     */    public Set getResourcePaths(final String path) {        return servletContext.getResourcePaths(path);    }    /**     * {@inheritDoc}     */    public Object getResponse() {        throw new UnsupportedOperationException();    }    /**     * {@inheritDoc}     */    public Object getSession(final boolean create) {        throw new UnsupportedOperationException();    }    /**     * {@inheritDoc}     */    public Map getSessionMap() {        return getRequestMap();    }    /**     * {@inheritDoc}     */    public Principal getUserPrincipal() {        throw new UnsupportedOperationException();    }    /**     * {@inheritDoc}     */    public boolean isUserInRole(final String role) {        throw new UnsupportedOperationException();    }    /**     * {@inheritDoc}     */    public void log(final String message) {        servletContext.log(message);    }    /**     * {@inheritDoc}     */    public void log(final String message, final Throwable exception) {        servletContext.log(message, exception);    }    /**     * {@inheritDoc}     */    public void redirect(final String url) throws IOException {        throw new UnsupportedOperationException();    }    /**     * {@inheritDoc}     */    public void destroy() throws Exception {        RequestContextHolder.setRequestAttributes(null);        if (requestMap != null) {            requestMap.clear();            requestMap = null;        }    }    /**     * Returns the ServletContext.     *      * @return The ServletContext     */    protected final ServletContext getServletContext() {        return servletContext;    }}

⌨️ 快捷键说明

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