All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.springframework.aop.framework.ProxyFactory Maven / Gradle / Ivy

There is a newer version: 2.0-M4
Show newest version
/*
 * Copyright 2002-2005 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 org.aopalliance.intercept.Interceptor;

import org.springframework.util.ClassUtils;
import org.springframework.aop.TargetSource;

/**
 * Factory for AOP proxies for programmatic use, rather than via a bean
 * factory. This class provides a simple way of obtaining and configuring
 * AOP proxies in code.
 *
 * @author Rod Johnson
 * @author Rob Harrop
 * @since 14.03.2003
 */
public class ProxyFactory extends AdvisedSupport {

	/**
	 * Create a new ProxyFactory.
	 */
	public ProxyFactory() {
	}

	/**
	 * Create a new ProxyFactory.
	 * Proxy all interfaces of the given target.
	 */
	public ProxyFactory(Object target) throws AopConfigException {
		if (target == null) {
			throw new AopConfigException("Can't proxy null object");
		}
		setInterfaces(ClassUtils.getAllInterfaces(target));
		setTarget(target);
	}

	/**
	 * Create a new ProxyFactory.
	 * No target, only interfaces. Must add interceptors.
	 */
	public ProxyFactory(Class[] interfaces) {
		setInterfaces(interfaces);
	}

	/**
	 * Create a new proxy according to the settings in this factory.
	 * Can be called repeatedly. Effect will vary if we've added
	 * or removed interfaces. Can add and remove interceptors.
	 * @return the new proxy
	 */
	public Object getProxy() {
		AopProxy proxy = createAopProxy();
		return proxy.getProxy();
	}

	/**
	 * Create a new proxy for the given interface and interceptor.
	 * 

Convenience method for creating a proxy for a single interceptor, * assuming that the interceptor handles all calls itself rather than * delegating to a target, like in the case of remoting proxies. * @param proxyInterface the interface that the proxy should implement * @param interceptor the interceptor that the proxy should invoke * @return the new proxy */ public static Object getProxy(Class proxyInterface, Interceptor interceptor) { ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.addInterface(proxyInterface); proxyFactory.addAdvice(interceptor); return proxyFactory.getProxy(); } /** * Creates a proxy for the specified TargetSource that implements the * specified interface. */ public static Object getProxy(Class proxyInterface, TargetSource targetSource) { ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.addInterface(proxyInterface); proxyFactory.setTargetSource(targetSource); return proxyFactory.getProxy(); } /** * Create a proxy for the specified TargetSource that extends the * target class of the TargetSource. */ public static Object getProxy(TargetSource targetSource) { if(targetSource.getTargetClass() == null) { throw new IllegalArgumentException("Cannot create class proxy for TargetSource with null target class"); } ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.setTargetSource(targetSource); proxyFactory.setProxyTargetClass(true); return proxyFactory.getProxy(); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy