1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.messaging.proxy;
24  
25  import com.liferay.portal.kernel.messaging.proxy.BaseProxyBean;
26  import com.liferay.portal.kernel.messaging.proxy.ProxyRequest;
27  import com.liferay.portal.kernel.messaging.proxy.ProxyResponse;
28  import com.liferay.portal.kernel.messaging.sender.SingleDestinationMessageSender;
29  import com.liferay.portal.kernel.messaging.sender.SingleDestinationSynchronousMessageSender;
30  import com.liferay.util.aspectj.AspectJUtil;
31  
32  import org.aspectj.lang.ProceedingJoinPoint;
33  
34  /**
35   * <a href="MessagingProxyAdvice.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Michael C. Han
38   * @author Brian Wing Shun Chan
39   * @author Shuyang Zhou
40   */
41  public class MessagingProxyAdvice {
42  
43      public Object invoke(ProceedingJoinPoint proceedingJoinPoint)
44          throws Throwable {
45  
46          ProxyRequest proxyRequest = createProxyRequest(proceedingJoinPoint);
47  
48          BaseProxyBean baseProxyBean =
49              (BaseProxyBean)proceedingJoinPoint.getTarget();
50  
51          if (proxyRequest.isSynchronous()) {
52              return doInvokeSynchronous(proxyRequest, baseProxyBean);
53          }
54          else {
55              doInvokeAsynchronous(proxyRequest, baseProxyBean);
56  
57              return null;
58          }
59      }
60  
61      protected ProxyRequest createProxyRequest(
62              ProceedingJoinPoint proceedingJoinPoint)
63          throws Exception {
64  
65          return new ProxyRequest(
66              AspectJUtil.getMethod(proceedingJoinPoint),
67              proceedingJoinPoint.getArgs());
68      }
69  
70      protected void doInvokeAsynchronous(
71          ProxyRequest proxyRequest, BaseProxyBean baseProxyBean) {
72  
73          SingleDestinationMessageSender messageSender =
74              baseProxyBean.getSingleDestinationMessageSender();
75  
76          if (messageSender == null) {
77              throw new IllegalStateException(
78                  "Asynchronous message sender was not configured properly for " +
79                      baseProxyBean.getClass().getName());
80          }
81  
82          messageSender.send(proxyRequest);
83      }
84  
85      protected Object doInvokeSynchronous(
86              ProxyRequest proxyRequest, BaseProxyBean baseProxyBean)
87          throws Exception {
88  
89          SingleDestinationSynchronousMessageSender messageSender =
90              baseProxyBean.getSingleDestinationSynchronousMessageSender();
91  
92          if (messageSender == null) {
93              throw new IllegalStateException(
94                  "Synchronous message sender was not configured properly for " +
95                      baseProxyBean.getClass().getName());
96          }
97  
98          ProxyResponse proxyResponse = (ProxyResponse)messageSender.send(
99              proxyRequest);
100 
101         if (proxyResponse == null) {
102             return null;
103         }
104         else if (proxyResponse.hasError()) {
105             throw proxyResponse.getException();
106         }
107         else {
108             return proxyResponse.getResult();
109         }
110     }
111 
112 }