001: /*
002: * Copyright 2002-2006 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.framework.autoproxy;
018:
019: import java.util.ArrayList;
020: import java.util.Iterator;
021: import java.util.List;
022:
023: import org.springframework.aop.TargetSource;
024: import org.springframework.beans.factory.BeanFactory;
025: import org.springframework.beans.factory.FactoryBean;
026: import org.springframework.util.Assert;
027: import org.springframework.util.PatternMatchUtils;
028: import org.springframework.util.StringUtils;
029:
030: /**
031: * Auto proxy creator that identifies beans to proxy via a list of names.
032: * Checks for direct, "xxx*", and "*xxx" matches.
033: *
034: * <p>For configuration details, see the javadoc of the parent class
035: * AbstractAutoProxyCreator. Typically, you will specify a list of
036: * interceptor names to apply to all identified beans, via the
037: * "interceptorNames" property.
038: *
039: * @author Juergen Hoeller
040: * @since 10.10.2003
041: * @see #setBeanNames
042: * @see #isMatch
043: * @see #setInterceptorNames
044: * @see AbstractAutoProxyCreator
045: */
046: public class BeanNameAutoProxyCreator extends AbstractAutoProxyCreator {
047:
048: private List beanNames;
049:
050: /**
051: * Set the names of the beans that should automatically get wrapped with proxies.
052: * A name can specify a prefix to match by ending with "*", e.g. "myBean,tx*"
053: * will match the bean named "myBean" and all beans whose name start with "tx".
054: * <p><b>NOTE:</b> In case of a FactoryBean, only the objects created by the
055: * FactoryBean will get proxied. This default behavior applies as of Spring 2.0.
056: * If you intend to proxy a FactoryBean instance itself (a rare use case, but
057: * Spring 1.2's default behavior), specify the bean name of the FactoryBean
058: * including the factory-bean prefix "&": e.g. "&myFactoryBean".
059: * @see org.springframework.beans.factory.FactoryBean
060: * @see org.springframework.beans.factory.BeanFactory#FACTORY_BEAN_PREFIX
061: */
062: public void setBeanNames(String[] beanNames) {
063: Assert.notEmpty(beanNames, "'beanNames' must not be empty");
064: this .beanNames = new ArrayList(beanNames.length);
065: for (int i = 0; i < beanNames.length; i++) {
066: this .beanNames
067: .add(StringUtils.trimWhitespace(beanNames[i]));
068: }
069: }
070:
071: /**
072: * Identify as bean to proxy if the bean name is in the configured list of names.
073: */
074: protected Object[] getAdvicesAndAdvisorsForBean(Class beanClass,
075: String beanName, TargetSource targetSource) {
076: if (this .beanNames != null) {
077: for (Iterator it = this .beanNames.iterator(); it.hasNext();) {
078: String mappedName = (String) it.next();
079: if (FactoryBean.class.isAssignableFrom(beanClass)) {
080: if (!mappedName
081: .startsWith(BeanFactory.FACTORY_BEAN_PREFIX)) {
082: continue;
083: }
084: mappedName = mappedName
085: .substring(BeanFactory.FACTORY_BEAN_PREFIX
086: .length());
087: }
088: if (isMatch(beanName, mappedName)) {
089: return PROXY_WITHOUT_ADDITIONAL_INTERCEPTORS;
090: }
091: }
092: }
093: return DO_NOT_PROXY;
094: }
095:
096: /**
097: * Return if the given bean name matches the mapped name.
098: * <p>The default implementation checks for "xxx*", "*xxx" and "*xxx*" matches,
099: * as well as direct equality. Can be overridden in subclasses.
100: * @param beanName the bean name to check
101: * @param mappedName the name in the configured list of names
102: * @return if the names match
103: * @see org.springframework.util.PatternMatchUtils#simpleMatch(String, String)
104: */
105: protected boolean isMatch(String beanName, String mappedName) {
106: return PatternMatchUtils.simpleMatch(mappedName, beanName);
107: }
108:
109: }
|