KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > webflow > engine > builder > FlowAssembler


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.engine.builder;
17
18 import org.springframework.util.Assert;
19 import org.springframework.webflow.core.collection.AttributeMap;
20 import org.springframework.webflow.core.collection.CollectionUtils;
21 import org.springframework.webflow.engine.Flow;
22
23 /**
24  * A director for assembling flows, delegating to a {@link FlowBuilder} to
25  * construct a flow. This class encapsulates the algorithm for using a
26  * FlowBuilder to assemble a Flow properly. It acts as the director in the
27  * classic GoF builder pattern.
28  * <p>
29  * Flow assemblers may be used in a standalone, programmatic fashion as follows:
30  *
31  * <pre>
32  * FlowBuilder builder = ...;
33  * Flow flow = new FlowAssembler(&quot;myFlow&quot;, builder).assembleFlow();
34  * </pre>
35  *
36  * @see org.springframework.webflow.engine.builder.FlowBuilder
37  *
38  * @author Keith Donald
39  * @author Erwin Vervaet
40  */

41 public class FlowAssembler {
42
43     /**
44      * The identifier to assign to the flow.
45      */

46     private String JavaDoc flowId;
47
48     /**
49      * Attributes that can be used to affect flow construction.
50      */

51     private AttributeMap flowAttributes;
52
53     /**
54      * The flow builder strategy used to construct the flow from its component
55      * parts.
56      */

57     private FlowBuilder flowBuilder;
58
59     /**
60      * Create a new flow assembler that will direct Flow assembly using the
61      * specified builder strategy.
62      * @param flowId the flow id to assign
63      * @param flowBuilder the builder the factory will use to build flows
64      */

65     public FlowAssembler(String JavaDoc flowId, FlowBuilder flowBuilder) {
66         this(flowId, null, flowBuilder);
67     }
68
69     /**
70      * Create a new flow assembler that will direct Flow assembly using the
71      * specified builder strategy.
72      * @param flowId the flow id to assign
73      * @param flowAttributes externally assigned flow attributes that can affect
74      * flow construction
75      * @param flowBuilder the builder the factory will use to build flows
76      */

77     public FlowAssembler(String JavaDoc flowId, AttributeMap flowAttributes, FlowBuilder flowBuilder) {
78         Assert.hasText(flowId, "The flow id is required");
79         Assert.notNull(flowBuilder, "The flow builder is required");
80         this.flowId = flowId;
81         this.flowAttributes = (flowAttributes != null ? flowAttributes : CollectionUtils.EMPTY_ATTRIBUTE_MAP);
82         this.flowBuilder = flowBuilder;
83     }
84
85     /**
86      * Returns the identifier to assign to the flow.
87      */

88     public String JavaDoc getFlowId() {
89         return flowId;
90     }
91
92     /**
93      * Returns externally assigned attributes that can be used to affect flow
94      * construction.
95      */

96     public AttributeMap getFlowAttributes() {
97         return flowAttributes;
98     }
99
100     /**
101      * Returns the flow builder strategy used to construct the flow from its
102      * component parts.
103      */

104     public FlowBuilder getFlowBuilder() {
105         return flowBuilder;
106     }
107
108     /**
109      * Assembles the flow, directing the construction process by delegating to
110      * the configured FlowBuilder. Every call to this method will assemble
111      * the Flow instance.
112      * <p>
113      * This will drive the flow construction process as described in the
114      * {@link FlowBuilder} JavaDoc, starting with builder initialisation using
115      * {@link FlowBuilder#init(String, AttributeMap)} and finishing by
116      * cleaning up the builder with a call to {@link FlowBuilder#dispose()}.
117      * @return the constructed flow
118      * @throws FlowBuilderException when flow assembly fails
119      */

120     public Flow assembleFlow() throws FlowBuilderException {
121         try {
122             flowBuilder.init(flowId, flowAttributes);
123             directAssembly();
124             return flowBuilder.getFlow();
125         }
126         finally {
127             flowBuilder.dispose();
128         }
129     }
130
131     /**
132      * Build all parts of the flow by directing flow assembly by the flow
133      * builder.
134      * @throws FlowBuilderException when flow assembly fails
135      */

136     protected void directAssembly() throws FlowBuilderException {
137         flowBuilder.buildVariables();
138         flowBuilder.buildInputMapper();
139         flowBuilder.buildStartActions();
140         flowBuilder.buildInlineFlows();
141         flowBuilder.buildStates();
142         flowBuilder.buildGlobalTransitions();
143         flowBuilder.buildEndActions();
144         flowBuilder.buildOutputMapper();
145         flowBuilder.buildExceptionHandlers();
146     }
147 }
Popular Tags