KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > aop > config > AdvisorComponentDefinition


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.aop.config;
18
19 import org.springframework.beans.MutablePropertyValues;
20 import org.springframework.beans.factory.config.BeanDefinition;
21 import org.springframework.beans.factory.config.BeanReference;
22 import org.springframework.beans.factory.parsing.AbstractComponentDefinition;
23 import org.springframework.beans.factory.parsing.ComponentDefinition;
24 import org.springframework.util.Assert;
25
26 /**
27  * A component definition that bridges the gap between the advisor bean definition
28  * configured by the <code>&lt;aop:advisor&gt;</code> tag and the component definition
29  * infrastructure.
30  *
31  * @see ComponentDefinition
32  * @author Rob Harrop
33  * @author Juergen Hoeller
34  * @since 2.0
35  */

36 public class AdvisorComponentDefinition extends AbstractComponentDefinition {
37
38     private final String JavaDoc advisorBeanName;
39
40     private final BeanDefinition advisorDefinition;
41
42     private String JavaDoc description;
43
44     private BeanReference[] beanReferences;
45
46     private BeanDefinition[] beanDefinitions;
47
48
49     public AdvisorComponentDefinition(String JavaDoc advisorBeanName, BeanDefinition advisorDefinition) {
50          this(advisorBeanName, advisorDefinition, null);
51     }
52
53     public AdvisorComponentDefinition(
54             String JavaDoc advisorBeanName, BeanDefinition advisorDefinition, BeanDefinition pointcutDefinition) {
55
56         Assert.notNull(advisorBeanName, "'advisorBeanName' must not be null");
57         Assert.notNull(advisorDefinition, "'advisorDefinition' must not be null");
58         this.advisorBeanName = advisorBeanName;
59         this.advisorDefinition = advisorDefinition;
60         unwrapDefinitions(advisorDefinition, pointcutDefinition);
61     }
62
63
64     private void unwrapDefinitions(BeanDefinition advisorDefinition, BeanDefinition pointcutDefinition) {
65         MutablePropertyValues pvs = advisorDefinition.getPropertyValues();
66         BeanReference adviceReference = (BeanReference) pvs.getPropertyValue("adviceBeanName").getValue();
67
68         if (pointcutDefinition != null) {
69             this.beanReferences = new BeanReference[] {adviceReference};
70             this.beanDefinitions = new BeanDefinition[] {advisorDefinition, pointcutDefinition};
71             this.description = buildDescription(adviceReference, pointcutDefinition);
72         }
73         else {
74             BeanReference pointcutReference = (BeanReference) pvs.getPropertyValue("pointcut").getValue();
75             this.beanReferences = new BeanReference[] {adviceReference, pointcutReference};
76             this.beanDefinitions = new BeanDefinition[] {advisorDefinition};
77             this.description = buildDescription(adviceReference, pointcutReference);
78         }
79     }
80
81     private String JavaDoc buildDescription(BeanReference adviceReference, BeanDefinition pointcutDefinition) {
82         return new StringBuffer JavaDoc("Advisor <advice(ref)='").
83                 append(adviceReference.getBeanName()).append("', pointcut(expression)=[").
84                 append(pointcutDefinition.getPropertyValues().getPropertyValue("expression").getValue()).
85                 append("]>").toString();
86     }
87
88     private String JavaDoc buildDescription(BeanReference adviceReference, BeanReference pointcutReference) {
89         return new StringBuffer JavaDoc("Advisor <advice(ref)='").
90                 append(adviceReference.getBeanName()).append("', pointcut(ref)='").
91                 append(pointcutReference.getBeanName()).append("'>").toString();
92     }
93
94
95     public String JavaDoc getName() {
96         return this.advisorBeanName;
97     }
98
99     public String JavaDoc getDescription() {
100         return this.description;
101     }
102
103     public BeanDefinition[] getBeanDefinitions() {
104         return this.beanDefinitions;
105     }
106
107     public BeanReference[] getBeanReferences() {
108         return this.beanReferences;
109     }
110
111     public Object JavaDoc getSource() {
112         return this.advisorDefinition.getSource();
113     }
114
115 }
116
Popular Tags