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.BaseProxyBean;
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 MessagingProxyInvocationHandler implements InvocationHandler {
029    
030            public static InvocationHandlerFactory getInvocationHandlerFactory() {
031                    return _invocationHandlerFactory;
032            }
033    
034            public MessagingProxyInvocationHandler(BaseProxyBean baseProxyBean) {
035                    _baseProxyBean = baseProxyBean;
036            }
037    
038            @Override
039            public Object invoke(Object proxy, Method method, Object[] args)
040                    throws Throwable {
041    
042                    ProxyRequest proxyRequest = new ProxyRequest(method, args);
043    
044                    if (proxyRequest.isSynchronous() ||
045                            ProxyModeThreadLocal.isForceSync()) {
046    
047                            return _baseProxyBean.synchronousSend(proxyRequest);
048                    }
049                    else {
050                            _baseProxyBean.send(proxyRequest);
051    
052                            return null;
053                    }
054            }
055    
056            private static InvocationHandlerFactory _invocationHandlerFactory =
057                    new InvocationHandlerFactory() {
058    
059                    @Override
060                    public InvocationHandler createInvocationHandler(Object obj) {
061                            return new MessagingProxyInvocationHandler((BaseProxyBean)obj);
062                    }
063    
064            };
065    
066            private BaseProxyBean _baseProxyBean;
067    
068    }