KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > beans > factory > parsing > BeanComponentDefinition


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.parsing;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.List JavaDoc;
21
22 import org.springframework.beans.PropertyValue;
23 import org.springframework.beans.PropertyValues;
24 import org.springframework.beans.factory.config.BeanDefinition;
25 import org.springframework.beans.factory.config.BeanDefinitionHolder;
26 import org.springframework.beans.factory.config.BeanReference;
27
28 /**
29  * ComponentDefinition based on a standard BeanDefinition, exposing the given bean
30  * definition as well as inner bean definitions and bean references for the given bean.
31  *
32  * @author Rob Harrop
33  * @author Juergen Hoeller
34  * @since 2.0
35  */

36 public class BeanComponentDefinition extends BeanDefinitionHolder implements ComponentDefinition {
37
38     private BeanDefinition[] innerBeanDefinitions;
39
40     private BeanReference[] beanReferences;
41
42
43     /**
44      * Create a new BeanComponentDefinition for the given bean.
45      * @param beanDefinition the BeanDefinition
46      * @param beanName the name of the bean
47      */

48     public BeanComponentDefinition(BeanDefinition beanDefinition, String JavaDoc beanName) {
49         super(beanDefinition, beanName);
50         findInnerBeanDefinitionsAndBeanReferences(beanDefinition);
51     }
52
53     /**
54      * Create a new BeanComponentDefinition for the given bean.
55      * @param holder the BeanDefinitionHolder encapsulating the
56      * bean definition as well as the name of the bean
57      */

58     public BeanComponentDefinition(BeanDefinitionHolder holder) {
59         super(holder);
60         findInnerBeanDefinitionsAndBeanReferences(holder.getBeanDefinition());
61     }
62
63
64     private void findInnerBeanDefinitionsAndBeanReferences(BeanDefinition beanDefinition) {
65         List JavaDoc innerBeans = new ArrayList JavaDoc();
66         List JavaDoc references = new ArrayList JavaDoc();
67         PropertyValues propertyValues = beanDefinition.getPropertyValues();
68         for (int i = 0; i < propertyValues.getPropertyValues().length; i++) {
69             PropertyValue propertyValue = propertyValues.getPropertyValues()[i];
70             Object JavaDoc value = propertyValue.getValue();
71             if (value instanceof BeanDefinitionHolder) {
72                 innerBeans.add(((BeanDefinitionHolder) value).getBeanDefinition());
73             }
74             else if (value instanceof BeanDefinition) {
75                 innerBeans.add(value);
76             }
77             else if (value instanceof BeanReference) {
78                 references.add(value);
79             }
80         }
81         this.innerBeanDefinitions = (BeanDefinition[]) innerBeans.toArray(new BeanDefinition[innerBeans.size()]);
82         this.beanReferences = (BeanReference[]) references.toArray(new BeanReference[references.size()]);
83     }
84
85
86     public String JavaDoc getName() {
87         return getBeanName();
88     }
89
90     public String JavaDoc getDescription() {
91         return getShortDescription();
92     }
93
94     public BeanDefinition[] getBeanDefinitions() {
95         return new BeanDefinition[] {getBeanDefinition()};
96     }
97
98     public BeanDefinition[] getInnerBeanDefinitions() {
99         return this.innerBeanDefinitions;
100     }
101
102     public BeanReference[] getBeanReferences() {
103         return this.beanReferences;
104     }
105
106
107     /**
108      * This implementation returns this ComponentDefinition's description.
109      * @see #getDescription()
110      */

111     public String JavaDoc toString() {
112         return getDescription();
113     }
114
115     /**
116      * This implementations expects the other object to be of type BeanComponentDefinition
117      * as well, in addition to the superclass's equality requirements.
118      */

119     public boolean equals(Object JavaDoc other) {
120         return (this == other || (other instanceof BeanComponentDefinition && super.equals(other)));
121     }
122
123 }
124
Popular Tags