KickJava   Java API By Example, From Geeks To Geeks.

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


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.beans.BeansException;
19 import org.springframework.beans.factory.BeanFactory;
20 import org.springframework.binding.convert.ConversionService;
21 import org.springframework.binding.convert.support.DefaultConversionService;
22 import org.springframework.binding.convert.support.GenericConversionService;
23 import org.springframework.binding.convert.support.TextToExpression;
24 import org.springframework.binding.expression.ExpressionParser;
25 import org.springframework.binding.method.TextToMethodSignature;
26 import org.springframework.core.io.DefaultResourceLoader;
27 import org.springframework.core.io.ResourceLoader;
28 import org.springframework.util.Assert;
29 import org.springframework.webflow.action.BeanInvokingActionFactory;
30 import org.springframework.webflow.core.DefaultExpressionParserFactory;
31 import org.springframework.webflow.engine.Flow;
32 import org.springframework.webflow.engine.FlowAttributeMapper;
33 import org.springframework.webflow.engine.FlowExecutionExceptionHandler;
34 import org.springframework.webflow.engine.State;
35 import org.springframework.webflow.engine.TargetStateResolver;
36 import org.springframework.webflow.engine.TransitionCriteria;
37 import org.springframework.webflow.engine.ViewSelector;
38 import org.springframework.webflow.execution.Action;
39
40 /**
41  * Base implementation that implements a minimal set of the
42  * <code>FlowServiceLocator</code> interface, throwing unsupported operation
43  * exceptions for some operations.
44  * <p>
45  * May be subclassed to offer additional factory/lookup support.
46  *
47  * @author Keith Donald
48  */

49 public class BaseFlowServiceLocator implements FlowServiceLocator {
50
51     /**
52      * The factory encapsulating the creation of central Flow artifacts such as
53      * {@link Flow flows} and {@link State states}.
54      */

55     private FlowArtifactFactory flowArtifactFactory = new FlowArtifactFactory();
56
57     /**
58      * The factory encapsulating the creation of bean invoking actions, actions
59      * that adapt methods on objects to the {@link Action} interface.
60      */

61     private BeanInvokingActionFactory beanInvokingActionFactory = new BeanInvokingActionFactory();
62
63     /**
64      * The parser for parsing expression strings into evaluatable expression
65      * objects.
66      */

67     private ExpressionParser expressionParser = DefaultExpressionParserFactory.getExpressionParser();
68
69     /**
70      * A conversion service that can convert between types.
71      */

72     private ConversionService conversionService = createConversionService(null);
73
74     /**
75      * A resource loader that can load resources.
76      */

77     private ResourceLoader resourceLoader = new DefaultResourceLoader();
78
79     /**
80      * Sets the factory encapsulating the creation of central Flow artifacts
81      * such as {@link Flow flows} and {@link State states}.
82      */

83     public void setFlowArtifactFactory(FlowArtifactFactory flowArtifactFactory) {
84         Assert.notNull(flowArtifactFactory, "The flow artifact factory is required");
85         this.flowArtifactFactory = flowArtifactFactory;
86     }
87
88     /**
89      * Sets the factory for creating bean invoking actions, actions that adapt
90      * methods on objects to the {@link Action} interface.
91      */

92     public void setBeanInvokingActionFactory(BeanInvokingActionFactory beanInvokingActionFactory) {
93         Assert.notNull(beanInvokingActionFactory, "The bean invoking action factory is required");
94         this.beanInvokingActionFactory = beanInvokingActionFactory;
95     }
96
97     /**
98      * Set the expression parser responsible for parsing expression strings into
99      * evaluatable expression objects.
100      */

101     public void setExpressionParser(ExpressionParser expressionParser) {
102         Assert.notNull(expressionParser, "The expression parser is required");
103         this.expressionParser = expressionParser;
104     }
105
106     /**
107      * Set the conversion service to use to convert between types; typically
108      * from string to a rich object type.
109      */

110     public void setConversionService(ConversionService conversionService) {
111         Assert.notNull(conversionService, "The conversion service is required");
112         this.conversionService = createConversionService(conversionService);
113     }
114
115     /**
116      * Set the resource loader to load file-based resources from string-encoded
117      * paths. This is optional.
118      */

119     public void setResourceLoader(ResourceLoader resourceLoader) {
120         this.resourceLoader = resourceLoader;
121     }
122
123     public Flow getSubflow(String JavaDoc id) throws FlowArtifactLookupException {
124         throw new FlowArtifactLookupException(id, Flow.class,
125                 "Subflow lookup is not supported by this service locator");
126     }
127
128     public Action getAction(String JavaDoc id) throws FlowArtifactLookupException {
129         return (Action)getBean(id, Action.class);
130     }
131
132     public FlowAttributeMapper getAttributeMapper(String JavaDoc id) throws FlowArtifactLookupException {
133         return (FlowAttributeMapper)getBean(id, FlowAttributeMapper.class);
134     }
135
136     public TransitionCriteria getTransitionCriteria(String JavaDoc id) throws FlowArtifactLookupException {
137         return (TransitionCriteria)getBean(id, TransitionCriteria.class);
138     }
139
140     public TargetStateResolver getTargetStateResolver(String JavaDoc id) throws FlowArtifactLookupException {
141         return (TargetStateResolver)getBean(id, TargetStateResolver.class);
142     }
143
144     public ViewSelector getViewSelector(String JavaDoc id) throws FlowArtifactLookupException {
145         return (ViewSelector)getBean(id, ViewSelector.class);
146     }
147
148     public FlowExecutionExceptionHandler getExceptionHandler(String JavaDoc id) throws FlowArtifactLookupException {
149         return (FlowExecutionExceptionHandler)getBean(id, FlowExecutionExceptionHandler.class);
150     }
151
152     public FlowArtifactFactory getFlowArtifactFactory() {
153         return flowArtifactFactory;
154     }
155
156     public BeanInvokingActionFactory getBeanInvokingActionFactory() {
157         return beanInvokingActionFactory;
158     }
159
160     public BeanFactory getBeanFactory() throws UnsupportedOperationException JavaDoc {
161         throw new UnsupportedOperationException JavaDoc("Bean factory lookup is not supported by this service locator");
162     }
163
164     public ResourceLoader getResourceLoader() {
165         return resourceLoader;
166     }
167
168     public ExpressionParser getExpressionParser() {
169         return expressionParser;
170     }
171
172     public ConversionService getConversionService() {
173         return conversionService;
174     }
175     
176     // helpers for use by subclasses
177

178     /**
179      * Helper method for determining if the configured bean factory contains the
180      * provided bean.
181      * @param id the id of the bean
182      * @return true if yes, false otherwise
183      */

184     protected boolean containsBean(String JavaDoc id) {
185         return getBeanFactory().containsBean(id);
186     }
187
188     /**
189      * Helper method to lookup the bean representing a flow artifact of the
190      * specified type.
191      * @param id the bean id
192      * @param artifactType the bean type
193      * @return the bean
194      * @throws FlowArtifactLookupException an exception occurred
195      */

196     protected Object JavaDoc getBean(String JavaDoc id, Class JavaDoc artifactType) throws FlowArtifactLookupException {
197         try {
198             return getBeanFactory().getBean(id, artifactType);
199         }
200         catch (BeansException e) {
201             throw new FlowArtifactLookupException(id, artifactType, e);
202         }
203     }
204
205     /**
206      * Helper method to lookup the type of the bean with the provided id.
207      * @param id the bean id
208      * @param artifactType the bean type
209      * @return the bean's type
210      * @throws FlowArtifactLookupException an exception occurred
211      */

212     protected Class JavaDoc getBeanType(String JavaDoc id, Class JavaDoc artifactType) throws FlowArtifactLookupException {
213         try {
214             return getBeanFactory().getType(id);
215         }
216         catch (BeansException e) {
217             throw new FlowArtifactLookupException(id, artifactType, e);
218         }
219     }
220
221     /**
222      * Setup a conversion service used by this flow service locator.
223      * @param parent the parent of the conversion service that will be created;
224      * optional
225      * @return the newly created conversion service
226      */

227     protected ConversionService createConversionService(ConversionService parent) {
228         if (parent != null) {
229             GenericConversionService conversionService = new GenericConversionService();
230             addWebFlowConverters(conversionService);
231             conversionService.setParent(parent);
232             return conversionService;
233         }
234         else {
235             DefaultConversionService conversionService = new DefaultConversionService();
236             addWebFlowConverters(conversionService);
237             return conversionService;
238         }
239     }
240
241     /**
242      * Add all web flow specific converters to given conversion service.
243      */

244     protected void addWebFlowConverters(GenericConversionService conversionService) {
245         conversionService.addConverter(new TextToTransitionCriteria(this));
246         conversionService.addConverter(new TextToTargetStateResolver(this));
247         conversionService.addConverter(new TextToViewSelector(this));
248         conversionService.addConverter(new TextToExpression(getExpressionParser()));
249         conversionService.addConverter(new TextToMethodSignature(conversionService));
250     }
251 }
Popular Tags