KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.springframework.beans.MutablePropertyValues;
20 import org.springframework.beans.factory.BeanDefinitionStoreException;
21 import org.springframework.beans.factory.BeanFactoryUtils;
22 import org.springframework.beans.factory.config.BeanDefinitionHolder;
23 import org.springframework.beans.factory.config.ConstructorArgumentValues;
24 import org.springframework.util.ClassUtils;
25 import org.springframework.util.ObjectUtils;
26 import org.springframework.util.StringUtils;
27
28 /**
29  * Utility methods that are useful for bean definition reader implementations.
30  * Mainly intended for internal use.
31  *
32  * @author Juergen Hoeller
33  * @author Rob Harrop
34  * @since 1.1
35  * @see PropertiesBeanDefinitionReader
36  * @see org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader
37  */

38 public class BeanDefinitionReaderUtils {
39
40     /**
41      * Separator for generated bean names. If a class name or parent name is not
42      * unique, "#1", "#2" etc will be appended, until the name becomes unique.
43      */

44     public static final String JavaDoc GENERATED_BEAN_NAME_SEPARATOR = BeanFactoryUtils.GENERATED_BEAN_NAME_SEPARATOR;
45
46
47     /**
48      * Create a new RootBeanDefinition or ChildBeanDefinition for the given
49      * class name, parent, constructor arguments, and property values.
50      * @param className the name of the bean class, if any
51      * @param parent the name of the parent bean, if any
52      * @param cargs the constructor arguments, if any
53      * @param pvs the property values, if any
54      * @param classLoader the ClassLoader to use for loading bean classes
55      * (can be <code>null</code> to just register bean classes by name)
56      * @return the bean definition
57      * @throws ClassNotFoundException if the bean class could not be loaded
58      * @deprecated in favor of <code>createBeanDefinition(String, String, ClassLoader)</code>
59      * @see #createBeanDefinition(String, String, ClassLoader)
60      */

61     public static AbstractBeanDefinition createBeanDefinition(
62             String JavaDoc className, String JavaDoc parent, ConstructorArgumentValues cargs,
63             MutablePropertyValues pvs, ClassLoader JavaDoc classLoader) throws ClassNotFoundException JavaDoc {
64
65         AbstractBeanDefinition bd = createBeanDefinition(parent, className, classLoader);
66         bd.setConstructorArgumentValues(cargs);
67         bd.setPropertyValues(pvs);
68         return bd;
69     }
70
71     /**
72      * Create a new RootBeanDefinition or ChildBeanDefinition for the given
73      * class name, parent, constructor arguments, and property values.
74      * @param parent the name of the parent bean, if any
75      * @param className the name of the bean class, if any
76      * @param classLoader the ClassLoader to use for loading bean classes
77      * (can be <code>null</code> to just register bean classes by name)
78      * @return the bean definition
79      * @throws ClassNotFoundException if the bean class could not be loaded
80      */

81     public static AbstractBeanDefinition createBeanDefinition(
82             String JavaDoc parent, String JavaDoc className, ClassLoader JavaDoc classLoader) throws ClassNotFoundException JavaDoc {
83
84         AbstractBeanDefinition bd = null;
85         if (parent != null) {
86             bd = new ChildBeanDefinition(parent);
87         }
88         else {
89             bd = new RootBeanDefinition();
90         }
91         if (className != null) {
92             if (classLoader != null) {
93                 bd.setBeanClass(ClassUtils.forName(className, classLoader));
94             }
95             else {
96                 bd.setBeanClassName(className);
97             }
98         }
99         return bd;
100     }
101
102     /**
103      * Generate a bean name for the given bean definition, unique within the
104      * given bean factory.
105      * @param beanDefinition the bean definition to generate a bean name for
106      * @param beanFactory the bean factory that the definition is going to be
107      * registered with (to check for existing bean names)
108      * @param isInnerBean whether the given bean definition will be registered
109      * as inner bean or as top-level bean (allowing for special name generation
110      * for inner beans versus top-level beans)
111      * @return the generated bean name
112      * @throws BeanDefinitionStoreException if no unique name can be generated
113      * for the given bean definition
114      */

115     public static String JavaDoc generateBeanName(
116             AbstractBeanDefinition beanDefinition, BeanDefinitionRegistry beanFactory, boolean isInnerBean)
117             throws BeanDefinitionStoreException {
118
119         String JavaDoc generatedId = beanDefinition.getBeanClassName();
120         if (generatedId == null) {
121             if (beanDefinition instanceof ChildBeanDefinition) {
122                 generatedId = ((ChildBeanDefinition) beanDefinition).getParentName() + "$child";
123             }
124             else if (beanDefinition.getFactoryBeanName() != null) {
125                 generatedId = beanDefinition.getFactoryBeanName() + "$created";
126             }
127         }
128         if (!StringUtils.hasText(generatedId)) {
129             throw new BeanDefinitionStoreException("Unnamed bean definition specifies neither " +
130                     "'class' nor 'parent' nor 'factory-bean' - can't generate bean name");
131         }
132
133         String JavaDoc id = generatedId;
134         if (isInnerBean) {
135             // Inner bean: generate identity hashcode suffix.
136
id = generatedId + GENERATED_BEAN_NAME_SEPARATOR + ObjectUtils.getIdentityHexString(beanDefinition);
137         }
138         else {
139             // Top-level bean: use plain class name. If not already unique,
140
// add counter - increasing the counter until the name is unique.
141
int counter = 0;
142             while (beanFactory.containsBeanDefinition(id)) {
143                 counter++;
144                 id = generatedId + GENERATED_BEAN_NAME_SEPARATOR + counter;
145             }
146         }
147         return id;
148     }
149
150     /**
151      * Generate a bean name for the given top-level bean definition,
152      * unique within the given bean factory.
153      * @param beanDefinition the bean definition to generate a bean name for
154      * @param beanFactory the bean factory that the definition is going to be
155      * registered with (to check for existing bean names)
156      * @return the generated bean name
157      * @throws BeanDefinitionStoreException if no unique name can be generated
158      * for the given bean definition
159      */

160     public static String JavaDoc generateBeanName(
161             AbstractBeanDefinition beanDefinition, BeanDefinitionRegistry beanFactory)
162             throws BeanDefinitionStoreException {
163
164         return generateBeanName(beanDefinition, beanFactory, false);
165     }
166
167     /**
168      * Register the given bean definition with the given bean factory.
169      * @param bdHolder the bean definition including name and aliases
170      * @param beanFactory the bean factory to register with
171      * @throws BeanDefinitionStoreException if registration failed
172      */

173     public static void registerBeanDefinition(
174             BeanDefinitionHolder bdHolder, BeanDefinitionRegistry beanFactory)
175             throws BeanDefinitionStoreException {
176
177         // Register bean definition under primary name.
178
String JavaDoc beanName = bdHolder.getBeanName();
179         beanFactory.registerBeanDefinition(beanName, bdHolder.getBeanDefinition());
180
181         // Register aliases for bean name, if any.
182
String JavaDoc[] aliases = bdHolder.getAliases();
183         if (aliases != null) {
184             for (int i = 0; i < aliases.length; i++) {
185                 beanFactory.registerAlias(beanName, aliases[i]);
186             }
187         }
188     }
189
190     /**
191      * Register the given bean definition with a generated name,
192      * unique within the given bean factory.
193      * @param beanDefinition the bean definition to generate a bean name for
194      * @param beanFactory the bean factory to register with
195      * @return the generated bean name
196      * @throws BeanDefinitionStoreException if no unique name can be generated
197      * for the given bean definition or the definition cannot be registered
198      */

199     public static String JavaDoc registerWithGeneratedName(
200             AbstractBeanDefinition beanDefinition, BeanDefinitionRegistry beanFactory)
201             throws BeanDefinitionStoreException {
202
203         String JavaDoc generatedName = generateBeanName(beanDefinition, beanFactory, false);
204         beanFactory.registerBeanDefinition(generatedName, beanDefinition);
205         return generatedName;
206     }
207
208 }
209
Popular Tags