KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > webflow > engine > builder > xml > LocalFlowServiceLocator


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.xml;
17
18 import java.util.Stack JavaDoc;
19
20 import org.springframework.beans.BeansException;
21 import org.springframework.beans.factory.BeanFactory;
22 import org.springframework.binding.convert.ConversionService;
23 import org.springframework.binding.expression.ExpressionParser;
24 import org.springframework.core.io.ResourceLoader;
25 import org.springframework.webflow.action.BeanInvokingActionFactory;
26 import org.springframework.webflow.engine.Flow;
27 import org.springframework.webflow.engine.FlowAttributeMapper;
28 import org.springframework.webflow.engine.FlowExecutionExceptionHandler;
29 import org.springframework.webflow.engine.TargetStateResolver;
30 import org.springframework.webflow.engine.TransitionCriteria;
31 import org.springframework.webflow.engine.ViewSelector;
32 import org.springframework.webflow.engine.builder.FlowArtifactFactory;
33 import org.springframework.webflow.engine.builder.FlowArtifactLookupException;
34 import org.springframework.webflow.engine.builder.FlowServiceLocator;
35 import org.springframework.webflow.execution.Action;
36
37 /**
38  * Searches flow-local registries first before querying the global, externally
39  * managed flow service locator.
40  * <p>
41  * Internal helper class of the {@link org.springframework.webflow.engine.builder.xml.XmlFlowBuilder}.
42  * Package private to highlight it's non-public nature.
43  *
44  * @see org.springframework.webflow.engine.builder.xml.XmlFlowBuilder
45  *
46  * @author Keith Donald
47  */

48 class LocalFlowServiceLocator implements FlowServiceLocator {
49
50     /**
51      * The stack of registries.
52      */

53     private Stack JavaDoc localRegistries = new Stack JavaDoc();
54
55     /**
56      * The parent service locator.
57      */

58     private FlowServiceLocator parent;
59
60     /**
61      * Creates a new local service locator.
62      * @param parent the parent service locator
63      */

64     public LocalFlowServiceLocator(FlowServiceLocator parent) {
65         this.parent = parent;
66     }
67
68     /**
69      * Push a new registry onto the stack.
70      * @param registry the local registry
71      */

72     public void push(LocalFlowServiceRegistry registry) {
73         registry.init(this, parent);
74         localRegistries.push(registry);
75     }
76
77     /**
78      * Pop a registry off the stack.
79      */

80     public LocalFlowServiceRegistry pop() {
81         return (LocalFlowServiceRegistry)localRegistries.pop();
82     }
83     
84     /**
85      * Pops all registries off the stack until the stack is empty.
86      */

87     public void diposeOfAnyRegistries() {
88         while (!localRegistries.isEmpty()) {
89             pop();
90         }
91     }
92
93     /**
94      * Returns the top registry on the stack
95      */

96     public LocalFlowServiceRegistry top() {
97         return (LocalFlowServiceRegistry)localRegistries.peek();
98     }
99
100     /**
101      * Returns true if this locator has no local registries.
102      */

103     public boolean isEmpty() {
104         return localRegistries.isEmpty();
105     }
106
107     // implementing FlowServiceLocator
108

109     public Flow getSubflow(String JavaDoc id) throws FlowArtifactLookupException {
110         Flow currentFlow = getCurrentFlow();
111         // quick check for recursive subflow
112
if (currentFlow.getId().equals(id)) {
113             return currentFlow;
114         }
115         // check local inline flows
116
if (currentFlow.containsInlineFlow(id)) {
117             return currentFlow.getInlineFlow(id);
118         }
119         // check externally managed top-level flows
120
return parent.getSubflow(id);
121     }
122
123     public Action getAction(String JavaDoc id) throws FlowArtifactLookupException {
124         if (containsBean(id)) {
125             return (Action)getBean(id, Action.class);
126         }
127         else {
128             return parent.getAction(id);
129         }
130     }
131
132     public FlowAttributeMapper getAttributeMapper(String JavaDoc id) throws FlowArtifactLookupException {
133         if (containsBean(id)) {
134             return (FlowAttributeMapper)getBean(id, FlowAttributeMapper.class);
135         }
136         else {
137             return parent.getAttributeMapper(id);
138         }
139     }
140
141     public TransitionCriteria getTransitionCriteria(String JavaDoc id) throws FlowArtifactLookupException {
142         if (containsBean(id)) {
143             return (TransitionCriteria)getBean(id, TransitionCriteria.class);
144         }
145         else {
146             return parent.getTransitionCriteria(id);
147         }
148     }
149
150     public TargetStateResolver getTargetStateResolver(String JavaDoc id) throws FlowArtifactLookupException {
151         if (containsBean(id)) {
152             return (TargetStateResolver)getBean(id, TargetStateResolver.class);
153         }
154         else {
155             return parent.getTargetStateResolver(id);
156         }
157     }
158
159     public ViewSelector getViewSelector(String JavaDoc id) throws FlowArtifactLookupException {
160         if (containsBean(id)) {
161             return (ViewSelector)getBean(id, ViewSelector.class);
162         }
163         else {
164             return parent.getViewSelector(id);
165         }
166     }
167
168     public FlowExecutionExceptionHandler getExceptionHandler(String JavaDoc id) throws FlowArtifactLookupException {
169         if (containsBean(id)) {
170             return (FlowExecutionExceptionHandler)getBean(id, FlowExecutionExceptionHandler.class);
171         }
172         else {
173             return parent.getExceptionHandler(id);
174         }
175     }
176
177     public FlowArtifactFactory getFlowArtifactFactory() {
178         return parent.getFlowArtifactFactory();
179     }
180
181     public BeanInvokingActionFactory getBeanInvokingActionFactory() {
182         return parent.getBeanInvokingActionFactory();
183     }
184
185     public BeanFactory getBeanFactory() {
186         return top().getContext();
187     }
188
189     public ResourceLoader getResourceLoader() {
190         return parent.getResourceLoader();
191     }
192
193     public ExpressionParser getExpressionParser() {
194         return parent.getExpressionParser();
195     }
196
197     public ConversionService getConversionService() {
198         return parent.getConversionService();
199     }
200
201     // internal helpers
202

203     /**
204      * Returns the flow for the registry at the top of the stack.
205      */

206     protected Flow getCurrentFlow() {
207         return top().getFlow();
208     }
209
210     /**
211      * Does this flow local service locator contain a bean defintion
212      * for given id?
213      */

214     protected boolean containsBean(String JavaDoc id) {
215         if (localRegistries.isEmpty()) {
216             return false;
217         }
218         else {
219             return getBeanFactory().containsBean(id);
220         }
221     }
222
223     /**
224      * Get the identified bean and make sure it is of the required type.
225      */

226     protected Object JavaDoc getBean(String JavaDoc id, Class JavaDoc artifactType) throws FlowArtifactLookupException {
227         try {
228             return getBeanFactory().getBean(id, artifactType);
229         }
230         catch (BeansException e) {
231             throw new FlowArtifactLookupException(id, artifactType, e);
232         }
233     }
234 }
Popular Tags