KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > beans > factory > support > AbstractBeanDefinitionReader


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.beans.factory.support;
18
19 import java.io.IOException JavaDoc;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23
24 import org.springframework.beans.factory.BeanDefinitionStoreException;
25 import org.springframework.core.io.Resource;
26 import org.springframework.core.io.ResourceLoader;
27 import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
28 import org.springframework.core.io.support.ResourcePatternResolver;
29 import org.springframework.util.Assert;
30
31 /**
32  * Abstract base class for bean definition readers which implement
33  * the {@link BeanDefinitionReader} interface.
34  *
35  * <p>Provides common properties like the bean factory to work on
36  * and the class loader to use for loading bean classes.
37  *
38  * @author Juergen Hoeller
39  * @since 11.12.2003
40  * @see BeanDefinitionReaderUtils
41  */

42 public abstract class AbstractBeanDefinitionReader implements BeanDefinitionReader {
43
44     protected final Log logger = LogFactory.getLog(getClass());
45
46     private final BeanDefinitionRegistry beanFactory;
47
48     private BeanNameGenerator beanNameGenerator = new DefaultBeanNameGenerator();
49
50     private ResourceLoader resourceLoader;
51
52     private ClassLoader JavaDoc beanClassLoader;
53
54
55     /**
56      * Create a new AbstractBeanDefinitionReader for the given bean factory.
57      * <p>If the passed-in bean factory does not only implement the BeanDefinitionRegistry
58      * interface but also the ResourceLoader interface, it will be used as default
59      * ResourceLoader as well. This will usually be the case for
60      * {@link org.springframework.context.ApplicationContext} implementations.
61      * <p>If given a plain BeanDefinitionRegistry, the default ResourceLoader will be a
62      * {@link org.springframework.core.io.support.PathMatchingResourcePatternResolver}.
63      * @param beanFactory the BeanFactory to load bean definitions into,
64      * in the form of a BeanDefinitionRegistry
65      * @see #setResourceLoader
66      */

67     protected AbstractBeanDefinitionReader(BeanDefinitionRegistry beanFactory) {
68         Assert.notNull(beanFactory, "Bean factory must not be null");
69         this.beanFactory = beanFactory;
70
71         // Determine ResourceLoader to use.
72
if (this.beanFactory instanceof ResourceLoader) {
73             this.resourceLoader = (ResourceLoader) this.beanFactory;
74         }
75         else {
76             this.resourceLoader = new PathMatchingResourcePatternResolver();
77         }
78     }
79
80     public BeanDefinitionRegistry getBeanFactory() {
81         return this.beanFactory;
82     }
83
84     public void setBeanNameGenerator(BeanNameGenerator beanNameGenerator) {
85         this.beanNameGenerator = (beanNameGenerator != null ? beanNameGenerator : new DefaultBeanNameGenerator());
86     }
87
88     public BeanNameGenerator getBeanNameGenerator() {
89         return this.beanNameGenerator;
90     }
91
92     /**
93      * Set the ResourceLoader to use for resource locations.
94      * If specifying a ResourcePatternResolver, the bean definition reader
95      * will be capable of resolving resource patterns to Resource arrays.
96      * <p>Default is PathMatchingResourcePatternResolver, also capable of
97      * resource pattern resolving through the ResourcePatternResolver interface.
98      * <p>Setting this to <code>null</code> suggests that absolute resource loading
99      * is not available for this bean definition reader.
100      * @see org.springframework.core.io.support.ResourcePatternResolver
101      * @see org.springframework.core.io.support.PathMatchingResourcePatternResolver
102      */

103     public void setResourceLoader(ResourceLoader resourceLoader) {
104         this.resourceLoader = resourceLoader;
105     }
106
107     public ResourceLoader getResourceLoader() {
108         return this.resourceLoader;
109     }
110
111     /**
112      * Set the ClassLoader to use for bean classes.
113      * <p>Default is <code>null</code>, which suggests to not load bean classes
114      * eagerly but rather to just register bean definitions with class names,
115      * with the corresponding Classes to be resolved later (or never).
116      * @see java.lang.Thread#getContextClassLoader()
117      */

118     public void setBeanClassLoader(ClassLoader JavaDoc beanClassLoader) {
119         this.beanClassLoader = beanClassLoader;
120     }
121
122     public ClassLoader JavaDoc getBeanClassLoader() {
123         return this.beanClassLoader;
124     }
125
126
127     public int loadBeanDefinitions(Resource[] resources) throws BeanDefinitionStoreException {
128         Assert.notNull(resources, "Resource array must not be null");
129         int counter = 0;
130         for (int i = 0; i < resources.length; i++) {
131             counter += loadBeanDefinitions(resources[i]);
132         }
133         return counter;
134     }
135
136     public int loadBeanDefinitions(String JavaDoc location) throws BeanDefinitionStoreException {
137         ResourceLoader resourceLoader = getResourceLoader();
138         if (resourceLoader == null) {
139             throw new BeanDefinitionStoreException(
140                     "Cannot import bean definitions from location [" + location + "]: no ResourceLoader available");
141         }
142
143         if (resourceLoader instanceof ResourcePatternResolver) {
144             // Resource pattern matching available.
145
try {
146                 Resource[] resources = ((ResourcePatternResolver) resourceLoader).getResources(location);
147                 int loadCount = loadBeanDefinitions(resources);
148                 if (logger.isDebugEnabled()) {
149                     logger.debug("Loaded " + loadCount + " bean definitions from location pattern [" + location + "]");
150                 }
151                 return loadCount;
152             }
153             catch (IOException JavaDoc ex) {
154                 throw new BeanDefinitionStoreException(
155                         "Could not resolve bean definition resource pattern [" + location + "]", ex);
156             }
157         }
158         else {
159             // Can only load single resources by absolute URL.
160
Resource resource = resourceLoader.getResource(location);
161             int loadCount = loadBeanDefinitions(resource);
162             if (logger.isDebugEnabled()) {
163                 logger.debug("Loaded " + loadCount + " bean definitions from location [" + location + "]");
164             }
165             return loadCount;
166         }
167     }
168
169     public int loadBeanDefinitions(String JavaDoc[] locations) throws BeanDefinitionStoreException {
170         Assert.notNull(locations, "Location array must not be null");
171         int counter = 0;
172         for (int i = 0; i < locations.length; i++) {
173             counter += loadBeanDefinitions(locations[i]);
174         }
175         return counter;
176     }
177
178 }
179
Popular Tags