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.engine.builder;
017:
018: import org.springframework.binding.convert.ConversionException;
019: import org.springframework.binding.convert.ConversionExecutor;
020: import org.springframework.util.Assert;
021: import org.springframework.webflow.core.collection.AttributeMap;
022: import org.springframework.webflow.engine.Flow;
023:
024: /**
025: * Abstract base implementation of a flow builder defining common functionality
026: * needed by most concrete flow builder implementations. This class implements
027: * all optional parts of the FlowBuilder process as no-op methods. Subclasses
028: * are only required to implement {@link #init(String, AttributeMap)} and
029: * {@link #buildStates()}.
030: * <p>
031: * This class also provides a {@link FlowServiceLocator} for use by
032: * subclasses in the flow construction process.
033: *
034: * @see org.springframework.webflow.engine.builder.FlowServiceLocator
035: *
036: * @author Keith Donald
037: * @author Erwin Vervaet
038: */
039: public abstract class BaseFlowBuilder implements FlowBuilder {
040:
041: /**
042: * The <code>Flow</code> built by this builder.
043: */
044: private Flow flow;
045:
046: /**
047: * Locates actions, attribute mappers, and other artifacts needed by the
048: * flow built by this builder.
049: */
050: private FlowServiceLocator flowServiceLocator;
051:
052: /**
053: * Default constructor for subclassing. Sets up use of a {@link BaseFlowServiceLocator}.
054: * @see #setFlowServiceLocator(FlowServiceLocator)
055: */
056: protected BaseFlowBuilder() {
057: setFlowServiceLocator(new BaseFlowServiceLocator());
058: }
059:
060: /**
061: * Creates a flow builder using the given locator to link in artifacts.
062: * @param flowServiceLocator the locator for services needed by this builder to build its Flow
063: */
064: protected BaseFlowBuilder(FlowServiceLocator flowServiceLocator) {
065: setFlowServiceLocator(flowServiceLocator);
066: }
067:
068: /**
069: * Returns the configured flow service locator.
070: */
071: public FlowServiceLocator getFlowServiceLocator() {
072: return flowServiceLocator;
073: }
074:
075: /**
076: * Sets the flow service locator to use. Defaults to {@link BaseFlowServiceLocator}.
077: */
078: public void setFlowServiceLocator(
079: FlowServiceLocator flowServiceLocator) {
080: Assert.notNull(flowServiceLocator,
081: "The flow service locator is required");
082: this .flowServiceLocator = flowServiceLocator;
083: }
084:
085: /**
086: * Set the flow being built by this builder. Typically called during
087: * initialization to set the initial flow reference returned by
088: * {@link #getFlow()} after building.
089: */
090: protected void setFlow(Flow flow) {
091: this .flow = flow;
092: }
093:
094: public abstract void init(String flowId, AttributeMap attributes)
095: throws FlowBuilderException;
096:
097: public void buildVariables() throws FlowBuilderException {
098: }
099:
100: public void buildInputMapper() throws FlowBuilderException {
101: }
102:
103: public void buildStartActions() throws FlowBuilderException {
104: }
105:
106: public void buildInlineFlows() throws FlowBuilderException {
107: }
108:
109: public abstract void buildStates() throws FlowBuilderException;
110:
111: public void buildGlobalTransitions() throws FlowBuilderException {
112: }
113:
114: public void buildEndActions() throws FlowBuilderException {
115: }
116:
117: public void buildOutputMapper() throws FlowBuilderException {
118: }
119:
120: public void buildExceptionHandlers() throws FlowBuilderException {
121: }
122:
123: /**
124: * Get the flow (result) built by this builder.
125: */
126: public Flow getFlow() {
127: return flow;
128: }
129:
130: public void dispose() {
131: setFlow(null);
132: }
133:
134: // helpers for use in subclasses
135:
136: /**
137: * Returns a conversion executor capable of converting string objects to the
138: * target class aliased by the provided alias.
139: * @param targetAlias the target class alias, e.g. "long" or "float"
140: * @return the conversion executor, or <code>null</code> if no suitable
141: * converter exists for given alias
142: */
143: protected ConversionExecutor fromStringTo(String targetAlias) {
144: return getFlowServiceLocator().getConversionService()
145: .getConversionExecutorByTargetAlias(String.class,
146: targetAlias);
147: }
148:
149: /**
150: * Returns a converter capable of converting a string value to the given
151: * type.
152: * @param targetType the type you wish to convert to (from a string)
153: * @return the converter
154: * @throws ConversionException when the converter cannot be found
155: */
156: protected ConversionExecutor fromStringTo(Class targetType)
157: throws ConversionException {
158: return getFlowServiceLocator().getConversionService()
159: .getConversionExecutor(String.class, targetType);
160: }
161: }
|