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.parsing;
018:
019: import java.util.ArrayList;
020: import java.util.List;
021:
022: import org.springframework.beans.PropertyValue;
023: import org.springframework.beans.PropertyValues;
024: import org.springframework.beans.factory.config.BeanDefinition;
025: import org.springframework.beans.factory.config.BeanDefinitionHolder;
026: import org.springframework.beans.factory.config.BeanReference;
027:
028: /**
029: * ComponentDefinition based on a standard BeanDefinition, exposing the given bean
030: * definition as well as inner bean definitions and bean references for the given bean.
031: *
032: * @author Rob Harrop
033: * @author Juergen Hoeller
034: * @since 2.0
035: */
036: public class BeanComponentDefinition extends BeanDefinitionHolder
037: implements ComponentDefinition {
038:
039: private BeanDefinition[] innerBeanDefinitions;
040:
041: private BeanReference[] beanReferences;
042:
043: /**
044: * Create a new BeanComponentDefinition for the given bean.
045: * @param beanDefinition the BeanDefinition
046: * @param beanName the name of the bean
047: */
048: public BeanComponentDefinition(BeanDefinition beanDefinition,
049: String beanName) {
050: super (beanDefinition, beanName);
051: findInnerBeanDefinitionsAndBeanReferences(beanDefinition);
052: }
053:
054: /**
055: * Create a new BeanComponentDefinition for the given bean.
056: * @param holder the BeanDefinitionHolder encapsulating the
057: * bean definition as well as the name of the bean
058: */
059: public BeanComponentDefinition(BeanDefinitionHolder holder) {
060: super (holder);
061: findInnerBeanDefinitionsAndBeanReferences(holder
062: .getBeanDefinition());
063: }
064:
065: private void findInnerBeanDefinitionsAndBeanReferences(
066: BeanDefinition beanDefinition) {
067: List innerBeans = new ArrayList();
068: List references = new ArrayList();
069: PropertyValues propertyValues = beanDefinition
070: .getPropertyValues();
071: for (int i = 0; i < propertyValues.getPropertyValues().length; i++) {
072: PropertyValue propertyValue = propertyValues
073: .getPropertyValues()[i];
074: Object value = propertyValue.getValue();
075: if (value instanceof BeanDefinitionHolder) {
076: innerBeans.add(((BeanDefinitionHolder) value)
077: .getBeanDefinition());
078: } else if (value instanceof BeanDefinition) {
079: innerBeans.add(value);
080: } else if (value instanceof BeanReference) {
081: references.add(value);
082: }
083: }
084: this .innerBeanDefinitions = (BeanDefinition[]) innerBeans
085: .toArray(new BeanDefinition[innerBeans.size()]);
086: this .beanReferences = (BeanReference[]) references
087: .toArray(new BeanReference[references.size()]);
088: }
089:
090: public String getName() {
091: return getBeanName();
092: }
093:
094: public String getDescription() {
095: return getShortDescription();
096: }
097:
098: public BeanDefinition[] getBeanDefinitions() {
099: return new BeanDefinition[] { getBeanDefinition() };
100: }
101:
102: public BeanDefinition[] getInnerBeanDefinitions() {
103: return this .innerBeanDefinitions;
104: }
105:
106: public BeanReference[] getBeanReferences() {
107: return this .beanReferences;
108: }
109:
110: /**
111: * This implementation returns this ComponentDefinition's description.
112: * @see #getDescription()
113: */
114: public String toString() {
115: return getDescription();
116: }
117:
118: /**
119: * This implementations expects the other object to be of type BeanComponentDefinition
120: * as well, in addition to the superclass's equality requirements.
121: */
122: public boolean equals(Object other) {
123: return (this == other || (other instanceof BeanComponentDefinition && super
124: .equals(other)));
125: }
126:
127: }
|