001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.spring.aop;
016    
017    import org.aopalliance.intercept.MethodInterceptor;
018    import org.aopalliance.intercept.MethodInvocation;
019    
020    /**
021     * @author Shuyang Zhou
022     * @author Brian Wing Shun Chan
023     */
024    public abstract class ChainableMethodAdvice implements MethodInterceptor {
025    
026            public void afterReturning(MethodInvocation methodInvocation, Object result)
027                    throws Throwable {
028            }
029    
030            public void afterThrowing(
031                            MethodInvocation methodInvocation, Throwable throwable)
032                    throws Throwable {
033            }
034    
035            public Object before(MethodInvocation methodInvocation) throws Throwable {
036                    return null;
037            }
038    
039            public void duringFinally(MethodInvocation methodInvocation) {
040            }
041    
042            public final Object invoke(MethodInvocation methodInvocation)
043                    throws Throwable {
044    
045                    Object returnValue = before(methodInvocation);
046    
047                    if (returnValue != null) {
048                            if (returnValue == nullResult) {
049                                    return null;
050                            }
051                            else {
052                                    return returnValue;
053                            }
054                    }
055    
056                    try {
057                            if (nextMethodInterceptor != null) {
058                                    returnValue = nextMethodInterceptor.invoke(methodInvocation);
059                            }
060                            else {
061                                    returnValue = methodInvocation.proceed();
062                            }
063    
064                            afterReturning(methodInvocation, returnValue);
065                    }
066                    catch (Throwable throwable) {
067                            afterThrowing(methodInvocation, throwable);
068    
069                            throw throwable;
070                    }
071                    finally {
072                            duringFinally(methodInvocation);
073                    }
074    
075                    return returnValue;
076            }
077    
078            public void setNextMethodInterceptor(
079                    MethodInterceptor nextMethodInterceptor) {
080    
081                    this.nextMethodInterceptor = nextMethodInterceptor;
082            }
083    
084            protected MethodInterceptor nextMethodInterceptor;
085            protected Object nullResult = new Object();
086    
087    }