1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.spring.aop;
16  
17  import org.aopalliance.intercept.MethodInterceptor;
18  import org.aopalliance.intercept.MethodInvocation;
19  
20  /**
21   * <a href="ChainableMethodAdvice.java.html"><b><i>View Source</i></b></a>
22   *
23   * @author Shuyang Zhou
24   * @author Brian Wing Shun Chan
25   */
26  public abstract class ChainableMethodAdvice implements MethodInterceptor {
27  
28      public void afterReturning(MethodInvocation methodInvocation, Object result)
29          throws Throwable {
30      }
31  
32      public void afterThrowing(
33              MethodInvocation methodInvocation, Throwable throwable)
34          throws Throwable {
35      }
36  
37      public Object before(MethodInvocation methodInvocation) throws Throwable {
38          return null;
39      }
40  
41      public final Object invoke(MethodInvocation methodInvocation)
42          throws Throwable {
43  
44          Object returnValue = before(methodInvocation);
45  
46          if (returnValue != null) {
47              if (returnValue == nullResult) {
48                  return null;
49              }
50              else {
51                  return returnValue;
52              }
53          }
54  
55          try {
56              if (nextMethodInterceptor != null) {
57                  returnValue = nextMethodInterceptor.invoke(methodInvocation);
58              }
59              else {
60                  returnValue = methodInvocation.proceed();
61              }
62  
63              afterReturning(methodInvocation, returnValue);
64          }
65          catch (Throwable throwable) {
66              afterThrowing(methodInvocation, throwable);
67  
68              throw throwable;
69          }
70  
71          return returnValue;
72      }
73  
74      public void setNextMethodInterceptor(
75          MethodInterceptor nextMethodInterceptor) {
76  
77          this.nextMethodInterceptor = nextMethodInterceptor;
78      }
79  
80      protected MethodInterceptor nextMethodInterceptor;
81      protected Object nullResult = new Object();
82  
83  }