001: /*
002: * Copyright 2002-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.beans.factory.config;
018:
019: import org.springframework.beans.BeanMetadataElement;
020: import org.springframework.util.Assert;
021: import org.springframework.util.ObjectUtils;
022: import org.springframework.util.StringUtils;
023:
024: /**
025: * Holder for a BeanDefinition with name and aliases.
026: * Can be registered as a placeholder for an inner bean.
027: *
028: * <p>Can also be used for programmatic registration of inner bean
029: * definitions. If you don't care about BeanNameAware and the like,
030: * registering RootBeanDefinition or ChildBeanDefinition is good enough.
031: *
032: * @author Juergen Hoeller
033: * @since 1.0.2
034: * @see org.springframework.beans.factory.BeanNameAware
035: * @see org.springframework.beans.factory.support.RootBeanDefinition
036: * @see org.springframework.beans.factory.support.ChildBeanDefinition
037: */
038: public class BeanDefinitionHolder implements BeanMetadataElement {
039:
040: private final BeanDefinition beanDefinition;
041:
042: private final String beanName;
043:
044: private final String[] aliases;
045:
046: /**
047: * Create a new BeanDefinitionHolder.
048: * @param beanDefinition the BeanDefinition to wrap
049: * @param beanName the name of the bean, as specified for the bean definition
050: */
051: public BeanDefinitionHolder(BeanDefinition beanDefinition,
052: String beanName) {
053: this (beanDefinition, beanName, null);
054: }
055:
056: /**
057: * Create a new BeanDefinitionHolder.
058: * @param beanDefinition the BeanDefinition to wrap
059: * @param beanName the name of the bean, as specified for the bean definition
060: * @param aliases alias names for the bean, or <code>null</code> if none
061: */
062: public BeanDefinitionHolder(BeanDefinition beanDefinition,
063: String beanName, String[] aliases) {
064: Assert.notNull(beanDefinition,
065: "BeanDefinition must not be null");
066: Assert.notNull(beanName, "Bean name must not be null");
067: this .beanDefinition = beanDefinition;
068: this .beanName = beanName;
069: this .aliases = aliases;
070: }
071:
072: /**
073: * Copy constructor: Create a new BeanDefinitionHolder with the
074: * same contents as the given BeanDefinitionHolder instance.
075: * <p>Note: The wrapped BeanDefinition reference is taken as-is;
076: * it is <code>not</code> deeply copied.
077: * @param beanDefinitionHolder the BeanDefinitionHolder to copy
078: */
079: public BeanDefinitionHolder(
080: BeanDefinitionHolder beanDefinitionHolder) {
081: Assert.notNull(beanDefinitionHolder,
082: "BeanDefinitionHolder must not be null");
083: this .beanDefinition = beanDefinitionHolder.getBeanDefinition();
084: this .beanName = beanDefinitionHolder.getBeanName();
085: this .aliases = beanDefinitionHolder.getAliases();
086: }
087:
088: /**
089: * Return the wrapped BeanDefinition.
090: */
091: public BeanDefinition getBeanDefinition() {
092: return this .beanDefinition;
093: }
094:
095: /**
096: * Return the primary name of the bean, as specified for the bean definition.
097: */
098: public String getBeanName() {
099: return this .beanName;
100: }
101:
102: /**
103: * Return the alias names for the bean, as specified directly for the bean definition.
104: * @return the array of alias names, or <code>null</code> if none
105: */
106: public String[] getAliases() {
107: return this .aliases;
108: }
109:
110: /**
111: * Expose the bean definition's source object.
112: * @see BeanDefinition#getSource()
113: */
114: public Object getSource() {
115: return this .beanDefinition.getSource();
116: }
117:
118: /**
119: * Return a friendly, short description for the bean, stating name and aliases.
120: * @see #getBeanName()
121: * @see #getAliases()
122: */
123: public String getShortDescription() {
124: StringBuffer sb = new StringBuffer();
125: sb.append("Bean definition with name '").append(this .beanName)
126: .append("'");
127: if (this .aliases != null) {
128: sb.append(" and aliases [").append(
129: StringUtils
130: .arrayToCommaDelimitedString(this .aliases))
131: .append("]");
132: }
133: return sb.toString();
134: }
135:
136: /**
137: * Return a long description for the bean, including name and aliases
138: * as well as a description of the contained {@link BeanDefinition}.
139: * @see #getShortDescription()
140: * @see #getBeanDefinition()
141: */
142: public String getLongDescription() {
143: StringBuffer sb = new StringBuffer(getShortDescription());
144: sb.append(": ").append(this .beanDefinition);
145: return sb.toString();
146: }
147:
148: /**
149: * This implementation returns the long description. Can be overridden
150: * to return the short description or any kind of custom description instead.
151: * @see #getLongDescription()
152: * @see #getShortDescription()
153: */
154: public String toString() {
155: return getLongDescription();
156: }
157:
158: public boolean equals(Object other) {
159: if (this == other) {
160: return true;
161: }
162: if (!(other instanceof BeanDefinitionHolder)) {
163: return false;
164: }
165: BeanDefinitionHolder otherHolder = (BeanDefinitionHolder) other;
166: return this .beanDefinition.equals(otherHolder.beanDefinition)
167: && this .beanName.equals(otherHolder.beanName)
168: && ObjectUtils.nullSafeEquals(this .aliases,
169: otherHolder.aliases);
170: }
171:
172: public int hashCode() {
173: int hashCode = this .beanDefinition.hashCode();
174: hashCode = 29 * hashCode + this .beanName.hashCode();
175: hashCode = 29 * hashCode
176: + ObjectUtils.nullSafeHashCode(this.aliases);
177: return hashCode;
178: }
179:
180: }
|