KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > beans > factory > config > BeanDefinitionHolder


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.beans.factory.config;
18
19 import org.springframework.beans.BeanMetadataElement;
20 import org.springframework.util.Assert;
21 import org.springframework.util.ObjectUtils;
22 import org.springframework.util.StringUtils;
23
24 /**
25  * Holder for a BeanDefinition with name and aliases.
26  * Can be registered as a placeholder for an inner bean.
27  *
28  * <p>Can also be used for programmatic registration of inner bean
29  * definitions. If you don't care about BeanNameAware and the like,
30  * registering RootBeanDefinition or ChildBeanDefinition is good enough.
31  *
32  * @author Juergen Hoeller
33  * @since 1.0.2
34  * @see org.springframework.beans.factory.BeanNameAware
35  * @see org.springframework.beans.factory.support.RootBeanDefinition
36  * @see org.springframework.beans.factory.support.ChildBeanDefinition
37  */

38 public class BeanDefinitionHolder implements BeanMetadataElement {
39
40     private final BeanDefinition beanDefinition;
41
42     private final String JavaDoc beanName;
43
44     private final String JavaDoc[] aliases;
45
46
47     /**
48      * Create a new BeanDefinitionHolder.
49      * @param beanDefinition the BeanDefinition to wrap
50      * @param beanName the name of the bean, as specified for the bean definition
51      */

52     public BeanDefinitionHolder(BeanDefinition beanDefinition, String JavaDoc beanName) {
53         this(beanDefinition, beanName, null);
54     }
55
56     /**
57      * Create a new BeanDefinitionHolder.
58      * @param beanDefinition the BeanDefinition to wrap
59      * @param beanName the name of the bean, as specified for the bean definition
60      * @param aliases alias names for the bean, or <code>null</code> if none
61      */

62     public BeanDefinitionHolder(BeanDefinition beanDefinition, String JavaDoc beanName, String JavaDoc[] aliases) {
63         Assert.notNull(beanDefinition, "BeanDefinition must not be null");
64         Assert.notNull(beanName, "Bean name must not be null");
65         this.beanDefinition = beanDefinition;
66         this.beanName = beanName;
67         this.aliases = aliases;
68     }
69
70     /**
71      * Copy constructor: Create a new BeanDefinitionHolder with the
72      * same contents as the given BeanDefinitionHolder instance.
73      * <p>Note: The wrapped BeanDefinition reference is taken as-is;
74      * it is <code>not</code> deeply copied.
75      * @param beanDefinitionHolder the BeanDefinitionHolder to copy
76      */

77     public BeanDefinitionHolder(BeanDefinitionHolder beanDefinitionHolder) {
78         Assert.notNull(beanDefinitionHolder, "BeanDefinitionHolder must not be null");
79         this.beanDefinition = beanDefinitionHolder.getBeanDefinition();
80         this.beanName = beanDefinitionHolder.getBeanName();
81         this.aliases = beanDefinitionHolder.getAliases();
82     }
83
84
85     /**
86      * Return the wrapped BeanDefinition.
87      */

88     public BeanDefinition getBeanDefinition() {
89         return this.beanDefinition;
90     }
91
92     /**
93      * Return the primary name of the bean, as specified for the bean definition.
94      */

95     public String JavaDoc getBeanName() {
96         return this.beanName;
97     }
98
99     /**
100      * Return the alias names for the bean, as specified directly for the bean definition.
101      * @return the array of alias names, or <code>null</code> if none
102      */

103     public String JavaDoc[] getAliases() {
104         return this.aliases;
105     }
106
107     /**
108      * Expose the bean definition's source object.
109      * @see BeanDefinition#getSource()
110      */

111     public Object JavaDoc getSource() {
112         return this.beanDefinition.getSource();
113     }
114
115
116     /**
117      * Return a friendly, short description for the bean, stating name and aliases.
118      * @see #getBeanName()
119      * @see #getAliases()
120      */

121     public String JavaDoc getShortDescription() {
122         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
123         sb.append("Bean definition with name '").append(this.beanName).append("'");
124         if (this.aliases != null) {
125             sb.append(" and aliases [").append(StringUtils.arrayToCommaDelimitedString(this.aliases)).append("]");
126         }
127         return sb.toString();
128     }
129
130     /**
131      * Return a long description for the bean, including name and aliases
132      * as well as a description of the contained {@link BeanDefinition}.
133      * @see #getShortDescription()
134      * @see #getBeanDefinition()
135      */

136     public String JavaDoc getLongDescription() {
137         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(getShortDescription());
138         sb.append(": ").append(this.beanDefinition);
139         return sb.toString();
140     }
141
142     /**
143      * This implementation returns the long description. Can be overridden
144      * to return the short description or any kind of custom description instead.
145      * @see #getLongDescription()
146      * @see #getShortDescription()
147      */

148     public String JavaDoc toString() {
149         return getLongDescription();
150     }
151
152
153     public boolean equals(Object JavaDoc other) {
154         if (this == other) {
155             return true;
156         }
157         if (!(other instanceof BeanDefinitionHolder)) {
158             return false;
159         }
160         BeanDefinitionHolder otherHolder = (BeanDefinitionHolder) other;
161         return this.beanDefinition.equals(otherHolder.beanDefinition) &&
162                 this.beanName.equals(otherHolder.beanName) &&
163                 ObjectUtils.nullSafeEquals(this.aliases, otherHolder.aliases);
164     }
165
166     public int hashCode() {
167         int hashCode = this.beanDefinition.hashCode();
168         hashCode = 29 * hashCode + this.beanName.hashCode();
169         hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.aliases);
170         return hashCode;
171     }
172
173 }
174
Popular Tags