KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > aop > support > DelegatingIntroductionInterceptor


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.support;
18
19 import org.aopalliance.intercept.MethodInvocation;
20
21 import org.springframework.aop.DynamicIntroductionAdvice;
22 import org.springframework.aop.IntroductionInterceptor;
23 import org.springframework.aop.ProxyMethodInvocation;
24 import org.springframework.util.Assert;
25
26 /**
27  * Convenient implementation of the
28  * {@link org.springframework.aop.IntroductionInterceptor} interface.
29  *
30  * <p>Subclasses merely need to extend this class and implement the interfaces
31  * to be introduced themselves. In this case the delegate is the subclass
32  * instance itself. Alternatively a separate delegate may implement the
33  * interface, and be set via the delegate bean property.
34  *
35  * <p>Delegates or subclasses may implement any number of interfaces.
36  * All interfaces except IntroductionInterceptor are picked up from
37  * the subclass or delegate by default.
38  *
39  * <p>The <code>suppressInterface</code> method can be used to suppress interfaces
40  * implemented by the delegate but which should not be introduced to the owning
41  * AOP proxy.
42  *
43  * <p>An instance of this class is serializable if the delegate is.
44  *
45  * @author Rod Johnson
46  * @author Juergen Hoeller
47  * @since 16.11.2003
48  * @see #suppressInterface
49  * @see DelegatePerTargetObjectIntroductionInterceptor
50  */

51 public class DelegatingIntroductionInterceptor extends IntroductionInfoSupport
52         implements IntroductionInterceptor {
53         
54     /**
55      * Object that actually implements the interfaces.
56      * May be "this" if a subclass implements the introduced interfaces.
57      */

58     private Object JavaDoc delegate;
59
60
61     /**
62      * Construct a new DelegatingIntroductionInterceptor, providing
63      * a delegate that implements the interfaces to be introduced.
64      * @param delegate the delegate that implements the introduced interfaces
65      */

66     public DelegatingIntroductionInterceptor(Object JavaDoc delegate) {
67         init(delegate);
68     }
69     
70     /**
71      * Construct a new DelegatingIntroductionInterceptor.
72      * The delegate will be the subclass, which must implement
73      * additional interfaces.
74      */

75     protected DelegatingIntroductionInterceptor() {
76         init(this);
77     }
78
79
80     /**
81      * Both constructors use this, as it's impossible to pass
82      * "this" from one constructor to another.
83      */

84     private void init(Object JavaDoc delegate) {
85         Assert.notNull(delegate, "delegate is required");
86         this.delegate = delegate;
87         implementInterfacesOnObject(delegate);
88
89         // We don't want to expose the control interface
90
suppressInterface(IntroductionInterceptor.class);
91         suppressInterface(DynamicIntroductionAdvice.class);
92     }
93         
94     
95     /**
96      * Subclasses may need to override this if they want to perform custom
97      * behaviour in around advice. However, subclasses should invoke this
98      * method, which handles introduced interfaces and forwarding to the target.
99      */

100     public Object JavaDoc invoke(MethodInvocation mi) throws Throwable JavaDoc {
101         if (isMethodOnIntroducedInterface(mi)) {
102             // Using the following method rather than direct reflection, we
103
// get correct handling of InvocationTargetException
104
// if the introduced method throws an exception.
105
Object JavaDoc retVal = AopUtils.invokeJoinpointUsingReflection(this.delegate, mi.getMethod(), mi.getArguments());
106             
107             // Massage return value if possible: if the delegate returned itself,
108
// we really want to return the proxy.
109
if (retVal == this.delegate && mi instanceof ProxyMethodInvocation) {
110                 retVal = ((ProxyMethodInvocation) mi).getProxy();
111             }
112             return retVal;
113         }
114
115         return doProceed(mi);
116     }
117
118     /**
119      * Proceed with the supplied {@link org.aopalliance.intercept.MethodInterceptor}.
120      * Subclasses can override this method to intercept method invocations on the
121      * target object which is useful when an introduction needs to monitor the object
122      * that it is introduced into. This method is <strong>never</strong> called for
123      * {@link MethodInvocation MethodInvocations} on the introduced interfaces.
124      */

125     protected Object JavaDoc doProceed(MethodInvocation mi) throws Throwable JavaDoc {
126         // If we get here, just pass the invocation on.
127
return mi.proceed();
128     }
129
130 }
131
Popular Tags