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