001: /*
002: * Copyright 2002-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.aop.support;
018:
019: import org.aopalliance.intercept.MethodInvocation;
020:
021: import org.springframework.aop.DynamicIntroductionAdvice;
022: import org.springframework.aop.IntroductionInterceptor;
023: import org.springframework.aop.ProxyMethodInvocation;
024: import org.springframework.util.Assert;
025:
026: /**
027: * Convenient implementation of the
028: * {@link org.springframework.aop.IntroductionInterceptor} interface.
029: *
030: * <p>Subclasses merely need to extend this class and implement the interfaces
031: * to be introduced themselves. In this case the delegate is the subclass
032: * instance itself. Alternatively a separate delegate may implement the
033: * interface, and be set via the delegate bean property.
034: *
035: * <p>Delegates or subclasses may implement any number of interfaces.
036: * All interfaces except IntroductionInterceptor are picked up from
037: * the subclass or delegate by default.
038: *
039: * <p>The <code>suppressInterface</code> method can be used to suppress interfaces
040: * implemented by the delegate but which should not be introduced to the owning
041: * AOP proxy.
042: *
043: * <p>An instance of this class is serializable if the delegate is.
044: *
045: * @author Rod Johnson
046: * @author Juergen Hoeller
047: * @since 16.11.2003
048: * @see #suppressInterface
049: * @see DelegatePerTargetObjectIntroductionInterceptor
050: */
051: public class DelegatingIntroductionInterceptor extends
052: IntroductionInfoSupport implements IntroductionInterceptor {
053:
054: /**
055: * Object that actually implements the interfaces.
056: * May be "this" if a subclass implements the introduced interfaces.
057: */
058: private Object delegate;
059:
060: /**
061: * Construct a new DelegatingIntroductionInterceptor, providing
062: * a delegate that implements the interfaces to be introduced.
063: * @param delegate the delegate that implements the introduced interfaces
064: */
065: public DelegatingIntroductionInterceptor(Object delegate) {
066: init(delegate);
067: }
068:
069: /**
070: * Construct a new DelegatingIntroductionInterceptor.
071: * The delegate will be the subclass, which must implement
072: * additional interfaces.
073: */
074: protected DelegatingIntroductionInterceptor() {
075: init(this );
076: }
077:
078: /**
079: * Both constructors use this init method, as it is impossible to pass
080: * a "this" reference from one constructor to another.
081: * @param delegate the delegate object
082: */
083: private void init(Object delegate) {
084: Assert.notNull(delegate, "Delegate must not be null");
085: this .delegate = delegate;
086: implementInterfacesOnObject(delegate);
087:
088: // We don't want to expose the control interface
089: suppressInterface(IntroductionInterceptor.class);
090: suppressInterface(DynamicIntroductionAdvice.class);
091: }
092:
093: /**
094: * Subclasses may need to override this if they want to perform custom
095: * behaviour in around advice. However, subclasses should invoke this
096: * method, which handles introduced interfaces and forwarding to the target.
097: */
098: public Object invoke(MethodInvocation mi) throws Throwable {
099: if (isMethodOnIntroducedInterface(mi)) {
100: // Using the following method rather than direct reflection, we
101: // get correct handling of InvocationTargetException
102: // if the introduced method throws an exception.
103: Object retVal = AopUtils.invokeJoinpointUsingReflection(
104: this .delegate, mi.getMethod(), mi.getArguments());
105:
106: // Massage return value if possible: if the delegate returned itself,
107: // we really want to return the proxy.
108: if (retVal == this .delegate
109: && mi instanceof ProxyMethodInvocation) {
110: Object proxy = ((ProxyMethodInvocation) mi).getProxy();
111: if (mi.getMethod().getReturnType().isInstance(proxy)) {
112: retVal = proxy;
113: }
114: }
115: return retVal;
116: }
117:
118: return doProceed(mi);
119: }
120:
121: /**
122: * Proceed with the supplied {@link org.aopalliance.intercept.MethodInterceptor}.
123: * Subclasses can override this method to intercept method invocations on the
124: * target object which is useful when an introduction needs to monitor the object
125: * that it is introduced into. This method is <strong>never</strong> called for
126: * {@link MethodInvocation MethodInvocations} on the introduced interfaces.
127: */
128: protected Object doProceed(MethodInvocation mi) throws Throwable {
129: // If we get here, just pass the invocation on.
130: return mi.proceed();
131: }
132:
133: }
|