001: /*
002: * Copyright 2002-2007 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.aop.config;
018:
019: import org.springframework.beans.MutablePropertyValues;
020: import org.springframework.beans.factory.config.BeanDefinition;
021: import org.springframework.beans.factory.config.BeanReference;
022: import org.springframework.beans.factory.parsing.AbstractComponentDefinition;
023: import org.springframework.util.Assert;
024:
025: /**
026: * {@link org.springframework.beans.factory.parsing.ComponentDefinition}
027: * that bridges the gap between the advisor bean definition configured
028: * by the <code><aop:advisor></code> tag and the component definition
029: * infrastructure.
030: *
031: * @author Rob Harrop
032: * @author Juergen Hoeller
033: * @since 2.0
034: */
035: public class AdvisorComponentDefinition extends
036: AbstractComponentDefinition {
037:
038: private final String advisorBeanName;
039:
040: private final BeanDefinition advisorDefinition;
041:
042: private String description;
043:
044: private BeanReference[] beanReferences;
045:
046: private BeanDefinition[] beanDefinitions;
047:
048: public AdvisorComponentDefinition(String advisorBeanName,
049: BeanDefinition advisorDefinition) {
050: this (advisorBeanName, advisorDefinition, null);
051: }
052:
053: public AdvisorComponentDefinition(String advisorBeanName,
054: BeanDefinition advisorDefinition,
055: BeanDefinition pointcutDefinition) {
056:
057: Assert.notNull(advisorBeanName,
058: "'advisorBeanName' must not be null");
059: Assert.notNull(advisorDefinition,
060: "'advisorDefinition' must not be null");
061: this .advisorBeanName = advisorBeanName;
062: this .advisorDefinition = advisorDefinition;
063: unwrapDefinitions(advisorDefinition, pointcutDefinition);
064: }
065:
066: private void unwrapDefinitions(BeanDefinition advisorDefinition,
067: BeanDefinition pointcutDefinition) {
068: MutablePropertyValues pvs = advisorDefinition
069: .getPropertyValues();
070: BeanReference adviceReference = (BeanReference) pvs
071: .getPropertyValue("adviceBeanName").getValue();
072:
073: if (pointcutDefinition != null) {
074: this .beanReferences = new BeanReference[] { adviceReference };
075: this .beanDefinitions = new BeanDefinition[] {
076: advisorDefinition, pointcutDefinition };
077: this .description = buildDescription(adviceReference,
078: pointcutDefinition);
079: } else {
080: BeanReference pointcutReference = (BeanReference) pvs
081: .getPropertyValue("pointcut").getValue();
082: this .beanReferences = new BeanReference[] {
083: adviceReference, pointcutReference };
084: this .beanDefinitions = new BeanDefinition[] { advisorDefinition };
085: this .description = buildDescription(adviceReference,
086: pointcutReference);
087: }
088: }
089:
090: private String buildDescription(BeanReference adviceReference,
091: BeanDefinition pointcutDefinition) {
092: return new StringBuffer("Advisor <advice(ref)='").append(
093: adviceReference.getBeanName()).append(
094: "', pointcut(expression)=[").append(
095: pointcutDefinition.getPropertyValues()
096: .getPropertyValue("expression").getValue())
097: .append("]>").toString();
098: }
099:
100: private String buildDescription(BeanReference adviceReference,
101: BeanReference pointcutReference) {
102: return new StringBuffer("Advisor <advice(ref)='").append(
103: adviceReference.getBeanName()).append(
104: "', pointcut(ref)='").append(
105: pointcutReference.getBeanName()).append("'>")
106: .toString();
107: }
108:
109: public String getName() {
110: return this .advisorBeanName;
111: }
112:
113: public String getDescription() {
114: return this .description;
115: }
116:
117: public BeanDefinition[] getBeanDefinitions() {
118: return this .beanDefinitions;
119: }
120:
121: public BeanReference[] getBeanReferences() {
122: return this .beanReferences;
123: }
124:
125: public Object getSource() {
126: return this.advisorDefinition.getSource();
127: }
128:
129: }
|