001    /**
002     * Copyright (c) 2000-2011 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 boolean afterThrowing(
031                            MethodInvocation methodInvocation, Throwable throwable)
032                    throws Throwable {
033    
034                    return true;
035            }
036    
037            public Object before(MethodInvocation methodInvocation) throws Throwable {
038                    return null;
039            }
040    
041            public void duringFinally(MethodInvocation methodInvocation) {
042            }
043    
044            public final Object invoke(MethodInvocation methodInvocation)
045                    throws Throwable {
046    
047                    Object returnValue = before(methodInvocation);
048    
049                    if (returnValue != null) {
050                            if (returnValue == nullResult) {
051                                    return null;
052                            }
053                            else {
054                                    return returnValue;
055                            }
056                    }
057    
058                    try {
059                            returnValue = methodInvocation.proceed();
060    
061                            afterReturning(methodInvocation, returnValue);
062                    }
063                    catch (Throwable throwable) {
064                            if (afterThrowing(methodInvocation, throwable)) {
065                                    throw throwable;
066                            }
067                    }
068                    finally {
069                            duringFinally(methodInvocation);
070                    }
071    
072                    return returnValue;
073            }
074    
075            public void setNextMethodInterceptor(
076                    MethodInterceptor nextMethodInterceptor) {
077    
078                    this.nextMethodInterceptor = nextMethodInterceptor;
079            }
080    
081            protected MethodInterceptor nextMethodInterceptor;
082            protected Object nullResult = new Object();
083    
084    }