KickJava   Java API By Example, From Geeks To Geeks.

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


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.aop.config;
18
19 import java.util.List JavaDoc;
20
21 import org.w3c.dom.Node JavaDoc;
22
23 import org.springframework.aop.framework.ProxyFactoryBean;
24 import org.springframework.beans.MutablePropertyValues;
25 import org.springframework.beans.factory.config.BeanDefinition;
26 import org.springframework.beans.factory.config.BeanDefinitionHolder;
27 import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
28 import org.springframework.beans.factory.support.BeanDefinitionRegistry;
29 import org.springframework.beans.factory.support.ManagedList;
30 import org.springframework.beans.factory.support.RootBeanDefinition;
31 import org.springframework.beans.factory.xml.BeanDefinitionDecorator;
32 import org.springframework.beans.factory.xml.ParserContext;
33 import org.springframework.util.ClassUtils;
34 import org.springframework.util.StringUtils;
35
36 /**
37  * Base implementation for {@link org.springframework.beans.factory.xml.BeanDefinitionDecorator BeanDefinitionDecorators} wishing
38  * to add an {@link org.aopalliance.intercept.MethodInterceptor interceptor} to the resulting
39  * bean.
40  *
41  * <p>This base class controls the creation of the {@link ProxyFactoryBean} bean definition
42  * and wraps the original as an inner-bean definition for the <code>target</code> property of
43  * {@link ProxyFactoryBean}.
44  *
45  * <p>Chaining is correctly handled, ensuring that only one {@link ProxyFactoryBean} definition
46  * is created. If a previous {@link org.springframework.beans.factory.xml.BeanDefinitionDecorator} already created the {@link org.springframework.aop.framework.ProxyFactoryBean}
47  * then the interceptor is simply added to the existing definition.
48  *
49  * <p>Subclasses have only to create the <code>BeanDefinition</code> to the interceptor they
50  * wish to add.
51  *
52  * @author Rob Harrop
53  * @since 2.0
54  * @see org.aopalliance.intercept.MethodInterceptor
55  */

56 public abstract class AbstractInterceptorDrivenBeanDefinitionDecorator implements BeanDefinitionDecorator {
57
58     public final BeanDefinitionHolder decorate(Node JavaDoc node, BeanDefinitionHolder definitionHolder, ParserContext parserContext) {
59         BeanDefinitionRegistry registry = parserContext.getRegistry();
60         
61         // get the root bean name - will be the name of the generated proxy factory bean
62
String JavaDoc existingBeanName = definitionHolder.getBeanName();
63         BeanDefinition existingDefinition = definitionHolder.getBeanDefinition();
64
65         // delegate to subclass for interceptor def
66
BeanDefinition interceptorDefinition = createInterceptorDefinition(node);
67
68         // generate name and register the interceptor
69
String JavaDoc interceptorName = existingBeanName + "." + getInterceptorNameSuffix(interceptorDefinition);
70         BeanDefinitionReaderUtils.registerBeanDefinition(new BeanDefinitionHolder(interceptorDefinition, interceptorName), registry);
71
72         BeanDefinitionHolder result = definitionHolder;
73
74         if (!isProxyFactoryBeanDefinition(existingDefinition)) {
75
76             // create the proxy definitionHolder
77
RootBeanDefinition proxyDefinition = new RootBeanDefinition();
78             // create proxy factory bean definitionHolder
79
proxyDefinition.setBeanClass(ProxyFactoryBean.class);
80
81             // set up property values
82
MutablePropertyValues mpvs = new MutablePropertyValues();
83             proxyDefinition.setPropertyValues(mpvs);
84
85             // set the target
86
mpvs.addPropertyValue("target", existingDefinition);
87
88             // create the interceptor names list
89
ManagedList interceptorList = new ManagedList();
90             mpvs.addPropertyValue("interceptorNames", interceptorList);
91
92             result = new BeanDefinitionHolder(proxyDefinition, existingBeanName);
93         }
94
95         addInterceptorNameToList(interceptorName, result.getBeanDefinition());
96
97         return result;
98
99     }
100
101     private void addInterceptorNameToList(String JavaDoc interceptorName, BeanDefinition beanDefinition) {
102         List JavaDoc list = (List JavaDoc) beanDefinition.getPropertyValues().getPropertyValue("interceptorNames").getValue();
103         list.add(interceptorName);
104     }
105
106     private boolean isProxyFactoryBeanDefinition(BeanDefinition existingDefinition) {
107         return existingDefinition.getBeanClassName().equals(ProxyFactoryBean.class.getName());
108     }
109
110     protected String JavaDoc getInterceptorNameSuffix(BeanDefinition interceptorDefinition) {
111         return StringUtils.uncapitalize(ClassUtils.getShortName(interceptorDefinition.getBeanClassName()));
112     }
113
114     /**
115      * Subclasses should implement this method to return the <code>BeanDefinition</code>
116      * for the interceptor they wish to apply to the bean being decorated.
117      */

118     protected abstract BeanDefinition createInterceptorDefinition(Node JavaDoc node);
119
120 }
121
Popular Tags