com.sun.faces.application.view.FaceletFullStateManagementStrategy Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jsf-impl Show documentation
Show all versions of jsf-impl Show documentation
This is the master POM file for Oracle's Implementation of the JSF 2.2 Specification.
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 1997-2013 Oracle and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
* or packager/legal/LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at packager/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* Oracle designates this particular file as subject to the "Classpath"
* exception as provided by Oracle in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
package com.sun.faces.application.view;
import com.sun.faces.context.StateContext;
import com.sun.faces.renderkit.RenderKitUtils;
import com.sun.faces.util.ComponentStruct;
import com.sun.faces.util.FacesLogger;
import com.sun.faces.util.Util;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.FacesException;
import javax.faces.application.ProjectStage;
import javax.faces.component.NamingContainer;
import javax.faces.component.UIComponent;
import javax.faces.component.UIForm;
import javax.faces.component.UIViewRoot;
import javax.faces.component.visit.VisitCallback;
import javax.faces.component.visit.VisitContext;
import javax.faces.component.visit.VisitHint;
import javax.faces.component.visit.VisitResult;
import javax.faces.context.FacesContext;
import javax.faces.render.ResponseStateManager;
import javax.faces.view.StateManagementStrategy;
import static com.sun.faces.RIConstants.DYNAMIC_ACTIONS;
import static com.sun.faces.RIConstants.DYNAMIC_COMPONENT;
import javax.faces.application.ViewHandler;
import javax.faces.view.ViewDeclarationLanguage;
/**
* A state management strategy for FSS.
*
* @author Manfred Riem ([email protected])
*/
public class FaceletFullStateManagementStrategy extends StateManagementStrategy {
/**
* Stores the logger.
*/
private static final Logger LOGGER = FacesLogger.APPLICATION_VIEW.getLogger();
/**
* Stores the skip hint.
*/
private static String SKIP_ITERATION_HINT = "javax.faces.visit.SKIP_ITERATION";
/**
* Stores the class map.
*/
private Map> classMap;
/**
* Are we in development mode.
*/
private boolean isDevelopmentMode;
/**
* Constructor.
*/
public FaceletFullStateManagementStrategy() {
this(FacesContext.getCurrentInstance());
}
/**
* Constructor.
*
* @param context the Faces context.
*/
public FaceletFullStateManagementStrategy(FacesContext context) {
isDevelopmentMode = context.isProjectStage(ProjectStage.Development);
classMap = new ConcurrentHashMap>(32);
}
/**
* Capture the child.
*
* @param tree the tree.
* @param parent the parent.
* @param c the component.
*/
private void captureChild(List tree, int parent, UIComponent c) {
if (!c.isTransient() && !c.getAttributes().containsKey(DYNAMIC_COMPONENT)) {
TreeNode n = new TreeNode(parent, c);
int pos = tree.size();
tree.add(n);
captureRest(tree, pos, c);
}
}
/**
* Capture the facet.
*
* @param tree the tree.
* @param parent the parent.
* @param name the facet name.
* @param c the component.
*/
private void captureFacet(List tree, int parent, String name, UIComponent c) {
if (!c.isTransient() && !c.getAttributes().containsKey(DYNAMIC_COMPONENT)) {
FacetNode n = new FacetNode(parent, name, c);
int pos = tree.size();
tree.add(n);
captureRest(tree, pos, c);
}
}
/**
* Capture the rest.
*
* @param tree the tree.
* @param pos the position.
* @param c the component.
*/
private void captureRest(List tree, int pos, UIComponent c) {
int sz = c.getChildCount();
if (sz > 0) {
List child = c.getChildren();
for (int i = 0; i < sz; i++) {
captureChild(tree, pos, child.get(i));
}
}
sz = c.getFacetCount();
if (sz > 0) {
for (Map.Entry entry : c.getFacets().entrySet()) {
captureFacet(tree, pos, entry.getKey(), entry.getValue());
}
}
}
/**
* Find the given component in the component tree.
*
* @param context the Faces context.
* @param clientId the client id of the component to find.
*/
private UIComponent locateComponentByClientId(final FacesContext context, final UIComponent subTree, final String clientId) {
if (LOGGER.isLoggable(Level.FINEST)) {
LOGGER.log(Level.FINEST, "FaceletFullStateManagementStrategy.locateComponentByClientId", clientId);
}
final List found = new ArrayList();
UIComponent result = null;
try {
context.getAttributes().put(SKIP_ITERATION_HINT, true);
Set hints = EnumSet.of(VisitHint.SKIP_ITERATION);
VisitContext visitContext = VisitContext.createVisitContext(context, null, hints);
subTree.visitTree(visitContext, new VisitCallback() {
public VisitResult visit(VisitContext visitContext, UIComponent component) {
VisitResult result = VisitResult.ACCEPT;
if (component.getClientId(visitContext.getFacesContext()).equals(clientId)) {
/*
* If the client id matches up we have found our match.
*/
found.add(component);
result = VisitResult.COMPLETE;
} else if (component instanceof UIForm) {
/*
* If the component is a UIForm and it is prepending its
* id then we can short circuit out of here if the the
* client id of the component we are trying to find does
* not begin with the id of the UIForm.
*/
UIForm form = (UIForm) component;
if (form.isPrependId() && !clientId.startsWith(form.getClientId(visitContext.getFacesContext()))) {
result = VisitResult.REJECT;
}
} else if (component instanceof NamingContainer
&& !clientId.startsWith(component.getClientId(visitContext.getFacesContext()))) {
/*
* If the component is a naming container then assume it
* is prepending its id so if our client id we are
* looking for does not start with the naming container
* id we can skip visiting this tree.
*/
result = VisitResult.REJECT;
}
return result;
}
});
} finally {
context.getAttributes().remove(SKIP_ITERATION_HINT);
}
if (!found.isEmpty()) {
result = found.get(0);
}
return result;
}
/**
* Create a new component instance.
*
* @param n the tree node.
* @return the UI component.
* @throws FacesException when a serious error occurs.
*/
private UIComponent newInstance(TreeNode n) throws FacesException {
if (LOGGER.isLoggable(Level.FINEST)) {
LOGGER.log(Level.FINEST, "FaceletFullStateManagementStrategy.newInstance", n.componentType);
}
try {
Class> t = ((classMap != null) ? classMap.get(n.componentType) : null);
if (t == null) {
t = Util.loadClass(n.componentType, n);
if (t != null && classMap != null) {
classMap.put(n.componentType, t);
} else {
if (!isDevelopmentMode) {
throw new NullPointerException();
}
}
}
assert (t != null);
UIComponent c = (UIComponent) t.newInstance();
c.setId(n.id);
return c;
} catch (Exception e) {
throw new FacesException(e);
}
}
/**
* Methods that takes care of pruning and re-adding an action to the dynamic
* action list.
*
*
* If you remove a component, re-add it to the same parent and then remove
* it again, you only have to capture the FIRST remove. Similarly if you add
* a component, remove it, and then re-add it to the same parent you only
* need to capture the LAST add.
*
* @param dynamicActionList the dynamic action list.
* @param struct the component struct to add.
*/
private void pruneAndReAddToDynamicActions(List dynamicActionList, ComponentStruct struct) {
if (LOGGER.isLoggable(Level.FINEST)) {
LOGGER.finest("FaceletFullStateManagementStrategy.pruneAndReAddToDynamicActions");
}
int firstIndex = dynamicActionList.indexOf(struct);
if (firstIndex == -1) {
dynamicActionList.add(struct);
} else {
int lastIndex = dynamicActionList.lastIndexOf(struct);
if (lastIndex == -1 || lastIndex == firstIndex) {
dynamicActionList.add(struct);
} else {
if (ComponentStruct.ADD.equals(struct.action)) {
dynamicActionList.remove(lastIndex);
dynamicActionList.remove(firstIndex);
dynamicActionList.add(struct);
}
if (ComponentStruct.REMOVE.equals(struct.action)) {
dynamicActionList.remove(lastIndex);
}
}
}
}
/**
* Restore the component state.
*
* @param context the Faces context.
* @param state the component state.
*/
private void restoreComponentState(final FacesContext context, final HashMap state) {
final StateContext stateContext = StateContext.getStateContext(context);
final UIViewRoot viewRoot = context.getViewRoot();
try {
context.getAttributes().put(SKIP_ITERATION_HINT, true);
Set hints = EnumSet.of(VisitHint.SKIP_ITERATION);
VisitContext visitContext = VisitContext.createVisitContext(context, null, hints);
viewRoot.visitTree(visitContext, new VisitCallback() {
public VisitResult visit(VisitContext visitContext, UIComponent component) {
VisitResult result = VisitResult.ACCEPT;
String cid = component.getClientId(context);
Object stateObj = state.get(cid);
if (stateObj != null && !stateContext.componentAddedDynamically(component)) {
boolean restoreStateNow = true;
if (stateObj instanceof StateHolderSaver) {
restoreStateNow = !((StateHolderSaver) stateObj).componentAddedDynamically();
}
if (restoreStateNow) {
try {
component.restoreState(context, stateObj);
} catch (Exception e) {
throw new FacesException(e);
}
}
}
return result;
}
});
} finally {
context.getAttributes().remove(SKIP_ITERATION_HINT);
}
}
/**
* Restore the list of dynamic actions and replay them.
*
* @param context the Faces context.
* @param stateContext the state context.
* @param stateMap the state.
* @param viewRoot the view root.
*/
private void restoreDynamicActions(FacesContext context, StateContext stateContext, HashMap state) {
if (LOGGER.isLoggable(Level.FINEST)) {
LOGGER.finest("FaceletFullStateManagementStrategy.restoreDynamicActions");
}
UIViewRoot viewRoot = context.getViewRoot();
List