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 jakarta.faces Show documentation
Show all versions of jakarta.faces Show documentation
EE4J Compatible Implementation for Jakarta Faces API
The newest version!
/*
* Copyright (c) 2023 Contributors to Eclipse Foundation.
* Copyright (c) 1997, 2020 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package com.sun.faces.application.view;
import static com.sun.faces.RIConstants.DYNAMIC_ACTIONS;
import static com.sun.faces.RIConstants.DYNAMIC_COMPONENT;
import static com.sun.faces.util.ComponentStruct.ADD;
import static com.sun.faces.util.ComponentStruct.REMOVE;
import static com.sun.faces.util.Util.isAnyNull;
import static com.sun.faces.util.Util.isEmpty;
import static com.sun.faces.util.Util.loadClass;
import static jakarta.faces.application.ProjectStage.Development;
import static jakarta.faces.component.visit.VisitHint.SKIP_ITERATION;
import static jakarta.faces.component.visit.VisitResult.ACCEPT;
import static jakarta.faces.component.visit.VisitResult.COMPLETE;
import static jakarta.faces.component.visit.VisitResult.REJECT;
import static java.util.logging.Level.FINEST;
import static java.util.logging.Level.WARNING;
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 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 jakarta.faces.FacesException;
import jakarta.faces.application.ViewHandler;
import jakarta.faces.component.NamingContainer;
import jakarta.faces.component.UIComponent;
import jakarta.faces.component.UIForm;
import jakarta.faces.component.UIViewRoot;
import jakarta.faces.component.visit.VisitContext;
import jakarta.faces.component.visit.VisitHint;
import jakarta.faces.component.visit.VisitResult;
import jakarta.faces.context.FacesContext;
import jakarta.faces.render.ResponseStateManager;
import jakarta.faces.view.StateManagementStrategy;
import jakarta.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 final Set SKIP_ITERATION_HINT = EnumSet.of(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(Development);
classMap = new ConcurrentHashMap<>(32);
}
/**
* Capture the child.
*
* @param tree the tree.
* @param parent the parent.
* @param component the component.
*/
private void captureChild(List tree, int parent, UIComponent component) {
if (!component.isTransient() && !component.getAttributes().containsKey(DYNAMIC_COMPONENT)) {
TreeNode treeNode = new TreeNode(parent, component);
int pos = tree.size();
tree.add(treeNode);
captureRest(tree, pos, component);
}
}
/**
* Capture the facet.
*
* @param tree the tree.
* @param parent the parent.
* @param name the facet name.
* @param component the component.
*/
private void captureFacet(List tree, int parent, String name, UIComponent component) {
if (!component.isTransient() && !component.getAttributes().containsKey(DYNAMIC_COMPONENT)) {
FacetNode facetNode = new FacetNode(parent, name, component);
int pos = tree.size();
tree.add(facetNode);
captureRest(tree, pos, component);
}
}
/**
* Capture the rest.
*
* @param tree the tree.
* @param pos the position.
* @param component the component.
*/
private void captureRest(List tree, int pos, UIComponent component) {
int sz = component.getChildCount();
if (sz > 0) {
List child = component.getChildren();
for (int i = 0; i < sz; i++) {
captureChild(tree, pos, child.get(i));
}
}
sz = component.getFacetCount();
if (sz > 0) {
for (Map.Entry entry : component.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(FINEST)) {
LOGGER.log(FINEST, "FaceletFullStateManagementStrategy.locateComponentByClientId", clientId);
}
final List found = new ArrayList<>();
UIComponent result = null;
VisitContext visitContext = VisitContext.createVisitContext(context, null, SKIP_ITERATION_HINT);
subTree.visitTree(visitContext, (visitContext1, component) -> {
VisitResult result1 = ACCEPT;
if (component.getClientId(visitContext1.getFacesContext()).equals(clientId)) {
/*
* If the client id matches up we have found our match.
*/
found.add(component);
result1 = 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(visitContext1.getFacesContext()))) {
result1 = REJECT;
}
} else if (component instanceof NamingContainer && !clientId.startsWith(component.getClientId(visitContext1.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.
*/
result1 = REJECT;
}
return result1;
});
if (!found.isEmpty()) {
result = found.get(0);
}
return result;
}
/**
* Create a new component instance.
*
* @param treeNode the tree node.
* @return the UI component.
* @throws FacesException when a serious error occurs.
*/
private UIComponent newInstance(TreeNode treeNode) throws FacesException {
if (LOGGER.isLoggable(FINEST)) {
LOGGER.log(FINEST, "FaceletFullStateManagementStrategy.newInstance", treeNode.componentType);
}
try {
Class> clazz = classMap != null ? classMap.get(treeNode.componentType) : null;
if (clazz == null) {
clazz = loadClass(treeNode.componentType, treeNode);
if (!isAnyNull(clazz, classMap)) {
classMap.put(treeNode.componentType, clazz);
} else {
if (!isDevelopmentMode) {
throw new NullPointerException();
}
}
}
UIComponent component = (UIComponent) clazz.getDeclaredConstructor().newInstance();
component.setId(treeNode.id);
return component;
} catch (NullPointerException | IllegalArgumentException | ReflectiveOperationException | SecurityException 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 (ADD.equals(struct.getAction())) {
dynamicActionList.remove(lastIndex);
dynamicActionList.remove(firstIndex);
dynamicActionList.add(struct);
}
if (REMOVE.equals(struct.getAction())) {
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();
VisitContext visitContext = VisitContext.createVisitContext(context, null, SKIP_ITERATION_HINT);
viewRoot.visitTree(visitContext, (visitContext1, component) -> {
VisitResult result = ACCEPT;
String clientId = component.getClientId(context);
Object stateObj = state.get(clientId);
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;
});
}
/**
* 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(FINEST)) {
LOGGER.finest("FaceletFullStateManagementStrategy.restoreDynamicActions");
}
@SuppressWarnings("unchecked")
List