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