0N/A/*
2362N/A * Copyright (c) 2003, 2005, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/Apackage java.rmi.server;
0N/A
0N/Aimport java.io.InvalidObjectException;
0N/Aimport java.lang.reflect.InvocationHandler;
0N/Aimport java.lang.reflect.Method;
0N/Aimport java.lang.reflect.Proxy;
0N/Aimport java.rmi.Remote;
0N/Aimport java.rmi.UnexpectedException;
0N/Aimport java.rmi.activation.Activatable;
0N/Aimport java.util.Map;
0N/Aimport java.util.WeakHashMap;
0N/Aimport sun.rmi.server.Util;
0N/Aimport sun.rmi.server.WeakClassHashMap;
0N/A
0N/A/**
0N/A * An implementation of the <code>InvocationHandler</code> interface for
0N/A * use with Java Remote Method Invocation (Java RMI). This invocation
0N/A * handler can be used in conjunction with a dynamic proxy instance as a
0N/A * replacement for a pregenerated stub class.
0N/A *
0N/A * <p>Applications are not expected to use this class directly. A remote
0N/A * object exported to use a dynamic proxy with {@link UnicastRemoteObject}
0N/A * or {@link Activatable} has an instance of this class as that proxy's
0N/A * invocation handler.
0N/A *
0N/A * @author Ann Wollrath
0N/A * @since 1.5
0N/A **/
0N/Apublic class RemoteObjectInvocationHandler
0N/A extends RemoteObject
0N/A implements InvocationHandler
0N/A{
0N/A private static final long serialVersionUID = 2L;
0N/A
0N/A /**
0N/A * A weak hash map, mapping classes to weak hash maps that map
0N/A * method objects to method hashes.
0N/A **/
0N/A private static final MethodToHash_Maps methodToHash_Maps =
0N/A new MethodToHash_Maps();
0N/A
0N/A /**
0N/A * Creates a new <code>RemoteObjectInvocationHandler</code> constructed
0N/A * with the specified <code>RemoteRef</code>.
0N/A *
0N/A * @param ref the remote ref
0N/A *
0N/A * @throws NullPointerException if <code>ref</code> is <code>null</code>
0N/A **/
0N/A public RemoteObjectInvocationHandler(RemoteRef ref) {
0N/A super(ref);
0N/A if (ref == null) {
0N/A throw new NullPointerException();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Processes a method invocation made on the encapsulating
0N/A * proxy instance, <code>proxy</code>, and returns the result.
0N/A *
0N/A * <p><code>RemoteObjectInvocationHandler</code> implements this method
0N/A * as follows:
0N/A *
0N/A * <p>If <code>method</code> is one of the following methods, it
0N/A * is processed as described below:
0N/A *
0N/A * <ul>
0N/A *
0N/A * <li>{@link Object#hashCode Object.hashCode}: Returns the hash
0N/A * code value for the proxy.
0N/A *
0N/A * <li>{@link Object#equals Object.equals}: Returns <code>true</code>
0N/A * if the argument (<code>args[0]</code>) is an instance of a dynamic
0N/A * proxy class and this invocation handler is equal to the invocation
0N/A * handler of that argument, and returns <code>false</code> otherwise.
0N/A *
0N/A * <li>{@link Object#toString Object.toString}: Returns a string
0N/A * representation of the proxy.
0N/A * </ul>
0N/A *
0N/A * <p>Otherwise, a remote call is made as follows:
0N/A *
0N/A * <ul>
0N/A * <li>If <code>proxy</code> is not an instance of the interface
0N/A * {@link Remote}, then an {@link IllegalArgumentException} is thrown.
0N/A *
0N/A * <li>Otherwise, the {@link RemoteRef#invoke invoke} method is invoked
0N/A * on this invocation handler's <code>RemoteRef</code>, passing
0N/A * <code>proxy</code>, <code>method</code>, <code>args</code>, and the
0N/A * method hash (defined in section 8.3 of the "Java Remote Method
0N/A * Invocation (RMI) Specification") for <code>method</code>, and the
0N/A * result is returned.
0N/A *
0N/A * <li>If an exception is thrown by <code>RemoteRef.invoke</code> and
0N/A * that exception is a checked exception that is not assignable to any
0N/A * exception in the <code>throws</code> clause of the method
0N/A * implemented by the <code>proxy</code>'s class, then that exception
0N/A * is wrapped in an {@link UnexpectedException} and the wrapped
0N/A * exception is thrown. Otherwise, the exception thrown by
0N/A * <code>invoke</code> is thrown by this method.
0N/A * </ul>
0N/A *
0N/A * <p>The semantics of this method are unspecified if the
0N/A * arguments could not have been produced by an instance of some
0N/A * valid dynamic proxy class containing this invocation handler.
0N/A *
0N/A * @param proxy the proxy instance that the method was invoked on
0N/A * @param method the <code>Method</code> instance corresponding to the
0N/A * interface method invoked on the proxy instance
0N/A * @param args an array of objects containing the values of the
0N/A * arguments passed in the method invocation on the proxy instance, or
0N/A * <code>null</code> if the method takes no arguments
0N/A * @return the value to return from the method invocation on the proxy
0N/A * instance
0N/A * @throws Throwable the exception to throw from the method invocation
0N/A * on the proxy instance
0N/A **/
0N/A public Object invoke(Object proxy, Method method, Object[] args)
0N/A throws Throwable
0N/A {
0N/A if (method.getDeclaringClass() == Object.class) {
0N/A return invokeObjectMethod(proxy, method, args);
0N/A } else {
0N/A return invokeRemoteMethod(proxy, method, args);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Handles java.lang.Object methods.
0N/A **/
0N/A private Object invokeObjectMethod(Object proxy,
0N/A Method method,
0N/A Object[] args)
0N/A {
0N/A String name = method.getName();
0N/A
0N/A if (name.equals("hashCode")) {
0N/A return hashCode();
0N/A
0N/A } else if (name.equals("equals")) {
0N/A Object obj = args[0];
0N/A return
0N/A proxy == obj ||
0N/A (obj != null &&
0N/A Proxy.isProxyClass(obj.getClass()) &&
0N/A equals(Proxy.getInvocationHandler(obj)));
0N/A
0N/A } else if (name.equals("toString")) {
0N/A return proxyToString(proxy);
0N/A
0N/A } else {
0N/A throw new IllegalArgumentException(
0N/A "unexpected Object method: " + method);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Handles remote methods.
0N/A **/
0N/A private Object invokeRemoteMethod(Object proxy,
0N/A Method method,
0N/A Object[] args)
0N/A throws Exception
0N/A {
0N/A try {
0N/A if (!(proxy instanceof Remote)) {
0N/A throw new IllegalArgumentException(
0N/A "proxy not Remote instance");
0N/A }
0N/A return ref.invoke((Remote) proxy, method, args,
0N/A getMethodHash(method));
0N/A } catch (Exception e) {
0N/A if (!(e instanceof RuntimeException)) {
0N/A Class<?> cl = proxy.getClass();
0N/A try {
0N/A method = cl.getMethod(method.getName(),
0N/A method.getParameterTypes());
0N/A } catch (NoSuchMethodException nsme) {
0N/A throw (IllegalArgumentException)
0N/A new IllegalArgumentException().initCause(nsme);
0N/A }
0N/A Class<?> thrownType = e.getClass();
0N/A for (Class<?> declaredType : method.getExceptionTypes()) {
0N/A if (declaredType.isAssignableFrom(thrownType)) {
0N/A throw e;
0N/A }
0N/A }
0N/A e = new UnexpectedException("unexpected exception", e);
0N/A }
0N/A throw e;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns a string representation for a proxy that uses this invocation
0N/A * handler.
0N/A **/
0N/A private String proxyToString(Object proxy) {
0N/A Class<?>[] interfaces = proxy.getClass().getInterfaces();
0N/A if (interfaces.length == 0) {
0N/A return "Proxy[" + this + "]";
0N/A }
0N/A String iface = interfaces[0].getName();
0N/A if (iface.equals("java.rmi.Remote") && interfaces.length > 1) {
0N/A iface = interfaces[1].getName();
0N/A }
0N/A int dot = iface.lastIndexOf('.');
0N/A if (dot >= 0) {
0N/A iface = iface.substring(dot + 1);
0N/A }
0N/A return "Proxy[" + iface + "," + this + "]";
0N/A }
0N/A
0N/A /**
0N/A * @throws InvalidObjectException unconditionally
0N/A **/
0N/A private void readObjectNoData() throws InvalidObjectException {
0N/A throw new InvalidObjectException("no data in stream; class: " +
0N/A this.getClass().getName());
0N/A }
0N/A
0N/A /**
0N/A * Returns the method hash for the specified method. Subsequent calls
0N/A * to "getMethodHash" passing the same method argument should be faster
0N/A * since this method caches internally the result of the method to
0N/A * method hash mapping. The method hash is calculated using the
0N/A * "computeMethodHash" method.
0N/A *
0N/A * @param method the remote method
0N/A * @return the method hash for the specified method
0N/A */
0N/A private static long getMethodHash(Method method) {
0N/A return methodToHash_Maps.get(method.getDeclaringClass()).get(method);
0N/A }
0N/A
0N/A /**
0N/A * A weak hash map, mapping classes to weak hash maps that map
0N/A * method objects to method hashes.
0N/A **/
0N/A private static class MethodToHash_Maps
0N/A extends WeakClassHashMap<Map<Method,Long>>
0N/A {
0N/A MethodToHash_Maps() {}
0N/A
0N/A protected Map<Method,Long> computeValue(Class<?> remoteClass) {
0N/A return new WeakHashMap<Method,Long>() {
0N/A public synchronized Long get(Object key) {
0N/A Long hash = super.get(key);
0N/A if (hash == null) {
0N/A Method method = (Method) key;
0N/A hash = Util.computeMethodHash(method);
0N/A put(method, hash);
0N/A }
0N/A return hash;
0N/A }
0N/A };
0N/A }
0N/A }
0N/A}