KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > aop > framework > autoproxy > AbstractAdvisorAutoProxyCreator


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.autoproxy;
18
19 import java.util.Collections JavaDoc;
20 import java.util.List JavaDoc;
21
22 import org.springframework.aop.TargetSource;
23 import org.springframework.aop.support.AopUtils;
24 import org.springframework.beans.factory.BeanFactory;
25 import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
26 import org.springframework.core.OrderComparator;
27
28 /**
29  * Abstract BeanPostProcessor implementation that creates AOP proxies.
30  * This class is completely generic; it contains no special code to handle
31  * any particular aspects, such as pooling aspects.
32  *
33  * <p>Subclasses must implement the abstract <code>findCandidateAdvisors()</code>
34  * method to return a list of Advisors applying to any object. Subclasses can
35  * also override the inherited <code>shouldSkip</code> method to exclude certain
36  * objects from auto-proxying, but they must be careful to invoke the base
37  * <code>shouldSkip</code> method, which tries to avoid circular reference
38  * problems and infinite loops.
39  *
40  * <p>Advisors or advices requiring ordering should implement the Ordered interface.
41  * This class sorts advisors by Ordered order value. Advisors that don't implement
42  * the Ordered interface will be considered to be unordered, and will appear
43  * at the end of the advisor chain in undefined order.
44  *
45  * @author Rod Johnson
46  * @author Juergen Hoeller
47  * @see #findCandidateAdvisors
48  * @see org.springframework.aop.support.AopUtils
49  */

50 public abstract class AbstractAdvisorAutoProxyCreator extends AbstractAutoProxyCreator {
51
52     private BeanFactoryAdvisorRetrievalHelper advisorRetrievalHelper;
53
54
55     public void setBeanFactory(BeanFactory beanFactory) {
56         super.setBeanFactory(beanFactory);
57         if (!(beanFactory instanceof ConfigurableListableBeanFactory)) {
58             throw new IllegalStateException JavaDoc("Cannot use AdvisorAutoProxyCreator without a ConfigurableListableBeanFactory");
59         }
60         initBeanFactory((ConfigurableListableBeanFactory) beanFactory);
61     }
62
63     protected void initBeanFactory(ConfigurableListableBeanFactory beanFactory) {
64         this.advisorRetrievalHelper = new BeanFactoryAdvisorRetrievalHelperAdapter(beanFactory);
65     }
66
67
68     protected Object JavaDoc[] getAdvicesAndAdvisorsForBean(Class JavaDoc beanClass, String JavaDoc name, TargetSource targetSource) {
69         List JavaDoc advisors = findEligibleAdvisors(beanClass);
70         if (advisors.isEmpty()) {
71             return DO_NOT_PROXY;
72         }
73         return advisors.toArray();
74     }
75
76     /**
77      * Find all eligible advices for auto-proxying this class.
78      * @return the empty list, not <code>null</code>,
79      * if there are no pointcuts or interceptors
80      * @see #findCandidateAdvisors
81      * @see #sortAdvisors
82      * @see #extendAdvisors
83      */

84     protected List JavaDoc findEligibleAdvisors(Class JavaDoc clazz) {
85         List JavaDoc eligibleAdvisors = AopUtils.findAdvisorsThatCanApply(findCandidateAdvisors(), clazz);
86         if (!eligibleAdvisors.isEmpty()) {
87             eligibleAdvisors = sortAdvisors(eligibleAdvisors);
88         }
89         extendAdvisors(eligibleAdvisors);
90         return eligibleAdvisors;
91     }
92
93     /**
94      * Find all candidate advisors to use in auto-proxying.
95      * @return list of candidate Advisors
96      */

97     protected List JavaDoc findCandidateAdvisors() {
98         return this.advisorRetrievalHelper.findAdvisorBeans();
99     }
100
101     /**
102      * Return whether the Advisor bean with the given name is eligible
103      * for proxying in the first place.
104      * @param beanName the name of the Advisor bean
105      */

106     protected boolean isEligibleAdvisorBean(String JavaDoc beanName) {
107         return true;
108     }
109
110     /**
111      * Sort advisors based on ordering. Subclasses may choose to override this
112      * method to customize the sorting strategy.
113      * @see org.springframework.core.Ordered
114      * @see org.springframework.core.OrderComparator
115      */

116     protected List JavaDoc sortAdvisors(List JavaDoc advisors) {
117         Collections.sort(advisors, new OrderComparator());
118         return advisors;
119     }
120
121     /**
122      * Extension hook that subclasses can choose to register additional Advisors,
123      * given the sorted Advisors obtained to date. The default implementation
124      * is empty.
125      * <p>Typically used to add Advisors that expose contextual information
126      * required by some of the later advisors.
127      * @param candidateAdvisors Advisors that have already been identified as
128      * applying to a given bean
129      */

130     protected void extendAdvisors(List JavaDoc candidateAdvisors) {
131     }
132
133
134     /**
135      * Subclass of BeanFactoryAdvisorRetrievalHelper that delegates to
136      * surrounding AbstractAdvisorAutoProxyCreator facilities.
137      */

138     private class BeanFactoryAdvisorRetrievalHelperAdapter extends BeanFactoryAdvisorRetrievalHelper {
139
140         public BeanFactoryAdvisorRetrievalHelperAdapter(ConfigurableListableBeanFactory beanFactory) {
141             super(beanFactory);
142         }
143
144         protected boolean isEligibleBean(String JavaDoc beanName) {
145             return AbstractAdvisorAutoProxyCreator.this.isEligibleAdvisorBean(beanName);
146         }
147     }
148
149 }
150
Popular Tags