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.messaging.proxy;
16  
17  import com.liferay.portal.kernel.messaging.proxy.BaseProxyBean;
18  import com.liferay.portal.kernel.messaging.proxy.ProxyRequest;
19  import com.liferay.portal.kernel.messaging.proxy.ProxyResponse;
20  import com.liferay.portal.kernel.messaging.sender.SingleDestinationMessageSender;
21  import com.liferay.portal.kernel.messaging.sender.SingleDestinationSynchronousMessageSender;
22  import com.liferay.util.aspectj.AspectJUtil;
23  
24  import org.aspectj.lang.ProceedingJoinPoint;
25  
26  /**
27   * <a href="MessagingProxyAdvice.java.html"><b><i>View Source</i></b></a>
28   *
29   * @author Michael C. Han
30   * @author Brian Wing Shun Chan
31   * @author Shuyang Zhou
32   */
33  public class MessagingProxyAdvice {
34  
35      public Object invoke(ProceedingJoinPoint proceedingJoinPoint)
36          throws Throwable {
37  
38          ProxyRequest proxyRequest = createProxyRequest(proceedingJoinPoint);
39  
40          BaseProxyBean baseProxyBean =
41              (BaseProxyBean)proceedingJoinPoint.getTarget();
42  
43          if (proxyRequest.isSynchronous()) {
44              return doInvokeSynchronous(proxyRequest, baseProxyBean);
45          }
46          else {
47              doInvokeAsynchronous(proxyRequest, baseProxyBean);
48  
49              return null;
50          }
51      }
52  
53      protected ProxyRequest createProxyRequest(
54              ProceedingJoinPoint proceedingJoinPoint)
55          throws Exception {
56  
57          return new ProxyRequest(
58              AspectJUtil.getMethod(proceedingJoinPoint),
59              proceedingJoinPoint.getArgs());
60      }
61  
62      protected void doInvokeAsynchronous(
63          ProxyRequest proxyRequest, BaseProxyBean baseProxyBean) {
64  
65          SingleDestinationMessageSender messageSender =
66              baseProxyBean.getSingleDestinationMessageSender();
67  
68          if (messageSender == null) {
69              throw new IllegalStateException(
70                  "Asynchronous message sender was not configured properly for " +
71                      baseProxyBean.getClass().getName());
72          }
73  
74          messageSender.send(proxyRequest);
75      }
76  
77      protected Object doInvokeSynchronous(
78              ProxyRequest proxyRequest, BaseProxyBean baseProxyBean)
79          throws Exception {
80  
81          SingleDestinationSynchronousMessageSender messageSender =
82              baseProxyBean.getSingleDestinationSynchronousMessageSender();
83  
84          if (messageSender == null) {
85              throw new IllegalStateException(
86                  "Synchronous message sender was not configured properly for " +
87                      baseProxyBean.getClass().getName());
88          }
89  
90          ProxyResponse proxyResponse = (ProxyResponse)messageSender.send(
91              proxyRequest);
92  
93          if (proxyResponse == null) {
94              return null;
95          }
96          else if (proxyResponse.hasError()) {
97              throw proxyResponse.getException();
98          }
99          else {
100             return proxyResponse.getResult();
101         }
102     }
103 
104 }