001    /**
002     * Copyright (c) 2000-present 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.BaseMultiDestinationProxyBean;
018    import com.liferay.portal.kernel.messaging.proxy.ProxyModeThreadLocal;
019    import com.liferay.portal.kernel.messaging.proxy.ProxyRequest;
020    import com.liferay.portal.kernel.spring.aop.InvocationHandlerFactory;
021    
022    import java.lang.reflect.InvocationHandler;
023    import java.lang.reflect.Method;
024    
025    /**
026     * @author Shuyang Zhou
027     */
028    public class MultiDestinationMessagingProxyInvocationHandler
029            implements InvocationHandler {
030    
031            public static InvocationHandlerFactory getInvocationHandlerFactory() {
032                    return _invocationHandlerFactory;
033            }
034    
035            public MultiDestinationMessagingProxyInvocationHandler(
036                    BaseMultiDestinationProxyBean baseMultiDestinationProxyBean) {
037    
038                    _baseMultiDestinationProxyBean = baseMultiDestinationProxyBean;
039            }
040    
041            @Override
042            public Object invoke(Object proxy, Method method, Object[] args)
043                    throws Throwable {
044    
045                    ProxyRequest proxyRequest = new ProxyRequest(method, args);
046    
047                    if (proxyRequest.isSynchronous() ||
048                            ProxyModeThreadLocal.isForceSync()) {
049    
050                            return _baseMultiDestinationProxyBean.synchronousSend(proxyRequest);
051                    }
052                    else {
053                            _baseMultiDestinationProxyBean.send(proxyRequest);
054    
055                            return null;
056                    }
057            }
058    
059            private static final InvocationHandlerFactory _invocationHandlerFactory =
060                    new InvocationHandlerFactory() {
061    
062                            @Override
063                            public InvocationHandler createInvocationHandler(Object obj) {
064                                    return new MultiDestinationMessagingProxyInvocationHandler(
065                                            (BaseMultiDestinationProxyBean)obj);
066                            }
067    
068                    };
069    
070            private final BaseMultiDestinationProxyBean _baseMultiDestinationProxyBean;
071    
072    }