KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > aop > framework > autoproxy > BeanNameAutoProxyCreator


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.framework.autoproxy;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22
23 import org.springframework.aop.TargetSource;
24 import org.springframework.beans.factory.BeanFactory;
25 import org.springframework.beans.factory.FactoryBean;
26 import org.springframework.util.Assert;
27 import org.springframework.util.PatternMatchUtils;
28 import org.springframework.util.StringUtils;
29
30 /**
31  * Auto proxy creator that identifies beans to proxy via a list of names.
32  * Checks for direct, "xxx*", and "*xxx" matches.
33  *
34  * <p>For configuration details, see the javadoc of the parent class
35  * AbstractAutoProxyCreator. Typically, you will specify a list of
36  * interceptor names to apply to all identified beans, via the
37  * "interceptorNames" property.
38  *
39  * @author Juergen Hoeller
40  * @since 10.10.2003
41  * @see #setBeanNames
42  * @see #isMatch
43  * @see #setInterceptorNames
44  * @see AbstractAutoProxyCreator
45  */

46 public class BeanNameAutoProxyCreator extends AbstractAutoProxyCreator {
47
48     private List JavaDoc beanNames;
49
50
51     /**
52      * Set the names of the beans that should automatically get wrapped with proxies.
53      * A name can specify a prefix to match by ending with "*", e.g. "myBean,tx*"
54      * will match the bean named "myBean" and all beans whose name start with "tx".
55      * <p><b>NOTE:</b> In case of a FactoryBean, only the objects created by the
56      * FactoryBean will get proxied. This default behavior applies as of Spring 2.0.
57      * If you intend to proxy a FactoryBean instance itself (a rare use case, but
58      * Spring 1.2's default behavior), specify the bean name of the FactoryBean
59      * including the factory-bean prefix "&": e.g. "&myFactoryBean".
60      * @see org.springframework.beans.factory.FactoryBean
61      * @see org.springframework.beans.factory.BeanFactory#FACTORY_BEAN_PREFIX
62      */

63     public void setBeanNames(String JavaDoc[] beanNames) {
64         Assert.notEmpty(beanNames, "'beanNames' must not be empty");
65         this.beanNames = new ArrayList JavaDoc(beanNames.length);
66         for (int i = 0; i < beanNames.length; i++) {
67             this.beanNames.add(StringUtils.trimWhitespace(beanNames[i]));
68         }
69     }
70
71
72     /**
73      * Identify as bean to proxy if the bean name is in the configured list of names.
74      */

75     protected Object JavaDoc[] getAdvicesAndAdvisorsForBean(Class JavaDoc beanClass, String JavaDoc beanName, TargetSource targetSource) {
76         if (this.beanNames != null) {
77             for (Iterator JavaDoc it = this.beanNames.iterator(); it.hasNext();) {
78                 String JavaDoc mappedName = (String JavaDoc) it.next();
79                 if (FactoryBean.class.isAssignableFrom(beanClass)) {
80                     if (!mappedName.startsWith(BeanFactory.FACTORY_BEAN_PREFIX)) {
81                         continue;
82                     }
83                     mappedName = mappedName.substring(BeanFactory.FACTORY_BEAN_PREFIX.length());
84                 }
85                 if (isMatch(beanName, mappedName)) {
86                     return PROXY_WITHOUT_ADDITIONAL_INTERCEPTORS;
87                 }
88             }
89         }
90         return DO_NOT_PROXY;
91     }
92
93     /**
94      * Return if the given bean name matches the mapped name.
95      * <p>The default implementation checks for "xxx*", "*xxx" and "*xxx*" matches,
96      * as well as direct equality. Can be overridden in subclasses.
97      * @param beanName the bean name to check
98      * @param mappedName the name in the configured list of names
99      * @return if the names match
100      * @see org.springframework.util.PatternMatchUtils#simpleMatch(String, String)
101      */

102     protected boolean isMatch(String JavaDoc beanName, String JavaDoc mappedName) {
103         return PatternMatchUtils.simpleMatch(mappedName, beanName);
104     }
105
106 }
107
Popular Tags