1. /*
  2. * @(#)RemoteObjectInvocationHandler.java 1.3 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.rmi.server;
  8. import java.io.InvalidObjectException;
  9. import java.lang.reflect.InvocationHandler;
  10. import java.lang.reflect.Method;
  11. import java.lang.reflect.Proxy;
  12. import java.rmi.Remote;
  13. import java.rmi.UnexpectedException;
  14. import java.rmi.activation.Activatable;
  15. import java.util.Map;
  16. import java.util.WeakHashMap;
  17. import sun.rmi.server.Util;
  18. import sun.rmi.server.WeakClassHashMap;
  19. /**
  20. * An implementation of the <code>InvocationHandler</code> interface for
  21. * use with Java Remote Method Invocation (Java RMI). This invocation
  22. * handler can be used in conjunction with a dynamic proxy instance as a
  23. * replacement for a pregenerated stub class.
  24. *
  25. * <p>Applications are not expected to use this class directly. A remote
  26. * object exported to use a dynamic proxy with {@link UnicastRemoteObject}
  27. * or {@link Activatable} has an instance of this class as that proxy's
  28. * invocation handler.
  29. *
  30. * @version 1.3, 03/12/19
  31. * @author Ann Wollrath
  32. * @since 1.5
  33. **/
  34. public class RemoteObjectInvocationHandler
  35. extends RemoteObject
  36. implements InvocationHandler
  37. {
  38. private static final long serialVersionUID = 2L;
  39. /**
  40. * A weak hash map, mapping classes to weak hash maps that map
  41. * method objects to method hashes.
  42. **/
  43. private static final MethodToHash_Maps methodToHash_Maps =
  44. new MethodToHash_Maps();
  45. /**
  46. * Creates a new <code>RemoteObjectInvocationHandler</code> constructed
  47. * with the specified <code>RemoteRef</code>.
  48. *
  49. * @param ref the remote ref
  50. *
  51. * @throws NullPointerException if <code>ref</code> is <code>null</code>
  52. **/
  53. public RemoteObjectInvocationHandler(RemoteRef ref) {
  54. super(ref);
  55. if (ref == null) {
  56. throw new NullPointerException();
  57. }
  58. }
  59. /**
  60. * Processes a method invocation made on the encapsulating
  61. * proxy instance, <code>proxy</code>, and returns the result.
  62. *
  63. * <p><code>RemoteObjectInvocationHandler</code> implements this method
  64. * as follows:
  65. *
  66. * <p>If <code>method</code> is one of the following methods, it
  67. * is processed as described below:
  68. *
  69. * <ul>
  70. *
  71. * <li>{@link Object#hashCode Object.hashCode}: Returns the hash
  72. * code value for the proxy.
  73. *
  74. * <li>{@link Object#equals Object.equals}: Returns <code>true</code>
  75. * if the argument (<code>args[0]</code>) is an instance of a dynamic
  76. * proxy class and this invocation handler is equal to the invocation
  77. * handler of that argument, and returns <code>false</code> otherwise.
  78. *
  79. * <li>{@link Object#toString Object.toString}: Returns a string
  80. * representation of the proxy.
  81. * </ul>
  82. *
  83. * <p>Otherwise, a remote call is made as follows:
  84. *
  85. * <ul>
  86. * <li>If <code>proxy</code> is not an instance of the interface
  87. * {@link Remote}, then an {@link IllegalArgumentException} is thrown.
  88. *
  89. * <li>Otherwise, the {@link RemoteRef#invoke invoke} method is invoked
  90. * on this invocation handler's <code>RemoteRef</code>, passing
  91. * <code>proxy</code>, <code>method</code>, <code>args</code>, and the
  92. * method hash (defined in section 8.3 of the "Java Remote Method
  93. * Invocation (RMI) Specification") for <code>method</code>, and the
  94. * result is returned.
  95. *
  96. * <li>If an exception is thrown by <code>RemoteRef.invoke</code> and
  97. * that exception is a checked exception that is not assignable to any
  98. * exception in the <code>throws</code> clause of the method
  99. * implemented by the <code>proxy</code>'s class, then that exception
  100. * is wrapped in an {@link UnexpectedException} and the wrapped
  101. * exception is thrown. Otherwise, the exception thrown by
  102. * <code>invoke</code> is thrown by this method.
  103. * </ul>
  104. *
  105. * <p>The semantics of this method are unspecified if the
  106. * arguments could not have been produced by an instance of some
  107. * valid dynamic proxy class containing this invocation handler.
  108. *
  109. * @param proxy the proxy instance that the method was invoked on
  110. * @param method the <code>Method</code> instance corresponding to the
  111. * interface method invoked on the proxy instance
  112. * @param args an array of objects containing the values of the
  113. * arguments passed in the method invocation on the proxy instance, or
  114. * <code>null</code> if the method takes no arguments
  115. * @return the value to return from the method invocation on the proxy
  116. * instance
  117. * @throws Throwable the exception to throw from the method invocation
  118. * on the proxy instance
  119. * @see
  120. **/
  121. public Object invoke(Object proxy, Method method, Object[] args)
  122. throws Throwable
  123. {
  124. if (method.getDeclaringClass() == Object.class) {
  125. return invokeObjectMethod(proxy, method, args);
  126. } else {
  127. return invokeRemoteMethod(proxy, method, args);
  128. }
  129. }
  130. /**
  131. * Handles java.lang.Object methods.
  132. **/
  133. private Object invokeObjectMethod(Object proxy,
  134. Method method,
  135. Object[] args)
  136. {
  137. String name = method.getName();
  138. if (name.equals("hashCode")) {
  139. return new Integer(hashCode());
  140. } else if (name.equals("equals")) {
  141. Object obj = args[0];
  142. boolean b =
  143. proxy == obj ||
  144. (obj != null &&
  145. Proxy.isProxyClass(obj.getClass()) &&
  146. equals(Proxy.getInvocationHandler(obj)));
  147. return Boolean.valueOf(b);
  148. } else if (name.equals("toString")) {
  149. return proxyToString(proxy);
  150. } else {
  151. throw new IllegalArgumentException(
  152. "unexpected Object method: " + method);
  153. }
  154. }
  155. /**
  156. * Handles remote methods.
  157. **/
  158. private Object invokeRemoteMethod(Object proxy,
  159. Method method,
  160. Object[] args)
  161. throws Exception
  162. {
  163. try {
  164. if (!(proxy instanceof Remote)) {
  165. throw new IllegalArgumentException(
  166. "proxy not Remote instance");
  167. }
  168. return ref.invoke((Remote) proxy, method, args,
  169. getMethodHash(method));
  170. } catch (Exception e) {
  171. if (!(e instanceof RuntimeException)) {
  172. Class cl = proxy.getClass();
  173. try {
  174. method = cl.getMethod(method.getName(),
  175. method.getParameterTypes());
  176. } catch (NoSuchMethodException nsme) {
  177. throw (IllegalArgumentException)
  178. new IllegalArgumentException().initCause(nsme);
  179. }
  180. Class[] exTypes = method.getExceptionTypes();
  181. Class thrownType = e.getClass();
  182. for (int i = 0; i < exTypes.length; i++) {
  183. if (exTypes[i].isAssignableFrom(thrownType)) {
  184. throw e;
  185. }
  186. }
  187. e = new UnexpectedException("unexpected exception", e);
  188. }
  189. throw e;
  190. }
  191. }
  192. /**
  193. * Returns a string representation for a proxy that uses this invocation
  194. * handler.
  195. **/
  196. private String proxyToString(Object proxy) {
  197. Class[] interfaces = proxy.getClass().getInterfaces();
  198. if (interfaces.length == 0) {
  199. return "Proxy[" + this + "]";
  200. }
  201. String iface = interfaces[0].getName();
  202. if (iface.equals("java.rmi.Remote") && interfaces.length > 1) {
  203. iface = interfaces[1].getName();
  204. }
  205. int dot = iface.lastIndexOf('.');
  206. if (dot >= 0) {
  207. iface = iface.substring(dot + 1);
  208. }
  209. return "Proxy[" + iface + "," + this + "]";
  210. }
  211. /**
  212. * @throws InvalidObjectException unconditionally
  213. **/
  214. private void readObjectNoData() throws InvalidObjectException {
  215. throw new InvalidObjectException("no data in stream; class: " +
  216. this.getClass().getName());
  217. }
  218. /**
  219. * Returns the method hash for the specified method. Subsequent calls
  220. * to "getMethodHash" passing the same method argument should be faster
  221. * since this method caches internally the result of the method to
  222. * method hash mapping. The method hash is calculated using the
  223. * "computeMethodHash" method.
  224. *
  225. * @param method the remote method
  226. * @return the method hash for the specified method
  227. */
  228. private static long getMethodHash(Method method) {
  229. Map map = methodToHash_Maps.getMap(method.getDeclaringClass());
  230. Long hash = (Long) map.get(method);
  231. return hash.longValue();
  232. }
  233. /**
  234. * A weak hash map, mapping classes to weak hash maps that map
  235. * method objects to method hashes.
  236. **/
  237. private static class MethodToHash_Maps extends WeakClassHashMap {
  238. MethodToHash_Maps() {}
  239. protected Map createMap(Class remoteClass) {
  240. return new WeakHashMap() {
  241. public synchronized Object get(Object key) {
  242. Object hash = super.get(key);
  243. if (hash == null) {
  244. Method method = (Method) key;
  245. hash = new Long(Util.computeMethodHash(method));
  246. put(method, hash);
  247. }
  248. return (Long) hash;
  249. }
  250. };
  251. }
  252. }
  253. }