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