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.kernel.messaging.proxy;
016    
017    import com.liferay.portal.kernel.util.MethodHandler;
018    
019    import java.io.Serializable;
020    
021    import java.lang.reflect.InvocationTargetException;
022    import java.lang.reflect.Method;
023    
024    /**
025     * @author Micha Kiener
026     * @author Michael C. Han
027     * @author Brian Wing Shun Chan
028     */
029    public class ProxyRequest implements Serializable {
030    
031            public ProxyRequest(Method method, Object[] arguments) throws Exception {
032                    _methodHandler = new MethodHandler(method, arguments);
033    
034                    _hasReturnValue = false;
035    
036                    if (method.getReturnType() != Void.TYPE) {
037                            _hasReturnValue = true;
038                    }
039    
040                    MessagingProxy messagingProxy = method.getAnnotation(
041                            MessagingProxy.class);
042    
043                    if (messagingProxy == null) {
044                            messagingProxy = method.getDeclaringClass().getAnnotation(
045                                    MessagingProxy.class);
046                    }
047    
048                    if ((messagingProxy != null) &&
049                            messagingProxy.mode().equals(ProxyMode.SYNC)) {
050    
051                            _synchronous = true;
052                    }
053            }
054    
055            public Object execute(Object object) throws Exception {
056                    try {
057                            return _methodHandler.invoke(object);
058                    }
059                    catch (InvocationTargetException ite) {
060                            Throwable t = ite.getCause();
061    
062                            if (t instanceof Exception) {
063                                    throw (Exception)t;
064                            }
065                            else {
066                                    throw new Exception(t);
067                            }
068                    }
069            }
070    
071            public Object[] getArguments() {
072                    return _methodHandler.getArguments();
073            }
074    
075            public boolean hasReturnValue() {
076                    return _hasReturnValue;
077            }
078    
079            public boolean isSynchronous() {
080                    return _synchronous;
081            }
082    
083            private boolean _hasReturnValue;
084            private MethodHandler _methodHandler;
085            private boolean _synchronous;
086    
087    }