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