1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.kernel.messaging.proxy;
16  
17  import com.liferay.portal.kernel.util.MethodInvoker;
18  import com.liferay.portal.kernel.util.MethodWrapper;
19  import com.liferay.portal.kernel.util.NullWrapper;
20  
21  import java.io.Serializable;
22  
23  import java.lang.reflect.InvocationTargetException;
24  import java.lang.reflect.Method;
25  
26  /**
27   * <a href="ProxyRequest.java.html"><b><i>View Source</i></b></a>
28   *
29   * @author Micha Kiener
30   * @author Michael C. Han
31   * @author Brian Wing Shun Chan
32   */
33  public class ProxyRequest implements Serializable {
34  
35      public ProxyRequest(Method method, Object[] arguments) throws Exception {
36          Class<?>[] argumentTypes = method.getParameterTypes();
37  
38          for (int i = 0; i < arguments.length; i++) {
39              if (arguments[i] == null) {
40                  arguments[i] = new NullWrapper(argumentTypes[i].getName());
41              }
42          }
43  
44          _methodWrapper = new MethodWrapper(method, arguments);
45  
46          _hasReturnValue = false;
47  
48          if (method.getReturnType() != Void.TYPE) {
49              _hasReturnValue = true;
50          }
51  
52          MessagingProxy messagingProxy = method.getAnnotation(
53              MessagingProxy.class);
54  
55          if (messagingProxy == null) {
56              messagingProxy = method.getDeclaringClass().getAnnotation(
57                  MessagingProxy.class);
58          }
59  
60          if ((messagingProxy != null) &&
61              (messagingProxy.mode().equals(ProxyMode.SYNC))) {
62  
63              _synchronous = true;
64          }
65      }
66  
67      public Object execute(Object object) throws Exception {
68          try {
69              return MethodInvoker.invoke(_methodWrapper, object);
70          }
71          catch (InvocationTargetException ite) {
72              Throwable t = ite.getCause();
73  
74              if (t instanceof Exception) {
75                  throw (Exception)t;
76              }
77              else {
78                  throw new Exception(t);
79              }
80          }
81      }
82  
83      public MethodWrapper getMethodWrapper() {
84          return _methodWrapper;
85      }
86  
87      public boolean hasReturnValue() {
88          return _hasReturnValue;
89      }
90  
91      public boolean isSynchronous() {
92          return _synchronous;
93      }
94  
95      private boolean _hasReturnValue;
96      private MethodWrapper _methodWrapper;
97      private boolean _synchronous;
98  
99  }