KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > webflow > executor > support > FlowRequestHandler


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.springframework.webflow.executor.support;
17
18 import org.apache.commons.logging.Log;
19 import org.apache.commons.logging.LogFactory;
20 import org.springframework.util.Assert;
21 import org.springframework.webflow.context.ExternalContext;
22 import org.springframework.webflow.core.FlowException;
23 import org.springframework.webflow.executor.FlowExecutor;
24 import org.springframework.webflow.executor.ResponseInstruction;
25
26 /**
27  * An immutable helper for flow controllers that encapsulates reusable workflow
28  * required to launch and resume flow executions using a {@link FlowExecutor}.
29  * <p>
30  * The {@link #handleFlowRequest(ExternalContext)} method is the central helper
31  * operation and implements the following algorithm:
32  * <ol>
33  * <li>Extract the flow execution id by calling
34  * {@link FlowExecutorArgumentExtractor#extractFlowExecutionKey(ExternalContext)}.
35  * <li>If a valid flow execution id was extracted, signal an event in that
36  * existing execution to resume it. The event to signal is determined by calling
37  * the {@link FlowExecutorArgumentExtractor#extractEventId(ExternalContext)}
38  * method. If no event can be extracted, the existing execution will be refreshed.
39  * <li>If no flow execution id was extracted, launch a new flow execution. The
40  * top-level flow definition for which an execution is created is determined by
41  * extracting the flow id using the method
42  * {@link FlowExecutorArgumentExtractor#extractFlowId(ExternalContext)}. If no
43  * valid flow id can be determined, an exception is thrown.
44  * </ol>
45  *
46  * @author Keith Donald
47  * @author Erwin Vervaet
48  */

49 public class FlowRequestHandler {
50
51     /**
52      * Logger.
53      */

54     private static final Log logger = LogFactory.getLog(FlowRequestHandler.class);
55
56     /**
57      * The flow executor this helper will coordinate with.
58      */

59     private FlowExecutor flowExecutor;
60
61     /**
62      * A helper for extracting arguments of flow executor operations
63      * from the external context.
64      */

65     private FlowExecutorArgumentExtractor argumentExtractor;
66
67     /**
68      * Creates a new flow controller helper. Will use the default
69      * {@link RequestParameterFlowExecutorArgumentHandler}.
70      * @param flowExecutor the flow execution manager to delegate to
71      */

72     public FlowRequestHandler(FlowExecutor flowExecutor) {
73         this(flowExecutor, new RequestParameterFlowExecutorArgumentHandler());
74     }
75
76     /**
77      * Creates a new flow controller helper.
78      * @param flowExecutor the flow executor to delegate to
79      * @param argumentExtractor the flow executor argument extractor to use
80      */

81     public FlowRequestHandler(FlowExecutor flowExecutor, FlowExecutorArgumentExtractor argumentExtractor) {
82         Assert.notNull(flowExecutor, "The flow executor is required");
83         Assert.notNull(argumentExtractor, "The flow executor argument extractor is required");
84         this.flowExecutor = flowExecutor;
85         this.argumentExtractor = argumentExtractor;
86     }
87     
88     /**
89      * Returns the flow executor used by this helper.
90      */

91     public FlowExecutor getFlowExecutor() {
92         return flowExecutor;
93     }
94
95     /**
96      * Returns the flow executor argument extractor used by this helper.
97      */

98     public FlowExecutorArgumentExtractor getArgumentExtractor() {
99         return argumentExtractor;
100     }
101
102     /**
103      * Handle a request into the Spring Web Flow system from an external system.
104      * @param context the external context in which the request occured
105      * @return the selected view that should be rendered as a response
106      */

107     public ResponseInstruction handleFlowRequest(ExternalContext context) throws FlowException {
108         if (logger.isDebugEnabled()) {
109             logger.debug("Request initiated by " + context);
110         }
111         if (argumentExtractor.isFlowExecutionKeyPresent(context)) {
112             String JavaDoc flowExecutionKey = argumentExtractor.extractFlowExecutionKey(context);
113             if (argumentExtractor.isEventIdPresent(context)) {
114                 String JavaDoc eventId = argumentExtractor.extractEventId(context);
115                 ResponseInstruction response = flowExecutor.resume(flowExecutionKey, eventId, context);
116                 if (logger.isDebugEnabled()) {
117                     logger.debug("Returning [resume] " + response);
118                 }
119                 return response;
120             }
121             else {
122                 ResponseInstruction response = flowExecutor.refresh(flowExecutionKey, context);
123                 if (logger.isDebugEnabled()) {
124                     logger.debug("Returning [refresh] " + response);
125                 }
126                 return response;
127             }
128         }
129         else {
130             String JavaDoc flowDefinitionId = argumentExtractor.extractFlowId(context);
131             ResponseInstruction response = flowExecutor.launch(flowDefinitionId, context);
132             if (logger.isDebugEnabled()) {
133                 logger.debug("Returning [launch] " + response);
134             }
135             return response;
136         }
137     }
138 }
Popular Tags