KickJava   Java API By Example, From Geeks To Geeks.

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


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.beans.factory.BeanFactory;
19 import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
20 import org.springframework.context.ApplicationContext;
21 import org.springframework.context.support.GenericApplicationContext;
22 import org.springframework.core.io.Resource;
23 import org.springframework.web.context.WebApplicationContext;
24 import org.springframework.web.context.support.GenericWebApplicationContext;
25 import org.springframework.webflow.engine.Flow;
26 import org.springframework.webflow.engine.builder.FlowServiceLocator;
27
28 /**
29  * Simple value object that holds a reference to a local artifact registry
30  * of a flow definition that is in the process of being constructed.
31  * <p>
32  * Internal helper class of the {@link org.springframework.webflow.engine.builder.xml.XmlFlowBuilder}.
33  * Package private to highlight it's non-public nature.
34  *
35  * @see org.springframework.webflow.engine.builder.xml.XmlFlowBuilder
36  * @see org.springframework.webflow.engine.builder.xml.LocalFlowServiceLocator
37  *
38  * @author Keith Donald
39  */

40 class LocalFlowServiceRegistry {
41
42     /**
43      * The flow this registry is for (and scoped by).
44      */

45     private Flow flow;
46
47     /**
48      * The locations of the registry resource definitions.
49      */

50     private Resource[] resources;
51
52     /**
53      * The local registry holding the artifacts local to the flow.
54      */

55     private GenericApplicationContext context;
56
57     /**
58      * Create a new registry, loading artifact definitions from
59      * given resources.
60      * @param flow the flow this registry is for (and scoped by)
61      * @param resources the registry resource definitions
62      */

63     public LocalFlowServiceRegistry(Flow flow, Resource[] resources) {
64         this.flow = flow;
65         this.resources = resources;
66     }
67
68     /**
69      * Returns the flow this registry is for (and scoped by).
70      */

71     public Flow getFlow() {
72         return flow;
73     }
74
75     /**
76      * Returns the resources defining registry artifacts.
77      */

78     public Resource[] getResources() {
79         return resources;
80     }
81
82     /**
83      * Retuns the application context holding registry artifacts.
84      */

85     public ApplicationContext getContext() {
86         return context;
87     }
88
89     /**
90      * Initialize this registry of the local flow service locator.
91      * @param localFactory the local flow service locator
92      * @param rootFactory the root service locator
93      */

94     public void init(LocalFlowServiceLocator localFactory, FlowServiceLocator rootFactory) {
95         BeanFactory parent = null;
96         if (localFactory.isEmpty()) {
97             try {
98                 parent = rootFactory.getBeanFactory();
99             }
100             catch (UnsupportedOperationException JavaDoc e) {
101                 // can't link to a parent
102
}
103         }
104         else {
105             parent = localFactory.top().context;
106         }
107         context = createLocalFlowContext(parent, rootFactory);
108         new XmlBeanDefinitionReader(context).loadBeanDefinitions(resources);
109         context.refresh();
110     }
111
112     /**
113      * Create the flow local application context.
114      * @param parent the parent application context
115      * @param rootFactory the root service locator, used to obtain a resource
116      * loader
117      * @return the flow local application context
118      */

119     private GenericApplicationContext createLocalFlowContext(BeanFactory parent, FlowServiceLocator rootFactory) {
120         if (parent instanceof WebApplicationContext) {
121             GenericWebApplicationContext context = new GenericWebApplicationContext();
122             context.setServletContext(((WebApplicationContext)parent).getServletContext());
123             context.setParent((WebApplicationContext)parent);
124             context.setResourceLoader(rootFactory.getResourceLoader());
125             return context;
126         }
127         else {
128             GenericApplicationContext context = new GenericApplicationContext();
129             if (parent instanceof ApplicationContext) {
130                 context.setParent((ApplicationContext)parent);
131             }
132             else {
133                 if (parent != null) {
134                     context.getBeanFactory().setParentBeanFactory(parent);
135                 }
136             }
137             context.setResourceLoader(rootFactory.getResourceLoader());
138             return context;
139         }
140     }
141 }
Popular Tags