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.messaging.async;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.messaging.MessageBusUtil;
20  import com.liferay.portal.kernel.messaging.async.Async;
21  import com.liferay.portal.kernel.util.MethodTargetClassKey;
22  import com.liferay.portal.spring.aop.AnnotationChainableMethodAdvice;
23  
24  import java.lang.annotation.Annotation;
25  import java.lang.reflect.Method;
26  
27  import java.util.Map;
28  
29  import org.aopalliance.intercept.MethodInvocation;
30  
31  /**
32   * <a href="AsyncAdvice.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Shuyang Zhou
35   * @author Brian Wing Shun Chan
36   */
37  public class AsyncAdvice extends AnnotationChainableMethodAdvice<Async> {
38  
39      public Object before(final MethodInvocation methodInvocation)
40          throws Throwable {
41  
42          MethodTargetClassKey methodTargetClassKey = buildMethodTargetClassKey(
43              methodInvocation);
44  
45          Async async = findAnnotation(methodTargetClassKey);
46  
47          if (async == _nullAsync) {
48              return null;
49          }
50  
51          Method method = methodTargetClassKey.getMethod();
52  
53          if (method.getReturnType() != void.class) {
54              if (_log.isWarnEnabled()) {
55                  _log.warn(
56                      "Async annotation on method " + method.getName() +
57                          " does not return void");
58              }
59  
60              annotations.put(methodTargetClassKey, _nullAsync);
61  
62              return null;
63          }
64  
65          String destinationName = null;
66  
67          if ((_destinationNames != null) && !_destinationNames.isEmpty()) {
68              destinationName = _destinationNames.get(
69                  methodTargetClassKey.getTargetClass());
70          }
71  
72          if (destinationName == null) {
73              destinationName = _defaultDestinationName;
74          }
75  
76          MessageBusUtil.sendMessage(
77              destinationName,
78              new Runnable() {
79  
80                  public void run() {
81                      try {
82                          nextMethodInterceptor.invoke(methodInvocation);
83                      }
84                      catch (Throwable t) {
85                          throw new RuntimeException(t);
86                      }
87                  }
88  
89                  public String toString() {
90                      return methodInvocation.toString();
91                  }
92  
93              });
94  
95          return nullResult;
96      }
97  
98      public Class<Async> getAnnotationClass() {
99          return Async.class;
100     }
101 
102     public String getDefaultDestinationName() {
103         return _defaultDestinationName;
104     }
105 
106     public Async getNullAnnotation() {
107         return _nullAsync;
108     }
109 
110     public void setDefaultDestinationName(String defaultDestinationName) {
111         _defaultDestinationName = defaultDestinationName;
112     }
113 
114     public void setDestinationNames(Map<Class<?>, String> destinationNames) {
115         _destinationNames = destinationNames;
116     }
117 
118     private static Async _nullAsync =
119         new Async() {
120 
121             public Class<? extends Annotation> annotationType() {
122                 return Async.class;
123             }
124 
125         };
126 
127     private static Log _log = LogFactoryUtil.getLog(AsyncAdvice.class);
128 
129     private String _defaultDestinationName;
130     private Map<Class<?>, String> _destinationNames;
131 
132 }