KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.LinkedList JavaDoc;
20 import java.util.List JavaDoc;
21
22 import org.springframework.util.Assert;
23
24 /**
25  * Base class for proxy factories.
26  * Provides convenient access to a configurable AopProxyFactory.
27  *
28  * @author Juergen Hoeller
29  * @since 2.0.3
30  * @see #createAopProxy()
31  */

32 public class ProxyCreatorSupport extends AdvisedSupport {
33
34     /** The AopProxyFactory to use */
35     private AopProxyFactory aopProxyFactory;
36
37     /** List of AdvisedSupportListener */
38     private List JavaDoc listeners = new LinkedList JavaDoc();
39
40     /** Set to true when the first AOP proxy has been created */
41     private boolean active = false;
42
43
44     /**
45      * Create a new ProxyCreatorSupport instance.
46      */

47     public ProxyCreatorSupport() {
48         this.aopProxyFactory = new DefaultAopProxyFactory();
49     }
50
51     /**
52      * Create a new ProxyCreatorSupport instance.
53      * @param aopProxyFactory the AopProxyFactory to use
54      */

55     public ProxyCreatorSupport(AopProxyFactory aopProxyFactory) {
56         Assert.notNull(aopProxyFactory, "AopProxyFactory must not be null");
57         this.aopProxyFactory = aopProxyFactory;
58     }
59
60
61     /**
62      * Customize the AopProxyFactory, allowing different strategies
63      * to be dropped in without changing the core framework.
64      * <p>Default is {@link DefaultAopProxyFactory}, using dynamic JDK
65      * proxies or CGLIB proxies based on the requirements.
66      */

67     public void setAopProxyFactory(AopProxyFactory aopProxyFactory) {
68         Assert.notNull(aopProxyFactory, "AopProxyFactory must not be null");
69         this.aopProxyFactory = aopProxyFactory;
70     }
71
72     /**
73      * Return the AopProxyFactory that this ProxyConfig uses.
74      */

75     public AopProxyFactory getAopProxyFactory() {
76         return this.aopProxyFactory;
77     }
78
79     /**
80      * Add the given AdvisedSupportListener to this proxy configuration.
81      * @param listener the listener to register
82      */

83     public void addListener(AdvisedSupportListener listener) {
84         Assert.notNull(listener, "AdvisedSupportListener must not be null");
85         this.listeners.add(listener);
86     }
87
88     /**
89      * Remove the given AdvisedSupportListener from this proxy configuration.
90      * @param listener the listener to deregister
91      */

92     public void removeListener(AdvisedSupportListener listener) {
93         Assert.notNull(listener, "AdvisedSupportListener must not be null");
94         this.listeners.remove(listener);
95     }
96
97
98     /**
99      * Subclasses should call this to get a new AOP proxy. They should <b>not</b>
100      * create an AOP proxy with <code>this</code> as an argument.
101      */

102     protected final synchronized AopProxy createAopProxy() {
103         if (!this.active) {
104             activate();
105         }
106         return getAopProxyFactory().createAopProxy(this);
107     }
108
109     /**
110      * Activate this proxy configuration.
111      * @see AdvisedSupportListener#activated
112      */

113     private void activate() {
114         this.active = true;
115         for (int i = 0; i < this.listeners.size(); i++) {
116             ((AdvisedSupportListener) this.listeners.get(i)).activated(this);
117         }
118     }
119
120     /**
121      * Propagate advice change event to all AdvisedSupportListeners.
122      * @see AdvisedSupportListener#adviceChanged
123      */

124     protected void adviceChanged() {
125         super.adviceChanged();
126         synchronized (this) {
127             if (this.active) {
128                 for (int i = 0; i < this.listeners.size(); i++) {
129                     ((AdvisedSupportListener) this.listeners.get(i)).adviceChanged(this);
130                 }
131             }
132         }
133     }
134
135     /**
136      * Subclasses can call this to check whether any AOP proxies have been created yet.
137      */

138     protected final synchronized boolean isActive() {
139         return this.active;
140     }
141
142 }
143
Popular Tags