001: /*
002: * Copyright 2002-2007 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;
018:
019: import org.aopalliance.intercept.Interceptor;
020:
021: import org.springframework.aop.TargetSource;
022: import org.springframework.util.Assert;
023: import org.springframework.util.ClassUtils;
024:
025: /**
026: * Factory for AOP proxies for programmatic use, rather than via a bean
027: * factory. This class provides a simple way of obtaining and configuring
028: * AOP proxies in code.
029: *
030: * @author Rod Johnson
031: * @author Juergen Hoeller
032: * @author Rob Harrop
033: * @since 14.03.2003
034: */
035: public class ProxyFactory extends ProxyCreatorSupport implements
036: AopProxy {
037:
038: /**
039: * Create a new ProxyFactory.
040: */
041: public ProxyFactory() {
042: }
043:
044: /**
045: * Create a new ProxyFactory.
046: * <p>Will proxy all interfaces that the given target implements.
047: * @param target the target object to be proxied
048: */
049: public ProxyFactory(Object target) {
050: Assert.notNull(target, "Target object must not be null");
051: setInterfaces(ClassUtils.getAllInterfaces(target));
052: setTarget(target);
053: }
054:
055: /**
056: * Create a new ProxyFactory.
057: * <p>No target, only interfaces. Must add interceptors.
058: * @param proxyInterfaces the interfaces that the proxy should implement
059: */
060: public ProxyFactory(Class[] proxyInterfaces) {
061: setInterfaces(proxyInterfaces);
062: }
063:
064: /**
065: * Create a new ProxyFactory for the given interface and interceptor.
066: * <p>Convenience method for creating a proxy for a single interceptor,
067: * assuming that the interceptor handles all calls itself rather than
068: * delegating to a target, like in the case of remoting proxies.
069: * @param proxyInterface the interface that the proxy should implement
070: * @param interceptor the interceptor that the proxy should invoke
071: */
072: public ProxyFactory(Class proxyInterface, Interceptor interceptor) {
073: addInterface(proxyInterface);
074: addAdvice(interceptor);
075: }
076:
077: /**
078: * Create a ProxyFactory for the specified <code>TargetSource</code>,
079: * making the proxy implement the specified interface.
080: * @param proxyInterface the interface that the proxy should implement
081: * @param targetSource the TargetSource that the proxy should invoke
082: */
083: public ProxyFactory(Class proxyInterface, TargetSource targetSource) {
084: addInterface(proxyInterface);
085: setTargetSource(targetSource);
086: }
087:
088: /**
089: * Create a new proxy according to the settings in this factory.
090: * <p>Can be called repeatedly. Effect will vary if we've added
091: * or removed interfaces. Can add and remove interceptors.
092: * <p>Uses a default class loader: Usually, the thread context class loader
093: * (if necessary for proxy creation).
094: * @return the proxy object
095: */
096: public Object getProxy() {
097: return createAopProxy().getProxy();
098: }
099:
100: /**
101: * Create a new proxy according to the settings in this factory.
102: * <p>Can be called repeatedly. Effect will vary if we've added
103: * or removed interfaces. Can add and remove interceptors.
104: * <p>Uses the given class loader (if necessary for proxy creation).
105: * @param classLoader the class loader to create the proxy with
106: * (or <code>null</code> for the low-level proxy facility's default)
107: * @return the proxy object
108: */
109: public Object getProxy(ClassLoader classLoader) {
110: return createAopProxy().getProxy(classLoader);
111: }
112:
113: /**
114: * Create a new proxy for the given interface and interceptor.
115: * <p>Convenience method for creating a proxy for a single interceptor,
116: * assuming that the interceptor handles all calls itself rather than
117: * delegating to a target, like in the case of remoting proxies.
118: * @param proxyInterface the interface that the proxy should implement
119: * @param interceptor the interceptor that the proxy should invoke
120: * @return the proxy object
121: * @see #ProxyFactory(Class, org.aopalliance.intercept.Interceptor)
122: */
123: public static Object getProxy(Class proxyInterface,
124: Interceptor interceptor) {
125: return new ProxyFactory(proxyInterface, interceptor).getProxy();
126: }
127:
128: /**
129: * Create a proxy for the specified <code>TargetSource</code>,
130: * implementing the specified interface.
131: * @param proxyInterface the interface that the proxy should implement
132: * @param targetSource the TargetSource that the proxy should invoke
133: * @return the proxy object
134: * @see #ProxyFactory(Class, org.springframework.aop.TargetSource)
135: */
136: public static Object getProxy(Class proxyInterface,
137: TargetSource targetSource) {
138: return new ProxyFactory(proxyInterface, targetSource)
139: .getProxy();
140: }
141:
142: /**
143: * Create a proxy for the specified <code>TargetSource</code> that extends
144: * the target class of the <code>TargetSource</code>.
145: * @param targetSource the TargetSource that the proxy should invoke
146: * @return the proxy object
147: */
148: public static Object getProxy(TargetSource targetSource) {
149: if (targetSource.getTargetClass() == null) {
150: throw new IllegalArgumentException(
151: "Cannot create class proxy for TargetSource with null target class");
152: }
153: ProxyFactory proxyFactory = new ProxyFactory();
154: proxyFactory.setTargetSource(targetSource);
155: proxyFactory.setProxyTargetClass(true);
156: return proxyFactory.getProxy();
157: }
158:
159: }
|