102 * b) forceStubUse is false.
103 *
104 * If the above criteria are satisfied, this method constructs a
105 * dynamic proxy instance (that implements the remote interfaces of
106 * implClass) constructed with a RemoteObjectInvocationHandler instance
107 * constructed with the clientRef.
108 *
109 * Otherwise, this method loads the pregenerated stub class (which
110 * extends RemoteStub and implements the remote interfaces of
111 * implClass) and constructs an instance of the pregenerated stub
112 * class with the clientRef.
113 *
114 * @param implClass the class to obtain remote interfaces from
115 * @param clientRef the remote ref to use in the invocation handler
116 * @param forceStubUse if true, forces creation of a RemoteStub
117 * @throws IllegalArgumentException if implClass implements illegal
118 * remote interfaces
119 * @throws StubNotFoundException if problem locating/creating stub or
120 * creating the dynamic proxy instance
121 **/
122 public static Remote createProxy(Class implClass,
123 RemoteRef clientRef,
124 boolean forceStubUse)
125 throws StubNotFoundException
126 {
127 Class remoteClass;
128
129 try {
130 remoteClass = getRemoteClass(implClass);
131 } catch (ClassNotFoundException ex ) {
132 throw new StubNotFoundException(
133 "object does not implement a remote interface: " +
134 implClass.getName());
135 }
136
137 if (forceStubUse ||
138 !(ignoreStubClasses || !stubClassExists(remoteClass)))
139 {
140 return createStub(remoteClass, clientRef);
141 }
142
143 ClassLoader loader = implClass.getClassLoader();
144 Class[] interfaces = getRemoteInterfaces(implClass);
145 InvocationHandler handler =
146 new RemoteObjectInvocationHandler(clientRef);
147
148 /* REMIND: private remote interfaces? */
149
150 try {
151 return (Remote) Proxy.newProxyInstance(loader,
152 interfaces,
153 handler);
154 } catch (IllegalArgumentException e) {
155 throw new StubNotFoundException("unable to create proxy", e);
156 }
157 }
158
159 /**
160 * Returns true if a stub class for the given impl class can be loaded,
161 * otherwise returns false.
162 *
163 * @param remoteClass the class to obtain remote interfaces from
164 */
165 private static boolean stubClassExists(Class remoteClass) {
166 if (!withoutStubs.containsKey(remoteClass)) {
167 try {
168 Class.forName(remoteClass.getName() + "_Stub",
169 false,
170 remoteClass.getClassLoader());
171 return true;
172
173 } catch (ClassNotFoundException cnfe) {
174 withoutStubs.put(remoteClass, null);
175 }
176 }
177 return false;
178 }
179
180 /*
181 * Returns the class/superclass that implements the remote interface.
182 * @throws ClassNotFoundException if no class is found to have a
183 * remote interface
184 */
185 private static Class getRemoteClass(Class cl)
186 throws ClassNotFoundException
187 {
188 while (cl != null) {
189 Class[] interfaces = cl.getInterfaces();
190 for (int i = interfaces.length -1; i >= 0; i--) {
191 if (Remote.class.isAssignableFrom(interfaces[i]))
192 return cl; // this class implements remote object
193 }
194 cl = cl.getSuperclass();
195 }
196 throw new ClassNotFoundException(
197 "class does not implement java.rmi.Remote");
198 }
199
200 /**
201 * Returns an array containing the remote interfaces implemented
202 * by the given class.
203 *
204 * @param remoteClass the class to obtain remote interfaces from
205 * @throws IllegalArgumentException if remoteClass implements
206 * any illegal remote interfaces
207 * @throws NullPointerException if remoteClass is null
208 */
209 private static Class[] getRemoteInterfaces(Class remoteClass) {
210 ArrayList<Class<?>> list = new ArrayList<Class<?>>();
211 getRemoteInterfaces(list, remoteClass);
212 return list.toArray(new Class<?>[list.size()]);
213 }
214
215 /**
216 * Fills the given array list with the remote interfaces implemented
217 * by the given class.
218 *
219 * @throws IllegalArgumentException if the specified class implements
220 * any illegal remote interfaces
221 * @throws NullPointerException if the specified class or list is null
222 */
223 private static void getRemoteInterfaces(ArrayList<Class<?>> list, Class cl) {
224 Class superclass = cl.getSuperclass();
225 if (superclass != null) {
226 getRemoteInterfaces(list, superclass);
227 }
228
229 Class[] interfaces = cl.getInterfaces();
230 for (int i = 0; i < interfaces.length; i++) {
231 Class intf = interfaces[i];
232 /*
233 * If it is a remote interface (if it extends from
234 * java.rmi.Remote) and is not already in the list,
235 * then add the interface to the list.
236 */
237 if (Remote.class.isAssignableFrom(intf)) {
238 if (!(list.contains(intf))) {
239 Method[] methods = intf.getMethods();
240 for (int j = 0; j < methods.length; j++) {
241 checkMethod(methods[j]);
242 }
243 list.add(intf);
244 }
245 }
246 }
247 }
248
249 /**
250 * Verifies that the supplied method has at least one declared exception
251 * type that is RemoteException or one of its superclasses. If not,
255 */
256 private static void checkMethod(Method m) {
257 Class<?>[] ex = m.getExceptionTypes();
258 for (int i = 0; i < ex.length; i++) {
259 if (ex[i].isAssignableFrom(RemoteException.class))
260 return;
261 }
262 throw new IllegalArgumentException(
263 "illegal remote method encountered: " + m);
264 }
265
266 /**
267 * Creates a RemoteStub instance for the specified class, constructed
268 * with the specified RemoteRef. The supplied class must be the most
269 * derived class in the remote object's superclass chain that
270 * implements a remote interface. The stub class name is the name of
271 * the specified remoteClass with the suffix "_Stub". The loading of
272 * the stub class is initiated from class loader of the specified class
273 * (which may be the bootstrap class loader).
274 **/
275 private static RemoteStub createStub(Class remoteClass, RemoteRef ref)
276 throws StubNotFoundException
277 {
278 String stubname = remoteClass.getName() + "_Stub";
279
280 /* Make sure to use the local stub loader for the stub classes.
281 * When loaded by the local loader the load path can be
282 * propagated to remote clients, by the MarshalOutputStream/InStream
283 * pickle methods
284 */
285 try {
286 Class<?> stubcl =
287 Class.forName(stubname, false, remoteClass.getClassLoader());
288 Constructor cons = stubcl.getConstructor(stubConsParamTypes);
289 return (RemoteStub) cons.newInstance(new Object[] { ref });
290
291 } catch (ClassNotFoundException e) {
292 throw new StubNotFoundException(
293 "Stub class not found: " + stubname, e);
294 } catch (NoSuchMethodException e) {
295 throw new StubNotFoundException(
296 "Stub class missing constructor: " + stubname, e);
297 } catch (InstantiationException e) {
298 throw new StubNotFoundException(
299 "Can't create instance of stub class: " + stubname, e);
300 } catch (IllegalAccessException e) {
301 throw new StubNotFoundException(
302 "Stub class constructor not public: " + stubname, e);
303 } catch (InvocationTargetException e) {
304 throw new StubNotFoundException(
305 "Exception creating instance of stub class: " + stubname, e);
306 } catch (ClassCastException e) {
307 throw new StubNotFoundException(
308 "Stub class not instance of RemoteStub: " + stubname, e);
309 }
310 }
311
312 /**
313 * Locate and return the Skeleton for the specified remote object
314 */
315 static Skeleton createSkeleton(Remote object)
316 throws SkeletonNotFoundException
317 {
318 Class cl;
319 try {
320 cl = getRemoteClass(object.getClass());
321 } catch (ClassNotFoundException ex ) {
322 throw new SkeletonNotFoundException(
323 "object does not implement a remote interface: " +
324 object.getClass().getName());
325 }
326
327 // now try to load the skeleton based ont he name of the class
328 String skelname = cl.getName() + "_Skel";
329 try {
330 Class skelcl = Class.forName(skelname, false, cl.getClassLoader());
331
332 return (Skeleton)skelcl.newInstance();
333 } catch (ClassNotFoundException ex) {
334 throw new SkeletonNotFoundException("Skeleton class not found: " +
335 skelname, ex);
336 } catch (InstantiationException ex) {
337 throw new SkeletonNotFoundException("Can't create skeleton: " +
338 skelname, ex);
339 } catch (IllegalAccessException ex) {
340 throw new SkeletonNotFoundException("No public constructor: " +
341 skelname, ex);
342 } catch (ClassCastException ex) {
343 throw new SkeletonNotFoundException(
344 "Skeleton not of correct class: " + skelname, ex);
345 }
346 }
347
348 /**
349 * Compute the "method hash" of a remote method. The method hash
350 * is a long containing the first 64 bits of the SHA digest from
374 } catch (IOException ignore) {
375 /* can't happen, but be deterministic anyway. */
376 hash = -1;
377 } catch (NoSuchAlgorithmException complain) {
378 throw new SecurityException(complain.getMessage());
379 }
380 return hash;
381 }
382
383 /**
384 * Return a string consisting of the given method's name followed by
385 * its "method descriptor", as appropriate for use in the computation
386 * of the "method hash".
387 *
388 * See section 4.3.3 of The Java Virtual Machine Specification for
389 * the definition of a "method descriptor".
390 */
391 private static String getMethodNameAndDescriptor(Method m) {
392 StringBuffer desc = new StringBuffer(m.getName());
393 desc.append('(');
394 Class[] paramTypes = m.getParameterTypes();
395 for (int i = 0; i < paramTypes.length; i++) {
396 desc.append(getTypeDescriptor(paramTypes[i]));
397 }
398 desc.append(')');
399 Class returnType = m.getReturnType();
400 if (returnType == void.class) { // optimization: handle void here
401 desc.append('V');
402 } else {
403 desc.append(getTypeDescriptor(returnType));
404 }
405 return desc.toString();
406 }
407
408 /**
409 * Get the descriptor of a particular type, as appropriate for either
410 * a parameter or return type in a method descriptor.
411 */
412 private static String getTypeDescriptor(Class type) {
413 if (type.isPrimitive()) {
414 if (type == int.class) {
415 return "I";
416 } else if (type == boolean.class) {
417 return "Z";
418 } else if (type == byte.class) {
419 return "B";
420 } else if (type == char.class) {
421 return "C";
422 } else if (type == short.class) {
423 return "S";
424 } else if (type == long.class) {
425 return "J";
426 } else if (type == float.class) {
427 return "F";
428 } else if (type == double.class) {
429 return "D";
430 } else if (type == void.class) {
431 return "V";
432 } else {
437 * According to JLS 20.3.2, the getName() method on Class does
438 * return the VM type descriptor format for array classes (only);
439 * using that should be quicker than the otherwise obvious code:
440 *
441 * return "[" + getTypeDescriptor(type.getComponentType());
442 */
443 return type.getName().replace('.', '/');
444 } else {
445 return "L" + type.getName().replace('.', '/') + ";";
446 }
447 }
448
449 /**
450 * Returns the binary name of the given type without package
451 * qualification. Nested types are treated no differently from
452 * top-level types, so for a nested type, the returned name will
453 * still be qualified with the simple name of its enclosing
454 * top-level type (and perhaps other enclosing types), the
455 * separator will be '$', etc.
456 **/
457 public static String getUnqualifiedName(Class c) {
458 String binaryName = c.getName();
459 return binaryName.substring(binaryName.lastIndexOf('.') + 1);
460 }
461 }
|
102 * b) forceStubUse is false.
103 *
104 * If the above criteria are satisfied, this method constructs a
105 * dynamic proxy instance (that implements the remote interfaces of
106 * implClass) constructed with a RemoteObjectInvocationHandler instance
107 * constructed with the clientRef.
108 *
109 * Otherwise, this method loads the pregenerated stub class (which
110 * extends RemoteStub and implements the remote interfaces of
111 * implClass) and constructs an instance of the pregenerated stub
112 * class with the clientRef.
113 *
114 * @param implClass the class to obtain remote interfaces from
115 * @param clientRef the remote ref to use in the invocation handler
116 * @param forceStubUse if true, forces creation of a RemoteStub
117 * @throws IllegalArgumentException if implClass implements illegal
118 * remote interfaces
119 * @throws StubNotFoundException if problem locating/creating stub or
120 * creating the dynamic proxy instance
121 **/
122 public static Remote createProxy(Class<?> implClass,
123 RemoteRef clientRef,
124 boolean forceStubUse)
125 throws StubNotFoundException
126 {
127 Class<?> remoteClass;
128
129 try {
130 remoteClass = getRemoteClass(implClass);
131 } catch (ClassNotFoundException ex ) {
132 throw new StubNotFoundException(
133 "object does not implement a remote interface: " +
134 implClass.getName());
135 }
136
137 if (forceStubUse ||
138 !(ignoreStubClasses || !stubClassExists(remoteClass)))
139 {
140 return createStub(remoteClass, clientRef);
141 }
142
143 ClassLoader loader = implClass.getClassLoader();
144 Class[] interfaces = getRemoteInterfaces(implClass);
145 InvocationHandler handler =
146 new RemoteObjectInvocationHandler(clientRef);
147
148 /* REMIND: private remote interfaces? */
149
150 try {
151 return (Remote) Proxy.newProxyInstance(loader,
152 interfaces,
153 handler);
154 } catch (IllegalArgumentException e) {
155 throw new StubNotFoundException("unable to create proxy", e);
156 }
157 }
158
159 /**
160 * Returns true if a stub class for the given impl class can be loaded,
161 * otherwise returns false.
162 *
163 * @param remoteClass the class to obtain remote interfaces from
164 */
165 private static boolean stubClassExists(Class<?> remoteClass) {
166 if (!withoutStubs.containsKey(remoteClass)) {
167 try {
168 Class.forName(remoteClass.getName() + "_Stub",
169 false,
170 remoteClass.getClassLoader());
171 return true;
172
173 } catch (ClassNotFoundException cnfe) {
174 withoutStubs.put(remoteClass, null);
175 }
176 }
177 return false;
178 }
179
180 /*
181 * Returns the class/superclass that implements the remote interface.
182 * @throws ClassNotFoundException if no class is found to have a
183 * remote interface
184 */
185 private static Class<?> getRemoteClass(Class<?> cl)
186 throws ClassNotFoundException
187 {
188 while (cl != null) {
189 Class<?>[] interfaces = cl.getInterfaces();
190 for (int i = interfaces.length -1; i >= 0; i--) {
191 if (Remote.class.isAssignableFrom(interfaces[i]))
192 return cl; // this class implements remote object
193 }
194 cl = cl.getSuperclass();
195 }
196 throw new ClassNotFoundException(
197 "class does not implement java.rmi.Remote");
198 }
199
200 /**
201 * Returns an array containing the remote interfaces implemented
202 * by the given class.
203 *
204 * @param remoteClass the class to obtain remote interfaces from
205 * @throws IllegalArgumentException if remoteClass implements
206 * any illegal remote interfaces
207 * @throws NullPointerException if remoteClass is null
208 */
209 private static Class<?>[] getRemoteInterfaces(Class<?> remoteClass) {
210 ArrayList<Class<?>> list = new ArrayList<>();
211 getRemoteInterfaces(list, remoteClass);
212 return list.toArray(new Class<?>[list.size()]);
213 }
214
215 /**
216 * Fills the given array list with the remote interfaces implemented
217 * by the given class.
218 *
219 * @throws IllegalArgumentException if the specified class implements
220 * any illegal remote interfaces
221 * @throws NullPointerException if the specified class or list is null
222 */
223 private static void getRemoteInterfaces(ArrayList<Class<?>> list, Class<?> cl) {
224 Class<?> superclass = cl.getSuperclass();
225 if (superclass != null) {
226 getRemoteInterfaces(list, superclass);
227 }
228
229 Class<?>[] interfaces = cl.getInterfaces();
230 for (int i = 0; i < interfaces.length; i++) {
231 Class<?> intf = interfaces[i];
232 /*
233 * If it is a remote interface (if it extends from
234 * java.rmi.Remote) and is not already in the list,
235 * then add the interface to the list.
236 */
237 if (Remote.class.isAssignableFrom(intf)) {
238 if (!(list.contains(intf))) {
239 Method[] methods = intf.getMethods();
240 for (int j = 0; j < methods.length; j++) {
241 checkMethod(methods[j]);
242 }
243 list.add(intf);
244 }
245 }
246 }
247 }
248
249 /**
250 * Verifies that the supplied method has at least one declared exception
251 * type that is RemoteException or one of its superclasses. If not,
255 */
256 private static void checkMethod(Method m) {
257 Class<?>[] ex = m.getExceptionTypes();
258 for (int i = 0; i < ex.length; i++) {
259 if (ex[i].isAssignableFrom(RemoteException.class))
260 return;
261 }
262 throw new IllegalArgumentException(
263 "illegal remote method encountered: " + m);
264 }
265
266 /**
267 * Creates a RemoteStub instance for the specified class, constructed
268 * with the specified RemoteRef. The supplied class must be the most
269 * derived class in the remote object's superclass chain that
270 * implements a remote interface. The stub class name is the name of
271 * the specified remoteClass with the suffix "_Stub". The loading of
272 * the stub class is initiated from class loader of the specified class
273 * (which may be the bootstrap class loader).
274 **/
275 private static RemoteStub createStub(Class<?> remoteClass, RemoteRef ref)
276 throws StubNotFoundException
277 {
278 String stubname = remoteClass.getName() + "_Stub";
279
280 /* Make sure to use the local stub loader for the stub classes.
281 * When loaded by the local loader the load path can be
282 * propagated to remote clients, by the MarshalOutputStream/InStream
283 * pickle methods
284 */
285 try {
286 Class<?> stubcl =
287 Class.forName(stubname, false, remoteClass.getClassLoader());
288 Constructor<?> cons = stubcl.getConstructor(stubConsParamTypes);
289 return (RemoteStub) cons.newInstance(new Object[] { ref });
290
291 } catch (ClassNotFoundException e) {
292 throw new StubNotFoundException(
293 "Stub class not found: " + stubname, e);
294 } catch (NoSuchMethodException e) {
295 throw new StubNotFoundException(
296 "Stub class missing constructor: " + stubname, e);
297 } catch (InstantiationException e) {
298 throw new StubNotFoundException(
299 "Can't create instance of stub class: " + stubname, e);
300 } catch (IllegalAccessException e) {
301 throw new StubNotFoundException(
302 "Stub class constructor not public: " + stubname, e);
303 } catch (InvocationTargetException e) {
304 throw new StubNotFoundException(
305 "Exception creating instance of stub class: " + stubname, e);
306 } catch (ClassCastException e) {
307 throw new StubNotFoundException(
308 "Stub class not instance of RemoteStub: " + stubname, e);
309 }
310 }
311
312 /**
313 * Locate and return the Skeleton for the specified remote object
314 */
315 static Skeleton createSkeleton(Remote object)
316 throws SkeletonNotFoundException
317 {
318 Class<?> cl;
319 try {
320 cl = getRemoteClass(object.getClass());
321 } catch (ClassNotFoundException ex ) {
322 throw new SkeletonNotFoundException(
323 "object does not implement a remote interface: " +
324 object.getClass().getName());
325 }
326
327 // now try to load the skeleton based ont he name of the class
328 String skelname = cl.getName() + "_Skel";
329 try {
330 Class<?> skelcl = Class.forName(skelname, false, cl.getClassLoader());
331
332 return (Skeleton)skelcl.newInstance();
333 } catch (ClassNotFoundException ex) {
334 throw new SkeletonNotFoundException("Skeleton class not found: " +
335 skelname, ex);
336 } catch (InstantiationException ex) {
337 throw new SkeletonNotFoundException("Can't create skeleton: " +
338 skelname, ex);
339 } catch (IllegalAccessException ex) {
340 throw new SkeletonNotFoundException("No public constructor: " +
341 skelname, ex);
342 } catch (ClassCastException ex) {
343 throw new SkeletonNotFoundException(
344 "Skeleton not of correct class: " + skelname, ex);
345 }
346 }
347
348 /**
349 * Compute the "method hash" of a remote method. The method hash
350 * is a long containing the first 64 bits of the SHA digest from
374 } catch (IOException ignore) {
375 /* can't happen, but be deterministic anyway. */
376 hash = -1;
377 } catch (NoSuchAlgorithmException complain) {
378 throw new SecurityException(complain.getMessage());
379 }
380 return hash;
381 }
382
383 /**
384 * Return a string consisting of the given method's name followed by
385 * its "method descriptor", as appropriate for use in the computation
386 * of the "method hash".
387 *
388 * See section 4.3.3 of The Java Virtual Machine Specification for
389 * the definition of a "method descriptor".
390 */
391 private static String getMethodNameAndDescriptor(Method m) {
392 StringBuffer desc = new StringBuffer(m.getName());
393 desc.append('(');
394 Class<?>[] paramTypes = m.getParameterTypes();
395 for (int i = 0; i < paramTypes.length; i++) {
396 desc.append(getTypeDescriptor(paramTypes[i]));
397 }
398 desc.append(')');
399 Class<?> returnType = m.getReturnType();
400 if (returnType == void.class) { // optimization: handle void here
401 desc.append('V');
402 } else {
403 desc.append(getTypeDescriptor(returnType));
404 }
405 return desc.toString();
406 }
407
408 /**
409 * Get the descriptor of a particular type, as appropriate for either
410 * a parameter or return type in a method descriptor.
411 */
412 private static String getTypeDescriptor(Class<?> type) {
413 if (type.isPrimitive()) {
414 if (type == int.class) {
415 return "I";
416 } else if (type == boolean.class) {
417 return "Z";
418 } else if (type == byte.class) {
419 return "B";
420 } else if (type == char.class) {
421 return "C";
422 } else if (type == short.class) {
423 return "S";
424 } else if (type == long.class) {
425 return "J";
426 } else if (type == float.class) {
427 return "F";
428 } else if (type == double.class) {
429 return "D";
430 } else if (type == void.class) {
431 return "V";
432 } else {
437 * According to JLS 20.3.2, the getName() method on Class does
438 * return the VM type descriptor format for array classes (only);
439 * using that should be quicker than the otherwise obvious code:
440 *
441 * return "[" + getTypeDescriptor(type.getComponentType());
442 */
443 return type.getName().replace('.', '/');
444 } else {
445 return "L" + type.getName().replace('.', '/') + ";";
446 }
447 }
448
449 /**
450 * Returns the binary name of the given type without package
451 * qualification. Nested types are treated no differently from
452 * top-level types, so for a nested type, the returned name will
453 * still be qualified with the simple name of its enclosing
454 * top-level type (and perhaps other enclosing types), the
455 * separator will be '$', etc.
456 **/
457 public static String getUnqualifiedName(Class<?> c) {
458 String binaryName = c.getName();
459 return binaryName.substring(binaryName.lastIndexOf('.') + 1);
460 }
461 }
|