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