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