jdk/src/java.rmi/share/classes/sun/rmi/registry/RegistryImpl.java
author mli
Mon, 09 Jan 2017 14:14:20 -0800
changeset 43061 257cac611780
parent 36670 acf999f92006
child 43211 f264afd5082c
permissions -rw-r--r--
8172347: Refactoring src/java.rmi/share/classes/sun/rmi/registry/RegistryImpl.java to improve testability of rmiregistry Reviewed-by: rriggs, msheppar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.rmi.registry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.MissingResourceException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.rmi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.rmi.server.ObjID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.rmi.server.RemoteServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.rmi.server.ServerNotActiveException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.rmi.registry.Registry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.rmi.server.RMIClientSocketFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.rmi.server.RMIServerSocketFactory;
10913
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
    47
import java.security.AccessControlContext;
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
    48
import java.security.AccessController;
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
    49
import java.security.CodeSource;
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
    50
import java.security.Policy;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import java.security.PrivilegedActionException;
10913
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
    52
import java.security.PrivilegedExceptionAction;
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
    53
import java.security.PermissionCollection;
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
    54
import java.security.Permissions;
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
    55
import java.security.ProtectionDomain;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import java.text.MessageFormat;
10913
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
    57
import sun.rmi.server.LoaderHandler;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
import sun.rmi.server.UnicastServerRef;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
import sun.rmi.server.UnicastServerRef2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
import sun.rmi.transport.LiveRef;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
import sun.rmi.transport.ObjectTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
import sun.rmi.transport.Target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * A "registry" exists on every node that allows RMI connections to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * servers on that node.  The registry on a particular node contains a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * transient database that maps names to remote objects.  When the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * node boots, the registry database is empty.  The names stored in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * registry are pure and are not parsed.  A service storing itself in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * the registry may want to prefix its name of the service by a package
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * name (although not required), to reduce name collisions in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * registry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * The LocateRegistry class is used to obtain registry for different hosts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * @see java.rmi.registry.LocateRegistry
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
public class RegistryImpl extends java.rmi.server.RemoteServer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        implements Registry
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /* indicate compatibility with JDK 1.1.x version of class */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    private static final long serialVersionUID = 4666870661827494597L;
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    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
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    private static RegistryImpl registry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    private static ObjID id = new ObjID(ObjID.REGISTRY_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    private static ResourceBundle resources = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * Construct a new RegistryImpl on the specified port with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * given custom socket factory pair.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    public RegistryImpl(int port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                        RMIClientSocketFactory csf,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                        RMIServerSocketFactory ssf)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        throws RemoteException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    {
22339
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 14342
diff changeset
   102
        if (port == Registry.REGISTRY_PORT && System.getSecurityManager() != null) {
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 14342
diff changeset
   103
            // grant permission for default port only.
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 14342
diff changeset
   104
            try {
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 14342
diff changeset
   105
                AccessController.doPrivileged(new PrivilegedExceptionAction<Void>() {
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 14342
diff changeset
   106
                    public Void run() throws RemoteException {
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 14342
diff changeset
   107
                        LiveRef lref = new LiveRef(id, port, csf, ssf);
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 14342
diff changeset
   108
                        setup(new UnicastServerRef2(lref));
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 14342
diff changeset
   109
                        return null;
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 14342
diff changeset
   110
                    }
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 14342
diff changeset
   111
                }, null, new SocketPermission("localhost:"+port, "listen,accept"));
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 14342
diff changeset
   112
            } catch (PrivilegedActionException pae) {
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 14342
diff changeset
   113
                throw (RemoteException)pae.getException();
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 14342
diff changeset
   114
            }
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 14342
diff changeset
   115
        } else {
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 14342
diff changeset
   116
            LiveRef lref = new LiveRef(id, port, csf, ssf);
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 14342
diff changeset
   117
            setup(new UnicastServerRef2(lref));
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 14342
diff changeset
   118
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * Construct a new RegistryImpl on the specified port.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    public RegistryImpl(int port)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        throws RemoteException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * Create the export the object using the parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * <code>uref</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    private void setup(UnicastServerRef uref)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        throws RemoteException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        /* Server ref must be created and assigned before remote
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
         * object 'this' can be exported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        ref = uref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        uref.exportObject(this, null, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * Returns the remote object for specified name in the registry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @exception RemoteException If remote operation failed.
30655
d83f50188ca9 8080422: some docs cleanup for core libs
avstepan
parents: 28053
diff changeset
   163
     * @exception NotBoundException If name is not currently bound.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    public Remote lookup(String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        throws RemoteException, NotBoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        synchronized (bindings) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   169
            Remote obj = bindings.get(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            if (obj == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                throw new NotBoundException(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            return obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * Binds the name to the specified remote object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * @exception RemoteException If remote operation failed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * @exception AlreadyBoundException If name is already bound.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    public void bind(String name, Remote obj)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        throws RemoteException, AlreadyBoundException, AccessException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        checkAccess("Registry.bind");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        synchronized (bindings) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   186
            Remote curr = bindings.get(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            if (curr != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                throw new AlreadyBoundException(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            bindings.put(name, obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * Unbind the name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * @exception RemoteException If remote operation failed.
30655
d83f50188ca9 8080422: some docs cleanup for core libs
avstepan
parents: 28053
diff changeset
   196
     * @exception NotBoundException If name is not currently bound.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    public void unbind(String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        throws RemoteException, NotBoundException, AccessException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        checkAccess("Registry.unbind");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        synchronized (bindings) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   203
            Remote obj = bindings.get(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            if (obj == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                throw new NotBoundException(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            bindings.remove(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * Rebind the name to a new object, replaces any existing binding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * @exception RemoteException If remote operation failed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    public void rebind(String name, Remote obj)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        throws RemoteException, AccessException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        checkAccess("Registry.rebind");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        bindings.put(name, obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * Returns an enumeration of the names in the registry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * @exception RemoteException If remote operation failed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    public String[] list()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        throws RemoteException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        String[] names;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        synchronized (bindings) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            int i = bindings.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        return names;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * Check that the caller has access to perform indicated operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * The client must be on same the same host as this server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    public static void checkAccess(String op) throws AccessException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
             * Get client host that this registry operation was made from.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            final String clientHostName = getClientHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            InetAddress clientHost;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            try {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   253
                clientHost = java.security.AccessController.doPrivileged(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   254
                    new java.security.PrivilegedExceptionAction<InetAddress>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   255
                        public InetAddress run()
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                            throws java.net.UnknownHostException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                            return InetAddress.getByName(clientHostName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                    });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            } catch (PrivilegedActionException pae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                throw (java.net.UnknownHostException) pae.getException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            // if client not yet seen, make sure client allowed access
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            if (allowedAccessCache.get(clientHost) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                if (clientHost.isAnyLocalAddress()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                    throw new AccessException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                        "Registry." + op + " disallowed; origin unknown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                    final InetAddress finalClientHost = clientHost;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                    java.security.AccessController.doPrivileged(
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   277
                        new java.security.PrivilegedExceptionAction<Void>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   278
                            public Void run() throws java.io.IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                                 * if a ServerSocket can be bound to the client's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                                 * address then that address must be local
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                                (new ServerSocket(0, 10, finalClientHost)).close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                                allowedAccessCache.put(finalClientHost,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                                                       finalClientHost);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                    });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                } catch (PrivilegedActionException pae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                    // must have been an IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                    throw new AccessException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                        "Registry." + op + " disallowed; origin " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                        clientHost + " is non-local host");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        } catch (ServerNotActiveException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
             * Local call from this VM: allow access.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        } catch (java.net.UnknownHostException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            throw new AccessException("Registry." + op +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                                      " disallowed; origin is unknown host");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    public static ObjID getID() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        return id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * Retrieves text resources from the locale-specific properties file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    private static String getTextResource(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        if (resources == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                resources = ResourceBundle.getBundle(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                    "sun.rmi.registry.resources.rmiregistry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            } catch (MissingResourceException mre) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            if (resources == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                // throwing an Error is a bit extreme, methinks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                return ("[missing resource file: " + key + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        String val = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            val = resources.getString(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        } catch (MissingResourceException mre) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        if (val == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            return ("[missing resource: " + key + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            return (val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        // Create and install the security manager if one is not installed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        // already.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   436
            final int regPort = (args.length >= 1) ? Integer.parseInt(args[0])
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   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
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   440
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            // prevent registry from exiting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                    Thread.sleep(Long.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        } catch (NumberFormatException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            System.err.println(MessageFormat.format(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                getTextResource("rmiregistry.port.badnumber"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                args[0] ));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            System.err.println(MessageFormat.format(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                getTextResource("rmiregistry.usage"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                "rmiregistry" ));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    }
10913
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   460
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   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
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   463
     * The approach used here is taken from the similar method
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   464
     * getAccessControlContext() in the sun.applet.AppletPanel class.
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   465
     */
22339
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 14342
diff changeset
   466
    private static AccessControlContext getAccessControlContext(int port) {
10913
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   467
        // begin with permissions granted to all code in current policy
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   468
        PermissionCollection perms = AccessController.doPrivileged(
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   469
            new java.security.PrivilegedAction<PermissionCollection>() {
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   470
                public PermissionCollection run() {
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   471
                    CodeSource codesource = new CodeSource(null,
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   472
                        (java.security.cert.Certificate[]) null);
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   473
                    Policy p = java.security.Policy.getPolicy();
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   474
                    if (p != null) {
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   475
                        return p.getPermissions(codesource);
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   476
                    } else {
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   477
                        return new Permissions();
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   478
                    }
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   479
                }
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   480
            });
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   481
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   482
        /*
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   483
         * Anyone can connect to the registry and the registry can connect
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   484
         * to and possibly download stubs from anywhere. Downloaded stubs and
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   485
         * related classes themselves are more tightly limited by RMI.
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   486
         */
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   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
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   489
14209
221f6d0a12ea 7093490: adjust package access in rmiregistry
smarks
parents: 12040
diff changeset
   490
        perms.add(new RuntimePermission("accessClassInPackage.sun.jvmstat.*"));
221f6d0a12ea 7093490: adjust package access in rmiregistry
smarks
parents: 12040
diff changeset
   491
        perms.add(new RuntimePermission("accessClassInPackage.sun.jvm.hotspot.*"));
10914
da696f9a3be6 7092186: adjust package access in rmiregistry
smarks
parents: 10913
diff changeset
   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
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   494
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   495
        /*
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   496
         * Create an AccessControlContext that consists of a single
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   497
         * protection domain with only the permissions calculated above.
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   498
         */
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   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
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   502
        return new AccessControlContext(new ProtectionDomain[] { pd });
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   503
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
}