Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
   *
   * Copyright (c) 1997-2010 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 javax.faces.context;
 
 
 import java.util.List;
 import java.util.Map;
 
 

FacesContext 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.

A FacesContext instance is associated with a particular request at the beginning of request processing, by a call to the getFacesContext() method of the FacesContextFactory instance associated with the current web application. The instance remains active until its release() method is called, after which no further references to this instance are allowed. While a 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.

 
 
 public abstract class FacesContext {
 
 
     @SuppressWarnings({"UnusedDeclaration"})
     private FacesContext defaultFacesContext;
     private boolean processingEvents = true;
     private boolean isCreatedFromValidFactory = true;
 
     private static ConcurrentHashMap threadInitContext = new ConcurrentHashMap(2);
     private static ConcurrentHashMap initContextServletContext = new ConcurrentHashMap(2);
 
     public FacesContext() {
         Thread curThread = Thread.currentThread();
         StackTraceElement[] callstack = curThread.getStackTrace();
         if (null != callstack) {
             String declaringClassName = callstack[3].getClassName();
             try {
                 ClassLoader curLoader = curThread.getContextClassLoader();
                 Class declaringClass = curLoader.loadClass(declaringClassName);
                if (!FacesContextFactory.class.isAssignableFrom(declaringClass)) {
                     = false;
                }
            } catch (ClassNotFoundException cnfe) {
            }
        }
    }
    // -------------------------------------------------------------- Properties


    

Return the javax.faces.application.Application instance associated with this web application.

It is valid to call this method during application startup or shutdown. If called during application startup or shutdown, returns the correct current javax.faces.application.Application instance.

Throws:
java.lang.IllegalStateException if this method is called after this instance has been released
    public abstract Application getApplication();
    
    
    

Return a mutable Map representing the attributes associated wth this FacesContext instance. This Map is useful to store attributes that you want to go out of scope when the Faces lifecycle for the current request ends, which is not always the same as the request ending, especially in the case of Servlet filters that are invoked after the Faces lifecycle for this request completes. Accessing this Map does not cause any events to fire, as is the case with the other maps: for request, session, and application scope. When release() is invoked, the attributes must be cleared.

The Map returned by this method is not associated with the request. If you would like to get or set request attributes, see ExternalContext.getRequestMap().

The default implementation throws UnsupportedOperationException and is provided for the sole purpose of not breaking existing applications that extend this class.

Throws:
java.lang.IllegalStateException if this method is called after this instance has been released
Since:
2.0
    public Map<ObjectObjectgetAttributes() {
        if ( != null) {
            return .getAttributes();
        }
        if (!) {
            if ( == null) {
                 = new HashMap<ObjectObject>();
            }
            return ;
        }
        throw new UnsupportedOperationException();
 
    }

Return the PartialViewContext for this request. The PartialViewContext is used to control the processing of specified components during the execute portion of the request processing lifecycle (known as partial processing) and the rendering of specified components (known as partial rendering). This method must return a new PartialViewContext if one does not already exist.

Throws:
java.lang.IllegalStateException if this method is called after this instance has been released
Since:
2.0
      
        if ( != null) {
            return .getPartialViewContext();
        }
        if (!) {
            if ( == null) {
                PartialViewContextFactory f = (PartialViewContextFactory)
                      FactoryFinder.getFactory(.);
            }
        }
        throw new UnsupportedOperationException();
    }

Return an Iterator over the client identifiers for which at least one javax.faces.application.FacesMessage has been queued. If there are no such client identifiers, an empty Iterator is returned. If any messages have been queued that were not associated with any specific client identifier, a null value will be included in the iterated values. The elements in the Iterator must be returned in the order in which they were added with addMessage(java.lang.String,javax.faces.application.FacesMessage).

Throws:
java.lang.IllegalStateException if this method is called after this instance has been released
    public abstract Iterator<StringgetClientIdsWithMessages();

    

Return the ELContext instance for this FacesContext instance. This ELContext instance has the same lifetime and scope as the FacesContext instance with which it is associated, and may be created lazily the first time this method is called for a given FacesContext instance. Upon creation of the ELContext instance, the implementation must take the following action:

Throws:
java.lang.IllegalStateException if this method is called after this instance has been released
Since:
1.2
 
    public ELContext getELContext() {
        if ( != null) {
            return .getELContext();
        }
        throw new UnsupportedOperationException();
    }


    

Return the ExceptionHandler for this request.

 
        if ( != null) {
            return .getExceptionHandler();
        }
        throw new UnsupportedOperationException();
    }

    
    

Set the ExceptionHandler for this request.

Parameters:
exceptionHandler the ExceptionHandler for this request.
 
    public void setExceptionHandler(ExceptionHandler exceptionHandler) {
        if ( != null) {
            .setExceptionHandler(exceptionHandler);
        } else {
           throw new UnsupportedOperationException();
        }
        
    }


    

Return the ExternalContext instance for this FacesContext instance.

It is valid to call this method during application startup or shutdown. If called during application startup or shutdown, this method returns an ExternalContext instance with the special behaviors indicated in the javadoc for that class. Methods document as being valid to call during application startup or shutdown must be supported.

Throws:
java.lang.IllegalStateException if this method is called after this instance has been released
    public abstract ExternalContext getExternalContext();


    

Return the maximum severity level recorded on any javax.faces.application.FacesMessages that has been queued, whether or not they are associated with any specific UIComponent. If no such messages have been queued, return null.

Throws:
java.lang.IllegalStateException if this method is called after this instance has been released
    public abstract Severity getMaximumSeverity();


    

Return an Iterator over the javax.faces.application.FacesMessages that have been queued, whether or not they are associated with any specific client identifier. If no such messages have been queued, return an empty Iterator. The elements of the Iterator must be returned in the order in which they were added with calls to addMessage(java.lang.String,javax.faces.application.FacesMessage).

Throws:
java.lang.IllegalStateException if this method is called after this instance has been released
    public abstract Iterator<FacesMessagegetMessages();

    

Like getMessages(), but returns a List<FacesMessage>, enabling use from EL expressions.

The default implementation throws UnsupportedOperationException and is provided for the sole purpose of not breaking existing applications that extend this class.

Returns:
an immutable List which is effectively a snapshot of the messages present at the time of invocation.
Throws:
java.lang.IllegalStateException if this method is called after this instance has been released
Since:
2.0
 
    public List<FacesMessagegetMessageList() {
        if ( != null) {
            return .getMessageList();
        }
        throw new UnsupportedOperationException();
    }


    

Like getMessages(java.lang.String), but returns a List<FacesMessage> of messages for the component with client id matching argument clientId.

The default implementation throws UnsupportedOperationException and is provided for the sole purpose of not breaking existing applications that extend this class.

Returns:
an immutable List which is effectively a snapshot of the messages present at the time of invocation.
Throws:
java.lang.IllegalStateException if this method is called after this instance has been released
Since:
2.0
 
    public List<FacesMessagegetMessageList(String clientId) {
        if ( != null) {
            return .getMessageList(clientId);
        }
        throw new UnsupportedOperationException();
    }




    

Return an Iterator over the javax.faces.application.FacesMessages that have been queued that are associated with the specified client identifier (if clientId is not null), or over the javax.faces.application.FacesMessages that have been queued that are not associated with any specific client identifier (if clientId is null). If no such messages have been queued, return an empty Iterator. The elements of the Iterator must be returned in the order in which they were added with calls to addMessage(java.lang.String,javax.faces.application.FacesMessage).

Parameters:
clientId The client identifier for which messages are requested, or null for messages not associated with any client identifier
Throws:
java.lang.IllegalStateException if this method is called after this instance has been released
    public abstract Iterator<FacesMessagegetMessages(String clientId);


    

Return the javax.faces.render.RenderKit instance for the render kit identifier specified on our javax.faces.component.UIViewRoot, if there is one. If there is no current javax.faces.component.UIViewRoot, if the javax.faces.component.UIViewRoot does not have a specified renderKitId, or if there is no javax.faces.render.RenderKit for the specified identifier, return null instead.

Throws:
java.lang.IllegalStateException if this method is called after this instance has been released
    public abstract RenderKit getRenderKit();


    

Return true if the renderResponse() method has been called for the current request.

Throws:
java.lang.IllegalStateException if this method is called after this instance has been released
    public abstract boolean getRenderResponse();


    

Return true if the responseComplete() method has been called for the current request.

Throws:
java.lang.IllegalStateException if this method is called after this instance has been released
    public abstract boolean getResponseComplete();

    

Return true if the validationFailed() method has been called for the current request.

Throws:
java.lang.IllegalStateException if this method is called after this instance has been released
    public boolean isValidationFailed() {
        if ( != null) {
            return .isValidationFailed();
        }
        throw new UnsupportedOperationException();
        
    }

    

Return the ResponseStream to which components should direct their binary output. Within a given response, components can use either the ResponseStream or the ResponseWriter, but not both.

Throws:
java.lang.IllegalStateException if this method is called after this instance has been released
    public abstract ResponseStream getResponseStream();


    

Set the ResponseStream to which components should direct their binary output.

Parameters:
responseStream The new ResponseStream for this response
Throws:
java.lang.NullPointerException if responseStream is null
java.lang.IllegalStateException if this method is called after this instance has been released
    public abstract void setResponseStream(ResponseStream responseStream);


    

Return the ResponseWriter to which components should direct their character-based output. Within a given response, components can use either the ResponseStream or the ResponseWriter, but not both.

Throws:
java.lang.IllegalStateException if this method is called after this instance has been released
    public abstract ResponseWriter getResponseWriter();


    

Set the ResponseWriter to which components should direct their character-based output.

Parameters:
responseWriter The new ResponseWriter for this response
Throws:
java.lang.IllegalStateException if this method is called after this instance has been released
java.lang.NullPointerException if responseWriter is null
    public abstract void setResponseWriter(ResponseWriter responseWriter);


    

Return the root component that is associated with the this request.

It is valid to call this method during application startup or shutdown. If called during application startup or shutdown, this method returns a new UIViewRoot with its locale set to Locale.getDefault().

Throws:
java.lang.IllegalStateException if this method is called after this instance has been released
    public abstract UIViewRoot getViewRoot();


    

Set the root component that is associated with this request.

This method can be called by the application handler (or a class that the handler calls), during the Invoke Application phase of the request processing lifecycle and during the Restore View phase of the request processing lifecycle (especially when a new root component is created). In the present version of the specification, implementations are not required to enforce this restriction, though a future version of the specification may require enforcement.

If the current UIViewRoot is non-null, and calling equals() on the argument root, passing the current UIViewRoot returns false, the clear method must be called on the Map returned from javax.faces.component.UIViewRoot.getViewMap().

Parameters:
root The new component javax.faces.component.UIViewRoot component
Throws:
java.lang.IllegalStateException if this method is called after this instance has been released
java.lang.NullPointerException if root is null
    public abstract void setViewRoot(UIViewRoot root);
    // ---------------------------------------------------------- Public Methods


    

Append a javax.faces.application.FacesMessage to the set of messages associated with the specified client identifier, if clientId is not null. If clientId is null, this javax.faces.application.FacesMessage is assumed to not be associated with any specific component instance.

Parameters:
clientId The client identifier with which this message is associated (if any)
message The message to be appended
Throws:
java.lang.IllegalStateException if this method is called after this instance has been released
java.lang.NullPointerException if message is null
    public abstract void addMessage(String clientIdFacesMessage message);

    

Return a flag indicating if the resources associated with this FacesContext instance have been released.

Returns:
true if the resources have been released.
Since:
2.1
  
    public boolean isReleased() {
        if ( != null) {
            return .isReleased();
        }
        throw new UnsupportedOperationException();
    }

    

Release any resources associated with this FacesContext instance. Faces implementations may choose to pool instances in the associated FacesContextFactory to avoid repeated object creation and garbage collection. After release() is called on a FacesContext instance (until the FacesContext instance has been recycled by the implementation for re-use), calling any other methods will cause an IllegalStateException to be thrown.

If a call was made to getAttributes() during the processing for this request, the implementation must call clear() on the Map returned from getAttributes(), and then de-allocate the data-structure behind that Map.

The implementation must call setCurrentInstance(javax.faces.context.FacesContext) passing null to remove the association between this thread and this dead FacesContext instance.

Throws:
java.lang.IllegalStateException if this method is called after this instance has been released
    public abstract void release();


    

Signal the JavaServer faces implementation that, as soon as the current phase of the request processing lifecycle has been completed, control should be passed to the Render Response phase, bypassing any phases that have not been executed yet.

Throws:
java.lang.IllegalStateException if this method is called after this instance has been released
    public abstract void renderResponse();

    

This utility method simply returns the result of javax.faces.render.ResponseStateManager.isPostback(javax.faces.context.FacesContext).

The default implementation throws UnsupportedOperationException and is provided for the sole purpose of not breaking existing applications that extend this class.

Throws:
java.lang.IllegalStateException if this method is called after this instance has been released
Since:
2.0
    public boolean isPostback() {
        if ( != null) {
            return .isPostback();
        }
        throw new UnsupportedOperationException();
    }


    

Signal the JavaServer Faces implementation that the HTTP response for this request has already been generated (such as an HTTP redirect), and that the request processing lifecycle should be terminated as soon as the current phase is completed.

Throws:
java.lang.IllegalStateException if this method is called after this instance has been released
    public abstract void responseComplete();
    
    

Sets a flag which indicates that a conversion or validation error occurred while processing the inputs. Inputs consist of either page parameters or form bindings. This flag can be read using isValidationFailed().

Throws:
java.lang.IllegalStateException if this method is called after this instance has been released
    public void validationFailed() {
        if ( != null) {
            .validationFailed();
        } else {
            throw new UnsupportedOperationException();
        }
        
    }

    

Return the value last set on this FacesContext instance when setCurrentPhaseId(javax.faces.event.PhaseId) was called.

Throws:
java.lang.IllegalStateException if this method is called after this instance has been released
Since:
2.0
    public PhaseId getCurrentPhaseId() {
        if ( != null) {
            return .getCurrentPhaseId();
        }
        if (!) {
            return this.;
        }
        throw new UnsupportedOperationException();
    }
    
    

The implementation must call this method at the earliest possble point in time after entering into a new phase in the request processing lifecycle.

Parameters:
currentPhaseId The javax.faces.event.PhaseId for the current phase.
Throws:
java.lang.IllegalStateException if this method is called after this instance has been released
Since:
2.0
    public void setCurrentPhaseId(PhaseId currentPhaseId) {
        if ( != null) {
            .setCurrentPhaseId(currentPhaseId);
        } else if (!) {
            this. = currentPhaseId;
        } else {
            throw new UnsupportedOperationException();
        }
    }
    public void setProcessingEvents(boolean processingEvents) {
        this. = processingEvents;    
    }


    

Returns a flag indicating whether or not the runtime should publish events when asked to do so.

Returns:
true if events should be published, otherwise false
    public boolean isProcessingEvents() {
        return this.;
    }


    

Return true if the current javax.faces.application.ProjectStage as returned by the javax.faces.application.Application instance is equal to stage, otherwise return false

Parameters:
stage the javax.faces.application.ProjectStage to check
Throws:
java.lang.IllegalStateException if this method is called after this instance has been released
java.lang.NullPointerException if stage is null
    public boolean isProjectStage(ProjectStage stage) {
        if (stage == null) {
            throw new NullPointerException();
        }
        return (stage.equals(getApplication().getProjectStage()));
    }
    
    // ---------------------------------------------------------- Static Methods


    

The ThreadLocal variable used to record the FacesContext instance for each processing thread.

    private static ThreadLocal<FacesContextinstance = new ThreadLocal<FacesContext>() {
            protected FacesContext initialValue() { return (null); }
        };


    

Return the FacesContext instance for the request that is being processed by the current thread. If called during application initialization or shutdown, any method documented as "valid to call this method during application startup or shutdown" must be supported during application startup or shutdown time. The result of calling a method during application startup or shutdown time that does not have this designation is undefined.

    public static FacesContext getCurrentInstance() {
        FacesContext facesContext = .get();
        if (null == facesContext) {
            facesContext = (FacesContext).get(Thread.currentThread());
        }
        return facesContext;
    }


    

Set the FacesContext instance for the request that is being processed by the current thread.

Parameters:
context The FacesContext instance for the current thread, or null if this thread no longer has a FacesContext instance.
    protected static void setCurrentInstance(FacesContext context) {
        if (context == null) {
            .remove();
        } else {
            .set(context);
        }
    }
New to GrepCode? Check out our FAQ X