Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * $Id: StateManager.java,v 1.40 2006/09/01 16:56:30 rlubke Exp $
   */
  
  /*
   * The contents of this file are subject to the terms
   * of the Common Development and Distribution License
   * (the License). You may not use this file except in
   * compliance with the License.
  * 
  * You can obtain a copy of the License at
  * https://javaserverfaces.dev.java.net/CDDL.html or
  * legal/CDDLv1.0.txt. 
  * See the License for the specific language governing
  * permission and limitations under the License.
  * 
  * When distributing Covered Code, include this CDDL
  * Header Notice in each file and include the License file
  * at legal/CDDLv1.0.txt.    
  * If applicable, add the following below the CDDL Header,
  * with the fields enclosed by brackets [] replaced by
  * your own identifying information:
  * "Portions Copyrighted [year] [name of copyright owner]"
  * 
  * [Name of File] [ver.__] [Date]
  * 
  * Copyright 2005 Sun Microsystems Inc. All Rights Reserved
  */
 
 package javax.faces.application;
 
 

StateManager directs the process of saving and restoring the view between requests. The StateManager instance for an application is retrieved from the Application instance, and thus cannot know any details of the markup language created by the javax.faces.render.RenderKit being used to render a view. The StateManager utilizes a helper object (javax.faces.render.ResponseStateManager), that is provided by the javax.faces.render.RenderKit implementation and is therefore aware of the markup language details.

 
 
 public abstract class
       StateManager {
 
     // ------------------------------------------------------ Manifest Constants
 

    

The ServletContext init parameter consulted by the StateManager to tell where the state should be saved. Valid values are given as the values of the constants: STATE_SAVING_METHOD_CLIENT or STATE_SAVING_METHOD_SERVER.

If this parameter is not specified, the default value is the value of the constant STATE_SAVING_METHOD_CLIENT.

 
     public static final String STATE_SAVING_METHOD_PARAM_NAME =
           "javax.faces.STATE_SAVING_METHOD";


    

Constant value for the initialization parameter named by the STATE_SAVING_METHOD_PARAM_NAME that indicates state saving should take place on the client.

 
     public static final String STATE_SAVING_METHOD_CLIENT = "client";


    

Constant value for the initialization parameter named by the STATE_SAVING_METHOD_PARAM_NAME that indicates state saving should take place on the server.

 
     public static final String STATE_SAVING_METHOD_SERVER = "server";
 
     // ---------------------------------------------------- State Saving Methods
 

    

Return the tree structure and component state information for the view contained in the specified javax.faces.context.FacesContext instance as an object of type StateManager.SerializedView. If there is no state information to be saved, return null instead.

Components may opt out of being included in the serialized view by setting their transient property to true. This must cause the component itself, as well as all of that component's children and facets, to be omitted from the saved tree structure and component state information.

This method must also enforce the rule that, for components with non-null ids, all components that are descendants of the same nearest javax.faces.component.NamingContainer must have unique identifiers.

Deprecated:
this has been replaced by saveView(javax.faces.context.FacesContext). The default implementation returns null.
Parameters:
context javax.faces.context.FacesContext for the current request
Throws:
java.lang.IllegalStateException if more than one component or facet within the same javax.faces.component.NamingContainer in this view has the same non-null component id
    public SerializedView saveSerializedView(FacesContext context) {
        return null;
    }

    

Return an opaque Object containing sufficient information for this same instance to restore the state of the current javax.faces.component.UIViewRoot on a subsequent request. The returned object must implement java.io.Serializable. If there is no state information to be saved, return null instead.

Components may opt out of being included in the serialized view by setting their transient property to true. This must cause the component itself, as well as all of that component's children and facets, to be omitted from the saved tree structure and component state information.

This method must also enforce the rule that, for components with non-null ids, all components that are descendants of the same nearest javax.faces.component.NamingContainer must have unique identifiers.

For backwards compatability with existing StateManager implementations, the default implementation of this method calls saveSerializedView(javax.faces.context.FacesContext) and creates and returns a two element Object array with element zero containing the structure property and element one containing the state property of the SerializedView.

Parameters:
context javax.faces.context.FacesContext for the current request
Throws:
java.lang.IllegalStateException if more than one component or facet within the same javax.faces.component.NamingContainer in this view has the same non-null component id
Since:
1.2
    public Object saveView(FacesContext context) {
        SerializedView view = saveSerializedView(context);
        Object stateArray[] = {view.getStructure(),
                               view.getState()};
        return stateArray;
    }


    

Convenience method, which must be called by saveSerializedView(), to construct and return a Serializable object that represents the structure of the entire component tree (including children and facets) of this view.

Components may opt-out of being included in the tree structure by setting their transient property to true. This must cause the component itself, as well as all of that component's children and facets, to be omitted from the saved tree structure information.

Deprecated:
the distinction between tree structure and component state is now an implementation detail. The default implementation returns null.
Parameters:
context javax.faces.context.FacesContext for the current request
    protected Object getTreeStructureToSave(FacesContext context) {
        return null;
    }


    

Convenience method, which must be called by saveSerializedView(), to construct and return a Serializable object that represents the state of all component properties, attributes, and attached objects, for the entire component tree (including children and facets) of this view.

Components may opt-out of being included in the component state by setting their transient property to true. This must cause the component itself, as well as all of that component's children and facets, to be omitted from the saved component state information.

Deprecated:
the distinction between tree structure and component state is now an implementation detail. The default implementation returns null.
Parameters:
context javax.faces.context.FacesContext for the current request
    protected Object getComponentStateToSave(FacesContext context) {
        return null;
    }

    

Save the state represented in the specified state Object instance, in an implementation dependent manner.

This method will typically simply delegate the actual writing to the writeState() method of the javax.faces.render.ResponseStateManager instance provided by the javax.faces.render.RenderKit being used to render this view. This method assumes that the caller has positioned the javax.faces.context.ResponseWriter at the correct position for the saved state to be written.

For backwards compatability with existing StateManager implementations, the default implementation of this method checks if the argument is an instance of Object [] of length greater than or equal to two. If so, it creates a SerializedView instance with the tree structure coming from element zero and the component state coming from element one and calls through to writeState(javax.faces.context.FacesContext,javax.faces.application.StateManager.SerializedView). If not, does nothing.

Parameters:
context javax.faces.context.FacesContext for the current request
state the Serializable state to be written, as returned by saveSerializedView(javax.faces.context.FacesContext)
Since:
1.2
    public void writeState(FacesContext contextObject state)
          throws IOException {
        if (null != state && state.getClass().isArray() &&
            state.getClass().getComponentType().equals(Object.class)) {
            Object stateArray[] = (Object[]) state;
            if (2 == stateArray.length) {
                SerializedView view = new SerializedView(stateArray[0],
                                                         stateArray[1]);
                writeState(contextview);
            }
        }
    }

    

Save the state represented in the specified SerializedView isntance, in an implementation dependent manner.

This method must consult the context initialization parameter named by the symbolic constant StateManager.STATE_SAVING_METHOD_PARAMETER_NAME to determine whether state should be saved on the client or the server. If not present, client side state saving is assumed.

If the init parameter indicates that client side state saving should be used, this method must delegate the actual writing to the writeState() method of the javax.faces.render.ResponseStateManager instance provided by the javax.faces.render.RenderKit being used to render this view. This method assumes that the caller has positioned the javax.faces.context.ResponseWriter at the correct position for the saved state to be written.

Deprecated:
This method has been replaced by writeState(javax.faces.context.FacesContext,java.lang.Object). The default implementation of this method does nothing.
Parameters:
context javax.faces.context.FacesContext for the current request
state the serialized state to be written
    public void writeState(FacesContext context,
                           SerializedView statethrows IOException {
    }
    // ------------------------------------------------- State Restoring Methods


    

Restore the tree structure and the component state of the view for the specified viewId, in an implementation dependent manner, and return the restored javax.faces.component.UIViewRoot. If there is no saved state information available for this viewId, return null instead.

This method must consult the context initialization parameter named by the symbolic constant StateManager.STATE_SAVING_METHOD_PARAMETER_NAME to determine whether state should be saved on the client or the server. If not present, client side state saving is assumed.

If the init parameter indicates that client side state saving should be used, this method must call the getTreeStructureToRestore() and (if the previous method call returned a non-null value) getComponentStateToRestore() methods of the javax.faces.render.ResponseStateManager instance provided by the javax.faces.render.RenderKit responsible for this view.

Parameters:
context javax.faces.context.FacesContext for the current request
viewId View identifier of the view to be restored
renderKitId the renderKitId used to render this response. Must not be null.
Throws:
java.lang.IllegalArgumentException if renderKitId is null.
    public abstract UIViewRoot restoreView(FacesContext contextString viewId,
                                           String renderKitId);


    

Convenience method, which must be called by restoreView(), to construct and return a javax.faces.component.UIViewRoot instance (populated with children and facets) representing the tree structure of the component tree being restored. If no saved state information is available, return null instead.

Deprecated:
the distinction between tree structure and component state is now an implementation detail. The default implementation returns null.
Parameters:
context javax.faces.context.FacesContext for the current request
viewId View identifier of the view to be restored
renderKitId the renderKitId used to render this response. Must not be null.
Throws:
java.lang.IllegalArgumentException if renderKitId is null.
    protected UIViewRoot restoreTreeStructure(FacesContext context,
                                              String viewId,
                                              String renderKitId) {
        return null;
    }


    

Convenience method, which must be called by restoreView(), to restore the attributes, properties, and attached objects of all components in the restored component tree.

Deprecated:
the distinction between tree structure and component state is now an implementation detail. The default implementation does nothing.
Parameters:
context javax.faces.context.FacesContext for the current request
viewRoot javax.faces.component.UIViewRoot returned by a previous call to restoreTreeStructure()
renderKitId the renderKitId used to render this response. Must not be null.
Throws:
java.lang.IllegalArgumentException if renderKitId is null.
    protected void restoreComponentState(FacesContext context,
                                         UIViewRoot viewRoot,
                                         String renderKitId) {
    }
    private Boolean savingStateInClient = null;

    

Returns:
true if and only if the value of the ServletContext init parameter named by the value of the constant STATE_SAVING_METHOD_PARAM_NAME is equal to the value of the constant STATE_SAVING_METHOD_CLIENT. false otherwise.
Throws:
java.lang.NullPointerException if context is null.
    public boolean isSavingStateInClient(FacesContext context) {
        if (null != ) {
            return .booleanValue();
        }
         = .;
        String saveStateParam = context.getExternalContext().
        if (saveStateParam != null &&
            saveStateParam.equalsIgnoreCase()) {
             = .;
        }
        return .booleanValue();
    }

    

Convenience struct for encapsulating tree structure and component state. This is necessary to allow the API to be flexible enough to work in JSP and non-JSP environments.

Deprecated:
This class was not marked Serializable in the 1.0 version of the spec. It was also not a static inner class, so it can't be made to be Serializable. Therefore, it is being deprecated in version 1.2 of the spec. The replacement is to use an implementation dependent Object.
    public class SerializedView extends Object {
        private Object structure = null;
        private Object state = null;
        public SerializedView(Object newStructureObject newState) {
             = newStructure;
             = newState;
        }
        public Object getStructure() {
            return ;
        }
        public Object getState() {
            return ;
        }
    }
New to GrepCode? Check out our FAQ X