KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > aop > scope > ScopedProxyFactoryBean


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.scope;
18
19 import java.lang.reflect.Modifier JavaDoc;
20
21 import org.springframework.aop.framework.AopInfrastructureBean;
22 import org.springframework.aop.framework.ProxyConfig;
23 import org.springframework.aop.framework.ProxyFactory;
24 import org.springframework.aop.support.DelegatingIntroductionInterceptor;
25 import org.springframework.aop.target.SimpleBeanTargetSource;
26 import org.springframework.beans.factory.BeanFactory;
27 import org.springframework.beans.factory.BeanFactoryAware;
28 import org.springframework.beans.factory.FactoryBean;
29 import org.springframework.beans.factory.FactoryBeanNotInitializedException;
30 import org.springframework.beans.factory.config.ConfigurableBeanFactory;
31 import org.springframework.util.ClassUtils;
32
33 /**
34  * Convenient proxy factory bean for scoped objects.
35  *
36  * <p>Proxies created using this factory bean are thread-safe singletons
37  * and may be injected into shared objects, with transparent scoping behavior.
38  *
39  * <p>Proxies returned by this class implement the {@link ScopedObject} interface.
40  * This presently allows for removing the corresponding object from the scope,
41  * seamlessly creating a new instance in the scope on next access.
42  *
43  * <p>Please note that the proxies created by this factory are
44  * <i>class-based</i> proxies by default. This can be customized
45  * through switching the "proxyTargetClass" property to "false".
46  *
47  * @author Rod Johnson
48  * @author Juergen Hoeller
49  * @since 2.0
50  * @see #setProxyTargetClass
51  */

52 public class ScopedProxyFactoryBean extends ProxyConfig implements FactoryBean, BeanFactoryAware {
53
54     /** The TargetSource that manages scoping */
55     private final SimpleBeanTargetSource scopedTargetSource = new SimpleBeanTargetSource();
56
57     /** The name of the target bean */
58     private String JavaDoc targetBeanName;
59
60     /** The cached singleton proxy */
61     private Object JavaDoc proxy;
62
63
64     /**
65      * Create a new ScopedProxyFactoryBean instance.
66      */

67     public ScopedProxyFactoryBean() {
68         setProxyTargetClass(true);
69     }
70
71
72     /**
73      * Set the name of the bean that is to be scoped.
74      */

75     public void setTargetBeanName(String JavaDoc targetBeanName) {
76         this.targetBeanName = targetBeanName;
77         this.scopedTargetSource.setTargetBeanName(targetBeanName);
78     }
79
80     public void setBeanFactory(BeanFactory beanFactory) {
81         if (!(beanFactory instanceof ConfigurableBeanFactory)) {
82             throw new IllegalStateException JavaDoc("Not running in a ConfigurableBeanFactory: " + beanFactory);
83         }
84         ConfigurableBeanFactory cbf = (ConfigurableBeanFactory) beanFactory;
85
86         this.scopedTargetSource.setBeanFactory(beanFactory);
87
88         ProxyFactory pf = new ProxyFactory();
89         pf.copyFrom(this);
90         pf.setTargetSource(this.scopedTargetSource);
91
92         Class JavaDoc beanType = beanFactory.getType(this.targetBeanName);
93         if (beanType == null) {
94             throw new IllegalStateException JavaDoc("Cannot create scoped proxy for bean '" + this.targetBeanName +
95                     "': Target type could not be determined at the time of proxy creation.");
96         }
97         if (!isProxyTargetClass() || beanType.isInterface() || Modifier.isPrivate(beanType.getModifiers())) {
98             pf.setInterfaces(ClassUtils.getAllInterfacesForClass(beanType));
99         }
100
101         // Add an introduction that implements only the methods on ScopedObject.
102
ScopedObject scopedObject = new DefaultScopedObject(cbf, this.scopedTargetSource.getTargetBeanName());
103         pf.addAdvice(new DelegatingIntroductionInterceptor(scopedObject));
104
105         // Add the AopInfrastructureBean marker to indicate that the scoped proxy
106
// itself is not subject to auto-proxying! Only its target bean is.
107
pf.addInterface(AopInfrastructureBean.class);
108
109         this.proxy = pf.getProxy(cbf.getBeanClassLoader());
110     }
111
112
113     public Object JavaDoc getObject() {
114         if (this.proxy == null) {
115             throw new FactoryBeanNotInitializedException();
116         }
117         return this.proxy;
118     }
119
120     public Class JavaDoc getObjectType() {
121         if (this.proxy != null) {
122             return this.proxy.getClass();
123         }
124         if (this.scopedTargetSource != null) {
125             return this.scopedTargetSource.getTargetClass();
126         }
127         return null;
128     }
129
130     public boolean isSingleton() {
131         return true;
132     }
133
134 }
135
Popular Tags