KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.springframework.core.io.Resource;
19 import org.springframework.util.Assert;
20 import org.springframework.webflow.definition.registry.ExternalizedFlowDefinitionRegistrar;
21 import org.springframework.webflow.definition.registry.FlowDefinitionHolder;
22 import org.springframework.webflow.definition.registry.FlowDefinitionResource;
23 import org.springframework.webflow.engine.builder.FlowAssembler;
24 import org.springframework.webflow.engine.builder.FlowBuilder;
25 import org.springframework.webflow.engine.builder.FlowServiceLocator;
26 import org.springframework.webflow.engine.builder.RefreshableFlowDefinitionHolder;
27
28 /**
29  * A flow definition registrar that populates a flow definition registry with
30  * flow definitions defined in externalized XML resources. Typically used in
31  * conjunction with a {@link XmlFlowRegistryFactoryBean} but may also be used
32  * standalone in programmatic fashion.
33  * <p>
34  * By default, a flow definition registered by this registrar will be assigned a
35  * registry identifier equal to the filename of the underlying definition
36  * resource, minus the filename extension. For example, a XML-based flow
37  * definition defined in the file "flow1.xml" will be identified as "flow1" when
38  * registered in a registry.
39  * <p>
40  * Programmatic usage example:
41  *
42  * <pre class="code">
43  * BeanFactory beanFactory = ...
44  * FlowDefinitionRegistry registry = new FlowDefinitionRegistryImpl();
45  * FlowServiceLocator flowServiceLocator =
46  * new DefaultFlowServiceLocator(registry, beanFactory);
47  * XmlFlowRegistrar registrar = new XmlFlowRegistrar(flowServiceLocator);
48  * File parent = new File(&quot;src/webapp/WEB-INF&quot;);
49  * registrar.addLocation(new FileSystemResource(new File(parent, &quot;flow1.xml&quot;));
50  * registrar.addLocation(new FileSystemResource(new File(parent, &quot;flow2.xml&quot;));
51  * registrar.registerFlowDefinitions(registry);
52  * </pre>
53  *
54  * @author Keith Donald
55  */

56 public class XmlFlowRegistrar extends ExternalizedFlowDefinitionRegistrar {
57
58     /**
59      * The xml file suffix constant.
60      */

61     private static final String JavaDoc XML_SUFFIX = ".xml";
62
63     /**
64      * The locator of services needed by flow definitions.
65      */

66     private FlowServiceLocator flowServiceLocator;
67
68     /**
69      * The loader of XML-based flow definition documents.
70      */

71     private DocumentLoader documentLoader;
72
73     /**
74      * Creates a new XML flow registrar. Protected constructor - if used, make
75      * sure the required {@link #flowServiceLocator} reference is set.
76      */

77     protected XmlFlowRegistrar() {
78     }
79
80     /**
81      * Creates a new XML flow registrar.
82      * @param flowServiceLocator the locator needed to support flow definition
83      * assembly
84      */

85     public XmlFlowRegistrar(FlowServiceLocator flowServiceLocator) {
86         setFlowServiceLocator(flowServiceLocator);
87     }
88
89     /**
90      * Sets the flow service locator.
91      * @param flowServiceLocator the flow service locator (may not be null)
92      */

93     protected void setFlowServiceLocator(FlowServiceLocator flowServiceLocator) {
94         Assert.notNull(flowServiceLocator, "The flow service locator is required");
95         this.flowServiceLocator = flowServiceLocator;
96     }
97
98     /**
99      * Returns the flow service locator.
100      */

101     protected FlowServiceLocator getFlowServiceLocator() {
102         return flowServiceLocator;
103     }
104
105     /**
106      * Sets the loader to load XML-based flow definition documents during flow
107      * definition assembly. Allows for customization over how documents are
108      * loaded. Optional.
109      * @param documentLoader the document loader
110      */

111     public void setDocumentLoader(DocumentLoader documentLoader) {
112         this.documentLoader = documentLoader;
113     }
114     
115     /**
116      * Returns the loader of XML-based flow definition documents.
117      */

118     public DocumentLoader getDocumentLoader() {
119         return documentLoader;
120     }
121
122     protected boolean isFlowDefinitionResource(Resource resource) {
123         return resource.getFilename().endsWith(XML_SUFFIX);
124     }
125
126     protected FlowDefinitionHolder createFlowDefinitionHolder(FlowDefinitionResource resource) {
127         FlowBuilder builder = createFlowBuilder(resource.getLocation());
128         FlowAssembler assembler = new FlowAssembler(resource.getId(), resource.getAttributes(), builder);
129         return new RefreshableFlowDefinitionHolder(assembler);
130     }
131     
132     // hook methods
133

134     /**
135      * Factory method that creates and fully initializes the XML-based flow
136      * definition builder.
137      * @param location the xml-based resource
138      * @return the builder to build the flow definition from the resource.
139      */

140     protected FlowBuilder createFlowBuilder(Resource location) {
141         XmlFlowBuilder builder = new XmlFlowBuilder(location, getFlowServiceLocator());
142         if (documentLoader != null) {
143             builder.setDocumentLoader(documentLoader);
144         }
145         return builder;
146     }
147 }
Popular Tags