author | rriggs |
Thu, 16 Mar 2017 16:16:31 -0400 | |
changeset 45984 | 75fef64e21fa |
parent 41231 | 3f8807f6fec3 |
child 45989 | e4f526fd8e09 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
45984 | 2 |
* Copyright (c) 1996, 2017, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.rmi.server; |
|
27 |
||
28 |
import java.io.IOException; |
|
29 |
import java.io.ObjectInput; |
|
41231
3f8807f6fec3
8165806: UnicastServerRef support to export an object with a filter
rriggs
parents:
37584
diff
changeset
|
30 |
import java.io.ObjectInputFilter; |
3f8807f6fec3
8165806: UnicastServerRef support to export an object with a filter
rriggs
parents:
37584
diff
changeset
|
31 |
import java.io.ObjectInputStream; |
2 | 32 |
import java.io.ObjectOutput; |
37584 | 33 |
import java.io.ObjectStreamClass; |
2 | 34 |
import java.lang.reflect.InvocationTargetException; |
35 |
import java.lang.reflect.Method; |
|
36 |
import java.rmi.MarshalException; |
|
37 |
import java.rmi.Remote; |
|
38 |
import java.rmi.RemoteException; |
|
39 |
import java.rmi.ServerError; |
|
40 |
import java.rmi.ServerException; |
|
41 |
import java.rmi.UnmarshalException; |
|
42 |
import java.rmi.server.ExportException; |
|
43 |
import java.rmi.server.RemoteCall; |
|
44 |
import java.rmi.server.RemoteRef; |
|
45 |
import java.rmi.server.RemoteStub; |
|
46 |
import java.rmi.server.ServerNotActiveException; |
|
47 |
import java.rmi.server.ServerRef; |
|
48 |
import java.rmi.server.Skeleton; |
|
49 |
import java.rmi.server.SkeletonNotFoundException; |
|
50 |
import java.security.AccessController; |
|
51 |
import java.security.PrivilegedAction; |
|
52 |
import java.util.Collections; |
|
53 |
import java.util.Date; |
|
54 |
import java.util.HashMap; |
|
55 |
import java.util.Map; |
|
56 |
import java.util.WeakHashMap; |
|
37584 | 57 |
import java.util.concurrent.atomic.AtomicInteger; |
2 | 58 |
import sun.rmi.runtime.Log; |
59 |
import sun.rmi.transport.LiveRef; |
|
45984 | 60 |
import sun.rmi.transport.StreamRemoteCall; |
2 | 61 |
import sun.rmi.transport.Target; |
62 |
import sun.rmi.transport.tcp.TCPTransport; |
|
63 |
||
64 |
/** |
|
65 |
* UnicastServerRef implements the remote reference layer server-side |
|
66 |
* behavior for remote objects exported with the "UnicastRef" reference |
|
67 |
* type. |
|
41231
3f8807f6fec3
8165806: UnicastServerRef support to export an object with a filter
rriggs
parents:
37584
diff
changeset
|
68 |
* If an {@link ObjectInputFilter ObjectInputFilter} is supplied it is |
3f8807f6fec3
8165806: UnicastServerRef support to export an object with a filter
rriggs
parents:
37584
diff
changeset
|
69 |
* invoked during deserialization to filter the arguments, |
3f8807f6fec3
8165806: UnicastServerRef support to export an object with a filter
rriggs
parents:
37584
diff
changeset
|
70 |
* otherwise the default filter of {@link ObjectInputStream ObjectInputStream} |
3f8807f6fec3
8165806: UnicastServerRef support to export an object with a filter
rriggs
parents:
37584
diff
changeset
|
71 |
* applies. |
2 | 72 |
* |
73 |
* @author Ann Wollrath |
|
74 |
* @author Roger Riggs |
|
75 |
* @author Peter Jones |
|
76 |
*/ |
|
19211 | 77 |
@SuppressWarnings("deprecation") |
2 | 78 |
public class UnicastServerRef extends UnicastRef |
79 |
implements ServerRef, Dispatcher |
|
80 |
{ |
|
81 |
/** value of server call log property */ |
|
82 |
public static final boolean logCalls = AccessController.doPrivileged( |
|
23333
b0af2c7c8c91
8035808: Eliminate dependency to GetPropertyAction and other sun.security.action convenient classes
mchung
parents:
21655
diff
changeset
|
83 |
(PrivilegedAction<Boolean>) () -> Boolean.getBoolean("java.rmi.server.logCalls")); |
2 | 84 |
|
85 |
/** server call log */ |
|
86 |
public static final Log callLog = |
|
87 |
Log.getLog("sun.rmi.server.call", "RMI", logCalls); |
|
88 |
||
89 |
// use serialVersionUID from JDK 1.2.2 for interoperability |
|
90 |
private static final long serialVersionUID = -7384275867073752268L; |
|
91 |
||
92 |
/** flag to enable writing exceptions to System.err */ |
|
93 |
private static final boolean wantExceptionLog = |
|
23333
b0af2c7c8c91
8035808: Eliminate dependency to GetPropertyAction and other sun.security.action convenient classes
mchung
parents:
21655
diff
changeset
|
94 |
AccessController.doPrivileged((PrivilegedAction<Boolean>) () -> |
b0af2c7c8c91
8035808: Eliminate dependency to GetPropertyAction and other sun.security.action convenient classes
mchung
parents:
21655
diff
changeset
|
95 |
Boolean.getBoolean("sun.rmi.server.exceptionTrace")); |
2 | 96 |
|
97 |
private boolean forceStubUse = false; |
|
98 |
||
99 |
/** |
|
100 |
* flag to remove server-side stack traces before marshalling |
|
101 |
* exceptions thrown by remote invocations to this VM |
|
102 |
*/ |
|
103 |
private static final boolean suppressStackTraces = |
|
23333
b0af2c7c8c91
8035808: Eliminate dependency to GetPropertyAction and other sun.security.action convenient classes
mchung
parents:
21655
diff
changeset
|
104 |
AccessController.doPrivileged((PrivilegedAction<Boolean>) () -> |
b0af2c7c8c91
8035808: Eliminate dependency to GetPropertyAction and other sun.security.action convenient classes
mchung
parents:
21655
diff
changeset
|
105 |
Boolean.getBoolean("sun.rmi.server.suppressStackTraces")); |
2 | 106 |
|
107 |
/** |
|
108 |
* skeleton to dispatch remote calls through, for 1.1 stub protocol |
|
109 |
* (may be null if stub class only uses 1.2 stub protocol) |
|
110 |
*/ |
|
111 |
private transient Skeleton skel; |
|
112 |
||
41231
3f8807f6fec3
8165806: UnicastServerRef support to export an object with a filter
rriggs
parents:
37584
diff
changeset
|
113 |
// The ObjectInputFilter for checking the invocation arguments |
3f8807f6fec3
8165806: UnicastServerRef support to export an object with a filter
rriggs
parents:
37584
diff
changeset
|
114 |
private final transient ObjectInputFilter filter; |
3f8807f6fec3
8165806: UnicastServerRef support to export an object with a filter
rriggs
parents:
37584
diff
changeset
|
115 |
|
2 | 116 |
/** maps method hash to Method object for each remote method */ |
117 |
private transient Map<Long,Method> hashToMethod_Map = null; |
|
118 |
||
119 |
/** |
|
120 |
* A weak hash map, mapping classes to hash maps that map method |
|
121 |
* hashes to method objects. |
|
122 |
**/ |
|
123 |
private static final WeakClassHashMap<Map<Long,Method>> hashToMethod_Maps = |
|
124 |
new HashToMethod_Maps(); |
|
125 |
||
126 |
/** cache of impl classes that have no corresponding skeleton class */ |
|
127 |
private static final Map<Class<?>,?> withoutSkeletons = |
|
128 |
Collections.synchronizedMap(new WeakHashMap<Class<?>,Void>()); |
|
129 |
||
37584 | 130 |
private final AtomicInteger methodCallIDCount = new AtomicInteger(0); |
131 |
||
2 | 132 |
/** |
133 |
* Create a new (empty) Unicast server remote reference. |
|
41231
3f8807f6fec3
8165806: UnicastServerRef support to export an object with a filter
rriggs
parents:
37584
diff
changeset
|
134 |
* The filter is null to defer to the default ObjectInputStream filter, if any. |
2 | 135 |
*/ |
136 |
public UnicastServerRef() { |
|
41231
3f8807f6fec3
8165806: UnicastServerRef support to export an object with a filter
rriggs
parents:
37584
diff
changeset
|
137 |
this.filter = null; |
2 | 138 |
} |
139 |
||
140 |
/** |
|
141 |
* Construct a Unicast server remote reference for a specified |
|
142 |
* liveRef. |
|
41231
3f8807f6fec3
8165806: UnicastServerRef support to export an object with a filter
rriggs
parents:
37584
diff
changeset
|
143 |
* The filter is null to defer to the default ObjectInputStream filter, if any. |
2 | 144 |
*/ |
145 |
public UnicastServerRef(LiveRef ref) { |
|
146 |
super(ref); |
|
41231
3f8807f6fec3
8165806: UnicastServerRef support to export an object with a filter
rriggs
parents:
37584
diff
changeset
|
147 |
this.filter = null; |
3f8807f6fec3
8165806: UnicastServerRef support to export an object with a filter
rriggs
parents:
37584
diff
changeset
|
148 |
} |
3f8807f6fec3
8165806: UnicastServerRef support to export an object with a filter
rriggs
parents:
37584
diff
changeset
|
149 |
|
3f8807f6fec3
8165806: UnicastServerRef support to export an object with a filter
rriggs
parents:
37584
diff
changeset
|
150 |
/** |
3f8807f6fec3
8165806: UnicastServerRef support to export an object with a filter
rriggs
parents:
37584
diff
changeset
|
151 |
* Construct a Unicast server remote reference for a specified |
3f8807f6fec3
8165806: UnicastServerRef support to export an object with a filter
rriggs
parents:
37584
diff
changeset
|
152 |
* liveRef and filter. |
3f8807f6fec3
8165806: UnicastServerRef support to export an object with a filter
rriggs
parents:
37584
diff
changeset
|
153 |
*/ |
3f8807f6fec3
8165806: UnicastServerRef support to export an object with a filter
rriggs
parents:
37584
diff
changeset
|
154 |
public UnicastServerRef(LiveRef ref, ObjectInputFilter filter) { |
3f8807f6fec3
8165806: UnicastServerRef support to export an object with a filter
rriggs
parents:
37584
diff
changeset
|
155 |
super(ref); |
3f8807f6fec3
8165806: UnicastServerRef support to export an object with a filter
rriggs
parents:
37584
diff
changeset
|
156 |
this.filter = filter; |
2 | 157 |
} |
158 |
||
159 |
/** |
|
160 |
* Construct a Unicast server remote reference to be exported |
|
161 |
* on the specified port. |
|
162 |
*/ |
|
163 |
public UnicastServerRef(int port) { |
|
164 |
super(new LiveRef(port)); |
|
41231
3f8807f6fec3
8165806: UnicastServerRef support to export an object with a filter
rriggs
parents:
37584
diff
changeset
|
165 |
this.filter = null; |
2 | 166 |
} |
167 |
||
168 |
/** |
|
169 |
* Constructs a UnicastServerRef to be exported on an |
|
170 |
* anonymous port (i.e., 0) and that uses a pregenerated stub class |
|
171 |
* (NOT a dynamic proxy instance) if 'forceStubUse' is 'true'. |
|
172 |
* |
|
173 |
* This constructor is only called by the method |
|
174 |
* UnicastRemoteObject.exportObject(Remote) passing 'true' for |
|
175 |
* 'forceStubUse'. The UnicastRemoteObject.exportObject(Remote) method |
|
176 |
* returns RemoteStub, so it must ensure that the stub for the |
|
177 |
* exported object is an instance of a pregenerated stub class that |
|
178 |
* extends RemoteStub (instead of an instance of a dynamic proxy class |
|
179 |
* which is not an instance of RemoteStub). |
|
180 |
**/ |
|
181 |
public UnicastServerRef(boolean forceStubUse) { |
|
182 |
this(0); |
|
183 |
this.forceStubUse = forceStubUse; |
|
184 |
} |
|
185 |
||
186 |
/** |
|
187 |
* With the addition of support for dynamic proxies as stubs, this |
|
188 |
* method is obsolete because it returns RemoteStub instead of the more |
|
189 |
* general Remote. It should not be called. It sets the |
|
190 |
* 'forceStubUse' flag to true so that the stub for the exported object |
|
191 |
* is forced to be an instance of the pregenerated stub class, which |
|
192 |
* extends RemoteStub. |
|
193 |
* |
|
194 |
* Export this object, create the skeleton and stubs for this |
|
195 |
* dispatcher. Create a stub based on the type of the impl, |
|
196 |
* initialize it with the appropriate remote reference. Create the |
|
197 |
* target defined by the impl, dispatcher (this) and stub. |
|
198 |
* Export that target via the Ref. |
|
199 |
**/ |
|
200 |
public RemoteStub exportObject(Remote impl, Object data) |
|
201 |
throws RemoteException |
|
202 |
{ |
|
203 |
forceStubUse = true; |
|
204 |
return (RemoteStub) exportObject(impl, data, false); |
|
205 |
} |
|
206 |
||
207 |
/** |
|
208 |
* Export this object, create the skeleton and stubs for this |
|
209 |
* dispatcher. Create a stub based on the type of the impl, |
|
210 |
* initialize it with the appropriate remote reference. Create the |
|
211 |
* target defined by the impl, dispatcher (this) and stub. |
|
212 |
* Export that target via the Ref. |
|
213 |
*/ |
|
214 |
public Remote exportObject(Remote impl, Object data, |
|
215 |
boolean permanent) |
|
216 |
throws RemoteException |
|
217 |
{ |
|
12040
558b0e0d5910
7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents:
10912
diff
changeset
|
218 |
Class<?> implClass = impl.getClass(); |
2 | 219 |
Remote stub; |
220 |
||
221 |
try { |
|
222 |
stub = Util.createProxy(implClass, getClientRef(), forceStubUse); |
|
223 |
} catch (IllegalArgumentException e) { |
|
224 |
throw new ExportException( |
|
225 |
"remote object implements illegal remote interface", e); |
|
226 |
} |
|
227 |
if (stub instanceof RemoteStub) { |
|
228 |
setSkeleton(impl); |
|
229 |
} |
|
230 |
||
231 |
Target target = |
|
232 |
new Target(impl, this, stub, ref.getObjID(), permanent); |
|
233 |
ref.exportObject(target); |
|
234 |
hashToMethod_Map = hashToMethod_Maps.get(implClass); |
|
235 |
return stub; |
|
236 |
} |
|
237 |
||
238 |
/** |
|
239 |
* Return the hostname of the current client. When called from a |
|
240 |
* thread actively handling a remote method invocation the |
|
241 |
* hostname of the client is returned. |
|
242 |
* @exception ServerNotActiveException If called outside of servicing |
|
243 |
* a remote method invocation. |
|
244 |
*/ |
|
245 |
public String getClientHost() throws ServerNotActiveException { |
|
246 |
return TCPTransport.getClientHost(); |
|
247 |
} |
|
248 |
||
249 |
/** |
|
250 |
* Discovers and sets the appropriate skeleton for the impl. |
|
251 |
*/ |
|
252 |
public void setSkeleton(Remote impl) throws RemoteException { |
|
253 |
if (!withoutSkeletons.containsKey(impl.getClass())) { |
|
254 |
try { |
|
255 |
skel = Util.createSkeleton(impl); |
|
256 |
} catch (SkeletonNotFoundException e) { |
|
257 |
/* |
|
258 |
* Ignore exception for skeleton class not found, because a |
|
259 |
* skeleton class is not necessary with the 1.2 stub protocol. |
|
260 |
* Remember that this impl's class does not have a skeleton |
|
261 |
* class so we don't waste time searching for it again. |
|
262 |
*/ |
|
263 |
withoutSkeletons.put(impl.getClass(), null); |
|
264 |
} |
|
265 |
} |
|
266 |
} |
|
267 |
||
268 |
/** |
|
269 |
* Call to dispatch to the remote object (on the server side). |
|
270 |
* The up-call to the server and the marshalling of return result |
|
271 |
* (or exception) should be handled before returning from this |
|
272 |
* method. |
|
273 |
* @param obj the target remote object for the call |
|
274 |
* @param call the "remote call" from which operation and |
|
275 |
* method arguments can be obtained. |
|
276 |
* @exception IOException If unable to marshal return result or |
|
277 |
* release input or output streams |
|
278 |
*/ |
|
279 |
public void dispatch(Remote obj, RemoteCall call) throws IOException { |
|
280 |
// positive operation number in 1.1 stubs; |
|
281 |
// negative version number in 1.2 stubs and beyond... |
|
282 |
int num; |
|
283 |
long op; |
|
284 |
||
285 |
try { |
|
286 |
// read remote call header |
|
287 |
ObjectInput in; |
|
288 |
try { |
|
289 |
in = call.getInputStream(); |
|
290 |
num = in.readInt(); |
|
291 |
if (num >= 0) { |
|
292 |
if (skel != null) { |
|
293 |
oldDispatch(obj, call, num); |
|
294 |
return; |
|
295 |
} else { |
|
296 |
throw new UnmarshalException( |
|
297 |
"skeleton class not found but required " + |
|
298 |
"for client version"); |
|
299 |
} |
|
300 |
} |
|
301 |
op = in.readLong(); |
|
302 |
} catch (Exception readEx) { |
|
303 |
throw new UnmarshalException("error unmarshalling call header", |
|
304 |
readEx); |
|
305 |
} |
|
306 |
||
307 |
/* |
|
308 |
* Since only system classes (with null class loaders) will be on |
|
309 |
* the execution stack during parameter unmarshalling for the 1.2 |
|
310 |
* stub protocol, tell the MarshalInputStream not to bother trying |
|
311 |
* to resolve classes using its superclasses's default method of |
|
312 |
* consulting the first non-null class loader on the stack. |
|
313 |
*/ |
|
314 |
MarshalInputStream marshalStream = (MarshalInputStream) in; |
|
315 |
marshalStream.skipDefaultResolveClass(); |
|
316 |
||
317 |
Method method = hashToMethod_Map.get(op); |
|
318 |
if (method == null) { |
|
319 |
throw new UnmarshalException("unrecognized method hash: " + |
|
320 |
"method not supported by remote object"); |
|
321 |
} |
|
322 |
||
323 |
// if calls are being logged, write out object id and operation |
|
324 |
logCall(obj, method); |
|
325 |
||
326 |
// unmarshal parameters |
|
37584 | 327 |
Object[] params = null; |
2 | 328 |
|
329 |
try { |
|
330 |
unmarshalCustomCallData(in); |
|
37584 | 331 |
params = unmarshalParameters(obj, method, marshalStream); |
45984 | 332 |
} catch (java.io.IOException | ClassNotFoundException e) { |
333 |
// disable saving any refs in the inputStream for GC |
|
334 |
((StreamRemoteCall) call).discardPendingRefs(); |
|
2 | 335 |
throw new UnmarshalException( |
336 |
"error unmarshalling arguments", e); |
|
337 |
} finally { |
|
338 |
call.releaseInputStream(); |
|
339 |
} |
|
340 |
||
341 |
// make upcall on remote object |
|
342 |
Object result; |
|
343 |
try { |
|
344 |
result = method.invoke(obj, params); |
|
345 |
} catch (InvocationTargetException e) { |
|
346 |
throw e.getTargetException(); |
|
347 |
} |
|
348 |
||
349 |
// marshal return value |
|
350 |
try { |
|
351 |
ObjectOutput out = call.getResultStream(true); |
|
12040
558b0e0d5910
7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents:
10912
diff
changeset
|
352 |
Class<?> rtype = method.getReturnType(); |
2 | 353 |
if (rtype != void.class) { |
354 |
marshalValue(rtype, result, out); |
|
355 |
} |
|
356 |
} catch (IOException ex) { |
|
357 |
throw new MarshalException("error marshalling return", ex); |
|
358 |
/* |
|
359 |
* This throw is problematic because when it is caught below, |
|
360 |
* we attempt to marshal it back to the client, but at this |
|
361 |
* point, a "normal return" has already been indicated, |
|
362 |
* so marshalling an exception will corrupt the stream. |
|
363 |
* This was the case with skeletons as well; there is no |
|
364 |
* immediately obvious solution without a protocol change. |
|
365 |
*/ |
|
366 |
} |
|
367 |
} catch (Throwable e) { |
|
368 |
logCallException(e); |
|
369 |
||
370 |
ObjectOutput out = call.getResultStream(false); |
|
371 |
if (e instanceof Error) { |
|
372 |
e = new ServerError( |
|
373 |
"Error occurred in server thread", (Error) e); |
|
374 |
} else if (e instanceof RemoteException) { |
|
375 |
e = new ServerException( |
|
376 |
"RemoteException occurred in server thread", |
|
377 |
(Exception) e); |
|
378 |
} |
|
379 |
if (suppressStackTraces) { |
|
380 |
clearStackTraces(e); |
|
381 |
} |
|
382 |
out.writeObject(e); |
|
383 |
} finally { |
|
384 |
call.releaseInputStream(); // in case skeleton doesn't |
|
385 |
call.releaseOutputStream(); |
|
386 |
} |
|
387 |
} |
|
388 |
||
41231
3f8807f6fec3
8165806: UnicastServerRef support to export an object with a filter
rriggs
parents:
37584
diff
changeset
|
389 |
/** |
3f8807f6fec3
8165806: UnicastServerRef support to export an object with a filter
rriggs
parents:
37584
diff
changeset
|
390 |
* Sets a filter for invocation arguments, if a filter has been set. |
3f8807f6fec3
8165806: UnicastServerRef support to export an object with a filter
rriggs
parents:
37584
diff
changeset
|
391 |
* Called by dispatch before the arguments are read. |
3f8807f6fec3
8165806: UnicastServerRef support to export an object with a filter
rriggs
parents:
37584
diff
changeset
|
392 |
*/ |
2 | 393 |
protected void unmarshalCustomCallData(ObjectInput in) |
41231
3f8807f6fec3
8165806: UnicastServerRef support to export an object with a filter
rriggs
parents:
37584
diff
changeset
|
394 |
throws IOException, ClassNotFoundException { |
3f8807f6fec3
8165806: UnicastServerRef support to export an object with a filter
rriggs
parents:
37584
diff
changeset
|
395 |
if (filter != null && |
3f8807f6fec3
8165806: UnicastServerRef support to export an object with a filter
rriggs
parents:
37584
diff
changeset
|
396 |
in instanceof ObjectInputStream) { |
3f8807f6fec3
8165806: UnicastServerRef support to export an object with a filter
rriggs
parents:
37584
diff
changeset
|
397 |
// Set the filter on the stream |
3f8807f6fec3
8165806: UnicastServerRef support to export an object with a filter
rriggs
parents:
37584
diff
changeset
|
398 |
ObjectInputStream ois = (ObjectInputStream) in; |
3f8807f6fec3
8165806: UnicastServerRef support to export an object with a filter
rriggs
parents:
37584
diff
changeset
|
399 |
|
3f8807f6fec3
8165806: UnicastServerRef support to export an object with a filter
rriggs
parents:
37584
diff
changeset
|
400 |
AccessController.doPrivileged((PrivilegedAction<Void>)() -> { |
3f8807f6fec3
8165806: UnicastServerRef support to export an object with a filter
rriggs
parents:
37584
diff
changeset
|
401 |
ois.setObjectInputFilter(filter); |
3f8807f6fec3
8165806: UnicastServerRef support to export an object with a filter
rriggs
parents:
37584
diff
changeset
|
402 |
return null; |
3f8807f6fec3
8165806: UnicastServerRef support to export an object with a filter
rriggs
parents:
37584
diff
changeset
|
403 |
}); |
3f8807f6fec3
8165806: UnicastServerRef support to export an object with a filter
rriggs
parents:
37584
diff
changeset
|
404 |
} |
3f8807f6fec3
8165806: UnicastServerRef support to export an object with a filter
rriggs
parents:
37584
diff
changeset
|
405 |
} |
2 | 406 |
|
407 |
/** |
|
408 |
* Handle server-side dispatch using the RMI 1.1 stub/skeleton |
|
409 |
* protocol, given a non-negative operation number that has |
|
410 |
* already been read from the call stream. |
|
411 |
* |
|
412 |
* @param obj the target remote object for the call |
|
413 |
* @param call the "remote call" from which operation and |
|
414 |
* method arguments can be obtained. |
|
415 |
* @param op the operation number |
|
416 |
* @exception IOException if unable to marshal return result or |
|
417 |
* release input or output streams |
|
418 |
*/ |
|
419 |
public void oldDispatch(Remote obj, RemoteCall call, int op) |
|
420 |
throws IOException |
|
421 |
{ |
|
422 |
long hash; // hash for matching stub with skeleton |
|
423 |
||
424 |
try { |
|
425 |
// read remote call header |
|
426 |
ObjectInput in; |
|
427 |
try { |
|
428 |
in = call.getInputStream(); |
|
10912 | 429 |
try { |
430 |
Class<?> clazz = Class.forName("sun.rmi.transport.DGCImpl_Skel"); |
|
431 |
if (clazz.isAssignableFrom(skel.getClass())) { |
|
432 |
((MarshalInputStream)in).useCodebaseOnly(); |
|
433 |
} |
|
434 |
} catch (ClassNotFoundException ignore) { } |
|
2 | 435 |
hash = in.readLong(); |
436 |
} catch (Exception readEx) { |
|
437 |
throw new UnmarshalException("error unmarshalling call header", |
|
438 |
readEx); |
|
439 |
} |
|
440 |
||
441 |
// if calls are being logged, write out object id and operation |
|
442 |
logCall(obj, skel.getOperations()[op]); |
|
443 |
unmarshalCustomCallData(in); |
|
444 |
// dispatch to skeleton for remote object |
|
445 |
skel.dispatch(obj, call, op, hash); |
|
446 |
||
447 |
} catch (Throwable e) { |
|
448 |
logCallException(e); |
|
449 |
||
450 |
ObjectOutput out = call.getResultStream(false); |
|
451 |
if (e instanceof Error) { |
|
452 |
e = new ServerError( |
|
453 |
"Error occurred in server thread", (Error) e); |
|
454 |
} else if (e instanceof RemoteException) { |
|
455 |
e = new ServerException( |
|
456 |
"RemoteException occurred in server thread", |
|
457 |
(Exception) e); |
|
458 |
} |
|
459 |
if (suppressStackTraces) { |
|
460 |
clearStackTraces(e); |
|
461 |
} |
|
462 |
out.writeObject(e); |
|
463 |
} finally { |
|
464 |
call.releaseInputStream(); // in case skeleton doesn't |
|
465 |
call.releaseOutputStream(); |
|
466 |
} |
|
467 |
} |
|
468 |
||
469 |
/** |
|
470 |
* Clear the stack trace of the given Throwable by replacing it with |
|
471 |
* an empty StackTraceElement array, and do the same for all of its |
|
472 |
* chained causative exceptions. |
|
473 |
*/ |
|
474 |
public static void clearStackTraces(Throwable t) { |
|
475 |
StackTraceElement[] empty = new StackTraceElement[0]; |
|
476 |
while (t != null) { |
|
477 |
t.setStackTrace(empty); |
|
478 |
t = t.getCause(); |
|
479 |
} |
|
480 |
} |
|
481 |
||
482 |
/** |
|
483 |
* Log the details of an incoming call. The method parameter is either of |
|
484 |
* type java.lang.reflect.Method or java.rmi.server.Operation. |
|
485 |
*/ |
|
486 |
private void logCall(Remote obj, Object method) { |
|
487 |
if (callLog.isLoggable(Log.VERBOSE)) { |
|
488 |
String clientHost; |
|
489 |
try { |
|
490 |
clientHost = getClientHost(); |
|
491 |
} catch (ServerNotActiveException snae) { |
|
492 |
clientHost = "(local)"; // shouldn't happen |
|
493 |
} |
|
494 |
callLog.log(Log.VERBOSE, "[" + clientHost + ": " + |
|
495 |
obj.getClass().getName() + |
|
496 |
ref.getObjID().toString() + ": " + |
|
497 |
method + "]"); |
|
498 |
} |
|
499 |
} |
|
500 |
||
501 |
/** |
|
502 |
* Log the exception detail of an incoming call. |
|
503 |
*/ |
|
504 |
private void logCallException(Throwable e) { |
|
505 |
// if calls are being logged, log them |
|
506 |
if (callLog.isLoggable(Log.BRIEF)) { |
|
507 |
String clientHost = ""; |
|
508 |
try { |
|
509 |
clientHost = "[" + getClientHost() + "] "; |
|
510 |
} catch (ServerNotActiveException snae) { |
|
511 |
} |
|
512 |
callLog.log(Log.BRIEF, clientHost + "exception: ", e); |
|
513 |
} |
|
514 |
||
515 |
// write exceptions (only) to System.err if desired |
|
516 |
if (wantExceptionLog) { |
|
517 |
java.io.PrintStream log = System.err; |
|
518 |
synchronized (log) { |
|
519 |
log.println(); |
|
520 |
log.println("Exception dispatching call to " + |
|
521 |
ref.getObjID() + " in thread \"" + |
|
522 |
Thread.currentThread().getName() + |
|
523 |
"\" at " + (new Date()) + ":"); |
|
524 |
e.printStackTrace(log); |
|
525 |
} |
|
526 |
} |
|
527 |
} |
|
528 |
||
529 |
/** |
|
530 |
* Returns the class of the ref type to be serialized. |
|
531 |
*/ |
|
532 |
public String getRefClass(ObjectOutput out) { |
|
533 |
return "UnicastServerRef"; |
|
534 |
} |
|
535 |
||
536 |
/** |
|
537 |
* Return the client remote reference for this remoteRef. |
|
538 |
* In the case of a client RemoteRef "this" is the answer. |
|
539 |
* For a server remote reference, a client side one will have to |
|
540 |
* found or created. |
|
541 |
*/ |
|
542 |
protected RemoteRef getClientRef() { |
|
543 |
return new UnicastRef(ref); |
|
544 |
} |
|
545 |
||
546 |
/** |
|
547 |
* Write out external representation for remote ref. |
|
548 |
*/ |
|
549 |
public void writeExternal(ObjectOutput out) throws IOException { |
|
550 |
} |
|
551 |
||
552 |
/** |
|
553 |
* Read in external representation for remote ref. |
|
554 |
* @exception ClassNotFoundException If the class for an object |
|
555 |
* being restored cannot be found. |
|
556 |
*/ |
|
557 |
public void readExternal(ObjectInput in) |
|
558 |
throws IOException, ClassNotFoundException |
|
559 |
{ |
|
560 |
// object is re-exported elsewhere (e.g., by UnicastRemoteObject) |
|
561 |
ref = null; |
|
562 |
skel = null; |
|
563 |
} |
|
564 |
||
565 |
||
566 |
/** |
|
567 |
* A weak hash map, mapping classes to hash maps that map method |
|
568 |
* hashes to method objects. |
|
569 |
**/ |
|
570 |
private static class HashToMethod_Maps |
|
571 |
extends WeakClassHashMap<Map<Long,Method>> |
|
572 |
{ |
|
573 |
HashToMethod_Maps() {} |
|
574 |
||
575 |
protected Map<Long,Method> computeValue(Class<?> remoteClass) { |
|
12040
558b0e0d5910
7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents:
10912
diff
changeset
|
576 |
Map<Long,Method> map = new HashMap<>(); |
2 | 577 |
for (Class<?> cl = remoteClass; |
578 |
cl != null; |
|
579 |
cl = cl.getSuperclass()) |
|
580 |
{ |
|
581 |
for (Class<?> intf : cl.getInterfaces()) { |
|
582 |
if (Remote.class.isAssignableFrom(intf)) { |
|
583 |
for (Method method : intf.getMethods()) { |
|
584 |
final Method m = method; |
|
585 |
/* |
|
586 |
* Set this Method object to override language |
|
587 |
* access checks so that the dispatcher can invoke |
|
588 |
* methods from non-public remote interfaces. |
|
589 |
*/ |
|
590 |
AccessController.doPrivileged( |
|
591 |
new PrivilegedAction<Void>() { |
|
592 |
public Void run() { |
|
593 |
m.setAccessible(true); |
|
594 |
return null; |
|
595 |
} |
|
596 |
}); |
|
597 |
map.put(Util.computeMethodHash(m), m); |
|
598 |
} |
|
599 |
} |
|
600 |
} |
|
601 |
} |
|
602 |
return map; |
|
603 |
} |
|
604 |
} |
|
37584 | 605 |
|
606 |
/** |
|
607 |
* Unmarshal parameters for the given method of the given instance over |
|
608 |
* the given marshalinputstream. Perform any necessary checks. |
|
609 |
*/ |
|
610 |
private Object[] unmarshalParameters(Object obj, Method method, MarshalInputStream in) |
|
611 |
throws IOException, ClassNotFoundException { |
|
612 |
return (obj instanceof DeserializationChecker) ? |
|
613 |
unmarshalParametersChecked((DeserializationChecker)obj, method, in) : |
|
614 |
unmarshalParametersUnchecked(method, in); |
|
615 |
} |
|
616 |
||
617 |
/** |
|
618 |
* Unmarshal parameters for the given method of the given instance over |
|
619 |
* the given marshalinputstream. Do not perform any additional checks. |
|
620 |
*/ |
|
621 |
private Object[] unmarshalParametersUnchecked(Method method, ObjectInput in) |
|
622 |
throws IOException, ClassNotFoundException { |
|
623 |
Class<?>[] types = method.getParameterTypes(); |
|
624 |
Object[] params = new Object[types.length]; |
|
625 |
for (int i = 0; i < types.length; i++) { |
|
626 |
params[i] = unmarshalValue(types[i], in); |
|
627 |
} |
|
628 |
return params; |
|
629 |
} |
|
630 |
||
631 |
/** |
|
632 |
* Unmarshal parameters for the given method of the given instance over |
|
633 |
* the given marshalinputstream. Do perform all additional checks. |
|
634 |
*/ |
|
635 |
private Object[] unmarshalParametersChecked( |
|
636 |
DeserializationChecker checker, |
|
637 |
Method method, MarshalInputStream in) |
|
638 |
throws IOException, ClassNotFoundException { |
|
639 |
int callID = methodCallIDCount.getAndIncrement(); |
|
640 |
MyChecker myChecker = new MyChecker(checker, method, callID); |
|
641 |
in.setStreamChecker(myChecker); |
|
642 |
try { |
|
643 |
Class<?>[] types = method.getParameterTypes(); |
|
644 |
Object[] values = new Object[types.length]; |
|
645 |
for (int i = 0; i < types.length; i++) { |
|
646 |
myChecker.setIndex(i); |
|
647 |
values[i] = unmarshalValue(types[i], in); |
|
648 |
} |
|
649 |
myChecker.end(callID); |
|
650 |
return values; |
|
651 |
} finally { |
|
652 |
in.setStreamChecker(null); |
|
653 |
} |
|
654 |
} |
|
655 |
||
656 |
private static class MyChecker implements MarshalInputStream.StreamChecker { |
|
657 |
private final DeserializationChecker descriptorCheck; |
|
658 |
private final Method method; |
|
659 |
private final int callID; |
|
660 |
private int parameterIndex; |
|
661 |
||
662 |
MyChecker(DeserializationChecker descriptorCheck, Method method, int callID) { |
|
663 |
this.descriptorCheck = descriptorCheck; |
|
664 |
this.method = method; |
|
665 |
this.callID = callID; |
|
666 |
} |
|
667 |
||
668 |
@Override |
|
669 |
public void validateDescriptor(ObjectStreamClass descriptor) { |
|
670 |
descriptorCheck.check(method, descriptor, parameterIndex, callID); |
|
671 |
} |
|
672 |
||
673 |
@Override |
|
674 |
public void checkProxyInterfaceNames(String[] ifaces) { |
|
675 |
descriptorCheck.checkProxyClass(method, ifaces, parameterIndex, callID); |
|
676 |
} |
|
677 |
||
678 |
void setIndex(int parameterIndex) { |
|
679 |
this.parameterIndex = parameterIndex; |
|
680 |
} |
|
681 |
||
682 |
void end(int callId) { |
|
683 |
descriptorCheck.end(callId); |
|
684 |
} |
|
685 |
} |
|
2 | 686 |
} |