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