author | smarks |
Thu, 24 Oct 2013 10:13:39 -0700 | |
changeset 21359 | 2d32ce9ea2b4 |
parent 19580 | 8ef7bdc1bb5a |
child 24865 | 09b1d992ca72 |
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 |
||
26 |
package java.rmi.server; |
|
27 |
||
28 |
import java.io.*; |
|
29 |
import java.net.*; |
|
30 |
||
31 |
/** |
|
32 |
* An <code>RMISocketFactory</code> instance is used by the RMI runtime |
|
33 |
* in order to obtain client and server sockets for RMI calls. An |
|
34 |
* application may use the <code>setSocketFactory</code> method to |
|
35 |
* request that the RMI runtime use its socket factory instance |
|
19201
80230405e311
8020854: change RMI javadocs to specify that remote objects are exported to the wildcard address
smarks
parents:
5506
diff
changeset
|
36 |
* instead of the default implementation. |
2 | 37 |
* |
19201
80230405e311
8020854: change RMI javadocs to specify that remote objects are exported to the wildcard address
smarks
parents:
5506
diff
changeset
|
38 |
* <p>The default socket factory implementation performs a |
2 | 39 |
* three-tiered approach to creating client sockets. First, a direct |
40 |
* socket connection to the remote VM is attempted. If that fails |
|
41 |
* (due to a firewall), the runtime uses HTTP with the explicit port |
|
42 |
* number of the server. If the firewall does not allow this type of |
|
43 |
* communication, then HTTP to a cgi-bin script on the server is used |
|
21359 | 44 |
* to POST the RMI call. The HTTP tunneling mechanisms are disabled by |
45 |
* default. This behavior is controlled by the {@code java.rmi.server.disableHttp} |
|
46 |
* property, whose default value is {@code true}. Setting this property's |
|
47 |
* value to {@code false} will enable the HTTP tunneling mechanisms. |
|
48 |
* |
|
49 |
* <p><strong>Deprecated: HTTP Tunneling.</strong> <em>The HTTP tunneling mechanisms |
|
50 |
* described above, specifically HTTP with an explicit port and HTTP to a |
|
51 |
* cgi-bin script, are deprecated. These HTTP tunneling mechanisms are |
|
52 |
* subject to removal in a future release of the platform.</em> |
|
19201
80230405e311
8020854: change RMI javadocs to specify that remote objects are exported to the wildcard address
smarks
parents:
5506
diff
changeset
|
53 |
* |
80230405e311
8020854: change RMI javadocs to specify that remote objects are exported to the wildcard address
smarks
parents:
5506
diff
changeset
|
54 |
* <p>The default socket factory implementation creates server sockets that |
80230405e311
8020854: change RMI javadocs to specify that remote objects are exported to the wildcard address
smarks
parents:
5506
diff
changeset
|
55 |
* are bound to the wildcard address, which accepts requests from all network |
80230405e311
8020854: change RMI javadocs to specify that remote objects are exported to the wildcard address
smarks
parents:
5506
diff
changeset
|
56 |
* interfaces. |
80230405e311
8020854: change RMI javadocs to specify that remote objects are exported to the wildcard address
smarks
parents:
5506
diff
changeset
|
57 |
* |
80230405e311
8020854: change RMI javadocs to specify that remote objects are exported to the wildcard address
smarks
parents:
5506
diff
changeset
|
58 |
* @implNote |
80230405e311
8020854: change RMI javadocs to specify that remote objects are exported to the wildcard address
smarks
parents:
5506
diff
changeset
|
59 |
* <p>You can use the {@code RMISocketFactory} class to create a server socket that |
80230405e311
8020854: change RMI javadocs to specify that remote objects are exported to the wildcard address
smarks
parents:
5506
diff
changeset
|
60 |
* is bound to a specific address, restricting the origin of requests. For example, |
19580
8ef7bdc1bb5a
8022445: fix RMISocketFactory example to avoid using localhost
smarks
parents:
19201
diff
changeset
|
61 |
* the following code implements a socket factory that binds server sockets to an IPv4 |
19201
80230405e311
8020854: change RMI javadocs to specify that remote objects are exported to the wildcard address
smarks
parents:
5506
diff
changeset
|
62 |
* loopback address. This restricts RMI to processing requests only from the local host. |
80230405e311
8020854: change RMI javadocs to specify that remote objects are exported to the wildcard address
smarks
parents:
5506
diff
changeset
|
63 |
* |
80230405e311
8020854: change RMI javadocs to specify that remote objects are exported to the wildcard address
smarks
parents:
5506
diff
changeset
|
64 |
* <pre>{@code |
80230405e311
8020854: change RMI javadocs to specify that remote objects are exported to the wildcard address
smarks
parents:
5506
diff
changeset
|
65 |
* class LoopbackSocketFactory extends RMISocketFactory { |
80230405e311
8020854: change RMI javadocs to specify that remote objects are exported to the wildcard address
smarks
parents:
5506
diff
changeset
|
66 |
* public ServerSocket createServerSocket(int port) throws IOException { |
19580
8ef7bdc1bb5a
8022445: fix RMISocketFactory example to avoid using localhost
smarks
parents:
19201
diff
changeset
|
67 |
* return new ServerSocket(port, 5, InetAddress.getByName("127.0.0.1")); |
19201
80230405e311
8020854: change RMI javadocs to specify that remote objects are exported to the wildcard address
smarks
parents:
5506
diff
changeset
|
68 |
* } |
80230405e311
8020854: change RMI javadocs to specify that remote objects are exported to the wildcard address
smarks
parents:
5506
diff
changeset
|
69 |
* |
80230405e311
8020854: change RMI javadocs to specify that remote objects are exported to the wildcard address
smarks
parents:
5506
diff
changeset
|
70 |
* public Socket createSocket(String host, int port) throws IOException { |
80230405e311
8020854: change RMI javadocs to specify that remote objects are exported to the wildcard address
smarks
parents:
5506
diff
changeset
|
71 |
* // just call the default client socket factory |
80230405e311
8020854: change RMI javadocs to specify that remote objects are exported to the wildcard address
smarks
parents:
5506
diff
changeset
|
72 |
* return RMISocketFactory.getDefaultSocketFactory() |
80230405e311
8020854: change RMI javadocs to specify that remote objects are exported to the wildcard address
smarks
parents:
5506
diff
changeset
|
73 |
* .createSocket(host, port); |
80230405e311
8020854: change RMI javadocs to specify that remote objects are exported to the wildcard address
smarks
parents:
5506
diff
changeset
|
74 |
* } |
80230405e311
8020854: change RMI javadocs to specify that remote objects are exported to the wildcard address
smarks
parents:
5506
diff
changeset
|
75 |
* } |
80230405e311
8020854: change RMI javadocs to specify that remote objects are exported to the wildcard address
smarks
parents:
5506
diff
changeset
|
76 |
* |
80230405e311
8020854: change RMI javadocs to specify that remote objects are exported to the wildcard address
smarks
parents:
5506
diff
changeset
|
77 |
* // ... |
80230405e311
8020854: change RMI javadocs to specify that remote objects are exported to the wildcard address
smarks
parents:
5506
diff
changeset
|
78 |
* |
80230405e311
8020854: change RMI javadocs to specify that remote objects are exported to the wildcard address
smarks
parents:
5506
diff
changeset
|
79 |
* RMISocketFactory.setSocketFactory(new LoopbackSocketFactory()); |
80230405e311
8020854: change RMI javadocs to specify that remote objects are exported to the wildcard address
smarks
parents:
5506
diff
changeset
|
80 |
* }</pre> |
80230405e311
8020854: change RMI javadocs to specify that remote objects are exported to the wildcard address
smarks
parents:
5506
diff
changeset
|
81 |
* |
80230405e311
8020854: change RMI javadocs to specify that remote objects are exported to the wildcard address
smarks
parents:
5506
diff
changeset
|
82 |
* Set the {@code java.rmi.server.hostname} system property |
19580
8ef7bdc1bb5a
8022445: fix RMISocketFactory example to avoid using localhost
smarks
parents:
19201
diff
changeset
|
83 |
* to {@code 127.0.0.1} to ensure that the generated stubs connect to the right |
8ef7bdc1bb5a
8022445: fix RMISocketFactory example to avoid using localhost
smarks
parents:
19201
diff
changeset
|
84 |
* network interface. |
2 | 85 |
* |
86 |
* @author Ann Wollrath |
|
87 |
* @author Peter Jones |
|
88 |
* @since JDK1.1 |
|
89 |
*/ |
|
90 |
public abstract class RMISocketFactory |
|
91 |
implements RMIClientSocketFactory, RMIServerSocketFactory |
|
92 |
{ |
|
93 |
||
94 |
/** Client/server socket factory to be used by RMI runtime */ |
|
95 |
private static RMISocketFactory factory = null; |
|
96 |
/** default socket factory used by this RMI implementation */ |
|
97 |
private static RMISocketFactory defaultSocketFactory; |
|
98 |
/** Handler for socket creation failure */ |
|
99 |
private static RMIFailureHandler handler = null; |
|
100 |
||
101 |
/** |
|
102 |
* Constructs an <code>RMISocketFactory</code>. |
|
103 |
* @since JDK1.1 |
|
104 |
*/ |
|
105 |
public RMISocketFactory() { |
|
106 |
super(); |
|
107 |
} |
|
108 |
||
109 |
/** |
|
110 |
* Creates a client socket connected to the specified host and port. |
|
111 |
* @param host the host name |
|
112 |
* @param port the port number |
|
113 |
* @return a socket connected to the specified host and port. |
|
114 |
* @exception IOException if an I/O error occurs during socket creation |
|
115 |
* @since JDK1.1 |
|
116 |
*/ |
|
117 |
public abstract Socket createSocket(String host, int port) |
|
118 |
throws IOException; |
|
119 |
||
120 |
/** |
|
121 |
* Create a server socket on the specified port (port 0 indicates |
|
122 |
* an anonymous port). |
|
123 |
* @param port the port number |
|
124 |
* @return the server socket on the specified port |
|
125 |
* @exception IOException if an I/O error occurs during server socket |
|
126 |
* creation |
|
127 |
* @since JDK1.1 |
|
128 |
*/ |
|
129 |
public abstract ServerSocket createServerSocket(int port) |
|
130 |
throws IOException; |
|
131 |
||
132 |
/** |
|
133 |
* Set the global socket factory from which RMI gets sockets (if the |
|
134 |
* remote object is not associated with a specific client and/or server |
|
135 |
* socket factory). The RMI socket factory can only be set once. Note: The |
|
136 |
* RMISocketFactory may only be set if the current security manager allows |
|
137 |
* setting a socket factory; if disallowed, a SecurityException will be |
|
138 |
* thrown. |
|
139 |
* @param fac the socket factory |
|
140 |
* @exception IOException if the RMI socket factory is already set |
|
141 |
* @exception SecurityException if a security manager exists and its |
|
142 |
* <code>checkSetFactory</code> method doesn't allow the operation. |
|
143 |
* @see #getSocketFactory |
|
144 |
* @see java.lang.SecurityManager#checkSetFactory() |
|
145 |
* @since JDK1.1 |
|
146 |
*/ |
|
147 |
public synchronized static void setSocketFactory(RMISocketFactory fac) |
|
148 |
throws IOException |
|
149 |
{ |
|
150 |
if (factory != null) { |
|
151 |
throw new SocketException("factory already defined"); |
|
152 |
} |
|
153 |
SecurityManager security = System.getSecurityManager(); |
|
154 |
if (security != null) { |
|
155 |
security.checkSetFactory(); |
|
156 |
} |
|
157 |
factory = fac; |
|
158 |
} |
|
159 |
||
160 |
/** |
|
161 |
* Returns the socket factory set by the <code>setSocketFactory</code> |
|
162 |
* method. Returns <code>null</code> if no socket factory has been |
|
163 |
* set. |
|
164 |
* @return the socket factory |
|
165 |
* @see #setSocketFactory(RMISocketFactory) |
|
166 |
* @since JDK1.1 |
|
167 |
*/ |
|
168 |
public synchronized static RMISocketFactory getSocketFactory() |
|
169 |
{ |
|
170 |
return factory; |
|
171 |
} |
|
172 |
||
173 |
/** |
|
174 |
* Returns a reference to the default socket factory used |
|
175 |
* by this RMI implementation. This will be the factory used |
|
176 |
* by the RMI runtime when <code>getSocketFactory</code> |
|
177 |
* returns <code>null</code>. |
|
178 |
* @return the default RMI socket factory |
|
179 |
* @since JDK1.1 |
|
180 |
*/ |
|
181 |
public synchronized static RMISocketFactory getDefaultSocketFactory() { |
|
182 |
if (defaultSocketFactory == null) { |
|
183 |
defaultSocketFactory = |
|
184 |
new sun.rmi.transport.proxy.RMIMasterSocketFactory(); |
|
185 |
} |
|
186 |
return defaultSocketFactory; |
|
187 |
} |
|
188 |
||
189 |
/** |
|
190 |
* Sets the failure handler to be called by the RMI runtime if server |
|
191 |
* socket creation fails. By default, if no failure handler is installed |
|
192 |
* and server socket creation fails, the RMI runtime does attempt to |
|
193 |
* recreate the server socket. |
|
194 |
* |
|
195 |
* <p>If there is a security manager, this method first calls |
|
196 |
* the security manager's <code>checkSetFactory</code> method |
|
197 |
* to ensure the operation is allowed. |
|
198 |
* This could result in a <code>SecurityException</code>. |
|
199 |
* |
|
200 |
* @param fh the failure handler |
|
201 |
* @throws SecurityException if a security manager exists and its |
|
202 |
* <code>checkSetFactory</code> method doesn't allow the |
|
203 |
* operation. |
|
204 |
* @see #getFailureHandler |
|
205 |
* @see java.rmi.server.RMIFailureHandler#failure(Exception) |
|
206 |
* @since JDK1.1 |
|
207 |
*/ |
|
208 |
public synchronized static void setFailureHandler(RMIFailureHandler fh) |
|
209 |
{ |
|
210 |
SecurityManager security = System.getSecurityManager(); |
|
211 |
if (security != null) { |
|
212 |
security.checkSetFactory(); |
|
213 |
} |
|
214 |
handler = fh; |
|
215 |
} |
|
216 |
||
217 |
/** |
|
218 |
* Returns the handler for socket creation failure set by the |
|
219 |
* <code>setFailureHandler</code> method. |
|
220 |
* @return the failure handler |
|
221 |
* @see #setFailureHandler(RMIFailureHandler) |
|
222 |
* @since JDK1.1 |
|
223 |
*/ |
|
224 |
public synchronized static RMIFailureHandler getFailureHandler() |
|
225 |
{ |
|
226 |
return handler; |
|
227 |
} |
|
228 |
} |