KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > servlet > view > XmlViewResolver


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
17 package org.springframework.web.servlet.view;
18
19 import java.util.Locale JavaDoc;
20
21 import org.springframework.beans.BeansException;
22 import org.springframework.beans.factory.BeanFactory;
23 import org.springframework.beans.factory.DisposableBean;
24 import org.springframework.beans.factory.NoSuchBeanDefinitionException;
25 import org.springframework.beans.factory.xml.ResourceEntityResolver;
26 import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
27 import org.springframework.context.ConfigurableApplicationContext;
28 import org.springframework.core.Ordered;
29 import org.springframework.core.io.Resource;
30 import org.springframework.web.context.support.GenericWebApplicationContext;
31 import org.springframework.web.servlet.View;
32
33 /**
34  * Implementation of ViewResolver that uses bean definitions in an
35  * XML file, specified by resource location. The file will typically
36  * be located in the WEB-INF directory; default is "/WEB-INF/views.xml".
37  *
38  * <p>This ViewResolver does not support internationalization.
39  * Consider ResourceBundleViewResolver if you need to apply
40  * different view resources per locale.
41  *
42  * <p>Note: This ViewResolver implements the Ordered interface to allow for
43  * flexible participation in ViewResolver chaining. For example, some special
44  * views could be defined via this ViewResolver (giving it 0 as "order" value),
45  * while all remaining views could be resolved by a UrlBasedViewResolver.
46  *
47  * @author Juergen Hoeller
48  * @since 18.06.2003
49  * @see org.springframework.context.ApplicationContext#getResource
50  * @see ResourceBundleViewResolver
51  * @see UrlBasedViewResolver
52  */

53 public class XmlViewResolver extends AbstractCachingViewResolver implements Ordered, DisposableBean {
54
55     /** Default if no other location is supplied */
56     public final static String JavaDoc DEFAULT_LOCATION = "/WEB-INF/views.xml";
57
58
59     private int order = Integer.MAX_VALUE; // default: same as non-Ordered
60

61     private Resource location;
62
63     private ConfigurableApplicationContext cachedFactory;
64
65
66     public void setOrder(int order) {
67         this.order = order;
68     }
69
70     public int getOrder() {
71         return order;
72     }
73
74     /**
75      * Set the location of the XML file that defines the view beans.
76      * <p>The default is "/WEB-INF/views.xml".
77      * @param location the location of the XML file.
78      */

79     public void setLocation(Resource location) {
80         this.location = location;
81     }
82
83     /**
84      * Pre-initialize the factory from the XML file.
85      * Only effective if caching is enabled.
86      */

87     protected void initApplicationContext() throws BeansException {
88         if (isCache()) {
89             initFactory();
90         }
91     }
92
93
94     /**
95      * This implementation returns just the view name,
96      * as XmlViewResolver doesn't support localized resolution.
97      */

98     protected Object JavaDoc getCacheKey(String JavaDoc viewName, Locale JavaDoc locale) {
99         return viewName;
100     }
101
102     protected View loadView(String JavaDoc viewName, Locale JavaDoc locale) throws BeansException {
103         BeanFactory factory = initFactory();
104         try {
105             return (View) factory.getBean(viewName, View.class);
106         }
107         catch (NoSuchBeanDefinitionException ex) {
108             // to allow for ViewResolver chaining
109
return null;
110         }
111     }
112
113     /**
114      * Initialize the view bean factory from the XML file.
115      * Synchronized because of access by parallel threads.
116      * @throws BeansException in case of initialization errors
117      */

118     protected synchronized BeanFactory initFactory() throws BeansException {
119         if (this.cachedFactory != null) {
120             return this.cachedFactory;
121         }
122
123         Resource actualLocation = this.location;
124         if (actualLocation == null) {
125             actualLocation = getApplicationContext().getResource(DEFAULT_LOCATION);
126         }
127
128         // Create child ApplicationContext for views.
129
GenericWebApplicationContext factory = new GenericWebApplicationContext();
130         factory.setParent(getApplicationContext());
131         factory.setServletContext(getServletContext());
132
133         // Load XML resource with context-aware entity resolver.
134
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);
135         reader.setEntityResolver(new ResourceEntityResolver(getApplicationContext()));
136         reader.loadBeanDefinitions(actualLocation);
137
138         factory.refresh();
139
140         if (isCache()) {
141             this.cachedFactory = factory;
142         }
143         return factory;
144     }
145
146
147     /**
148      * Close the view bean factory on context shutdown.
149      */

150     public void destroy() throws BeansException {
151         if (this.cachedFactory != null) {
152             this.cachedFactory.close();
153         }
154     }
155
156 }
157
Popular Tags