KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > aop > framework > ProxyFactory


1 /*
2  * Copyright 2002-2007 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;
18
19 import org.aopalliance.intercept.Interceptor;
20
21 import org.springframework.aop.TargetSource;
22 import org.springframework.util.Assert;
23 import org.springframework.util.ClassUtils;
24
25 /**
26  * Factory for AOP proxies for programmatic use, rather than via a bean
27  * factory. This class provides a simple way of obtaining and configuring
28  * AOP proxies in code.
29  *
30  * @author Rod Johnson
31  * @author Juergen Hoeller
32  * @author Rob Harrop
33  * @since 14.03.2003
34  */

35 public class ProxyFactory extends ProxyCreatorSupport implements AopProxy {
36
37     /**
38      * Create a new ProxyFactory.
39      */

40     public ProxyFactory() {
41     }
42
43     /**
44      * Create a new ProxyFactory.
45      * <p>Will proxy all interfaces that the given target implements.
46      * @param target the target object to be proxied
47      */

48     public ProxyFactory(Object JavaDoc target) {
49         Assert.notNull(target, "Target object must not be null");
50         setInterfaces(ClassUtils.getAllInterfaces(target));
51         setTarget(target);
52     }
53
54     /**
55      * Create a new ProxyFactory.
56      * <p>No target, only interfaces. Must add interceptors.
57      * @param proxyInterfaces the interfaces that the proxy should implement
58      */

59     public ProxyFactory(Class JavaDoc[] proxyInterfaces) {
60         setInterfaces(proxyInterfaces);
61     }
62
63     /**
64      * Create a new ProxyFactory for the given interface and interceptor.
65      * <p>Convenience method for creating a proxy for a single interceptor,
66      * assuming that the interceptor handles all calls itself rather than
67      * delegating to a target, like in the case of remoting proxies.
68      * @param proxyInterface the interface that the proxy should implement
69      * @param interceptor the interceptor that the proxy should invoke
70      */

71     public ProxyFactory(Class JavaDoc proxyInterface, Interceptor interceptor) {
72         addInterface(proxyInterface);
73         addAdvice(interceptor);
74     }
75
76     /**
77      * Create a ProxyFactory for the specified <code>TargetSource</code>,
78      * making the proxy implement the specified interface.
79      * @param proxyInterface the interface that the proxy should implement
80      * @param targetSource the TargetSource that the proxy should invoke
81      */

82     public ProxyFactory(Class JavaDoc proxyInterface, TargetSource targetSource) {
83         addInterface(proxyInterface);
84         setTargetSource(targetSource);
85     }
86
87
88     /**
89      * Create a new proxy according to the settings in this factory.
90      * <p>Can be called repeatedly. Effect will vary if we've added
91      * or removed interfaces. Can add and remove interceptors.
92      * <p>Uses a default class loader: Usually, the thread context class loader
93      * (if necessary for proxy creation).
94      * @return the proxy object
95      */

96     public Object JavaDoc getProxy() {
97         return createAopProxy().getProxy();
98     }
99
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 JavaDoc getProxy(ClassLoader JavaDoc classLoader) {
110         return createAopProxy().getProxy(classLoader);
111     }
112
113
114     /**
115      * Create a new proxy for the given interface and interceptor.
116      * <p>Convenience method for creating a proxy for a single interceptor,
117      * assuming that the interceptor handles all calls itself rather than
118      * delegating to a target, like in the case of remoting proxies.
119      * @param proxyInterface the interface that the proxy should implement
120      * @param interceptor the interceptor that the proxy should invoke
121      * @return the proxy object
122      * @see #ProxyFactory(Class, org.aopalliance.intercept.Interceptor)
123      */

124     public static Object JavaDoc getProxy(Class JavaDoc proxyInterface, 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 JavaDoc getProxy(Class JavaDoc proxyInterface, TargetSource targetSource) {
137         return new ProxyFactory(proxyInterface, targetSource).getProxy();
138     }
139
140     /**
141      * Create a proxy for the specified <code>TargetSource</code> that extends
142      * the target class of the <code>TargetSource</code>.
143      * @param targetSource the TargetSource that the proxy should invoke
144      * @return the proxy object
145      */

146     public static Object JavaDoc getProxy(TargetSource targetSource) {
147         if (targetSource.getTargetClass() == null) {
148             throw new IllegalArgumentException JavaDoc("Cannot create class proxy for TargetSource with null target class");
149         }
150         ProxyFactory proxyFactory = new ProxyFactory();
151         proxyFactory.setTargetSource(targetSource);
152         proxyFactory.setProxyTargetClass(true);
153         return proxyFactory.getProxy();
154     }
155
156 }
157
Popular Tags