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.FacesContext;
019:
020: import org.springframework.binding.collection.SharedMapDecorator;
021: import org.springframework.core.style.ToStringCreator;
022: import org.springframework.webflow.context.ExternalContext;
023: import org.springframework.webflow.core.collection.LocalAttributeMap;
024: import org.springframework.webflow.core.collection.LocalParameterMap;
025: import org.springframework.webflow.core.collection.LocalSharedAttributeMap;
026: import org.springframework.webflow.core.collection.MutableAttributeMap;
027: import org.springframework.webflow.core.collection.ParameterMap;
028: import org.springframework.webflow.core.collection.SharedAttributeMap;
029:
030: /**
031: * Provides contextual information about a JSF environment that has interacted with SWF.
032: *
033: * @author Keith Donald
034: */
035: public class JsfExternalContext implements ExternalContext {
036:
037: /**
038: * The JSF Faces context.
039: */
040: private FacesContext facesContext;
041:
042: /**
043: * The id of the action or "command button" that fired.
044: */
045: private String actionId;
046:
047: /**
048: * The action outcome.
049: */
050: private String outcome;
051:
052: /**
053: * An accessor for the JSF request parameter map.
054: */
055: private ParameterMap requestParameterMap;
056:
057: /**
058: * An accessor for the JSF request attribute map.
059: */
060: private MutableAttributeMap requestMap;
061:
062: /**
063: * An accessor for the JSF session map.
064: */
065: private SharedAttributeMap sessionMap;
066:
067: /**
068: * An accessor for the JSF application map.
069: */
070: private SharedAttributeMap applicationMap;
071:
072: /**
073: * Creates a JSF External Context.
074: * @param facesContext the JSF faces context
075: */
076: public JsfExternalContext(FacesContext facesContext) {
077: this .facesContext = facesContext;
078: initMaps(facesContext);
079: }
080:
081: /**
082: * Initializes parameter and attribute maps from context data structures.
083: * @param facesContext the faces context
084: */
085: private void initMaps(FacesContext facesContext) {
086: this .requestParameterMap = new LocalParameterMap(facesContext
087: .getExternalContext().getRequestParameterMap());
088: this .requestMap = new LocalAttributeMap(facesContext
089: .getExternalContext().getRequestMap());
090: this .sessionMap = new LocalSharedAttributeMap(
091: new SessionSharedMap(facesContext));
092: this .applicationMap = new LocalSharedAttributeMap(
093: new ApplicationSharedMap(facesContext));
094: }
095:
096: public String getContextPath() {
097: return facesContext.getExternalContext()
098: .getRequestContextPath();
099: }
100:
101: public String getDispatcherPath() {
102: return facesContext.getExternalContext()
103: .getRequestServletPath();
104: }
105:
106: public String getRequestPathInfo() {
107: return facesContext.getExternalContext().getRequestPathInfo();
108: }
109:
110: public ParameterMap getRequestParameterMap() {
111: return requestParameterMap;
112: }
113:
114: public MutableAttributeMap getRequestMap() {
115: return requestMap;
116: }
117:
118: public SharedAttributeMap getSessionMap() {
119: return sessionMap;
120: }
121:
122: public SharedAttributeMap getGlobalSessionMap() {
123: return getSessionMap();
124: }
125:
126: public SharedAttributeMap getApplicationMap() {
127: return applicationMap;
128: }
129:
130: /**
131: * Returns the JSF FacesContext.
132: */
133: public FacesContext getFacesContext() {
134: return facesContext;
135: }
136:
137: /**
138: * Returns the action identifier.
139: */
140: public String getActionId() {
141: return actionId;
142: }
143:
144: /**
145: * Returns the action outcome.
146: */
147: public String getOutcome() {
148: return outcome;
149: }
150:
151: /**
152: * Records the action and outcome context information when navigation handling occurs.
153: * @param actionId the from action identifier
154: * @param outcome the action outcome
155: */
156: public void handleNavigationCalled(String actionId, String outcome) {
157: this .actionId = actionId;
158: this .outcome = outcome;
159: }
160:
161: /**
162: * An accessor of a JSF session map.
163: * @author Keith Donald
164: */
165: private static class SessionSharedMap extends SharedMapDecorator {
166:
167: private FacesContext facesContext;
168:
169: public SessionSharedMap(FacesContext facesContext) {
170: super (facesContext.getExternalContext().getSessionMap());
171: this .facesContext = facesContext;
172: }
173:
174: public Object getMutex() {
175: return facesContext.getExternalContext().getSession(false);
176: }
177: }
178:
179: /**
180: * An accessor of an JSF application map.
181: * @author Keith Donald
182: */
183: private static class ApplicationSharedMap extends
184: SharedMapDecorator {
185:
186: private FacesContext facesContext;
187:
188: public ApplicationSharedMap(FacesContext facesContext) {
189: super (facesContext.getExternalContext().getApplicationMap());
190: this .facesContext = facesContext;
191: }
192:
193: public Object getMutex() {
194: return facesContext.getExternalContext().getContext();
195: }
196: }
197:
198: public String toString() {
199: return new ToStringCreator(this ).append("actionId", actionId)
200: .append("outcome", outcome).append("facesContext",
201: facesContext).toString();
202: }
203: }
|