001    /**
002     * Copyright (c) 2000-2013 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.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.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 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 BaseMultiDestinationProxyBean _baseMultiDestinationProxyBean;
071    
072    }