📄 lifecycleimpl.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.lifecycle;import java.util.ArrayList;import java.util.Arrays;import java.util.Collections;import java.util.Iterator;import java.util.List;import javax.faces.FacesException;import javax.faces.context.FacesContext;import javax.faces.event.PhaseEvent;import javax.faces.event.PhaseId;import javax.faces.event.PhaseListener;import javax.faces.lifecycle.Lifecycle;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.springframework.util.Assert;/** * <strong>Lifecycle</strong> manages the processing of the entire lifecycle of * a particular JavaServer Faces request. It is responsible for executing all of * the phases that have been defined by the JavaServer Faces Specification, in * the specified order, unless otherwise directed by activities that occurred * during the execution of each phase. * * <p> * This implemetation delegates the execution of each phase to an instance of * {@link Phase}. At least a Lifecycle should execute phases as follows: * * <table class="javaDoc"> * <tr> * <th>Phase</th> * <th>default implementation</th> * </tr> * <tr> * <td>RESTORE_VIEW</td> * <td>{@link RestoreViewPhase}</td> * </tr> * <tr> * <td>APPLY_REQUEST_VALUES</td> * <td>{@link ApplyRequestValuesPhase}</td> * </tr> * <tr> * <td>PROCESS_VALIDATIONS</td> * <td>{@link ProcessValidationsPhase}</td> * </tr> * <tr> * <td>UPDATE_MODEL_VALUES</td> * <td>{@link UpdateModelValuesPhase}</td> * </tr> * <tr> * <td>INVOKE_APPLICATION</td> * <td>{@link InvokeApplicationPhase}</td> * </tr> * <tr> * <td>RENDER_RESPONSE</td> * <td>{@link RenderResponsePhase}</td> * </tr> * </table> * </p> * * @author Andreas Kuhrwahl * @see Phase * @see Phase#execute(FacesContext) * */public final class LifecycleImpl extends Lifecycle { /** * The lifecycle-id of the Lifecycle which processes the entire lifecycle of * a particular JavaServer Faces request mapped against the * {@link de.mindmatters.faces.spring.context.servlet.FacesDispatcherServlet} * (which integrates SpringMVC into JSF). */ public static final String JSF_SPRING_LIFECYCLE_ID = "JSF_SPRING_DEFAULT"; /** * Factory method creating the default Lifecycle for a particular JavaServer * Faces request. * * @return the default Lifecycle * @see javax.faces.lifecycle.LifecycleFactory#DEFAULT_LIFECYCLE */ public static Lifecycle createDefaultLifecycle() { return new LifecycleImpl(new Phase[] { new RestoreViewPhase(), new ApplyRequestValuesPhase(), new ProcessValidationsPhase(), new UpdateModelValuesPhase(), new InvokeApplicationPhase() }, new RenderResponsePhase()); } /** For logging. */ protected final Log log = LogFactory.getLog(getClass()); /** List of {@link PhaseListener}s. */ private List listeners = new ArrayList(); /** List of {@link Phase}s. */ private List executionPhases = Collections.EMPTY_LIST; /** Phase responsible for rendering the response. */ private Phase renderResponsePhase; /** * Creates a lifecycle with the given <code>executionPhases</code> and * <code>renderResponsePhase</code>. * * @param executionPhases * phases to execute * @param renderResponsePhase * for rendering */ public LifecycleImpl(final Phase[] executionPhases, final Phase renderResponsePhase) { super(); setExecutionPhases(executionPhases); setRenderResponsePhase(renderResponsePhase); } /** * Executes the given <code>phase</code>. Notifies the interested * {@link PhaseListener}s before and after the execution. * * @param phase * the phse to execute * @param context * {@link FacesContext} for the current request * @throws FacesException * in case of errors */ private void executePhase(final Phase phase, final FacesContext context) { PhaseId phaseId = phase.getId(); if (phaseId == null) { throw new FacesException("PhaseId must not be null"); } if (log.isTraceEnabled()) { log.trace("phase(" + phaseId.toString() + "," + context + ")"); } synchronized (listeners) { PhaseEvent event = new PhaseEvent(context, phaseId, this); for (int i = 0; i < listeners.size(); i++) { PhaseListener listener = (PhaseListener) listeners.get(i); if (phaseId.equals(listener.getPhaseId()) || PhaseId.ANY_PHASE.equals(listener.getPhaseId())) { listener.beforePhase(event); } } } if (!skipping(phaseId, context)) { phase.execute(context); } synchronized (listeners) { PhaseEvent event = new PhaseEvent(context, phaseId, this); for (int i = 0; i < listeners.size(); i++) { PhaseListener listener = (PhaseListener) listeners.get(i); if (phaseId.equals(listener.getPhaseId()) || PhaseId.ANY_PHASE.equals(listener.getPhaseId())) { listener.afterPhase(event); } } } } /** * Checks whether to skip the phase with the id <code>phaseId</code> or * not. * * @param phaseId * the id identifying the phase to execute * @param context * {@link FacesContext} for the current request * @return <code>true</code> if the phase identified by the phaseId should * be skipped <code>false</code> otherwise */ private boolean skipping(final PhaseId phaseId, final FacesContext context) { return context.getResponseComplete() || (context.getRenderResponse() && !phaseId .equals(PhaseId.RENDER_RESPONSE)); } /** * {@inheritDoc} */ public void execute(final FacesContext context) { if (context == null) { throw new NullPointerException("FacesContext must not be null"); } if (log.isDebugEnabled()) { log.debug("execute(" + context + ")"); } for (Iterator i = executionPhases.iterator(); i.hasNext();) { if (context.getRenderResponse() || context.getResponseComplete()) { break; } executePhase((Phase) i.next(), context); } } /** * {@inheritDoc} */ public void render(final FacesContext context) { if (context == null) { throw new NullPointerException("FacesContext must not be null"); } if (log.isDebugEnabled()) { log.debug("render(" + context + ")"); } if (!context.getResponseComplete()) { executePhase(renderResponsePhase, context); } } /** * {@inheritDoc} */ public void addPhaseListener(final PhaseListener listener) { if (listener == null) { throw new NullPointerException("PhaseListener must not be null"); } if (log.isDebugEnabled()) { log.debug("addPhaseListener(" + listener.getPhaseId().toString() + "," + listener); } synchronized (listeners) { listeners.add(listener); } } /** * {@inheritDoc} */ public PhaseListener[] getPhaseListeners() { synchronized (listeners) { PhaseListener[] results = new PhaseListener[listeners.size()]; return ((PhaseListener[]) listeners.toArray(results)); } } /** * {@inheritDoc} */ public void removePhaseListener(final PhaseListener listener) { if (listener == null) { throw new NullPointerException("PhaseListener must not be null"); } if (log.isDebugEnabled()) { log.debug("removePhaseListener(" + listener.getPhaseId().toString() + "," + listener); } synchronized (listeners) { listeners.remove(listener); } } /** * Sets the phases to execute. * * @param executionPhases * phases to execute * @see Lifecycle#execute(FacesContext) */ public void setExecutionPhases(final Phase[] executionPhases) { Assert.notNull(executionPhases); this.executionPhases = Arrays.asList(executionPhases); } /** * Sets the render-response phase. * * @param renderResponsePhase * for rendering * @see Lifecycle#render(FacesContext) */ public void setRenderResponsePhase(final Phase renderResponsePhase) { Assert.notNull(renderResponsePhase); this.renderResponsePhase = renderResponsePhase; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -