001: /*
002: * Copyright 2004-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.springframework.webflow.executor.jsf;
017:
018: import javax.faces.context.ExternalContext;
019: import javax.faces.context.FacesContext;
020: import javax.faces.el.EvaluationException;
021:
022: import org.springframework.webflow.execution.FlowExecution;
023:
024: /**
025: * A static utility class for accessing the current flow execution holder.
026: * <p>
027: * By default, the current flow execution holder is stored associated with the current thread in the
028: * {@link FacesContext}'s {@link ExternalContext#getRequestMap()}.
029: *
030: * @author Keith Donald
031: */
032: public class FlowExecutionHolderUtils {
033:
034: /**
035: * Returns the current flow execution holder for the given faces context.
036: * @param context faces context
037: * @return the flow execution holder, or <code>null</code> if none set.
038: */
039: public static FlowExecutionHolder getFlowExecutionHolder(
040: FacesContext context) {
041: return (FlowExecutionHolder) context.getExternalContext()
042: .getRequestMap().get(getFlowExecutionHolderKey());
043: }
044:
045: /**
046: * Sets the current flow execution holder for the given faces context.
047: * @param holder the flow execution holder
048: * @param context faces context
049: */
050: public static void setFlowExecutionHolder(
051: FlowExecutionHolder holder, FacesContext context) {
052: context.getExternalContext().getRequestMap().put(
053: getFlowExecutionHolderKey(), holder);
054: }
055:
056: /**
057: * Returns true if the flow execution has been restored in the current thread.
058: * @param context the faces context
059: * @return true if restored, false otherwise
060: */
061: public static boolean isFlowExecutionRestored(FacesContext context) {
062: return getFlowExecutionHolder(context) != null;
063: }
064:
065: /**
066: * Returns the current flow execution in the given faces context.
067: * @param context faces context
068: * @return the flow execution or <code>null</code> if no execution is bound
069: */
070: public static FlowExecution getCurrentFlowExecution(
071: FacesContext context) {
072: FlowExecutionHolder holder = getFlowExecutionHolder(context);
073: if (holder != null) {
074: return holder.getFlowExecution();
075: } else {
076: return null;
077: }
078: }
079:
080: /**
081: * Returns the current required flow execution in the given faces context.
082: * @param context faces context
083: * @return the flow execution
084: * @throws EvaluationException if no flow execution was bound
085: */
086: public static FlowExecution getRequiredCurrentFlowExecution(
087: FacesContext context) throws EvaluationException {
088: FlowExecution execution = getCurrentFlowExecution(context);
089: if (execution != null) {
090: return execution;
091: } else {
092: throw new EvaluationException(
093: "No current FlowExecution bound to the Faces Context "
094: + "- was the current flow execution not restored before a view referenced it? "
095: + "Has the flow execution ended or expired?");
096: }
097: }
098:
099: /**
100: * Cleans up the current flow execution in the faces context if necessary.
101: * Specifically, handles unlocking the execution if necessary and setting the
102: * holder to null.
103: * @param context the faces context
104: */
105: public static void cleanupCurrentFlowExecution(FacesContext context) {
106: if (isFlowExecutionRestored(context)) {
107: getFlowExecutionHolder(context)
108: .unlockFlowExecutionIfNecessary();
109: context.getExternalContext().getRequestMap().remove(
110: getFlowExecutionHolderKey());
111: }
112: }
113:
114: /**
115: * Returns the key used to index the flow execution holder in the request
116: * attributes.
117: */
118: static String getFlowExecutionHolderKey() {
119: return FlowExecutionHolder.class.getName();
120: }
121: }
|