KickJava   Java API By Example, From Geeks To Geeks.

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


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.springframework.aop.TargetSource;
20 import org.springframework.aop.framework.adapter.AdvisorAdapterRegistry;
21 import org.springframework.aop.framework.adapter.GlobalAdvisorAdapterRegistry;
22 import org.springframework.aop.target.SingletonTargetSource;
23 import org.springframework.beans.factory.BeanClassLoaderAware;
24 import org.springframework.beans.factory.FactoryBean;
25 import org.springframework.beans.factory.FactoryBeanNotInitializedException;
26 import org.springframework.beans.factory.InitializingBean;
27 import org.springframework.util.ClassUtils;
28
29 /**
30  * Convenient proxy factory bean superclass for proxy factory
31  * beans that create only singletons.
32  *
33  * <p>Manages pre- and post-interceptors (references, rather than
34  * interceptor names, as in {@link ProxyFactoryBean}) and provides
35  * consistent interface management.
36  *
37  * @author Juergen Hoeller
38  * @since 2.0
39  */

40 public abstract class AbstractSingletonProxyFactoryBean extends ProxyConfig
41         implements FactoryBean, BeanClassLoaderAware, InitializingBean {
42
43     private Object JavaDoc target;
44
45     private Class JavaDoc[] proxyInterfaces;
46
47     private Object JavaDoc[] preInterceptors;
48
49     private Object JavaDoc[] postInterceptors;
50
51     /** Default is global AdvisorAdapterRegistry */
52     private AdvisorAdapterRegistry advisorAdapterRegistry = GlobalAdvisorAdapterRegistry.getInstance();
53
54     private ClassLoader JavaDoc beanClassLoader = ClassUtils.getDefaultClassLoader();
55
56     private Object JavaDoc proxy;
57
58
59     /**
60      * Set the target object, that is, the bean to be wrapped with a transactional proxy.
61      * <p>The target may be any object, in which case a SingletonTargetSource will
62      * be created. If it is a TargetSource, no wrapper TargetSource is created:
63      * This enables the use of a pooling or prototype TargetSource etc.
64      * @see org.springframework.aop.TargetSource
65      * @see org.springframework.aop.target.SingletonTargetSource
66      * @see org.springframework.aop.target.LazyInitTargetSource
67      * @see org.springframework.aop.target.PrototypeTargetSource
68      * @see org.springframework.aop.target.CommonsPoolTargetSource
69      */

70     public void setTarget(Object JavaDoc target) {
71         this.target = target;
72     }
73
74     /**
75      * Specify the set of interfaces being proxied.
76      * <p>If not specified (the default), the AOP infrastructure works
77      * out which interfaces need proxying by analyzing the target,
78      * proxying all the interfaces that the target object implements.
79      */

80     public void setProxyInterfaces(Class JavaDoc[] proxyInterfaces) {
81         this.proxyInterfaces = proxyInterfaces;
82     }
83
84     /**
85      * Set additional interceptors (or advisors) to be applied before the
86      * implicit transaction interceptor, e.g. PerformanceMonitorInterceptor.
87      * @see org.springframework.aop.interceptor.PerformanceMonitorInterceptor
88      */

89     public void setPreInterceptors(Object JavaDoc[] preInterceptors) {
90         this.preInterceptors = preInterceptors;
91     }
92
93     /**
94      * Set additional interceptors (or advisors) to be applied after the
95      * implicit transaction interceptor, e.g. HibernateInterceptors for
96      * eagerly binding Sessions to the current thread when using JTA.
97      * <p>Note that this is just necessary if you rely on those interceptors in general:
98      * HibernateTemplate and JdoTemplate work nicely with JtaTransactionManager through
99      * implicit on-demand thread binding.
100      * @see org.springframework.orm.hibernate.HibernateInterceptor
101      * @see org.springframework.orm.jdo.JdoInterceptor
102      */

103     public void setPostInterceptors(Object JavaDoc[] postInterceptors) {
104         this.postInterceptors = postInterceptors;
105     }
106
107     /**
108      * Specify the AdvisorAdapterRegistry to use.
109      * Default is the global AdvisorAdapterRegistry.
110      * @see org.springframework.aop.framework.adapter.GlobalAdvisorAdapterRegistry
111      */

112     public void setAdvisorAdapterRegistry(AdvisorAdapterRegistry advisorAdapterRegistry) {
113         this.advisorAdapterRegistry = advisorAdapterRegistry;
114     }
115
116     public void setBeanClassLoader(ClassLoader JavaDoc classLoader) {
117         this.beanClassLoader = classLoader;
118     }
119
120
121     public void afterPropertiesSet() {
122         if (this.target == null) {
123             throw new IllegalArgumentException JavaDoc("'target' is required");
124         }
125         if (this.target instanceof String JavaDoc) {
126             throw new IllegalArgumentException JavaDoc("'target' needs to be a bean reference, not a bean name as value");
127         }
128
129         ProxyFactory proxyFactory = new ProxyFactory();
130
131         if (this.preInterceptors != null) {
132             for (int i = 0; i < this.preInterceptors.length; i++) {
133                 proxyFactory.addAdvisor(this.advisorAdapterRegistry.wrap(this.preInterceptors[i]));
134             }
135         }
136
137         // Add the main interceptor (typically an Advisor).
138
proxyFactory.addAdvisor(this.advisorAdapterRegistry.wrap(createMainInterceptor()));
139
140         if (this.postInterceptors != null) {
141             for (int i = 0; i < this.postInterceptors.length; i++) {
142                 proxyFactory.addAdvisor(this.advisorAdapterRegistry.wrap(this.postInterceptors[i]));
143             }
144         }
145
146         proxyFactory.copyFrom(this);
147
148         TargetSource targetSource = createTargetSource(this.target);
149         proxyFactory.setTargetSource(targetSource);
150
151         if (this.proxyInterfaces != null) {
152             proxyFactory.setInterfaces(this.proxyInterfaces);
153         }
154         else if (!isProxyTargetClass()) {
155             // Rely on AOP infrastructure to tell us what interfaces to proxy.
156
proxyFactory.setInterfaces(ClassUtils.getAllInterfacesForClass(targetSource.getTargetClass()));
157         }
158
159         this.proxy = getProxy(proxyFactory);
160     }
161
162     /**
163      * Determine a TargetSource for the given target (or TargetSource).
164      * @param target target. If this is an implementation of TargetSource it is
165      * used as our TargetSource; otherwise it is wrapped in a SingletonTargetSource.
166      * @return a TargetSource for this object
167      */

168     protected TargetSource createTargetSource(Object JavaDoc target) {
169         if (target instanceof TargetSource) {
170             return (TargetSource) target;
171         }
172         else {
173             return new SingletonTargetSource(target);
174         }
175     }
176
177     /**
178      * Return the proxy object to expose.
179      * <p>The default implementation uses a <code>getProxy</code> call with
180      * the factory's bean class loader. Can be overridden to specify a
181      * custom class loader.
182      * @param aopProxy the prepared AopProxy instance to get the proxy from
183      * @return the proxy object to expose
184      * @see AopProxy#getProxy(ClassLoader)
185      */

186     protected Object JavaDoc getProxy(AopProxy aopProxy) {
187         return aopProxy.getProxy(this.beanClassLoader);
188     }
189
190
191     public Object JavaDoc getObject() {
192         if (this.proxy == null) {
193             throw new FactoryBeanNotInitializedException();
194         }
195         return this.proxy;
196     }
197
198     public Class JavaDoc getObjectType() {
199         if (this.proxy != null) {
200             return this.proxy.getClass();
201         }
202         if (this.proxyInterfaces != null && this.proxyInterfaces.length == 1) {
203             return this.proxyInterfaces[0];
204         }
205         if (this.target instanceof TargetSource) {
206             return ((TargetSource) this.target).getTargetClass();
207         }
208         if (this.target != null) {
209             return this.target.getClass();
210         }
211         return null;
212     }
213
214     public final boolean isSingleton() {
215         return true;
216     }
217
218
219     /**
220      * Create the "main" interceptor for this proxy factory bean.
221      * Typically an Advisor, but can also be any type of Advice.
222      * <p>Pre-interceptors will be applied before, post-interceptors
223      * will be applied after this interceptor.
224      */

225     protected abstract Object JavaDoc createMainInterceptor();
226
227 }
228
Popular Tags