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 java.util.Map;
019:
020: import javax.faces.el.PropertyNotFoundException;
021: import javax.faces.el.PropertyResolver;
022:
023: import org.springframework.webflow.execution.FlowExecution;
024:
025: /**
026: * Custom property resolver that resolves supported properties of the current flow execution.
027: * Supports resolving all scopes as java.util.Maps: "flowScope", "conversationScope", and "flashScope".
028: * Also supports attribute searching when no scope prefix is specified. The search order is
029: * flash, flow, conversation.
030: *
031: * @author Keith Donald
032: */
033: public class FlowExecutionPropertyResolver extends
034: AbstractFlowExecutionPropertyResolver {
035:
036: /**
037: * The name of the special flash scope execution property.
038: */
039: private static final String FLASH_SCOPE_PROPERTY = "flashScope";
040:
041: /**
042: * The name of the special flow scope execution property.
043: */
044: private static final String FLOW_SCOPE_PROPERTY = "flowScope";
045:
046: /**
047: * The name of the special conversation scope execution property.
048: */
049: private static final String CONVERSATION_SCOPE_PROPERTY = "conversationScope";
050:
051: /**
052: * Creates a new flow executon property resolver that resolves flash, flow, and conversation scope attributes.
053: * @param resolverDelegate the resolver to delegate to when the property is not a flow execution attribute
054: */
055: public FlowExecutionPropertyResolver(
056: PropertyResolver resolverDelegate) {
057: super (resolverDelegate);
058: }
059:
060: protected Class doGetAttributeType(FlowExecution execution,
061: String attributeName) {
062: if (FLASH_SCOPE_PROPERTY.equals(attributeName)) {
063: return Map.class;
064: } else if (FLOW_SCOPE_PROPERTY.equals(attributeName)) {
065: return Map.class;
066: } else if (CONVERSATION_SCOPE_PROPERTY.equals(attributeName)) {
067: return Map.class;
068: } else {
069: // perform an attribute search
070:
071: // try flash scope first
072: Object value = execution.getActiveSession().getFlashMap()
073: .get(attributeName);
074: if (value != null) {
075: return value.getClass();
076: }
077: // try flow scope
078: value = execution.getActiveSession().getScope().get(
079: attributeName);
080: if (value != null) {
081: return value.getClass();
082: }
083: // try conversation scope
084: value = execution.getConversationScope().get(attributeName);
085: if (value != null) {
086: return value.getClass();
087: }
088: // cannot determine
089: return null;
090: }
091: }
092:
093: protected Object doGetAttribute(FlowExecution execution,
094: String attributeName) {
095: if (FLASH_SCOPE_PROPERTY.equals(attributeName)) {
096: return execution.getActiveSession().getFlashMap().asMap();
097: } else if (FLOW_SCOPE_PROPERTY.equals(attributeName)) {
098: return execution.getActiveSession().getScope().asMap();
099: } else if (CONVERSATION_SCOPE_PROPERTY.equals(attributeName)) {
100: return execution.getConversationScope().asMap();
101: } else {
102: // perform an attribute search
103:
104: // try flash scope
105: Object value = execution.getActiveSession().getFlashMap()
106: .get(attributeName);
107: if (value != null) {
108: return value;
109: }
110: // try flow scope
111: value = execution.getActiveSession().getScope().get(
112: attributeName);
113: if (value != null) {
114: return value;
115: }
116: // try conversation scope
117: value = execution.getConversationScope().get(attributeName);
118: if (value != null) {
119: return value;
120: }
121: // cannot resolve as expected
122: throw new PropertyNotFoundException(
123: "Readable flow execution attribute '"
124: + attributeName
125: + "' not found in any scope (flash, flow, or conversation)");
126: }
127: }
128:
129: protected void doSetAttribute(FlowExecution execution,
130: String attributeName, Object attributeValue) {
131: // perform a search
132: if (execution.getActiveSession().getFlashMap().contains(
133: attributeName)) {
134: execution.getActiveSession().getFlashMap().put(
135: attributeName, attributeValue);
136: } else if (execution.getActiveSession().getScope().contains(
137: attributeName)) {
138: execution.getActiveSession().getScope().put(attributeName,
139: attributeValue);
140: } else if (execution.getConversationScope().contains(
141: attributeName)) {
142: execution.getConversationScope().put(attributeName,
143: attributeValue);
144: } else {
145: // cannot resolve as expected
146: throw new PropertyNotFoundException(
147: "Settable flow execution attribute '"
148: + attributeName
149: + "' not found in any scope (flash, flow, or conversation)");
150: }
151: }
152: }
|