001    /**
002     * Copyright (c) 2000-2012 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 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 Object invoke(MethodInvocation methodInvocation) throws Throwable {
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                            returnValue = methodInvocation.proceed();
058    
059                            afterReturning(methodInvocation, returnValue);
060                    }
061                    catch (Throwable throwable) {
062                            if (afterThrowing(methodInvocation, throwable)) {
063                                    throw throwable;
064                            }
065                    }
066                    finally {
067                            duringFinally(methodInvocation);
068                    }
069    
070                    return returnValue;
071            }
072    
073            public void setNextMethodInterceptor(
074                    MethodInterceptor nextMethodInterceptor) {
075    
076                    this.nextMethodInterceptor = nextMethodInterceptor;
077            }
078    
079            protected void setServiceBeanAopCacheManager(
080                    ServiceBeanAopCacheManager serviceBeanAopCacheManager) {
081    
082                    if (this.serviceBeanAopCacheManager != null) {
083                            return;
084                    }
085    
086                    this.serviceBeanAopCacheManager = serviceBeanAopCacheManager;
087            }
088    
089            protected MethodInterceptor nextMethodInterceptor;
090            protected Object nullResult = new Object();
091            protected ServiceBeanAopCacheManager serviceBeanAopCacheManager;
092    
093    }