1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
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 void duringFinally(MethodInvocation methodInvocation) {
42      }
43  
44      public final Object invoke(MethodInvocation methodInvocation)
45          throws Throwable {
46  
47          Object returnValue = before(methodInvocation);
48  
49          if (returnValue != null) {
50              if (returnValue == nullResult) {
51                  return null;
52              }
53              else {
54                  return returnValue;
55              }
56          }
57  
58          try {
59              if (nextMethodInterceptor != null) {
60                  returnValue = nextMethodInterceptor.invoke(methodInvocation);
61              }
62              else {
63                  returnValue = methodInvocation.proceed();
64              }
65  
66              afterReturning(methodInvocation, returnValue);
67          }
68          catch (Throwable throwable) {
69              afterThrowing(methodInvocation, throwable);
70  
71              throw throwable;
72          }
73          finally {
74              duringFinally(methodInvocation);
75          }
76  
77          return returnValue;
78      }
79  
80      public void setNextMethodInterceptor(
81          MethodInterceptor nextMethodInterceptor) {
82  
83          this.nextMethodInterceptor = nextMethodInterceptor;
84      }
85  
86      protected MethodInterceptor nextMethodInterceptor;
87      protected Object nullResult = new Object();
88  
89  }