Print this page
Split |
Close |
Expand all |
Collapse all |
--- old/src/share/classes/sun/rmi/server/Util.java
+++ new/src/share/classes/sun/rmi/server/Util.java
1 1 /*
2 2 * Copyright (c) 1996, 2008, Oracle and/or its affiliates. All rights reserved.
3 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 4 *
5 5 * This code is free software; you can redistribute it and/or modify it
6 6 * under the terms of the GNU General Public License version 2 only, as
7 7 * published by the Free Software Foundation. Oracle designates this
8 8 * particular file as subject to the "Classpath" exception as provided
9 9 * by Oracle in the LICENSE file that accompanied this code.
10 10 *
11 11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 14 * version 2 for more details (a copy is included in the LICENSE file that
15 15 * accompanied this code).
16 16 *
17 17 * You should have received a copy of the GNU General Public License version
18 18 * 2 along with this work; if not, write to the Free Software Foundation,
19 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 20 *
21 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 22 * or visit www.oracle.com if you need additional information or have any
23 23 * questions.
24 24 */
25 25 package sun.rmi.server;
26 26
27 27 import java.io.ByteArrayOutputStream;
28 28 import java.io.IOException;
29 29 import java.io.DataOutputStream;
30 30 import java.lang.reflect.Constructor;
31 31 import java.lang.reflect.InvocationHandler;
32 32 import java.lang.reflect.InvocationTargetException;
33 33 import java.lang.reflect.Proxy;
34 34 import java.lang.reflect.Method;
35 35 import java.rmi.Remote;
36 36 import java.rmi.RemoteException;
37 37 import java.rmi.StubNotFoundException;
38 38 import java.rmi.registry.Registry;
39 39 import java.rmi.server.LogStream;
40 40 import java.rmi.server.ObjID;
41 41 import java.rmi.server.RMIClientSocketFactory;
42 42 import java.rmi.server.RemoteObjectInvocationHandler;
43 43 import java.rmi.server.RemoteRef;
44 44 import java.rmi.server.RemoteStub;
45 45 import java.rmi.server.Skeleton;
46 46 import java.rmi.server.SkeletonNotFoundException;
47 47 import java.security.AccessController;
48 48 import java.security.MessageDigest;
49 49 import java.security.DigestOutputStream;
50 50 import java.security.NoSuchAlgorithmException;
51 51 import java.util.ArrayList;
52 52 import java.util.Collections;
53 53 import java.util.Map;
54 54 import java.util.WeakHashMap;
55 55 import sun.rmi.registry.RegistryImpl;
56 56 import sun.rmi.runtime.Log;
57 57 import sun.rmi.transport.LiveRef;
58 58 import sun.rmi.transport.tcp.TCPEndpoint;
59 59 import sun.security.action.GetBooleanAction;
60 60 import sun.security.action.GetPropertyAction;
61 61
62 62 /**
63 63 * A utility class with static methods for creating stubs/proxies and
64 64 * skeletons for remote objects.
65 65 */
66 66 public final class Util {
67 67
68 68 /** "server" package log level */
69 69 static final int logLevel = LogStream.parseLevel(
70 70 AccessController.doPrivileged(
71 71 new GetPropertyAction("sun.rmi.server.logLevel")));
72 72
73 73 /** server reference log */
74 74 public static final Log serverRefLog =
75 75 Log.getLog("sun.rmi.server.ref", "transport", Util.logLevel);
76 76
77 77 /** cached value of property java.rmi.server.ignoreStubClasses */
78 78 private static final boolean ignoreStubClasses =
79 79 AccessController.doPrivileged(
80 80 new GetBooleanAction("java.rmi.server.ignoreStubClasses")).
81 81 booleanValue();
82 82
83 83 /** cache of impl classes that have no corresponding stub class */
84 84 private static final Map<Class<?>, Void> withoutStubs =
85 85 Collections.synchronizedMap(new WeakHashMap<Class<?>, Void>(11));
86 86
87 87 /** parameter types for stub constructor */
88 88 private static final Class[] stubConsParamTypes = { RemoteRef.class };
89 89
90 90 private Util() {
91 91 }
92 92
93 93 /**
94 94 * Returns a proxy for the specified implClass.
95 95 *
96 96 * If both of the following criteria is satisfied, a dynamic proxy for
97 97 * the specified implClass is returned (otherwise a RemoteStub instance
98 98 * for the specified implClass is returned):
99 99 *
100 100 * a) either the property java.rmi.server.ignoreStubClasses is true or
101 101 * a pregenerated stub class does not exist for the impl class, and
102 102 * b) forceStubUse is false.
103 103 *
104 104 * If the above criteria are satisfied, this method constructs a
105 105 * dynamic proxy instance (that implements the remote interfaces of
106 106 * implClass) constructed with a RemoteObjectInvocationHandler instance
107 107 * constructed with the clientRef.
108 108 *
109 109 * Otherwise, this method loads the pregenerated stub class (which
110 110 * extends RemoteStub and implements the remote interfaces of
111 111 * implClass) and constructs an instance of the pregenerated stub
↓ open down ↓ |
111 lines elided |
↑ open up ↑ |
112 112 * class with the clientRef.
113 113 *
114 114 * @param implClass the class to obtain remote interfaces from
115 115 * @param clientRef the remote ref to use in the invocation handler
116 116 * @param forceStubUse if true, forces creation of a RemoteStub
117 117 * @throws IllegalArgumentException if implClass implements illegal
118 118 * remote interfaces
119 119 * @throws StubNotFoundException if problem locating/creating stub or
120 120 * creating the dynamic proxy instance
121 121 **/
122 - public static Remote createProxy(Class implClass,
122 + public static Remote createProxy(Class<?> implClass,
123 123 RemoteRef clientRef,
124 124 boolean forceStubUse)
125 125 throws StubNotFoundException
126 126 {
127 - Class remoteClass;
127 + Class<?> remoteClass;
128 128
129 129 try {
130 130 remoteClass = getRemoteClass(implClass);
131 131 } catch (ClassNotFoundException ex ) {
132 132 throw new StubNotFoundException(
133 133 "object does not implement a remote interface: " +
134 134 implClass.getName());
135 135 }
136 136
137 137 if (forceStubUse ||
138 138 !(ignoreStubClasses || !stubClassExists(remoteClass)))
139 139 {
140 140 return createStub(remoteClass, clientRef);
141 141 }
142 142
143 143 ClassLoader loader = implClass.getClassLoader();
144 144 Class[] interfaces = getRemoteInterfaces(implClass);
145 145 InvocationHandler handler =
146 146 new RemoteObjectInvocationHandler(clientRef);
147 147
148 148 /* REMIND: private remote interfaces? */
149 149
150 150 try {
151 151 return (Remote) Proxy.newProxyInstance(loader,
152 152 interfaces,
153 153 handler);
154 154 } catch (IllegalArgumentException e) {
↓ open down ↓ |
17 lines elided |
↑ open up ↑ |
155 155 throw new StubNotFoundException("unable to create proxy", e);
156 156 }
157 157 }
158 158
159 159 /**
160 160 * Returns true if a stub class for the given impl class can be loaded,
161 161 * otherwise returns false.
162 162 *
163 163 * @param remoteClass the class to obtain remote interfaces from
164 164 */
165 - private static boolean stubClassExists(Class remoteClass) {
165 + private static boolean stubClassExists(Class<?> remoteClass) {
166 166 if (!withoutStubs.containsKey(remoteClass)) {
167 167 try {
168 168 Class.forName(remoteClass.getName() + "_Stub",
169 169 false,
170 170 remoteClass.getClassLoader());
171 171 return true;
172 172
173 173 } catch (ClassNotFoundException cnfe) {
174 174 withoutStubs.put(remoteClass, null);
175 175 }
176 176 }
177 177 return false;
178 178 }
179 179
180 180 /*
181 181 * Returns the class/superclass that implements the remote interface.
182 182 * @throws ClassNotFoundException if no class is found to have a
183 183 * remote interface
184 184 */
185 - private static Class getRemoteClass(Class cl)
185 + private static Class<?> getRemoteClass(Class<?> cl)
186 186 throws ClassNotFoundException
187 187 {
188 188 while (cl != null) {
189 - Class[] interfaces = cl.getInterfaces();
189 + Class<?>[] interfaces = cl.getInterfaces();
190 190 for (int i = interfaces.length -1; i >= 0; i--) {
191 191 if (Remote.class.isAssignableFrom(interfaces[i]))
192 192 return cl; // this class implements remote object
193 193 }
194 194 cl = cl.getSuperclass();
195 195 }
196 196 throw new ClassNotFoundException(
197 197 "class does not implement java.rmi.Remote");
198 198 }
199 199
200 200 /**
201 201 * Returns an array containing the remote interfaces implemented
202 202 * by the given class.
203 203 *
204 204 * @param remoteClass the class to obtain remote interfaces from
205 205 * @throws IllegalArgumentException if remoteClass implements
206 206 * any illegal remote interfaces
207 207 * @throws NullPointerException if remoteClass is null
208 208 */
209 - private static Class[] getRemoteInterfaces(Class remoteClass) {
210 - ArrayList<Class<?>> list = new ArrayList<Class<?>>();
209 + private static Class<?>[] getRemoteInterfaces(Class<?> remoteClass) {
210 + ArrayList<Class<?>> list = new ArrayList<>();
211 211 getRemoteInterfaces(list, remoteClass);
212 212 return list.toArray(new Class<?>[list.size()]);
213 213 }
214 214
215 215 /**
216 216 * Fills the given array list with the remote interfaces implemented
217 217 * by the given class.
218 218 *
219 219 * @throws IllegalArgumentException if the specified class implements
220 220 * any illegal remote interfaces
221 221 * @throws NullPointerException if the specified class or list is null
222 222 */
223 - private static void getRemoteInterfaces(ArrayList<Class<?>> list, Class cl) {
224 - Class superclass = cl.getSuperclass();
223 + private static void getRemoteInterfaces(ArrayList<Class<?>> list, Class<?> cl) {
224 + Class<?> superclass = cl.getSuperclass();
225 225 if (superclass != null) {
226 226 getRemoteInterfaces(list, superclass);
227 227 }
228 228
229 - Class[] interfaces = cl.getInterfaces();
229 + Class<?>[] interfaces = cl.getInterfaces();
230 230 for (int i = 0; i < interfaces.length; i++) {
231 - Class intf = interfaces[i];
231 + Class<?> intf = interfaces[i];
232 232 /*
233 233 * If it is a remote interface (if it extends from
234 234 * java.rmi.Remote) and is not already in the list,
235 235 * then add the interface to the list.
236 236 */
237 237 if (Remote.class.isAssignableFrom(intf)) {
238 238 if (!(list.contains(intf))) {
239 239 Method[] methods = intf.getMethods();
240 240 for (int j = 0; j < methods.length; j++) {
241 241 checkMethod(methods[j]);
242 242 }
243 243 list.add(intf);
244 244 }
245 245 }
246 246 }
247 247 }
248 248
249 249 /**
250 250 * Verifies that the supplied method has at least one declared exception
251 251 * type that is RemoteException or one of its superclasses. If not,
252 252 * then this method throws IllegalArgumentException.
253 253 *
254 254 * @throws IllegalArgumentException if m is an illegal remote method
255 255 */
256 256 private static void checkMethod(Method m) {
257 257 Class<?>[] ex = m.getExceptionTypes();
258 258 for (int i = 0; i < ex.length; i++) {
259 259 if (ex[i].isAssignableFrom(RemoteException.class))
260 260 return;
261 261 }
262 262 throw new IllegalArgumentException(
263 263 "illegal remote method encountered: " + m);
264 264 }
↓ open down ↓ |
23 lines elided |
↑ open up ↑ |
265 265
266 266 /**
267 267 * Creates a RemoteStub instance for the specified class, constructed
268 268 * with the specified RemoteRef. The supplied class must be the most
269 269 * derived class in the remote object's superclass chain that
270 270 * implements a remote interface. The stub class name is the name of
271 271 * the specified remoteClass with the suffix "_Stub". The loading of
272 272 * the stub class is initiated from class loader of the specified class
273 273 * (which may be the bootstrap class loader).
274 274 **/
275 - private static RemoteStub createStub(Class remoteClass, RemoteRef ref)
275 + private static RemoteStub createStub(Class<?> remoteClass, RemoteRef ref)
276 276 throws StubNotFoundException
277 277 {
278 278 String stubname = remoteClass.getName() + "_Stub";
279 279
280 280 /* Make sure to use the local stub loader for the stub classes.
281 281 * When loaded by the local loader the load path can be
282 282 * propagated to remote clients, by the MarshalOutputStream/InStream
283 283 * pickle methods
284 284 */
285 285 try {
286 286 Class<?> stubcl =
287 287 Class.forName(stubname, false, remoteClass.getClassLoader());
288 - Constructor cons = stubcl.getConstructor(stubConsParamTypes);
288 + Constructor<?> cons = stubcl.getConstructor(stubConsParamTypes);
289 289 return (RemoteStub) cons.newInstance(new Object[] { ref });
290 290
291 291 } catch (ClassNotFoundException e) {
292 292 throw new StubNotFoundException(
293 293 "Stub class not found: " + stubname, e);
294 294 } catch (NoSuchMethodException e) {
295 295 throw new StubNotFoundException(
296 296 "Stub class missing constructor: " + stubname, e);
297 297 } catch (InstantiationException e) {
298 298 throw new StubNotFoundException(
299 299 "Can't create instance of stub class: " + stubname, e);
300 300 } catch (IllegalAccessException e) {
301 301 throw new StubNotFoundException(
302 302 "Stub class constructor not public: " + stubname, e);
303 303 } catch (InvocationTargetException e) {
304 304 throw new StubNotFoundException(
305 305 "Exception creating instance of stub class: " + stubname, e);
306 306 } catch (ClassCastException e) {
307 307 throw new StubNotFoundException(
↓ open down ↓ |
9 lines elided |
↑ open up ↑ |
308 308 "Stub class not instance of RemoteStub: " + stubname, e);
309 309 }
310 310 }
311 311
312 312 /**
313 313 * Locate and return the Skeleton for the specified remote object
314 314 */
315 315 static Skeleton createSkeleton(Remote object)
316 316 throws SkeletonNotFoundException
317 317 {
318 - Class cl;
318 + Class<?> cl;
319 319 try {
320 320 cl = getRemoteClass(object.getClass());
321 321 } catch (ClassNotFoundException ex ) {
322 322 throw new SkeletonNotFoundException(
323 323 "object does not implement a remote interface: " +
324 324 object.getClass().getName());
325 325 }
326 326
327 327 // now try to load the skeleton based ont he name of the class
328 328 String skelname = cl.getName() + "_Skel";
329 329 try {
330 - Class skelcl = Class.forName(skelname, false, cl.getClassLoader());
330 + Class<?> skelcl = Class.forName(skelname, false, cl.getClassLoader());
331 331
332 332 return (Skeleton)skelcl.newInstance();
333 333 } catch (ClassNotFoundException ex) {
334 334 throw new SkeletonNotFoundException("Skeleton class not found: " +
335 335 skelname, ex);
336 336 } catch (InstantiationException ex) {
337 337 throw new SkeletonNotFoundException("Can't create skeleton: " +
338 338 skelname, ex);
339 339 } catch (IllegalAccessException ex) {
340 340 throw new SkeletonNotFoundException("No public constructor: " +
341 341 skelname, ex);
342 342 } catch (ClassCastException ex) {
343 343 throw new SkeletonNotFoundException(
344 344 "Skeleton not of correct class: " + skelname, ex);
345 345 }
346 346 }
347 347
348 348 /**
349 349 * Compute the "method hash" of a remote method. The method hash
350 350 * is a long containing the first 64 bits of the SHA digest from
351 351 * the UTF encoded string of the method name and descriptor.
352 352 */
353 353 public static long computeMethodHash(Method m) {
354 354 long hash = 0;
355 355 ByteArrayOutputStream sink = new ByteArrayOutputStream(127);
356 356 try {
357 357 MessageDigest md = MessageDigest.getInstance("SHA");
358 358 DataOutputStream out = new DataOutputStream(
359 359 new DigestOutputStream(sink, md));
360 360
361 361 String s = getMethodNameAndDescriptor(m);
362 362 if (serverRefLog.isLoggable(Log.VERBOSE)) {
363 363 serverRefLog.log(Log.VERBOSE,
364 364 "string used for method hash: \"" + s + "\"");
365 365 }
366 366 out.writeUTF(s);
367 367
368 368 // use only the first 64 bits of the digest for the hash
369 369 out.flush();
370 370 byte hasharray[] = md.digest();
371 371 for (int i = 0; i < Math.min(8, hasharray.length); i++) {
372 372 hash += ((long) (hasharray[i] & 0xFF)) << (i * 8);
373 373 }
374 374 } catch (IOException ignore) {
375 375 /* can't happen, but be deterministic anyway. */
376 376 hash = -1;
377 377 } catch (NoSuchAlgorithmException complain) {
378 378 throw new SecurityException(complain.getMessage());
379 379 }
380 380 return hash;
381 381 }
382 382
383 383 /**
↓ open down ↓ |
43 lines elided |
↑ open up ↑ |
384 384 * Return a string consisting of the given method's name followed by
385 385 * its "method descriptor", as appropriate for use in the computation
386 386 * of the "method hash".
387 387 *
388 388 * See section 4.3.3 of The Java Virtual Machine Specification for
389 389 * the definition of a "method descriptor".
390 390 */
391 391 private static String getMethodNameAndDescriptor(Method m) {
392 392 StringBuffer desc = new StringBuffer(m.getName());
393 393 desc.append('(');
394 - Class[] paramTypes = m.getParameterTypes();
394 + Class<?>[] paramTypes = m.getParameterTypes();
395 395 for (int i = 0; i < paramTypes.length; i++) {
396 396 desc.append(getTypeDescriptor(paramTypes[i]));
397 397 }
398 398 desc.append(')');
399 - Class returnType = m.getReturnType();
399 + Class<?> returnType = m.getReturnType();
400 400 if (returnType == void.class) { // optimization: handle void here
401 401 desc.append('V');
402 402 } else {
403 403 desc.append(getTypeDescriptor(returnType));
404 404 }
405 405 return desc.toString();
406 406 }
407 407
408 408 /**
409 409 * Get the descriptor of a particular type, as appropriate for either
410 410 * a parameter or return type in a method descriptor.
411 411 */
412 - private static String getTypeDescriptor(Class type) {
412 + private static String getTypeDescriptor(Class<?> type) {
413 413 if (type.isPrimitive()) {
414 414 if (type == int.class) {
415 415 return "I";
416 416 } else if (type == boolean.class) {
417 417 return "Z";
418 418 } else if (type == byte.class) {
419 419 return "B";
420 420 } else if (type == char.class) {
421 421 return "C";
422 422 } else if (type == short.class) {
423 423 return "S";
424 424 } else if (type == long.class) {
425 425 return "J";
426 426 } else if (type == float.class) {
427 427 return "F";
428 428 } else if (type == double.class) {
429 429 return "D";
430 430 } else if (type == void.class) {
431 431 return "V";
432 432 } else {
433 433 throw new Error("unrecognized primitive type: " + type);
434 434 }
435 435 } else if (type.isArray()) {
436 436 /*
437 437 * According to JLS 20.3.2, the getName() method on Class does
438 438 * return the VM type descriptor format for array classes (only);
439 439 * using that should be quicker than the otherwise obvious code:
440 440 *
441 441 * return "[" + getTypeDescriptor(type.getComponentType());
442 442 */
443 443 return type.getName().replace('.', '/');
444 444 } else {
445 445 return "L" + type.getName().replace('.', '/') + ";";
446 446 }
↓ open down ↓ |
24 lines elided |
↑ open up ↑ |
447 447 }
448 448
449 449 /**
450 450 * Returns the binary name of the given type without package
451 451 * qualification. Nested types are treated no differently from
452 452 * top-level types, so for a nested type, the returned name will
453 453 * still be qualified with the simple name of its enclosing
454 454 * top-level type (and perhaps other enclosing types), the
455 455 * separator will be '$', etc.
456 456 **/
457 - public static String getUnqualifiedName(Class c) {
457 + public static String getUnqualifiedName(Class<?> c) {
458 458 String binaryName = c.getName();
459 459 return binaryName.substring(binaryName.lastIndexOf('.') + 1);
460 460 }
461 461 }
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX