author | mli |
Mon, 09 Jan 2017 14:14:20 -0800 | |
changeset 43061 | 257cac611780 |
parent 36670 | acf999f92006 |
child 43211 | f264afd5082c |
permissions | -rw-r--r-- |
2 | 1 |
/* |
43061
257cac611780
8172347: Refactoring src/java.rmi/share/classes/sun/rmi/registry/RegistryImpl.java to improve testability of rmiregistry
mli
parents:
36670
diff
changeset
|
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.registry; |
|
27 |
||
36670
acf999f92006
8152277: Move URLClassPath.pathToURLs(String) to RegistryImpl
chegar
parents:
30655
diff
changeset
|
28 |
import java.nio.file.Path; |
acf999f92006
8152277: Move URLClassPath.pathToURLs(String) to RegistryImpl
chegar
parents:
30655
diff
changeset
|
29 |
import java.nio.file.Paths; |
acf999f92006
8152277: Move URLClassPath.pathToURLs(String) to RegistryImpl
chegar
parents:
30655
diff
changeset
|
30 |
import java.util.ArrayList; |
2 | 31 |
import java.util.Enumeration; |
32 |
import java.util.Hashtable; |
|
36670
acf999f92006
8152277: Move URLClassPath.pathToURLs(String) to RegistryImpl
chegar
parents:
30655
diff
changeset
|
33 |
import java.util.List; |
2 | 34 |
import java.util.MissingResourceException; |
35 |
import java.util.ResourceBundle; |
|
36670
acf999f92006
8152277: Move URLClassPath.pathToURLs(String) to RegistryImpl
chegar
parents:
30655
diff
changeset
|
36 |
import java.io.File; |
11039
4ee27839f531
7102369: remove java.rmi.server.codebase property parsing from registyimpl
coffeys
parents:
10914
diff
changeset
|
37 |
import java.io.FilePermission; |
2 | 38 |
import java.io.IOException; |
39 |
import java.net.*; |
|
40 |
import java.rmi.*; |
|
41 |
import java.rmi.server.ObjID; |
|
42 |
import java.rmi.server.RemoteServer; |
|
43 |
import java.rmi.server.ServerNotActiveException; |
|
44 |
import java.rmi.registry.Registry; |
|
45 |
import java.rmi.server.RMIClientSocketFactory; |
|
46 |
import java.rmi.server.RMIServerSocketFactory; |
|
10913 | 47 |
import java.security.AccessControlContext; |
48 |
import java.security.AccessController; |
|
49 |
import java.security.CodeSource; |
|
50 |
import java.security.Policy; |
|
2 | 51 |
import java.security.PrivilegedActionException; |
10913 | 52 |
import java.security.PrivilegedExceptionAction; |
53 |
import java.security.PermissionCollection; |
|
54 |
import java.security.Permissions; |
|
55 |
import java.security.ProtectionDomain; |
|
2 | 56 |
import java.text.MessageFormat; |
10913 | 57 |
import sun.rmi.server.LoaderHandler; |
2 | 58 |
import sun.rmi.server.UnicastServerRef; |
59 |
import sun.rmi.server.UnicastServerRef2; |
|
60 |
import sun.rmi.transport.LiveRef; |
|
61 |
import sun.rmi.transport.ObjectTable; |
|
62 |
import sun.rmi.transport.Target; |
|
63 |
||
64 |
/** |
|
65 |
* A "registry" exists on every node that allows RMI connections to |
|
66 |
* servers on that node. The registry on a particular node contains a |
|
67 |
* transient database that maps names to remote objects. When the |
|
68 |
* node boots, the registry database is empty. The names stored in the |
|
69 |
* registry are pure and are not parsed. A service storing itself in |
|
70 |
* the registry may want to prefix its name of the service by a package |
|
71 |
* name (although not required), to reduce name collisions in the |
|
72 |
* registry. |
|
73 |
* |
|
74 |
* The LocateRegistry class is used to obtain registry for different hosts. |
|
75 |
* |
|
76 |
* @see java.rmi.registry.LocateRegistry |
|
77 |
*/ |
|
78 |
public class RegistryImpl extends java.rmi.server.RemoteServer |
|
79 |
implements Registry |
|
80 |
{ |
|
81 |
||
82 |
/* indicate compatibility with JDK 1.1.x version of class */ |
|
83 |
private static final long serialVersionUID = 4666870661827494597L; |
|
51 | 84 |
private Hashtable<String, Remote> bindings |
12040
558b0e0d5910
7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents:
11039
diff
changeset
|
85 |
= new Hashtable<>(101); |
51 | 86 |
private static Hashtable<InetAddress, InetAddress> allowedAccessCache |
12040
558b0e0d5910
7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents:
11039
diff
changeset
|
87 |
= new Hashtable<>(3); |
2 | 88 |
private static RegistryImpl registry; |
89 |
private static ObjID id = new ObjID(ObjID.REGISTRY_ID); |
|
90 |
||
91 |
private static ResourceBundle resources = null; |
|
92 |
||
93 |
/** |
|
94 |
* Construct a new RegistryImpl on the specified port with the |
|
95 |
* given custom socket factory pair. |
|
96 |
*/ |
|
97 |
public RegistryImpl(int port, |
|
98 |
RMIClientSocketFactory csf, |
|
99 |
RMIServerSocketFactory ssf) |
|
100 |
throws RemoteException |
|
101 |
{ |
|
22339 | 102 |
if (port == Registry.REGISTRY_PORT && System.getSecurityManager() != null) { |
103 |
// grant permission for default port only. |
|
104 |
try { |
|
105 |
AccessController.doPrivileged(new PrivilegedExceptionAction<Void>() { |
|
106 |
public Void run() throws RemoteException { |
|
107 |
LiveRef lref = new LiveRef(id, port, csf, ssf); |
|
108 |
setup(new UnicastServerRef2(lref)); |
|
109 |
return null; |
|
110 |
} |
|
111 |
}, null, new SocketPermission("localhost:"+port, "listen,accept")); |
|
112 |
} catch (PrivilegedActionException pae) { |
|
113 |
throw (RemoteException)pae.getException(); |
|
114 |
} |
|
115 |
} else { |
|
116 |
LiveRef lref = new LiveRef(id, port, csf, ssf); |
|
117 |
setup(new UnicastServerRef2(lref)); |
|
118 |
} |
|
2 | 119 |
} |
120 |
||
121 |
/** |
|
122 |
* Construct a new RegistryImpl on the specified port. |
|
123 |
*/ |
|
124 |
public RegistryImpl(int port) |
|
125 |
throws RemoteException |
|
126 |
{ |
|
22341
4689530d03b9
8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
22339
diff
changeset
|
127 |
if (port == Registry.REGISTRY_PORT && System.getSecurityManager() != null) { |
4689530d03b9
8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
22339
diff
changeset
|
128 |
// grant permission for default port only. |
4689530d03b9
8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
22339
diff
changeset
|
129 |
try { |
4689530d03b9
8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
22339
diff
changeset
|
130 |
AccessController.doPrivileged(new PrivilegedExceptionAction<Void>() { |
4689530d03b9
8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
22339
diff
changeset
|
131 |
public Void run() throws RemoteException { |
4689530d03b9
8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
22339
diff
changeset
|
132 |
LiveRef lref = new LiveRef(id, port); |
4689530d03b9
8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
22339
diff
changeset
|
133 |
setup(new UnicastServerRef(lref)); |
4689530d03b9
8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
22339
diff
changeset
|
134 |
return null; |
4689530d03b9
8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
22339
diff
changeset
|
135 |
} |
4689530d03b9
8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
22339
diff
changeset
|
136 |
}, null, new SocketPermission("localhost:"+port, "listen,accept")); |
4689530d03b9
8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
22339
diff
changeset
|
137 |
} catch (PrivilegedActionException pae) { |
4689530d03b9
8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
22339
diff
changeset
|
138 |
throw (RemoteException)pae.getException(); |
4689530d03b9
8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
22339
diff
changeset
|
139 |
} |
4689530d03b9
8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
22339
diff
changeset
|
140 |
} else { |
4689530d03b9
8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
22339
diff
changeset
|
141 |
LiveRef lref = new LiveRef(id, port); |
4689530d03b9
8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
22339
diff
changeset
|
142 |
setup(new UnicastServerRef(lref)); |
4689530d03b9
8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
22339
diff
changeset
|
143 |
} |
2 | 144 |
} |
145 |
||
146 |
/* |
|
147 |
* Create the export the object using the parameter |
|
148 |
* <code>uref</code> |
|
149 |
*/ |
|
150 |
private void setup(UnicastServerRef uref) |
|
151 |
throws RemoteException |
|
152 |
{ |
|
153 |
/* Server ref must be created and assigned before remote |
|
154 |
* object 'this' can be exported. |
|
155 |
*/ |
|
156 |
ref = uref; |
|
157 |
uref.exportObject(this, null, true); |
|
158 |
} |
|
159 |
||
160 |
/** |
|
161 |
* Returns the remote object for specified name in the registry. |
|
162 |
* @exception RemoteException If remote operation failed. |
|
30655 | 163 |
* @exception NotBoundException If name is not currently bound. |
2 | 164 |
*/ |
165 |
public Remote lookup(String name) |
|
166 |
throws RemoteException, NotBoundException |
|
167 |
{ |
|
168 |
synchronized (bindings) { |
|
51 | 169 |
Remote obj = bindings.get(name); |
2 | 170 |
if (obj == null) |
171 |
throw new NotBoundException(name); |
|
172 |
return obj; |
|
173 |
} |
|
174 |
} |
|
175 |
||
176 |
/** |
|
177 |
* Binds the name to the specified remote object. |
|
178 |
* @exception RemoteException If remote operation failed. |
|
179 |
* @exception AlreadyBoundException If name is already bound. |
|
180 |
*/ |
|
181 |
public void bind(String name, Remote obj) |
|
182 |
throws RemoteException, AlreadyBoundException, AccessException |
|
183 |
{ |
|
184 |
checkAccess("Registry.bind"); |
|
185 |
synchronized (bindings) { |
|
51 | 186 |
Remote curr = bindings.get(name); |
2 | 187 |
if (curr != null) |
188 |
throw new AlreadyBoundException(name); |
|
189 |
bindings.put(name, obj); |
|
190 |
} |
|
191 |
} |
|
192 |
||
193 |
/** |
|
194 |
* Unbind the name. |
|
195 |
* @exception RemoteException If remote operation failed. |
|
30655 | 196 |
* @exception NotBoundException If name is not currently bound. |
2 | 197 |
*/ |
198 |
public void unbind(String name) |
|
199 |
throws RemoteException, NotBoundException, AccessException |
|
200 |
{ |
|
201 |
checkAccess("Registry.unbind"); |
|
202 |
synchronized (bindings) { |
|
51 | 203 |
Remote obj = bindings.get(name); |
2 | 204 |
if (obj == null) |
205 |
throw new NotBoundException(name); |
|
206 |
bindings.remove(name); |
|
207 |
} |
|
208 |
} |
|
209 |
||
210 |
/** |
|
211 |
* Rebind the name to a new object, replaces any existing binding. |
|
212 |
* @exception RemoteException If remote operation failed. |
|
213 |
*/ |
|
214 |
public void rebind(String name, Remote obj) |
|
215 |
throws RemoteException, AccessException |
|
216 |
{ |
|
217 |
checkAccess("Registry.rebind"); |
|
218 |
bindings.put(name, obj); |
|
219 |
} |
|
220 |
||
221 |
/** |
|
222 |
* Returns an enumeration of the names in the registry. |
|
223 |
* @exception RemoteException If remote operation failed. |
|
224 |
*/ |
|
225 |
public String[] list() |
|
226 |
throws RemoteException |
|
227 |
{ |
|
228 |
String[] names; |
|
229 |
synchronized (bindings) { |
|
230 |
int i = bindings.size(); |
|
231 |
names = new String[i]; |
|
12040
558b0e0d5910
7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents:
11039
diff
changeset
|
232 |
Enumeration<String> enum_ = bindings.keys(); |
2 | 233 |
while ((--i) >= 0) |
12040
558b0e0d5910
7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents:
11039
diff
changeset
|
234 |
names[i] = enum_.nextElement(); |
2 | 235 |
} |
236 |
return names; |
|
237 |
} |
|
238 |
||
239 |
/** |
|
240 |
* Check that the caller has access to perform indicated operation. |
|
241 |
* The client must be on same the same host as this server. |
|
242 |
*/ |
|
243 |
public static void checkAccess(String op) throws AccessException { |
|
244 |
||
245 |
try { |
|
246 |
/* |
|
247 |
* Get client host that this registry operation was made from. |
|
248 |
*/ |
|
249 |
final String clientHostName = getClientHost(); |
|
250 |
InetAddress clientHost; |
|
251 |
||
252 |
try { |
|
51 | 253 |
clientHost = java.security.AccessController.doPrivileged( |
254 |
new java.security.PrivilegedExceptionAction<InetAddress>() { |
|
255 |
public InetAddress run() |
|
2 | 256 |
throws java.net.UnknownHostException |
257 |
{ |
|
258 |
return InetAddress.getByName(clientHostName); |
|
259 |
} |
|
260 |
}); |
|
261 |
} catch (PrivilegedActionException pae) { |
|
262 |
throw (java.net.UnknownHostException) pae.getException(); |
|
263 |
} |
|
264 |
||
265 |
// if client not yet seen, make sure client allowed access |
|
266 |
if (allowedAccessCache.get(clientHost) == null) { |
|
267 |
||
268 |
if (clientHost.isAnyLocalAddress()) { |
|
269 |
throw new AccessException( |
|
270 |
"Registry." + op + " disallowed; origin unknown"); |
|
271 |
} |
|
272 |
||
273 |
try { |
|
274 |
final InetAddress finalClientHost = clientHost; |
|
275 |
||
276 |
java.security.AccessController.doPrivileged( |
|
51 | 277 |
new java.security.PrivilegedExceptionAction<Void>() { |
278 |
public Void run() throws java.io.IOException { |
|
2 | 279 |
/* |
280 |
* if a ServerSocket can be bound to the client's |
|
281 |
* address then that address must be local |
|
282 |
*/ |
|
283 |
(new ServerSocket(0, 10, finalClientHost)).close(); |
|
284 |
allowedAccessCache.put(finalClientHost, |
|
285 |
finalClientHost); |
|
286 |
return null; |
|
287 |
} |
|
288 |
}); |
|
289 |
} catch (PrivilegedActionException pae) { |
|
290 |
// must have been an IOException |
|
291 |
||
292 |
throw new AccessException( |
|
293 |
"Registry." + op + " disallowed; origin " + |
|
294 |
clientHost + " is non-local host"); |
|
295 |
} |
|
296 |
} |
|
297 |
} catch (ServerNotActiveException ex) { |
|
298 |
/* |
|
299 |
* Local call from this VM: allow access. |
|
300 |
*/ |
|
301 |
} catch (java.net.UnknownHostException ex) { |
|
302 |
throw new AccessException("Registry." + op + |
|
303 |
" disallowed; origin is unknown host"); |
|
304 |
} |
|
305 |
} |
|
306 |
||
307 |
public static ObjID getID() { |
|
308 |
return id; |
|
309 |
} |
|
310 |
||
311 |
/** |
|
312 |
* Retrieves text resources from the locale-specific properties file. |
|
313 |
*/ |
|
314 |
private static String getTextResource(String key) { |
|
315 |
if (resources == null) { |
|
316 |
try { |
|
317 |
resources = ResourceBundle.getBundle( |
|
318 |
"sun.rmi.registry.resources.rmiregistry"); |
|
319 |
} catch (MissingResourceException mre) { |
|
320 |
} |
|
321 |
if (resources == null) { |
|
322 |
// throwing an Error is a bit extreme, methinks |
|
323 |
return ("[missing resource file: " + key + "]"); |
|
324 |
} |
|
325 |
} |
|
326 |
||
327 |
String val = null; |
|
328 |
try { |
|
329 |
val = resources.getString(key); |
|
330 |
} catch (MissingResourceException mre) { |
|
331 |
} |
|
332 |
||
333 |
if (val == null) { |
|
334 |
return ("[missing resource: " + key + "]"); |
|
335 |
} else { |
|
336 |
return (val); |
|
337 |
} |
|
338 |
} |
|
339 |
||
340 |
/** |
|
36670
acf999f92006
8152277: Move URLClassPath.pathToURLs(String) to RegistryImpl
chegar
parents:
30655
diff
changeset
|
341 |
* Convert class path specification into an array of file URLs. |
acf999f92006
8152277: Move URLClassPath.pathToURLs(String) to RegistryImpl
chegar
parents:
30655
diff
changeset
|
342 |
* |
acf999f92006
8152277: Move URLClassPath.pathToURLs(String) to RegistryImpl
chegar
parents:
30655
diff
changeset
|
343 |
* The path of the file is converted to a URI then into URL |
acf999f92006
8152277: Move URLClassPath.pathToURLs(String) to RegistryImpl
chegar
parents:
30655
diff
changeset
|
344 |
* form so that reserved characters can safely appear in the path. |
acf999f92006
8152277: Move URLClassPath.pathToURLs(String) to RegistryImpl
chegar
parents:
30655
diff
changeset
|
345 |
*/ |
acf999f92006
8152277: Move URLClassPath.pathToURLs(String) to RegistryImpl
chegar
parents:
30655
diff
changeset
|
346 |
private static URL[] pathToURLs(String path) { |
acf999f92006
8152277: Move URLClassPath.pathToURLs(String) to RegistryImpl
chegar
parents:
30655
diff
changeset
|
347 |
List<URL> paths = new ArrayList<>(); |
acf999f92006
8152277: Move URLClassPath.pathToURLs(String) to RegistryImpl
chegar
parents:
30655
diff
changeset
|
348 |
for (String entry: path.split(File.pathSeparator)) { |
acf999f92006
8152277: Move URLClassPath.pathToURLs(String) to RegistryImpl
chegar
parents:
30655
diff
changeset
|
349 |
Path p = Paths.get(entry); |
acf999f92006
8152277: Move URLClassPath.pathToURLs(String) to RegistryImpl
chegar
parents:
30655
diff
changeset
|
350 |
try { |
acf999f92006
8152277: Move URLClassPath.pathToURLs(String) to RegistryImpl
chegar
parents:
30655
diff
changeset
|
351 |
p = p.toRealPath(); |
acf999f92006
8152277: Move URLClassPath.pathToURLs(String) to RegistryImpl
chegar
parents:
30655
diff
changeset
|
352 |
} catch (IOException x) { |
acf999f92006
8152277: Move URLClassPath.pathToURLs(String) to RegistryImpl
chegar
parents:
30655
diff
changeset
|
353 |
p = p.toAbsolutePath(); |
acf999f92006
8152277: Move URLClassPath.pathToURLs(String) to RegistryImpl
chegar
parents:
30655
diff
changeset
|
354 |
} |
acf999f92006
8152277: Move URLClassPath.pathToURLs(String) to RegistryImpl
chegar
parents:
30655
diff
changeset
|
355 |
try { |
acf999f92006
8152277: Move URLClassPath.pathToURLs(String) to RegistryImpl
chegar
parents:
30655
diff
changeset
|
356 |
paths.add(p.toUri().toURL()); |
acf999f92006
8152277: Move URLClassPath.pathToURLs(String) to RegistryImpl
chegar
parents:
30655
diff
changeset
|
357 |
} catch (MalformedURLException e) { |
acf999f92006
8152277: Move URLClassPath.pathToURLs(String) to RegistryImpl
chegar
parents:
30655
diff
changeset
|
358 |
//ignore / skip entry |
acf999f92006
8152277: Move URLClassPath.pathToURLs(String) to RegistryImpl
chegar
parents:
30655
diff
changeset
|
359 |
} |
acf999f92006
8152277: Move URLClassPath.pathToURLs(String) to RegistryImpl
chegar
parents:
30655
diff
changeset
|
360 |
} |
acf999f92006
8152277: Move URLClassPath.pathToURLs(String) to RegistryImpl
chegar
parents:
30655
diff
changeset
|
361 |
return paths.toArray(new URL[0]); |
acf999f92006
8152277: Move URLClassPath.pathToURLs(String) to RegistryImpl
chegar
parents:
30655
diff
changeset
|
362 |
} |
acf999f92006
8152277: Move URLClassPath.pathToURLs(String) to RegistryImpl
chegar
parents:
30655
diff
changeset
|
363 |
|
acf999f92006
8152277: Move URLClassPath.pathToURLs(String) to RegistryImpl
chegar
parents:
30655
diff
changeset
|
364 |
/** |
43061
257cac611780
8172347: Refactoring src/java.rmi/share/classes/sun/rmi/registry/RegistryImpl.java to improve testability of rmiregistry
mli
parents:
36670
diff
changeset
|
365 |
* Return a new RegistryImpl on the requested port and export it to serve |
257cac611780
8172347: Refactoring src/java.rmi/share/classes/sun/rmi/registry/RegistryImpl.java to improve testability of rmiregistry
mli
parents:
36670
diff
changeset
|
366 |
* registry requests. A classloader is initialized from the system property |
257cac611780
8172347: Refactoring src/java.rmi/share/classes/sun/rmi/registry/RegistryImpl.java to improve testability of rmiregistry
mli
parents:
36670
diff
changeset
|
367 |
* "env.class.path" and a security manager is set unless one is already set. |
257cac611780
8172347: Refactoring src/java.rmi/share/classes/sun/rmi/registry/RegistryImpl.java to improve testability of rmiregistry
mli
parents:
36670
diff
changeset
|
368 |
* <p> |
257cac611780
8172347: Refactoring src/java.rmi/share/classes/sun/rmi/registry/RegistryImpl.java to improve testability of rmiregistry
mli
parents:
36670
diff
changeset
|
369 |
* The returned Registry is fully functional within the current process and |
257cac611780
8172347: Refactoring src/java.rmi/share/classes/sun/rmi/registry/RegistryImpl.java to improve testability of rmiregistry
mli
parents:
36670
diff
changeset
|
370 |
* is usable for internal and testing purposes. |
257cac611780
8172347: Refactoring src/java.rmi/share/classes/sun/rmi/registry/RegistryImpl.java to improve testability of rmiregistry
mli
parents:
36670
diff
changeset
|
371 |
* |
257cac611780
8172347: Refactoring src/java.rmi/share/classes/sun/rmi/registry/RegistryImpl.java to improve testability of rmiregistry
mli
parents:
36670
diff
changeset
|
372 |
* @param regPort port on which the rmiregistry accepts requests; |
257cac611780
8172347: Refactoring src/java.rmi/share/classes/sun/rmi/registry/RegistryImpl.java to improve testability of rmiregistry
mli
parents:
36670
diff
changeset
|
373 |
* if 0, an implementation specific port is assigned |
257cac611780
8172347: Refactoring src/java.rmi/share/classes/sun/rmi/registry/RegistryImpl.java to improve testability of rmiregistry
mli
parents:
36670
diff
changeset
|
374 |
* @return a RegistryImpl instance |
257cac611780
8172347: Refactoring src/java.rmi/share/classes/sun/rmi/registry/RegistryImpl.java to improve testability of rmiregistry
mli
parents:
36670
diff
changeset
|
375 |
* @exception RemoteException If remote operation failed. |
257cac611780
8172347: Refactoring src/java.rmi/share/classes/sun/rmi/registry/RegistryImpl.java to improve testability of rmiregistry
mli
parents:
36670
diff
changeset
|
376 |
* @since 9 |
2 | 377 |
*/ |
43061
257cac611780
8172347: Refactoring src/java.rmi/share/classes/sun/rmi/registry/RegistryImpl.java to improve testability of rmiregistry
mli
parents:
36670
diff
changeset
|
378 |
public static RegistryImpl createRegistry(int regPort) throws RemoteException { |
2 | 379 |
// Create and install the security manager if one is not installed |
380 |
// already. |
|
381 |
if (System.getSecurityManager() == null) { |
|
28053
b2366f339e39
8066633: Fix deprecation warnings in java.rmi module
smarks
parents:
27805
diff
changeset
|
382 |
System.setSecurityManager(new SecurityManager()); |
2 | 383 |
} |
384 |
||
43061
257cac611780
8172347: Refactoring src/java.rmi/share/classes/sun/rmi/registry/RegistryImpl.java to improve testability of rmiregistry
mli
parents:
36670
diff
changeset
|
385 |
/* |
257cac611780
8172347: Refactoring src/java.rmi/share/classes/sun/rmi/registry/RegistryImpl.java to improve testability of rmiregistry
mli
parents:
36670
diff
changeset
|
386 |
* Fix bugid 4147561: When JDK tools are executed, the value of |
257cac611780
8172347: Refactoring src/java.rmi/share/classes/sun/rmi/registry/RegistryImpl.java to improve testability of rmiregistry
mli
parents:
36670
diff
changeset
|
387 |
* the CLASSPATH environment variable for the shell in which they |
257cac611780
8172347: Refactoring src/java.rmi/share/classes/sun/rmi/registry/RegistryImpl.java to improve testability of rmiregistry
mli
parents:
36670
diff
changeset
|
388 |
* were invoked is no longer incorporated into the application |
257cac611780
8172347: Refactoring src/java.rmi/share/classes/sun/rmi/registry/RegistryImpl.java to improve testability of rmiregistry
mli
parents:
36670
diff
changeset
|
389 |
* class path; CLASSPATH's only effect is to be the value of the |
257cac611780
8172347: Refactoring src/java.rmi/share/classes/sun/rmi/registry/RegistryImpl.java to improve testability of rmiregistry
mli
parents:
36670
diff
changeset
|
390 |
* system property "env.class.path". To preserve the previous |
257cac611780
8172347: Refactoring src/java.rmi/share/classes/sun/rmi/registry/RegistryImpl.java to improve testability of rmiregistry
mli
parents:
36670
diff
changeset
|
391 |
* (JDK1.1 and JDK1.2beta3) behavior of this tool, however, its |
257cac611780
8172347: Refactoring src/java.rmi/share/classes/sun/rmi/registry/RegistryImpl.java to improve testability of rmiregistry
mli
parents:
36670
diff
changeset
|
392 |
* CLASSPATH should still be considered when resolving classes |
257cac611780
8172347: Refactoring src/java.rmi/share/classes/sun/rmi/registry/RegistryImpl.java to improve testability of rmiregistry
mli
parents:
36670
diff
changeset
|
393 |
* being unmarshalled. To effect this old behavior, a class |
257cac611780
8172347: Refactoring src/java.rmi/share/classes/sun/rmi/registry/RegistryImpl.java to improve testability of rmiregistry
mli
parents:
36670
diff
changeset
|
394 |
* loader that loads from the file path specified in the |
257cac611780
8172347: Refactoring src/java.rmi/share/classes/sun/rmi/registry/RegistryImpl.java to improve testability of rmiregistry
mli
parents:
36670
diff
changeset
|
395 |
* "env.class.path" property is created and set to be the context |
257cac611780
8172347: Refactoring src/java.rmi/share/classes/sun/rmi/registry/RegistryImpl.java to improve testability of rmiregistry
mli
parents:
36670
diff
changeset
|
396 |
* class loader before the remote object is exported. |
257cac611780
8172347: Refactoring src/java.rmi/share/classes/sun/rmi/registry/RegistryImpl.java to improve testability of rmiregistry
mli
parents:
36670
diff
changeset
|
397 |
*/ |
257cac611780
8172347: Refactoring src/java.rmi/share/classes/sun/rmi/registry/RegistryImpl.java to improve testability of rmiregistry
mli
parents:
36670
diff
changeset
|
398 |
String envcp = System.getProperty("env.class.path"); |
257cac611780
8172347: Refactoring src/java.rmi/share/classes/sun/rmi/registry/RegistryImpl.java to improve testability of rmiregistry
mli
parents:
36670
diff
changeset
|
399 |
if (envcp == null) { |
257cac611780
8172347: Refactoring src/java.rmi/share/classes/sun/rmi/registry/RegistryImpl.java to improve testability of rmiregistry
mli
parents:
36670
diff
changeset
|
400 |
envcp = "."; // preserve old default behavior |
257cac611780
8172347: Refactoring src/java.rmi/share/classes/sun/rmi/registry/RegistryImpl.java to improve testability of rmiregistry
mli
parents:
36670
diff
changeset
|
401 |
} |
257cac611780
8172347: Refactoring src/java.rmi/share/classes/sun/rmi/registry/RegistryImpl.java to improve testability of rmiregistry
mli
parents:
36670
diff
changeset
|
402 |
URL[] urls = pathToURLs(envcp); |
257cac611780
8172347: Refactoring src/java.rmi/share/classes/sun/rmi/registry/RegistryImpl.java to improve testability of rmiregistry
mli
parents:
36670
diff
changeset
|
403 |
ClassLoader cl = new URLClassLoader(urls); |
257cac611780
8172347: Refactoring src/java.rmi/share/classes/sun/rmi/registry/RegistryImpl.java to improve testability of rmiregistry
mli
parents:
36670
diff
changeset
|
404 |
|
257cac611780
8172347: Refactoring src/java.rmi/share/classes/sun/rmi/registry/RegistryImpl.java to improve testability of rmiregistry
mli
parents:
36670
diff
changeset
|
405 |
/* |
257cac611780
8172347: Refactoring src/java.rmi/share/classes/sun/rmi/registry/RegistryImpl.java to improve testability of rmiregistry
mli
parents:
36670
diff
changeset
|
406 |
* Fix bugid 4242317: Classes defined by this class loader should |
257cac611780
8172347: Refactoring src/java.rmi/share/classes/sun/rmi/registry/RegistryImpl.java to improve testability of rmiregistry
mli
parents:
36670
diff
changeset
|
407 |
* be annotated with the value of the "java.rmi.server.codebase" |
257cac611780
8172347: Refactoring src/java.rmi/share/classes/sun/rmi/registry/RegistryImpl.java to improve testability of rmiregistry
mli
parents:
36670
diff
changeset
|
408 |
* property, not the "file:" URLs for the CLASSPATH elements. |
257cac611780
8172347: Refactoring src/java.rmi/share/classes/sun/rmi/registry/RegistryImpl.java to improve testability of rmiregistry
mli
parents:
36670
diff
changeset
|
409 |
*/ |
257cac611780
8172347: Refactoring src/java.rmi/share/classes/sun/rmi/registry/RegistryImpl.java to improve testability of rmiregistry
mli
parents:
36670
diff
changeset
|
410 |
sun.rmi.server.LoaderHandler.registerCodebaseLoader(cl); |
257cac611780
8172347: Refactoring src/java.rmi/share/classes/sun/rmi/registry/RegistryImpl.java to improve testability of rmiregistry
mli
parents:
36670
diff
changeset
|
411 |
|
257cac611780
8172347: Refactoring src/java.rmi/share/classes/sun/rmi/registry/RegistryImpl.java to improve testability of rmiregistry
mli
parents:
36670
diff
changeset
|
412 |
Thread.currentThread().setContextClassLoader(cl); |
257cac611780
8172347: Refactoring src/java.rmi/share/classes/sun/rmi/registry/RegistryImpl.java to improve testability of rmiregistry
mli
parents:
36670
diff
changeset
|
413 |
|
257cac611780
8172347: Refactoring src/java.rmi/share/classes/sun/rmi/registry/RegistryImpl.java to improve testability of rmiregistry
mli
parents:
36670
diff
changeset
|
414 |
RegistryImpl registryImpl = null; |
2 | 415 |
try { |
43061
257cac611780
8172347: Refactoring src/java.rmi/share/classes/sun/rmi/registry/RegistryImpl.java to improve testability of rmiregistry
mli
parents:
36670
diff
changeset
|
416 |
registryImpl = AccessController.doPrivileged( |
257cac611780
8172347: Refactoring src/java.rmi/share/classes/sun/rmi/registry/RegistryImpl.java to improve testability of rmiregistry
mli
parents:
36670
diff
changeset
|
417 |
new PrivilegedExceptionAction<RegistryImpl>() { |
257cac611780
8172347: Refactoring src/java.rmi/share/classes/sun/rmi/registry/RegistryImpl.java to improve testability of rmiregistry
mli
parents:
36670
diff
changeset
|
418 |
public RegistryImpl run() throws RemoteException { |
257cac611780
8172347: Refactoring src/java.rmi/share/classes/sun/rmi/registry/RegistryImpl.java to improve testability of rmiregistry
mli
parents:
36670
diff
changeset
|
419 |
return new RegistryImpl(regPort); |
257cac611780
8172347: Refactoring src/java.rmi/share/classes/sun/rmi/registry/RegistryImpl.java to improve testability of rmiregistry
mli
parents:
36670
diff
changeset
|
420 |
} |
257cac611780
8172347: Refactoring src/java.rmi/share/classes/sun/rmi/registry/RegistryImpl.java to improve testability of rmiregistry
mli
parents:
36670
diff
changeset
|
421 |
}, getAccessControlContext(regPort)); |
257cac611780
8172347: Refactoring src/java.rmi/share/classes/sun/rmi/registry/RegistryImpl.java to improve testability of rmiregistry
mli
parents:
36670
diff
changeset
|
422 |
} catch (PrivilegedActionException ex) { |
257cac611780
8172347: Refactoring src/java.rmi/share/classes/sun/rmi/registry/RegistryImpl.java to improve testability of rmiregistry
mli
parents:
36670
diff
changeset
|
423 |
throw (RemoteException) ex.getException(); |
257cac611780
8172347: Refactoring src/java.rmi/share/classes/sun/rmi/registry/RegistryImpl.java to improve testability of rmiregistry
mli
parents:
36670
diff
changeset
|
424 |
} |
2 | 425 |
|
43061
257cac611780
8172347: Refactoring src/java.rmi/share/classes/sun/rmi/registry/RegistryImpl.java to improve testability of rmiregistry
mli
parents:
36670
diff
changeset
|
426 |
return registryImpl; |
257cac611780
8172347: Refactoring src/java.rmi/share/classes/sun/rmi/registry/RegistryImpl.java to improve testability of rmiregistry
mli
parents:
36670
diff
changeset
|
427 |
} |
2 | 428 |
|
43061
257cac611780
8172347: Refactoring src/java.rmi/share/classes/sun/rmi/registry/RegistryImpl.java to improve testability of rmiregistry
mli
parents:
36670
diff
changeset
|
429 |
/** |
257cac611780
8172347: Refactoring src/java.rmi/share/classes/sun/rmi/registry/RegistryImpl.java to improve testability of rmiregistry
mli
parents:
36670
diff
changeset
|
430 |
* Main program to start a registry. <br> |
257cac611780
8172347: Refactoring src/java.rmi/share/classes/sun/rmi/registry/RegistryImpl.java to improve testability of rmiregistry
mli
parents:
36670
diff
changeset
|
431 |
* The port number can be specified on the command line. |
257cac611780
8172347: Refactoring src/java.rmi/share/classes/sun/rmi/registry/RegistryImpl.java to improve testability of rmiregistry
mli
parents:
36670
diff
changeset
|
432 |
*/ |
257cac611780
8172347: Refactoring src/java.rmi/share/classes/sun/rmi/registry/RegistryImpl.java to improve testability of rmiregistry
mli
parents:
36670
diff
changeset
|
433 |
public static void main(String args[]) |
257cac611780
8172347: Refactoring src/java.rmi/share/classes/sun/rmi/registry/RegistryImpl.java to improve testability of rmiregistry
mli
parents:
36670
diff
changeset
|
434 |
{ |
257cac611780
8172347: Refactoring src/java.rmi/share/classes/sun/rmi/registry/RegistryImpl.java to improve testability of rmiregistry
mli
parents:
36670
diff
changeset
|
435 |
try { |
10913 | 436 |
final int regPort = (args.length >= 1) ? Integer.parseInt(args[0]) |
437 |
: Registry.REGISTRY_PORT; |
|
43061
257cac611780
8172347: Refactoring src/java.rmi/share/classes/sun/rmi/registry/RegistryImpl.java to improve testability of rmiregistry
mli
parents:
36670
diff
changeset
|
438 |
|
257cac611780
8172347: Refactoring src/java.rmi/share/classes/sun/rmi/registry/RegistryImpl.java to improve testability of rmiregistry
mli
parents:
36670
diff
changeset
|
439 |
registry = createRegistry(regPort); |
10913 | 440 |
|
2 | 441 |
// prevent registry from exiting |
442 |
while (true) { |
|
443 |
try { |
|
444 |
Thread.sleep(Long.MAX_VALUE); |
|
445 |
} catch (InterruptedException e) { |
|
446 |
} |
|
447 |
} |
|
448 |
} catch (NumberFormatException e) { |
|
449 |
System.err.println(MessageFormat.format( |
|
450 |
getTextResource("rmiregistry.port.badnumber"), |
|
451 |
args[0] )); |
|
452 |
System.err.println(MessageFormat.format( |
|
453 |
getTextResource("rmiregistry.usage"), |
|
454 |
"rmiregistry" )); |
|
455 |
} catch (Exception e) { |
|
456 |
e.printStackTrace(); |
|
457 |
} |
|
458 |
System.exit(1); |
|
459 |
} |
|
10913 | 460 |
|
461 |
/** |
|
11039
4ee27839f531
7102369: remove java.rmi.server.codebase property parsing from registyimpl
coffeys
parents:
10914
diff
changeset
|
462 |
* Generates an AccessControlContext with minimal permissions. |
10913 | 463 |
* The approach used here is taken from the similar method |
464 |
* getAccessControlContext() in the sun.applet.AppletPanel class. |
|
465 |
*/ |
|
22339 | 466 |
private static AccessControlContext getAccessControlContext(int port) { |
10913 | 467 |
// begin with permissions granted to all code in current policy |
468 |
PermissionCollection perms = AccessController.doPrivileged( |
|
469 |
new java.security.PrivilegedAction<PermissionCollection>() { |
|
470 |
public PermissionCollection run() { |
|
471 |
CodeSource codesource = new CodeSource(null, |
|
472 |
(java.security.cert.Certificate[]) null); |
|
473 |
Policy p = java.security.Policy.getPolicy(); |
|
474 |
if (p != null) { |
|
475 |
return p.getPermissions(codesource); |
|
476 |
} else { |
|
477 |
return new Permissions(); |
|
478 |
} |
|
479 |
} |
|
480 |
}); |
|
481 |
||
482 |
/* |
|
483 |
* Anyone can connect to the registry and the registry can connect |
|
484 |
* to and possibly download stubs from anywhere. Downloaded stubs and |
|
485 |
* related classes themselves are more tightly limited by RMI. |
|
486 |
*/ |
|
487 |
perms.add(new SocketPermission("*", "connect,accept")); |
|
22341
4689530d03b9
8028293: Check local configuration for actual ephemeral port range
michaelm
parents:
22339
diff
changeset
|
488 |
perms.add(new SocketPermission("localhost:"+port, "listen,accept")); |
10913 | 489 |
|
14209 | 490 |
perms.add(new RuntimePermission("accessClassInPackage.sun.jvmstat.*")); |
491 |
perms.add(new RuntimePermission("accessClassInPackage.sun.jvm.hotspot.*")); |
|
10914 | 492 |
|
11039
4ee27839f531
7102369: remove java.rmi.server.codebase property parsing from registyimpl
coffeys
parents:
10914
diff
changeset
|
493 |
perms.add(new FilePermission("<<ALL FILES>>", "read")); |
10913 | 494 |
|
495 |
/* |
|
496 |
* Create an AccessControlContext that consists of a single |
|
497 |
* protection domain with only the permissions calculated above. |
|
498 |
*/ |
|
499 |
ProtectionDomain pd = new ProtectionDomain( |
|
11039
4ee27839f531
7102369: remove java.rmi.server.codebase property parsing from registyimpl
coffeys
parents:
10914
diff
changeset
|
500 |
new CodeSource(null, |
4ee27839f531
7102369: remove java.rmi.server.codebase property parsing from registyimpl
coffeys
parents:
10914
diff
changeset
|
501 |
(java.security.cert.Certificate[]) null), perms); |
10913 | 502 |
return new AccessControlContext(new ProtectionDomain[] { pd }); |
503 |
} |
|
2 | 504 |
} |