KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > portlet > context > XmlPortletApplicationContext


1 /*
2  * Copyright 2002-2007 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.portlet.context;
18
19 import java.io.IOException JavaDoc;
20
21 import org.springframework.beans.BeansException;
22 import org.springframework.beans.factory.support.DefaultListableBeanFactory;
23 import org.springframework.beans.factory.xml.ResourceEntityResolver;
24 import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
25
26 /**
27  * Portlet-based {@link org.springframework.web.context.WebApplicationContext}
28  * implementation which takes its configuration from XML documents, understood
29  * by an {@link org.springframework.beans.factory.xml.XmlBeanDefinitionReader}.
30  * This is essentially the equivalent of
31  * {@link org.springframework.context.support.AbstractXmlApplicationContext}
32  * for a portlet environment.
33  *
34  * <p>By default, the configuration will be taken from "/WEB-INF/applicationContext.xml"
35  * for the root context, and "/WEB-INF/test-portlet.xml" for a context with the namespace
36  * "test-portlet" (like for a DispatcherPortlet instance with the portlet-name "test").
37  *
38  * <p>The config location defaults can be overridden via the "contextConfigLocation"
39  * portlet init-param of {@link org.springframework.web.portlet.FrameworkPortlet}.
40  * Config locations can either denote concrete files like "/WEB-INF/context.xml"
41  * or Ant-style patterns like "/WEB-INF/*-context.xml" (see
42  * {@link org.springframework.util.PathMatcher} javadoc for pattern details).
43  *
44  * <p>Note: In case of multiple config locations, later bean definitions will
45  * override ones defined in earlier loaded files. This can be leveraged to
46  * deliberately override certain bean definitions via an extra XML file.
47  *
48  * <p><b>For a Portlet-based context that reads in a different bean definition format,
49  * create an analogous subclass of {@link AbstractRefreshablePortletApplicationContext}.</b>
50  * Such a context implementation can be specified as "contextClass" init-param
51  * for a FrameworkPortlet instance.
52  *
53  * @author Juergen Hoeller
54  * @author John A. Lewis
55  * @since 2.0
56  * @see #setNamespace
57  * @see #setConfigLocations
58  * @see org.springframework.beans.factory.xml.XmlBeanDefinitionReader
59  * @see org.springframework.web.portlet.FrameworkPortlet#initPortletApplicationContext
60  */

61 public class XmlPortletApplicationContext extends AbstractRefreshablePortletApplicationContext {
62
63     /** Default config location for the root context */
64     public static final String JavaDoc DEFAULT_CONFIG_LOCATION = "/WEB-INF/applicationContext.xml";
65
66     /** Default prefix for building a config location for a namespace */
67     public static final String JavaDoc DEFAULT_CONFIG_LOCATION_PREFIX = "/WEB-INF/";
68
69     /** Default suffix for building a config location for a namespace */
70     public static final String JavaDoc DEFAULT_CONFIG_LOCATION_SUFFIX = ".xml";
71
72
73     /**
74      * Loads the bean definitions via an XmlBeanDefinitionReader.
75      * @see org.springframework.beans.factory.xml.XmlBeanDefinitionReader
76      * @see #initBeanDefinitionReader
77      * @see #loadBeanDefinitions
78      */

79     protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws IOException JavaDoc {
80         // Create a new XmlBeanDefinitionReader for the given BeanFactory.
81
XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);
82
83         // Configure the bean definition reader with this context's
84
// resource loading environment.
85
beanDefinitionReader.setResourceLoader(this);
86         beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));
87
88         // Allow a subclass to provide custom initialization of the reader,
89
// then proceed with actually loading the bean definitions.
90
initBeanDefinitionReader(beanDefinitionReader);
91         loadBeanDefinitions(beanDefinitionReader);
92     }
93
94     /**
95      * Initialize the bean definition reader used for loading the bean
96      * definitions of this context. Default implementation is empty.
97      * <p>Can be overridden in subclasses, e.g. for turning off XML validation
98      * or using a different XmlBeanDefinitionParser implementation.
99      * @param beanDefinitionReader the bean definition reader used by this context
100      * @see org.springframework.beans.factory.xml.XmlBeanDefinitionReader#setValidationMode
101      * @see org.springframework.beans.factory.xml.XmlBeanDefinitionReader#setDocumentReaderClass
102      */

103     protected void initBeanDefinitionReader(XmlBeanDefinitionReader beanDefinitionReader) {
104     }
105
106     /**
107      * Load the bean definitions with the given XmlBeanDefinitionReader.
108      * <p>The lifecycle of the bean factory is handled by the refreshBeanFactory method;
109      * therefore this method is just supposed to load and/or register bean definitions.
110      * <p>Delegates to a ResourcePatternResolver for resolving location patterns
111      * into Resource instances.
112      * @throws org.springframework.beans.BeansException in case of bean registration errors
113      * @throws java.io.IOException if the required XML document isn't found
114      * @see #refreshBeanFactory
115      * @see #getConfigLocations
116      * @see #getResources
117      * @see #getResourcePatternResolver
118      */

119     protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws BeansException, IOException JavaDoc {
120         String JavaDoc[] configLocations = getConfigLocations();
121         if (configLocations != null) {
122             for (int i = 0; i < configLocations.length; i++) {
123                 reader.loadBeanDefinitions(configLocations[i]);
124             }
125         }
126     }
127
128     /**
129      * The default location for the root context is "/WEB-INF/applicationContext.xml",
130      * and "/WEB-INF/test-portlet.xml" for a context with the namespace "test-portlet"
131      * (like for a DispatcherPortlet instance with the portlet-name "test").
132      */

133     protected String JavaDoc[] getDefaultConfigLocations() {
134         if (getNamespace() != null) {
135             return new String JavaDoc[] {DEFAULT_CONFIG_LOCATION_PREFIX + getNamespace() + DEFAULT_CONFIG_LOCATION_SUFFIX};
136         }
137         else {
138             return new String JavaDoc[] {DEFAULT_CONFIG_LOCATION};
139         }
140     }
141
142 }
143
Popular Tags