001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.messaging.proxy;
016    
017    import com.liferay.portal.kernel.messaging.Message;
018    import com.liferay.portal.kernel.messaging.proxy.BaseProxyBean;
019    import com.liferay.portal.kernel.messaging.proxy.ProxyRequest;
020    import com.liferay.portal.kernel.messaging.proxy.ProxyResponse;
021    import com.liferay.portal.kernel.messaging.sender.SingleDestinationMessageSender;
022    import com.liferay.portal.kernel.messaging.sender.SingleDestinationSynchronousMessageSender;
023    import com.liferay.util.aspectj.AspectJUtil;
024    
025    import java.util.Map;
026    
027    import org.aspectj.lang.ProceedingJoinPoint;
028    
029    /**
030     * @author Michael C. Han
031     * @author Brian Wing Shun Chan
032     * @author Shuyang Zhou
033     */
034    public class MessagingProxyAdvice {
035    
036            public Object invoke(ProceedingJoinPoint proceedingJoinPoint)
037                    throws Throwable {
038    
039                    Message message = new Message();
040    
041                    ProxyRequest proxyRequest = createProxyRequest(proceedingJoinPoint);
042    
043                    message.setPayload(proxyRequest);
044    
045                    Map<String, Object> messageValues =
046                            MessageValuesThreadLocal.getValues();
047    
048                    if (!messageValues.isEmpty()) {
049                            for (String key : messageValues.keySet()) {
050                                    message.put(key, messageValues.get(key));
051                            }
052                    }
053    
054                    BaseProxyBean baseProxyBean =
055                            (BaseProxyBean)proceedingJoinPoint.getTarget();
056    
057                    if (proxyRequest.isSynchronous() ||
058                            ProxyModeThreadLocal.isForceSync()) {
059    
060                            return doInvokeSynchronous(message, baseProxyBean);
061                    }
062                    else {
063                            doInvokeAsynchronous(message, baseProxyBean);
064    
065                            return null;
066                    }
067            }
068    
069            protected ProxyRequest createProxyRequest(
070                            ProceedingJoinPoint proceedingJoinPoint)
071                    throws Exception {
072    
073                    return new ProxyRequest(
074                            AspectJUtil.getMethod(proceedingJoinPoint),
075                            proceedingJoinPoint.getArgs());
076            }
077    
078            protected void doInvokeAsynchronous(
079                    Message message, BaseProxyBean baseProxyBean) {
080    
081                    SingleDestinationMessageSender messageSender =
082                            baseProxyBean.getSingleDestinationMessageSender();
083    
084                    if (messageSender == null) {
085                            throw new IllegalStateException(
086                                    "Asynchronous message sender was not configured properly for " +
087                                            baseProxyBean.getClass().getName());
088                    }
089    
090                    messageSender.send(message);
091            }
092    
093            protected Object doInvokeSynchronous(
094                            Message message, BaseProxyBean baseProxyBean)
095                    throws Exception {
096    
097                    SingleDestinationSynchronousMessageSender messageSender =
098                            baseProxyBean.getSingleDestinationSynchronousMessageSender();
099    
100                    if (messageSender == null) {
101                            throw new IllegalStateException(
102                                    "Synchronous message sender was not configured properly for " +
103                                            baseProxyBean.getClass().getName());
104                    }
105    
106                    ProxyResponse proxyResponse = (ProxyResponse)messageSender.send(
107                            message);
108    
109                    if (proxyResponse == null) {
110                            return null;
111                    }
112                    else if (proxyResponse.hasError()) {
113                            throw proxyResponse.getException();
114                    }
115                    else {
116                            return proxyResponse.getResult();
117                    }
118            }
119    
120    }