KickJava   Java API By Example, From Geeks To Geeks.

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


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.lang.reflect.Method JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.Arrays JavaDoc;
22 import java.util.List JavaDoc;
23
24 import org.aopalliance.intercept.Interceptor;
25 import org.aopalliance.intercept.MethodInterceptor;
26
27 import org.springframework.aop.Advisor;
28 import org.springframework.aop.IntroductionAdvisor;
29 import org.springframework.aop.MethodMatcher;
30 import org.springframework.aop.PointcutAdvisor;
31 import org.springframework.aop.framework.adapter.AdvisorAdapterRegistry;
32 import org.springframework.aop.framework.adapter.GlobalAdvisorAdapterRegistry;
33 import org.springframework.aop.support.MethodMatchers;
34
35 /**
36  * A simple but definitive way of working out an advice chain for a Method,
37  * given an {@link Advised} object. Always rebuilds each advice chain;
38  * caching can be provided by subclasses.
39  *
40  * @author Juergen Hoeller
41  * @author Rod Johnson
42  * @author Adrian Colyer
43  * @since 2.0.3
44  */

45 public class DefaultAdvisorChainFactory implements AdvisorChainFactory {
46
47     public List JavaDoc getInterceptorsAndDynamicInterceptionAdvice(
48             Advised config, Object JavaDoc proxy, Method JavaDoc method, Class JavaDoc targetClass) {
49
50         // This is somewhat tricky... we have to process introductions first,
51
// but we need to preserve order in the ultimate list.
52
List JavaDoc interceptorList = new ArrayList JavaDoc(config.getAdvisors().length);
53         boolean hasIntroductions = hasMatchingIntroductions(config, targetClass);
54         AdvisorAdapterRegistry registry = GlobalAdvisorAdapterRegistry.getInstance();
55         Advisor[] advisors = config.getAdvisors();
56         for (int i = 0; i < advisors.length; i++) {
57             Advisor advisor = advisors[i];
58             if (advisor instanceof PointcutAdvisor) {
59                 // Add it conditionally.
60
PointcutAdvisor pointcutAdvisor = (PointcutAdvisor) advisor;
61                 if (pointcutAdvisor.getPointcut().getClassFilter().matches(targetClass)) {
62                     MethodInterceptor[] interceptors = registry.getInterceptors(advisor);
63                     MethodMatcher mm = pointcutAdvisor.getPointcut().getMethodMatcher();
64                     if (MethodMatchers.matches(mm, method, targetClass, hasIntroductions)) {
65                         if (mm.isRuntime()) {
66                             // Creating a new object instance in the getInterceptors() method
67
// isn't a problem as we normally cache created chains.
68
for (int j = 0; j < interceptors.length; j++) {
69                                 interceptorList.add(new InterceptorAndDynamicMethodMatcher(interceptors[j], mm));
70                             }
71                         }
72                         else {
73                             interceptorList.addAll(Arrays.asList(interceptors));
74                         }
75                     }
76                 }
77             }
78             else if (advisor instanceof IntroductionAdvisor) {
79                 IntroductionAdvisor ia = (IntroductionAdvisor) advisor;
80                 if (ia.getClassFilter().matches(targetClass)) {
81                     Interceptor[] interceptors = registry.getInterceptors(advisor);
82                     interceptorList.addAll(Arrays.asList(interceptors));
83                 }
84             }
85         }
86         return interceptorList;
87     }
88
89     /**
90      * Determine whether the Advisors contain matching introductions.
91      */

92     private static boolean hasMatchingIntroductions(Advised config, Class JavaDoc targetClass) {
93         for (int i = 0; i < config.getAdvisors().length; i++) {
94             Advisor advisor = config.getAdvisors()[i];
95             if (advisor instanceof IntroductionAdvisor) {
96                 IntroductionAdvisor ia = (IntroductionAdvisor) advisor;
97                 if (ia.getClassFilter().matches(targetClass)) {
98                     return true;
99                 }
100             }
101         }
102         return false;
103     }
104
105 }
106
Popular Tags