⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 advisedsupport.java

📁 spring的源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * Copyright 2002-2004 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */ 

package org.springframework.aop.framework;

import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import org.aopalliance.intercept.Interceptor;
import org.aopalliance.intercept.MethodInterceptor;

import org.springframework.aop.Advisor;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.IntroductionAdvisor;
import org.springframework.aop.IntroductionInterceptor;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.Pointcut;
import org.springframework.aop.TargetSource;
import org.springframework.aop.ThrowsAdvice;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.target.SingletonTargetSource;
import org.springframework.util.StringUtils;

/**
 * Superclass for AOP Proxy configuration managers.
 * These are not themselves AOP proxies, but
 * subclasses of this class are normally factories from which 
 * AOP proxy instances are obtained directly.
 *
 * <p>This class frees subclasses of the housekeeping of Interceptors
 * and Advisors, but doesn't actually implement proxy creation
 * methods, which are provided by subclasses.
 *
 * @author Rod Johnson
 * @version $Id: AdvisedSupport.java,v 1.29 2004/04/21 17:49:36 jhoeller Exp $
 * @see org.springframework.aop.framework.AopProxy
 */
public class AdvisedSupport extends ProxyConfig implements Advised {
	
	/**
	 * Canonical TargetSource when there's no target, and behaviour is supplied
	 * by the advisors.
	 */
	public static final TargetSource EMPTY_TARGET_SOURCE = new TargetSource() {

		public Class getTargetClass() {
			return null;
		}

		public boolean isStatic() {
			return true;
		}

		public Object getTarget() {
			return null;
		}

		public void releaseTarget(Object target) {
		}
	};


	/** 
	 * List of Advice. If an Interceptor is added, it will be wrapped
	 * in an Advice before being added to this List. 
	 */
	private List advisors = new LinkedList();
	
	/**
	 * Array updated on changes to the advisors list,
	 * which is easier to manipulate internally
	 */
	private Advisor[] advisorsArray = new Advisor[0];

	/** Interfaces to be implemented by the proxy */
	private Set interfaces = new HashSet();

	protected TargetSource targetSource = EMPTY_TARGET_SOURCE;

	private MethodInvocationFactory methodInvocationFactory;
	
	/**
	 * Set to true when the first AOP proxy has been created, meaning that we must
	 * track advice changes via onAdviceChange() callback.
	 */
	private boolean isActive;
	
	/** List of AdvisedSupportListener */
	private LinkedList listeners = new LinkedList();
	
	protected AdvisorChainFactory advisorChainFactory;
	

	/**
	 * No arg constructor to allow use as a Java bean.
	 */
	public AdvisedSupport() {
		setAdvisorChainFactory(new HashMapCachingAdvisorChainFactory());
		//setMethodInvocationFactory(new SimpleMethodInvocationFactory());
	}
	
	/**
	 * Create a DefaultProxyConfig with the given parameters.
	 * @param interfaces the proxied interfaces
	 */
	public AdvisedSupport(Class[] interfaces) {
		// Make sure we get default advisor chain and method invocation factories
		this();
		setInterfaces(interfaces);
	}


	public void addListener(AdvisedSupportListener l) {
		listeners.add(l);
	}
	
	public void removeListener(AdvisedSupportListener l) {
		listeners.remove(l);
	}

	public void setTargetSource(TargetSource ts) {
		if (isActive() && getOptimize()) {
			throw new AopConfigException("Can't change target with an optimized CGLIB proxy: it has it's own target");
		}
		this.targetSource = ts;
	}
	
	public void setTarget(Object target) {
		setTargetSource(new SingletonTargetSource(target));
	}
	
	/**
	 *  @return the TargetSource. Never returns null
	 */
	public final TargetSource getTargetSource() {
		return this.targetSource;
	}
	
	public void setAdvisorChainFactory(AdvisorChainFactory advisorChainFactory) {
		this.advisorChainFactory = advisorChainFactory;
		addListener(advisorChainFactory);
	}
	
	/**
	 * Return the AdvisorChainFactory associated with this ProxyConfig.
	 */
	public final AdvisorChainFactory getAdvisorChainFactory() {
		return this.advisorChainFactory;
	}
	
	public final MethodInvocationFactory getMethodInvocationFactory() {
		return this.methodInvocationFactory;
	}

	public void setMethodInvocationFactory(MethodInvocationFactory methodInvocationFactory) {
		this.methodInvocationFactory = methodInvocationFactory;
	}
	
	/**
	 * Call this method on a new instance created by the no-arg constructor
	 * to create an independent copy of the configuration from the other.
	 * <p>Does not copy MethodInvocationFactory; a parameter should be provided
	 * to the constructor if necessary. Note that the same MethodInvocationFactory
	 * should <b>not</b> be used for the new instance, or it may not be independent.
	 * @param other DefaultProxyConfig to copy configuration from
	 */
	protected void copyConfigurationFrom(AdvisedSupport other) {
		copyFrom(other);
		this.targetSource = other.targetSource;
		setInterfaces((Class[]) other.interfaces.toArray(new Class[other.interfaces.size()]));
		this.advisors = new LinkedList();
		for (int i = 0; i < other.advisors.size(); i++) {
			Advisor advice = (Advisor) other.advisors.get(i);
			addAdvisor(advice);
		}
	}

	public void addInterceptor(Interceptor interceptor) throws AopConfigException {
		int pos = (this.advisors != null) ? this.advisors.size() : 0;
		addInterceptor(pos, interceptor);
	}
	
	public boolean isInterfaceProxied(Class intf) {
		for (Iterator it = this.interfaces.iterator(); it.hasNext();) {
			Class proxyIntf = (Class) it.next();
			if (intf.isAssignableFrom(proxyIntf)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * Cannot add IntroductionInterceptors this way.
	 */
	public void addInterceptor(int pos, Interceptor interceptor) throws AopConfigException {
		if (!(interceptor instanceof MethodInterceptor)) {
			throw new AopConfigException(getClass().getName() + " only handles MethodInterceptors");
		}
		if (interceptor instanceof IntroductionInterceptor) {
			throw new AopConfigException("IntroductionInterceptors may only be added as part of IntroductionAdvice");
		}
		addAdvisor(pos, new DefaultPointcutAdvisor(interceptor));
	}
	
	public void addAfterReturningAdvice(final AfterReturningAdvice ara) throws AopConfigException {
		addAdvisor(new DefaultPointcutAdvisor(Pointcut.TRUE, ara));
	}
	
	public void addBeforeAdvice(final MethodBeforeAdvice ba) throws AopConfigException {
		addAdvisor(new DefaultPointcutAdvisor(Pointcut.TRUE, ba));
	}
	
	public void addThrowsAdvice(final ThrowsAdvice throwsAdvice) throws AopConfigException {
		addAdvisor(new DefaultPointcutAdvisor(throwsAdvice));
	}
	
	/**
	 * Return the index (from 0) of the given AOP Alliance interceptor,
	 * or -1 if no such interceptor is an advice for this proxy.
	 * The return value of this method can be used to index into
	 * the Advisors array.
	 * @param interceptor AOP Alliance interceptor to search for
	 * @return index from 0 of this interceptor, or -1 if there's
	 * no such advice.
	 */
	public int indexOf(Interceptor interceptor) {
		for (int i = 0; i < this.advisors.size(); i++) {
			Advisor advisor = (Advisor) this.advisors.get(i);
			if (advisor.getAdvice() == interceptor) {
				return i;
			}
		}
		return -1;
	}
	

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -