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