author | smarks |
Mon, 05 Aug 2013 19:12:33 -0700 | |
changeset 19201 | 80230405e311 |
parent 5506 | 202f599c92aa |
child 21334 | c60dfce46a77 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
19201
80230405e311
8020854: change RMI javadocs to specify that remote objects are exported to the wildcard address
smarks
parents:
5506
diff
changeset
|
2 |
* Copyright (c) 1996, 2013, 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 |
package java.rmi.server; |
|
26 |
||
27 |
import java.rmi.*; |
|
28 |
import sun.rmi.server.UnicastServerRef; |
|
29 |
import sun.rmi.server.UnicastServerRef2; |
|
30 |
||
31 |
/** |
|
32 |
* Used for exporting a remote object with JRMP and obtaining a stub |
|
33 |
* that communicates to the remote object. |
|
34 |
* |
|
35 |
* <p>For the constructors and static <code>exportObject</code> methods |
|
36 |
* below, the stub for a remote object being exported is obtained as |
|
37 |
* follows: |
|
38 |
* |
|
39 |
* <p><ul> |
|
40 |
* |
|
41 |
* <li>If the remote object is exported using the {@link |
|
42 |
* #exportObject(Remote) UnicastRemoteObject.exportObject(Remote)} method, |
|
43 |
* a stub class (typically pregenerated from the remote object's class |
|
44 |
* using the <code>rmic</code> tool) is loaded and an instance of that stub |
|
45 |
* class is constructed as follows. |
|
46 |
* <ul> |
|
47 |
* |
|
48 |
* <li>A "root class" is determined as follows: if the remote object's |
|
49 |
* class directly implements an interface that extends {@link Remote}, then |
|
50 |
* the remote object's class is the root class; otherwise, the root class is |
|
51 |
* the most derived superclass of the remote object's class that directly |
|
52 |
* implements an interface that extends <code>Remote</code>. |
|
53 |
* |
|
54 |
* <li>The name of the stub class to load is determined by concatenating |
|
55 |
* the binary name of the root class with the suffix <code>"_Stub"</code>. |
|
56 |
* |
|
57 |
* <li>The stub class is loaded by name using the class loader of the root |
|
58 |
* class. The stub class must extend {@link RemoteStub} and must have a |
|
59 |
* public constructor that has one parameter, of type {@link RemoteRef}. |
|
60 |
* |
|
61 |
* <li>Finally, an instance of the stub class is constructed with a |
|
62 |
* {@link RemoteRef}. |
|
63 |
* </ul> |
|
64 |
* |
|
65 |
* <li>If the appropriate stub class could not be found, or the stub class |
|
66 |
* could not be loaded, or a problem occurs creating the stub instance, a |
|
67 |
* {@link StubNotFoundException} is thrown. |
|
68 |
* |
|
69 |
* <p> |
|
70 |
* <li>For all other means of exporting: |
|
71 |
* <p><ul> |
|
72 |
* |
|
73 |
* <li>If the remote object's stub class (as defined above) could not be |
|
74 |
* loaded or the system property |
|
75 |
* <code>java.rmi.server.ignoreStubClasses</code> is set to |
|
76 |
* <code>"true"</code> (case insensitive), a {@link |
|
77 |
* java.lang.reflect.Proxy} instance is constructed with the following |
|
78 |
* properties: |
|
79 |
* |
|
80 |
* <ul> |
|
81 |
* |
|
82 |
* <li>The proxy's class is defined by the class loader of the remote |
|
83 |
* object's class. |
|
84 |
* |
|
85 |
* <li>The proxy implements all the remote interfaces implemented by the |
|
86 |
* remote object's class. |
|
87 |
* |
|
88 |
* <li>The proxy's invocation handler is a {@link |
|
89 |
* RemoteObjectInvocationHandler} instance constructed with a |
|
90 |
* {@link RemoteRef}. |
|
91 |
* |
|
92 |
* <li>If the proxy could not be created, a {@link StubNotFoundException} |
|
93 |
* will be thrown. |
|
94 |
* </ul> |
|
95 |
* |
|
96 |
* <p> |
|
97 |
* <li>Otherwise, an instance of the remote object's stub class (as |
|
98 |
* described above) is used as the stub. |
|
99 |
* |
|
100 |
* </ul> |
|
101 |
* </ul> |
|
102 |
* |
|
19201
80230405e311
8020854: change RMI javadocs to specify that remote objects are exported to the wildcard address
smarks
parents:
5506
diff
changeset
|
103 |
* <p>If an object is exported with the |
80230405e311
8020854: change RMI javadocs to specify that remote objects are exported to the wildcard address
smarks
parents:
5506
diff
changeset
|
104 |
* {@link #exportObject(Remote) exportObject(Remote)} |
80230405e311
8020854: change RMI javadocs to specify that remote objects are exported to the wildcard address
smarks
parents:
5506
diff
changeset
|
105 |
* or |
80230405e311
8020854: change RMI javadocs to specify that remote objects are exported to the wildcard address
smarks
parents:
5506
diff
changeset
|
106 |
* {@link #exportObject(Remote, int) exportObject(Remote, port)} |
80230405e311
8020854: change RMI javadocs to specify that remote objects are exported to the wildcard address
smarks
parents:
5506
diff
changeset
|
107 |
* methods, or if a subclass constructor invokes one of the |
80230405e311
8020854: change RMI javadocs to specify that remote objects are exported to the wildcard address
smarks
parents:
5506
diff
changeset
|
108 |
* {@link #UnicastRemoteObject()} |
80230405e311
8020854: change RMI javadocs to specify that remote objects are exported to the wildcard address
smarks
parents:
5506
diff
changeset
|
109 |
* or |
80230405e311
8020854: change RMI javadocs to specify that remote objects are exported to the wildcard address
smarks
parents:
5506
diff
changeset
|
110 |
* {@link #UnicastRemoteObject(int) UnicastRemoteObject(port)} |
80230405e311
8020854: change RMI javadocs to specify that remote objects are exported to the wildcard address
smarks
parents:
5506
diff
changeset
|
111 |
* constructors, the object is exported with a server socket created using the |
80230405e311
8020854: change RMI javadocs to specify that remote objects are exported to the wildcard address
smarks
parents:
5506
diff
changeset
|
112 |
* {@link RMISocketFactory} |
80230405e311
8020854: change RMI javadocs to specify that remote objects are exported to the wildcard address
smarks
parents:
5506
diff
changeset
|
113 |
* class. |
80230405e311
8020854: change RMI javadocs to specify that remote objects are exported to the wildcard address
smarks
parents:
5506
diff
changeset
|
114 |
* |
80230405e311
8020854: change RMI javadocs to specify that remote objects are exported to the wildcard address
smarks
parents:
5506
diff
changeset
|
115 |
* @implNote |
80230405e311
8020854: change RMI javadocs to specify that remote objects are exported to the wildcard address
smarks
parents:
5506
diff
changeset
|
116 |
* <p>By default, server sockets created by the {@link RMISocketFactory} class |
80230405e311
8020854: change RMI javadocs to specify that remote objects are exported to the wildcard address
smarks
parents:
5506
diff
changeset
|
117 |
* listen on all network interfaces. See the |
80230405e311
8020854: change RMI javadocs to specify that remote objects are exported to the wildcard address
smarks
parents:
5506
diff
changeset
|
118 |
* {@link RMISocketFactory} class and the section |
80230405e311
8020854: change RMI javadocs to specify that remote objects are exported to the wildcard address
smarks
parents:
5506
diff
changeset
|
119 |
* <a href="{@docRoot}/../platform/rmi/spec/rmi-server29.html">RMI Socket Factories</a> |
80230405e311
8020854: change RMI javadocs to specify that remote objects are exported to the wildcard address
smarks
parents:
5506
diff
changeset
|
120 |
* in the |
80230405e311
8020854: change RMI javadocs to specify that remote objects are exported to the wildcard address
smarks
parents:
5506
diff
changeset
|
121 |
* <a href="{@docRoot}/../platform/rmi/spec/rmiTOC.html">Java RMI Specification</a>. |
80230405e311
8020854: change RMI javadocs to specify that remote objects are exported to the wildcard address
smarks
parents:
5506
diff
changeset
|
122 |
* |
2 | 123 |
* @author Ann Wollrath |
124 |
* @author Peter Jones |
|
125 |
* @since JDK1.1 |
|
126 |
**/ |
|
127 |
public class UnicastRemoteObject extends RemoteServer { |
|
128 |
||
129 |
/** |
|
130 |
* @serial port number on which to export object |
|
131 |
*/ |
|
132 |
private int port = 0; |
|
133 |
||
134 |
/** |
|
135 |
* @serial client-side socket factory (if any) |
|
136 |
*/ |
|
137 |
private RMIClientSocketFactory csf = null; |
|
138 |
||
139 |
/** |
|
140 |
* @serial server-side socket factory (if any) to use when |
|
141 |
* exporting object |
|
142 |
*/ |
|
143 |
private RMIServerSocketFactory ssf = null; |
|
144 |
||
145 |
/* indicate compatibility with JDK 1.1.x version of class */ |
|
146 |
private static final long serialVersionUID = 4974527148936298033L; |
|
147 |
||
148 |
/** |
|
149 |
* Creates and exports a new UnicastRemoteObject object using an |
|
150 |
* anonymous port. |
|
151 |
* @throws RemoteException if failed to export object |
|
152 |
* @since JDK1.1 |
|
153 |
*/ |
|
154 |
protected UnicastRemoteObject() throws RemoteException |
|
155 |
{ |
|
156 |
this(0); |
|
157 |
} |
|
158 |
||
159 |
/** |
|
160 |
* Creates and exports a new UnicastRemoteObject object using the |
|
161 |
* particular supplied port. |
|
162 |
* @param port the port number on which the remote object receives calls |
|
163 |
* (if <code>port</code> is zero, an anonymous port is chosen) |
|
164 |
* @throws RemoteException if failed to export object |
|
165 |
* @since 1.2 |
|
166 |
*/ |
|
167 |
protected UnicastRemoteObject(int port) throws RemoteException |
|
168 |
{ |
|
169 |
this.port = port; |
|
170 |
exportObject((Remote) this, port); |
|
171 |
} |
|
172 |
||
173 |
/** |
|
174 |
* Creates and exports a new UnicastRemoteObject object using the |
|
175 |
* particular supplied port and socket factories. |
|
176 |
* @param port the port number on which the remote object receives calls |
|
177 |
* (if <code>port</code> is zero, an anonymous port is chosen) |
|
178 |
* @param csf the client-side socket factory for making calls to the |
|
179 |
* remote object |
|
180 |
* @param ssf the server-side socket factory for receiving remote calls |
|
181 |
* @throws RemoteException if failed to export object |
|
182 |
* @since 1.2 |
|
183 |
*/ |
|
184 |
protected UnicastRemoteObject(int port, |
|
185 |
RMIClientSocketFactory csf, |
|
186 |
RMIServerSocketFactory ssf) |
|
187 |
throws RemoteException |
|
188 |
{ |
|
189 |
this.port = port; |
|
190 |
this.csf = csf; |
|
191 |
this.ssf = ssf; |
|
192 |
exportObject((Remote) this, port, csf, ssf); |
|
193 |
} |
|
194 |
||
195 |
/** |
|
196 |
* Re-export the remote object when it is deserialized. |
|
197 |
*/ |
|
198 |
private void readObject(java.io.ObjectInputStream in) |
|
199 |
throws java.io.IOException, java.lang.ClassNotFoundException |
|
200 |
{ |
|
201 |
in.defaultReadObject(); |
|
202 |
reexport(); |
|
203 |
} |
|
204 |
||
205 |
/** |
|
206 |
* Returns a clone of the remote object that is distinct from |
|
207 |
* the original. |
|
208 |
* |
|
209 |
* @exception CloneNotSupportedException if clone failed due to |
|
210 |
* a RemoteException. |
|
211 |
* @return the new remote object |
|
212 |
* @since JDK1.1 |
|
213 |
*/ |
|
214 |
public Object clone() throws CloneNotSupportedException |
|
215 |
{ |
|
216 |
try { |
|
217 |
UnicastRemoteObject cloned = (UnicastRemoteObject) super.clone(); |
|
218 |
cloned.reexport(); |
|
219 |
return cloned; |
|
220 |
} catch (RemoteException e) { |
|
221 |
throw new ServerCloneException("Clone failed", e); |
|
222 |
} |
|
223 |
} |
|
224 |
||
225 |
/* |
|
226 |
* Exports this UnicastRemoteObject using its initialized fields because |
|
227 |
* its creation bypassed running its constructors (via deserialization |
|
228 |
* or cloning, for example). |
|
229 |
*/ |
|
230 |
private void reexport() throws RemoteException |
|
231 |
{ |
|
232 |
if (csf == null && ssf == null) { |
|
233 |
exportObject((Remote) this, port); |
|
234 |
} else { |
|
235 |
exportObject((Remote) this, port, csf, ssf); |
|
236 |
} |
|
237 |
} |
|
238 |
||
239 |
/** |
|
240 |
* Exports the remote object to make it available to receive incoming |
|
241 |
* calls using an anonymous port. |
|
242 |
* @param obj the remote object to be exported |
|
243 |
* @return remote object stub |
|
244 |
* @exception RemoteException if export fails |
|
245 |
* @since JDK1.1 |
|
246 |
*/ |
|
247 |
public static RemoteStub exportObject(Remote obj) |
|
248 |
throws RemoteException |
|
249 |
{ |
|
250 |
/* |
|
251 |
* Use UnicastServerRef constructor passing the boolean value true |
|
252 |
* to indicate that only a generated stub class should be used. A |
|
253 |
* generated stub class must be used instead of a dynamic proxy |
|
254 |
* because the return value of this method is RemoteStub which a |
|
255 |
* dynamic proxy class cannot extend. |
|
256 |
*/ |
|
257 |
return (RemoteStub) exportObject(obj, new UnicastServerRef(true)); |
|
258 |
} |
|
259 |
||
260 |
/** |
|
261 |
* Exports the remote object to make it available to receive incoming |
|
262 |
* calls, using the particular supplied port. |
|
263 |
* @param obj the remote object to be exported |
|
264 |
* @param port the port to export the object on |
|
265 |
* @return remote object stub |
|
266 |
* @exception RemoteException if export fails |
|
267 |
* @since 1.2 |
|
268 |
*/ |
|
269 |
public static Remote exportObject(Remote obj, int port) |
|
270 |
throws RemoteException |
|
271 |
{ |
|
272 |
return exportObject(obj, new UnicastServerRef(port)); |
|
273 |
} |
|
274 |
||
275 |
/** |
|
276 |
* Exports the remote object to make it available to receive incoming |
|
277 |
* calls, using a transport specified by the given socket factory. |
|
278 |
* @param obj the remote object to be exported |
|
279 |
* @param port the port to export the object on |
|
280 |
* @param csf the client-side socket factory for making calls to the |
|
281 |
* remote object |
|
282 |
* @param ssf the server-side socket factory for receiving remote calls |
|
283 |
* @return remote object stub |
|
284 |
* @exception RemoteException if export fails |
|
285 |
* @since 1.2 |
|
286 |
*/ |
|
287 |
public static Remote exportObject(Remote obj, int port, |
|
288 |
RMIClientSocketFactory csf, |
|
289 |
RMIServerSocketFactory ssf) |
|
290 |
throws RemoteException |
|
291 |
{ |
|
292 |
||
293 |
return exportObject(obj, new UnicastServerRef2(port, csf, ssf)); |
|
294 |
} |
|
295 |
||
296 |
/** |
|
297 |
* Removes the remote object, obj, from the RMI runtime. If |
|
298 |
* successful, the object can no longer accept incoming RMI calls. |
|
299 |
* If the force parameter is true, the object is forcibly unexported |
|
300 |
* even if there are pending calls to the remote object or the |
|
301 |
* remote object still has calls in progress. If the force |
|
302 |
* parameter is false, the object is only unexported if there are |
|
303 |
* no pending or in progress calls to the object. |
|
304 |
* |
|
305 |
* @param obj the remote object to be unexported |
|
306 |
* @param force if true, unexports the object even if there are |
|
307 |
* pending or in-progress calls; if false, only unexports the object |
|
308 |
* if there are no pending or in-progress calls |
|
309 |
* @return true if operation is successful, false otherwise |
|
310 |
* @exception NoSuchObjectException if the remote object is not |
|
311 |
* currently exported |
|
312 |
* @since 1.2 |
|
313 |
*/ |
|
314 |
public static boolean unexportObject(Remote obj, boolean force) |
|
315 |
throws java.rmi.NoSuchObjectException |
|
316 |
{ |
|
317 |
return sun.rmi.transport.ObjectTable.unexportObject(obj, force); |
|
318 |
} |
|
319 |
||
320 |
/** |
|
321 |
* Exports the specified object using the specified server ref. |
|
322 |
*/ |
|
323 |
private static Remote exportObject(Remote obj, UnicastServerRef sref) |
|
324 |
throws RemoteException |
|
325 |
{ |
|
326 |
// if obj extends UnicastRemoteObject, set its ref. |
|
327 |
if (obj instanceof UnicastRemoteObject) { |
|
328 |
((UnicastRemoteObject) obj).ref = sref; |
|
329 |
} |
|
330 |
return sref.exportObject(obj, null, false); |
|
331 |
} |
|
332 |
} |