KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > aop > framework > adapter > DefaultAdvisorAdapterRegistry


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.adapter;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.List JavaDoc;
21
22 import org.aopalliance.aop.Advice;
23 import org.aopalliance.intercept.MethodInterceptor;
24
25 import org.springframework.aop.Advisor;
26 import org.springframework.aop.support.DefaultPointcutAdvisor;
27
28 /**
29  * Default implementation of the {@link AdvisorAdapterRegistry} interface.
30  * Supports {@link org.aopalliance.intercept.MethodInterceptor},
31  * {@link org.springframework.aop.MethodBeforeAdvice},
32  * {@link org.springframework.aop.AfterReturningAdvice},
33  * {@link org.springframework.aop.ThrowsAdvice}.
34  *
35  * @author Rod Johnson
36  * @author Rob Harrop
37  */

38 public class DefaultAdvisorAdapterRegistry implements AdvisorAdapterRegistry {
39
40     private final List JavaDoc adapters = new ArrayList JavaDoc(3);
41
42
43     /**
44      * Create a new DefaultAdvisorAdapterRegistry, registering well-known adapters.
45      */

46     public DefaultAdvisorAdapterRegistry() {
47         registerAdvisorAdapter(new MethodBeforeAdviceAdapter());
48         registerAdvisorAdapter(new AfterReturningAdviceAdapter());
49         registerAdvisorAdapter(new ThrowsAdviceAdapter());
50     }
51
52
53     public Advisor wrap(Object JavaDoc adviceObject) throws UnknownAdviceTypeException {
54         if (adviceObject instanceof Advisor) {
55             return (Advisor) adviceObject;
56         }
57         if (!(adviceObject instanceof Advice)) {
58             throw new UnknownAdviceTypeException(adviceObject);
59         }
60         Advice advice = (Advice) adviceObject;
61         if (advice instanceof MethodInterceptor) {
62             // So well-known it doesn't even need an adapter.
63
return new DefaultPointcutAdvisor(advice);
64         }
65         for (int i = 0; i < this.adapters.size(); i++) {
66             // Check that it is supported.
67
AdvisorAdapter adapter = (AdvisorAdapter) this.adapters.get(i);
68             if (adapter.supportsAdvice(advice)) {
69                 return new DefaultPointcutAdvisor(advice);
70             }
71         }
72         throw new UnknownAdviceTypeException(advice);
73     }
74
75     public MethodInterceptor[] getInterceptors(Advisor advisor) throws UnknownAdviceTypeException {
76         List JavaDoc interceptors = new ArrayList JavaDoc(3);
77         Advice advice = advisor.getAdvice();
78         if (advice instanceof MethodInterceptor) {
79             interceptors.add(advice);
80         }
81         for (int i = 0; i < this.adapters.size(); i++) {
82             AdvisorAdapter adapter = (AdvisorAdapter) this.adapters.get(i);
83             if (adapter.supportsAdvice(advice)) {
84                 interceptors.add(adapter.getInterceptor(advisor));
85             }
86         }
87         if (interceptors.isEmpty()) {
88             throw new UnknownAdviceTypeException(advisor.getAdvice());
89         }
90         return (MethodInterceptor[]) interceptors.toArray(new MethodInterceptor[interceptors.size()]);
91     }
92
93     public void registerAdvisorAdapter(AdvisorAdapter adapter) {
94         this.adapters.add(adapter);
95     }
96
97 }
98
Popular Tags