001 /* RemoteObjectInvocationHandler.java -- RMI stub replacement.
002 Copyright (C) 2005 Free Software Foundation, Inc.
003
004 This file is part of GNU Classpath.
005
006 GNU Classpath is free software; you can redistribute it and/or modify
007 it under the terms of the GNU General Public License as published by
008 the Free Software Foundation; either version 2, or (at your option)
009 any later version.
010
011 GNU Classpath is distributed in the hope that it will be useful, but
012 WITHOUT ANY WARRANTY; without even the implied warranty of
013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 General Public License for more details.
015
016 You should have received a copy of the GNU General Public License
017 along with GNU Classpath; see the file COPYING. If not, write to the
018 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019 02110-1301 USA.
020
021 Linking this library statically or dynamically with other modules is
022 making a combined work based on this library. Thus, the terms and
023 conditions of the GNU General Public License cover the whole
024 combination.
025
026 As a special exception, the copyright holders of this library give you
027 permission to link this library with independent modules to produce an
028 executable, regardless of the license terms of these independent
029 modules, and to copy and distribute the resulting executable under
030 terms of your choice, provided that you also meet, for each linked
031 independent module, the terms and conditions of the license of that
032 module. An independent module is a module which is not derived from
033 or based on this library. If you modify this library, you may extend
034 this exception to your version of the library, but you are not
035 obligated to do so. If you do not wish to do so, delete this
036 exception statement from your version. */
037
038
039 package java.rmi.server;
040
041 import gnu.java.rmi.server.RMIHashes;
042
043 import java.io.Serializable;
044 import java.lang.reflect.InvocationHandler;
045 import java.lang.reflect.Method;
046 import java.lang.reflect.Proxy;
047 import java.rmi.Remote;
048 import java.rmi.RemoteException;
049 import java.rmi.UnexpectedException;
050 import java.rmi.registry.Registry;
051 import java.rmi.server.RemoteObject;
052 import java.rmi.server.RemoteRef;
053 import java.rmi.server.UnicastRemoteObject;
054 import java.util.Hashtable;
055
056 /**
057 * Together with dynamic proxy instance, this class replaces the generated RMI
058 * stub (*_Stub) classes that (following 1.5 specification) should be no longer
059 * required. It is unusual to use the instances of this class directly in the
060 * user program. Such instances are automatically created and returned by
061 * {@link Registry} or {@link UnicastRemoteObject} methods if the remote
062 * reference is known but the corresponding stub class is not accessible.
063 *
064 * @see Registry#lookup
065 *
066 * @author Audrius Meskauskas (AudriusA@Bioinformatics.org)
067 */
068 public class RemoteObjectInvocationHandler extends RemoteObject implements
069 InvocationHandler, Remote, Serializable
070 {
071 /**
072 * Use the jdk 1.5 SUID for interoperability.
073 */
074 static final long serialVersionUID = 2L;
075
076 /**
077 * The RMI method hash codes, computed once as described in the section 8.3
078 * of the Java Remote Method Invocation (RMI) Specification.
079 */
080 static Hashtable methodHashCodes = new Hashtable();
081
082 /**
083 * The empty class array to define parameters of .hashCode and .toString.
084 */
085 static final Class[] noArgsC = new Class[0];
086
087 /**
088 * The class array to define parameters of .equals
089 */
090 static final Class[] anObjectC = new Class[] { Object.class };
091
092 /**
093 * The empty object array to replace null when no args are passed.
094 */
095 static final Object[] noArgs = new Object[0];
096
097 /**
098 * Construct the remote invocation handler that forwards calls to the given
099 * remote object.
100 *
101 * @param reference the reference to the remote object where the method
102 * calls should be forwarded.
103 */
104 public RemoteObjectInvocationHandler(RemoteRef reference)
105 {
106 super(reference);
107 }
108
109 /**
110 * Invoke the remote method. When the known method is invoked on a created RMI
111 * stub proxy class, the call is delivered to this method and then transferred
112 * to the {@link RemoteRef#invoke(Remote, Method, Object[], long)} of the
113 * remote reference that was passed in constructor. The methods are handled as
114 * following:
115 * <ul>
116 * <li> The toString() method is delegated to the passed proxy instance.</li>
117 * <li>The .equals method only returns true if the passed object is an
118 * instance of proxy class and its invocation handler is equal to this
119 * invocation handles.</li>
120 * <li>The .hashCode returns the hashCode of this invocation handler (if the.</li>
121 * <li>All other methods are converted to remote calls and forwarded to the
122 * remote reference. </li>
123 * </ul>
124 *
125 * @param proxyInstance
126 * the instance of the proxy stub
127 * @param method
128 * the method being invoked
129 * @param parameters
130 * the method parameters
131 * @return the method return value, returned by RemoteRef.invoke
132 * @throws IllegalAccessException
133 * if the passed proxy instance does not implement Remote interface.
134 * @throws UnexpectedException
135 * if remote call throws some exception, not listed in the
136 * <code>throws</code> clause of the method being called.
137 * @throws Throwable
138 * that is thrown by remote call, if that exception is listend in
139 * the <code>throws</code> clause of the method being called.
140 */
141 public Object invoke(Object proxyInstance, Method method, Object[] parameters)
142 throws Throwable
143 {
144 if (!(proxyInstance instanceof Remote))
145 {
146 String name = proxyInstance == null ? "null"
147 : proxyInstance.getClass().getName();
148 throw new IllegalAccessException(name + " does not implement "
149 + Remote.class.getName());
150 }
151
152 if (parameters == null)
153 parameters = noArgs;
154
155 String name = method.getName();
156 switch (name.charAt(0))
157 {
158 case 'e':
159 if (parameters.length == 1 && name.equals("equals")
160 && method.getParameterTypes()[0].equals(Object.class))
161 {
162 if (parameters[0] instanceof Proxy)
163 {
164 Object handler = Proxy.getInvocationHandler(parameters[0]);
165 if (handler == null)
166 return Boolean.FALSE;
167 else
168 return handler.equals(this) ? Boolean.TRUE : Boolean.FALSE;
169 }
170 else
171 return Boolean.FALSE;
172 }
173 break;
174 case 'h':
175 if (parameters.length == 0 && name.equals("hashCode"))
176 {
177 int hashC = Proxy.getInvocationHandler(proxyInstance).hashCode();
178 return new Integer(hashC);
179 }
180 break;
181 case 't':
182 if (parameters.length == 0 && name.equals("toString"))
183 return "Proxy stub:"+ref.remoteToString();
184 break;
185 default:
186 break;
187 }
188
189 Long hash = (Long) methodHashCodes.get(method);
190 if (hash == null)
191 {
192 hash = new Long(RMIHashes.getMethodHash(method));
193 methodHashCodes.put(method, hash);
194 }
195
196 try
197 {
198 return getRef().invoke((Remote) proxyInstance, method, parameters,
199 hash.longValue());
200 }
201 catch (RuntimeException exception)
202 {
203 // RuntimeException is always supported.
204 throw exception;
205 }
206 catch (RemoteException exception)
207 {
208 // All remote methods can throw RemoteException.
209 throw exception;
210 }
211 catch (Error exception)
212 {
213 throw exception;
214 }
215 catch (Exception exception)
216 {
217 Class[] exceptions = method.getExceptionTypes();
218 Class exceptionClass = exception.getClass();
219
220 for (int i = 0; i < exceptions.length; i++)
221 {
222 if (exceptions[i].equals(exceptionClass))
223 throw exception;
224 }
225 throw new UnexpectedException(method.getName(), exception);
226 }
227 }
228
229 }