KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayList JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.Properties JavaDoc;
23
24 import org.springframework.core.io.Resource;
25 import org.springframework.webflow.definition.registry.FlowDefinitionRegistry;
26 import org.springframework.webflow.definition.registry.FlowDefinitionResource;
27 import org.springframework.webflow.engine.builder.AbstractFlowBuildingFlowRegistryFactoryBean;
28 import org.springframework.webflow.engine.builder.DefaultFlowServiceLocator;
29 import org.springframework.webflow.engine.builder.FlowServiceLocator;
30
31 /**
32  * A factory bean that produces a populated flow registry using a
33  * {@link XmlFlowRegistrar}. This is the simplest implementation to use when
34  * using a Spring BeanFactory to deploy an explicit registry of XML-based Flow
35  * definitions for execution.
36  * <p>
37  * By default, a configured flow definition will be assigned a registry
38  * identifier equal to the filename of the underlying definition resource, minus
39  * the filename extension. For example, a XML-based flow definition defined in
40  * the file <code>flow1.xml</code> will be identified as <code>flow1</code>
41  * in the registry created by this factory bean.
42  * <p>
43  * This class is also <code>BeanFactoryAware</code> and when used with Spring
44  * will automatically create a configured {@link DefaultFlowServiceLocator} for
45  * loading Flow artifacts like Actions from the Spring bean factory during the
46  * Flow registration process.
47  * <p>
48  * This class is also <code>ResourceLoaderAware</code>; when an instance is
49  * created by a Spring BeanFactory the factory will automatically configure the
50  * XmlFlowRegistrar with a context-relative resource loader for accessing other
51  * resources during Flow assembly.
52  *
53  * Usage example:
54  *
55  * <pre>
56  * &lt;bean id=&quot;flowRegistry&quot; class=&quot;org.springframework.webflow.engine.builder.registry.XmlFlowRegistryFactoryBean&quot;&gt;
57  * &lt;property name=&quot;flowLocations&quot;&gt; value=&quot;/WEB-INF/flows/*-flow.xml&quot;/&gt;
58  * &lt;/bean&gt;
59  * </pre>
60  *
61  * @author Keith Donald
62  */

63 public class XmlFlowRegistryFactoryBean extends AbstractFlowBuildingFlowRegistryFactoryBean {
64
65     /**
66      * The flow registrar that will perform the definition registrations.
67      */

68     private XmlFlowRegistrar flowRegistrar = new XmlFlowRegistrar();
69
70     /**
71      * Temporary holder for flow definitions configured using a property map.
72      */

73     private Properties JavaDoc flowDefinitions;
74
75     /**
76      * Returns the configured externalized XML flow registrar.
77      */

78     protected XmlFlowRegistrar getXmlFlowRegistrar() {
79         return flowRegistrar;
80     }
81
82     /**
83      * Sets the locations (resource file paths) pointing to XML-based flow
84      * definitions.
85      * <p>
86      * When configuring as a Spring bean definition, ANT-style resource
87      * patterns/wildcards are also supported, taking advantage of Spring's built
88      * in ResourceArrayPropertyEditor machinery.
89      * <p>
90      * For example:
91      *
92      * <pre>
93      * &lt;bean id=&quot;flowRegistry&quot; class=&quot;org.springframework.webflow.engine.builder.xml.XmlFlowRegistryFactoryBean&quot;&gt;
94      * &lt;property name=&quot;flowLocations&quot;&gt; value=&quot;/WEB-INF/flows/*-flow.xml&quot;/&gt;
95      * &lt;/bean&gt;
96      * </pre>
97      *
98      * Another example:
99      *
100      * <pre>
101      * &lt;bean id=&quot;flowRegistry&quot; class=&quot;org.springframework.webflow.engine.builder.xml.XmlFlowRegistryFactoryBean&quot;&gt;
102      * &lt;property name=&quot;flowLocations&quot;&gt; value=&quot;classpath*:/example/flows/*-flow.xml&quot;/&gt;
103      * &lt;/bean&gt;
104      * </pre>
105      *
106      * Flows registered from this set will be automatically assigned an id based
107      * on the filename of the matched XML resource.
108      * @param locations the resource locations
109      */

110     public void setFlowLocations(Resource[] locations) {
111         getXmlFlowRegistrar().setLocations(locations);
112     }
113
114     /**
115      * Convenience method for setting externalized flow definitions
116      * from a <code>java.util.Properties</code> map. Allows for more control
117      * over the definition, including which <code>flowId</code> is assigned.
118      * <p>
119      * Each property key is the <code>flowId</code> and each property value is
120      * the string encoded location of the externalized flow definition resource.
121      * <p>
122      * Here is the exact format:
123      *
124      * <pre>
125      * flow id=resource
126      * </pre>
127      *
128      * For example:
129      *
130      * <pre>
131      * &lt;bean id=&quot;flowRegistry&quot; class=&quot;org.springframework.webflow.engine.builder.xml.XmlFlowRegistryFactoryBean&quot;&gt;
132      * &lt;property name=&quot;flowDefinitions&quot;&gt;
133      * &lt;value&gt;
134      * searchFlow=/WEB-INF/flows/search-flow.xml
135      * detailFlow=/WEB-INF/flows/detail-flow.xml
136      * &lt;/value&gt;
137      * &lt;/property&gt;
138      * &lt;/bean&gt;
139      * </pre>
140      * @param flowDefinitions the flow definitions, defined within a properties
141      * map
142      */

143     public void setFlowDefinitions(Properties JavaDoc flowDefinitions) {
144         this.flowDefinitions = flowDefinitions;
145     }
146
147     /**
148      * Sets the loader to load XML-based flow definition documents during flow
149      * definition assembly. Allows for customization over how flow definition
150      * documents are loaded. Optional.
151      * @param documentLoader the document loader
152      */

153     public void setDocumentLoader(DocumentLoader documentLoader) {
154         getXmlFlowRegistrar().setDocumentLoader(documentLoader);
155     }
156
157     protected void init(FlowServiceLocator flowServiceLocator) {
158         // simply wire in the locator to the registrar
159
flowRegistrar.setFlowServiceLocator(flowServiceLocator);
160     }
161
162     protected void doPopulate(FlowDefinitionRegistry registry) {
163         addFlowDefinitionsFromProperties();
164         getXmlFlowRegistrar().registerFlowDefinitions(registry);
165     }
166
167     /**
168      * Add flow definitions configured using a property map to
169      * the flow definition registrar.
170      */

171     private void addFlowDefinitionsFromProperties() {
172         if (flowDefinitions != null && flowDefinitions.size() > 0) {
173             List JavaDoc flows = new ArrayList JavaDoc(flowDefinitions.size());
174             Iterator JavaDoc it = flowDefinitions.entrySet().iterator();
175             while (it.hasNext()) {
176                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc)it.next();
177                 String JavaDoc flowId = (String JavaDoc)entry.getKey();
178                 String JavaDoc location = (String JavaDoc)entry.getValue();
179                 Resource resource = getFlowServiceLocator().getResourceLoader().getResource(location);
180                 flows.add(new FlowDefinitionResource(flowId, resource));
181             }
182             getXmlFlowRegistrar().addResources(
183                     (FlowDefinitionResource[])flows.toArray(new FlowDefinitionResource[flows.size()]));
184             // cleanup
185
flowDefinitions = null;
186         }
187     }
188 }
Popular Tags