001    /**
002     * Copyright (c) 2000-2013 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.MessageValuesThreadLocal;
020    import com.liferay.portal.kernel.messaging.proxy.ProxyModeThreadLocal;
021    import com.liferay.portal.kernel.messaging.proxy.ProxyRequest;
022    import com.liferay.portal.kernel.messaging.proxy.ProxyResponse;
023    import com.liferay.portal.kernel.messaging.sender.SingleDestinationMessageSender;
024    import com.liferay.portal.kernel.messaging.sender.SingleDestinationSynchronousMessageSender;
025    import com.liferay.util.aspectj.AspectJUtil;
026    
027    import java.util.Map;
028    
029    import org.aspectj.lang.ProceedingJoinPoint;
030    
031    /**
032     * @author Michael C. Han
033     * @author Brian Wing Shun Chan
034     * @author Shuyang Zhou
035     */
036    public class MessagingProxyAdvice {
037    
038            public Object invoke(ProceedingJoinPoint proceedingJoinPoint)
039                    throws Throwable {
040    
041                    Message message = new Message();
042    
043                    ProxyRequest proxyRequest = createProxyRequest(proceedingJoinPoint);
044    
045                    message.setPayload(proxyRequest);
046    
047                    Map<String, Object> messageValues =
048                            MessageValuesThreadLocal.getValues();
049    
050                    if (!messageValues.isEmpty()) {
051                            for (String key : messageValues.keySet()) {
052                                    message.put(key, messageValues.get(key));
053                            }
054                    }
055    
056                    BaseProxyBean baseProxyBean =
057                            (BaseProxyBean)proceedingJoinPoint.getTarget();
058    
059                    if (proxyRequest.isSynchronous() ||
060                            ProxyModeThreadLocal.isForceSync()) {
061    
062                            return doInvokeSynchronous(message, baseProxyBean);
063                    }
064                    else {
065                            doInvokeAsynchronous(message, baseProxyBean);
066    
067                            return null;
068                    }
069            }
070    
071            protected ProxyRequest createProxyRequest(
072                            ProceedingJoinPoint proceedingJoinPoint)
073                    throws Exception {
074    
075                    return new ProxyRequest(
076                            AspectJUtil.getMethod(proceedingJoinPoint),
077                            proceedingJoinPoint.getArgs());
078            }
079    
080            protected void doInvokeAsynchronous(
081                    Message message, BaseProxyBean baseProxyBean) {
082    
083                    SingleDestinationMessageSender messageSender =
084                            baseProxyBean.getSingleDestinationMessageSender();
085    
086                    if (messageSender == null) {
087                            throw new IllegalStateException(
088                                    "Asynchronous message sender was not configured properly for " +
089                                            baseProxyBean.getClass().getName());
090                    }
091    
092                    messageSender.send(message);
093            }
094    
095            protected Object doInvokeSynchronous(
096                            Message message, BaseProxyBean baseProxyBean)
097                    throws Exception {
098    
099                    SingleDestinationSynchronousMessageSender messageSender =
100                            baseProxyBean.getSingleDestinationSynchronousMessageSender();
101    
102                    if (messageSender == null) {
103                            throw new IllegalStateException(
104                                    "Synchronous message sender was not configured properly for " +
105                                            baseProxyBean.getClass().getName());
106                    }
107    
108                    ProxyResponse proxyResponse = (ProxyResponse)messageSender.send(
109                            message);
110    
111                    if (proxyResponse == null) {
112                            return null;
113                    }
114                    else if (proxyResponse.hasError()) {
115                            throw proxyResponse.getException();
116                    }
117                    else {
118                            return proxyResponse.getResult();
119                    }
120            }
121    
122    }