001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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 Object invoke(MethodInvocation methodInvocation) throws Throwable {
043                    Object returnValue = before(methodInvocation);
044    
045                    if (returnValue != null) {
046                            if (returnValue == nullResult) {
047                                    return null;
048                            }
049                            else {
050                                    return returnValue;
051                            }
052                    }
053    
054                    try {
055                            returnValue = methodInvocation.proceed();
056    
057                            afterReturning(methodInvocation, returnValue);
058                    }
059                    catch (Throwable throwable) {
060                            afterThrowing(methodInvocation, throwable);
061    
062                            throw throwable;
063                    }
064                    finally {
065                            duringFinally(methodInvocation);
066                    }
067    
068                    return returnValue;
069            }
070    
071            public void setNextMethodInterceptor(
072                    MethodInterceptor nextMethodInterceptor) {
073    
074                    this.nextMethodInterceptor = nextMethodInterceptor;
075            }
076    
077            protected void setServiceBeanAopCacheManager(
078                    ServiceBeanAopCacheManager serviceBeanAopCacheManager) {
079    
080                    if (this.serviceBeanAopCacheManager != null) {
081                            return;
082                    }
083    
084                    this.serviceBeanAopCacheManager = serviceBeanAopCacheManager;
085            }
086    
087            protected MethodInterceptor nextMethodInterceptor;
088            protected Object nullResult = new Object();
089            protected ServiceBeanAopCacheManager serviceBeanAopCacheManager;
090    
091    }