📄 facescontextimpl.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.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Set;import javax.faces.FactoryFinder;import javax.faces.application.Application;import javax.faces.application.ApplicationFactory;import javax.faces.application.FacesMessage;import javax.faces.application.FacesMessage.Severity;import javax.faces.component.UIViewRoot;import javax.faces.context.ExternalContext;import javax.faces.context.FacesContext;import javax.faces.context.ResponseStream;import javax.faces.context.ResponseWriter;import javax.faces.render.RenderKit;import javax.faces.render.RenderKitFactory;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.springframework.beans.factory.DisposableBean;import org.springframework.util.Assert;/** * <strong>FacesContext</strong> contains all of the per-request state * information related to the processing of a single JavaServer Faces request, * and the rendering of the corresponding response. It is passed to, and * potentially modified by, each phase of the request processing lifecycle. * * <p> * A {@link FacesContext} instance is associated with a particular request at * the beginning of request processing, by a call to the * <code>getFacesContext()</code> method of the * {@link javax.faces.context.FacesContextFactory} instance associated with the * current web application. The instance remains active until its * <code>release()</code> method is called, after which no further references * to this instance are allowed. While a {@link FacesContext} instance is * active, it must not be referenced from any thread other than the one upon * which the servlet container executing this web application utilizes for the * processing of this request. * </p> * * <p> * <i>Note: copied from FacesContext docs.</i> * </p> * * @author Andreas Kuhrwahl * */public final class FacesContextImpl extends FacesContext { /** * A container for messages. * * @author Andreas Kuhrwahl * @see FacesMessage */ private class MessageContainer { /** The messages for a client id. */ private final Map clientIdMessagesMapping = new HashMap(); /** The global messages. */ private final List nullClientIdMessages = new ArrayList(); /** The maximum severity of a message within this request. */ private FacesMessage.Severity maxSeverity; /** All messages. */ private final List messages = new ArrayList(); /** * Adds a message to this container. * * @param clientId * the client id of a component mapping to the message * @param message * the FacesMessage */ public void addMessage(final String clientId, final FacesMessage message) { checkSeverity(message); if (clientId == null) { nullClientIdMessages.add(message); } else { getMessageList(clientId).add(message); } messages.add(message); } /** * All client ids which have added messages. * * @return the ids */ public Set getClientIdsWithMessages() { return clientIdMessagesMapping.keySet(); } /** * Checks wether the current severity of the given FacesMessage * <code>message</code> is higher than the one saved one * <code>maxSeverity</code>. * * @param message * a message potentially with a severity */ public void checkSeverity(final FacesMessage message) { if (maxSeverity == null) { maxSeverity = message.getSeverity(); return; } final FacesMessage.Severity severity = message.getSeverity(); if (severity != null && severity.compareTo(maxSeverity) > 0) { maxSeverity = severity; } } /** * Returns all messages for a given client id. * * @param clientId * the client id of a component * @return the requested messages for a component with an id * <code>clientId</code> */ private List getMessageList(final String clientId) { List messagesForClientId = (List) clientIdMessagesMapping .get(clientId); if (messagesForClientId == null) { messagesForClientId = new ArrayList(); clientIdMessagesMapping.put(clientId, messagesForClientId); } return messagesForClientId; } /** * Returns the maximum severity of this lifecycle execution. * * @return the maximum severity */ public FacesMessage.Severity getMaxSeverity() { return maxSeverity; } /** * Returns all messages of this lifecycle execution. * * @return all messages */ public List getMessages() { return messages; } /** * Returns all messages of this lifecycle execution for a component with * the given id <code>clientId</code>. * * @param clientId * the id of a component * @return all messages for the requested component */ public List getMessages(final String clientId) { List allMessages = nullClientIdMessages; if (clientId != null) { allMessages = getMessageList(clientId); } return allMessages; } } /** For logging. */ protected final Log logger = LogFactory.getLog(getClass()); /** * The ExternalContext implementation - usually a fake context. * * @see ServletExternalContextFake */ private ExternalContext externalContext; /** The response stream to use. */ private ResponseStream responseStream; /** The response writer to use. */ private ResponseWriter responseWriter; /** The root of the restored respectively rendered view. */ private UIViewRoot viewRoot; /** The container for messages. */ private MessageContainer messageContainer; /** Is the context already released? */ private boolean released = false; /** Is the response already rendered? */ private boolean renderResponse = false; /** Is the response already completed? */ private boolean responseComplete = false; /** * Constructs a FacesContext with the given ExternalContext * <code>externalContext</code>. * * @param externalContext * usually a fake context * @see ServletExternalContextFake */ public FacesContextImpl(final ExternalContext externalContext) { super(); this.externalContext = externalContext; setCurrentInstance(this); } /** * Is the context already released? * * @throws IllegalStateException */ private void assertReleased() { if (released) { throw new IllegalStateException("FacesContext already released"); } } /** * {@inheritDoc} */ public Application getApplication() { assertReleased(); return ((ApplicationFactory) FactoryFinder .getFactory(FactoryFinder.APPLICATION_FACTORY)) .getApplication(); } /** * {@inheritDoc} */ public Iterator getClientIdsWithMessages() { assertReleased(); return getMessageContainer().getClientIdsWithMessages().iterator(); } /** * {@inheritDoc} */ public ExternalContext getExternalContext() { assertReleased(); return externalContext; } /** * {@inheritDoc} */ public Severity getMaximumSeverity() { assertReleased(); return getMessageContainer().getMaxSeverity(); } /** * {@inheritDoc} */ public Iterator getMessages() { assertReleased(); return getMessageContainer().getMessages().iterator(); } /** * {@inheritDoc} */ public Iterator getMessages(final String clientId) { assertReleased(); return getMessageContainer().getMessages(clientId).iterator(); } /** * {@inheritDoc} */ public RenderKit getRenderKit() { assertReleased(); RenderKit renderKit = null; if (getViewRoot() != null) { final String renderKitId = getViewRoot().getRenderKitId(); if (renderKitId != null) { renderKit = ((RenderKitFactory) FactoryFinder .getFactory(FactoryFinder.RENDER_KIT_FACTORY)) .getRenderKit(this, renderKitId); } } return renderKit; } /** * {@inheritDoc} */ public boolean getRenderResponse() { assertReleased(); return renderResponse; } /** * {@inheritDoc} */ public boolean getResponseComplete() { assertReleased(); return responseComplete; } /** * {@inheritDoc} */ public void addMessage(final String clientId, final FacesMessage message) { assertReleased(); getMessageContainer().addMessage(clientId, message); } /** * {@inheritDoc} */ public void release() { assertReleased(); if (externalContext instanceof DisposableBean) { try { ((DisposableBean) externalContext).destroy(); } catch (Exception ex) { logger.error(ex); } } externalContext = null; messageContainer = null; responseStream = null; responseWriter = null; viewRoot = null; released = true; FacesContext.setCurrentInstance(null); } /** * {@inheritDoc} */ public void renderResponse() { assertReleased(); renderResponse = true; } /** * {@inheritDoc} */ public void responseComplete() { assertReleased(); responseComplete = true; } /** * {@inheritDoc} */ public ResponseStream getResponseStream() { assertReleased(); return responseStream; } /** * {@inheritDoc} */ public void setResponseStream(final ResponseStream responseStream) { assertReleased(); Assert.notNull(responseStream); this.responseStream = responseStream; } /** * {@inheritDoc} */ public ResponseWriter getResponseWriter() { assertReleased(); return responseWriter; } /** * {@inheritDoc} */ public void setResponseWriter(final ResponseWriter responseWriter) { assertReleased(); Assert.notNull(responseWriter); this.responseWriter = responseWriter; } /** * Returns the message container for message organization. * * @return the message container */ private MessageContainer getMessageContainer() { if (messageContainer == null) { messageContainer = new MessageContainer(); } return messageContainer; } /** * {@inheritDoc} */ public UIViewRoot getViewRoot() { assertReleased(); return viewRoot; } /** * {@inheritDoc} */ public void setViewRoot(final UIViewRoot viewRoot) { assertReleased(); Assert.notNull(viewRoot); this.viewRoot = viewRoot; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -