001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.ProxyRequest;
021    import com.liferay.portal.kernel.messaging.proxy.ProxyResponse;
022    import com.liferay.portal.kernel.messaging.sender.SingleDestinationMessageSender;
023    import com.liferay.portal.kernel.messaging.sender.SingleDestinationSynchronousMessageSender;
024    import com.liferay.util.aspectj.AspectJUtil;
025    
026    import java.util.Map;
027    
028    import org.aspectj.lang.ProceedingJoinPoint;
029    
030    /**
031     * @author Michael C. Han
032     * @author Brian Wing Shun Chan
033     * @author Shuyang Zhou
034     */
035    public class MessagingProxyAdvice {
036    
037            public Object invoke(ProceedingJoinPoint proceedingJoinPoint)
038                    throws Throwable {
039    
040                    Message message = new Message();
041    
042                    ProxyRequest proxyRequest = createProxyRequest(proceedingJoinPoint);
043    
044                    message.setPayload(proxyRequest);
045    
046                    Map<String, Object> messageValues =
047                            MessageValuesThreadLocal.getValues();
048    
049                    if (!messageValues.isEmpty()) {
050                            for (String key : messageValues.keySet()) {
051                                    message.put(key, messageValues.get(key));
052                            }
053                    }
054    
055                    BaseProxyBean baseProxyBean =
056                            (BaseProxyBean)proceedingJoinPoint.getTarget();
057    
058                    if (proxyRequest.isSynchronous() ||
059                            ProxyModeThreadLocal.isForceSync()) {
060    
061                            return doInvokeSynchronous(message, baseProxyBean);
062                    }
063                    else {
064                            doInvokeAsynchronous(message, baseProxyBean);
065    
066                            return null;
067                    }
068            }
069    
070            protected ProxyRequest createProxyRequest(
071                            ProceedingJoinPoint proceedingJoinPoint)
072                    throws Exception {
073    
074                    return new ProxyRequest(
075                            AspectJUtil.getMethod(proceedingJoinPoint),
076                            proceedingJoinPoint.getArgs());
077            }
078    
079            protected void doInvokeAsynchronous(
080                    Message message, BaseProxyBean baseProxyBean) {
081    
082                    SingleDestinationMessageSender messageSender =
083                            baseProxyBean.getSingleDestinationMessageSender();
084    
085                    if (messageSender == null) {
086                            throw new IllegalStateException(
087                                    "Asynchronous message sender was not configured properly for " +
088                                            baseProxyBean.getClass().getName());
089                    }
090    
091                    messageSender.send(message);
092            }
093    
094            protected Object doInvokeSynchronous(
095                            Message message, BaseProxyBean baseProxyBean)
096                    throws Exception {
097    
098                    SingleDestinationSynchronousMessageSender messageSender =
099                            baseProxyBean.getSingleDestinationSynchronousMessageSender();
100    
101                    if (messageSender == null) {
102                            throw new IllegalStateException(
103                                    "Synchronous message sender was not configured properly for " +
104                                            baseProxyBean.getClass().getName());
105                    }
106    
107                    ProxyResponse proxyResponse = (ProxyResponse)messageSender.send(
108                            message);
109    
110                    if (proxyResponse == null) {
111                            return null;
112                    }
113                    else if (proxyResponse.hasError()) {
114                            throw proxyResponse.getException();
115                    }
116                    else {
117                            return proxyResponse.getResult();
118                    }
119            }
120    
121    }