jdk/src/share/classes/sun/rmi/registry/RegistryImpl.java
author smarks
Wed, 21 Sep 2011 15:37:52 -0700
changeset 10914 da696f9a3be6
parent 10913 e1acf4473704
child 11039 4ee27839f531
permissions -rw-r--r--
7092186: adjust package access in rmiregistry Reviewed-by: asaha, coffeys
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     2
 * Copyright (c) 1996, 2008, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.MissingResourceException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.ResourceBundle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.rmi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.rmi.server.ObjID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.rmi.server.RemoteServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.rmi.server.ServerNotActiveException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.rmi.registry.Registry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.rmi.server.RMIClientSocketFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.rmi.server.RMIServerSocketFactory;
10913
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
    41
import java.security.AccessControlContext;
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
    42
import java.security.AccessController;
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
    43
import java.security.CodeSource;
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
    44
import java.security.Policy;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.security.PrivilegedActionException;
10913
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
    46
import java.security.PrivilegedExceptionAction;
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
    47
import java.security.PermissionCollection;
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
    48
import java.security.Permissions;
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
    49
import java.security.ProtectionDomain;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import java.text.MessageFormat;
10913
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
    51
import sun.rmi.server.LoaderHandler;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import sun.rmi.server.UnicastServerRef;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import sun.rmi.server.UnicastServerRef2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import sun.rmi.transport.LiveRef;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import sun.rmi.transport.ObjectTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import sun.rmi.transport.Target;
10913
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
    57
import sun.security.action.GetPropertyAction;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * A "registry" exists on every node that allows RMI connections to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * servers on that node.  The registry on a particular node contains a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * transient database that maps names to remote objects.  When the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * node boots, the registry database is empty.  The names stored in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * registry are pure and are not parsed.  A service storing itself in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * the registry may want to prefix its name of the service by a package
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * name (although not required), to reduce name collisions in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * registry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * The LocateRegistry class is used to obtain registry for different hosts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * @see java.rmi.registry.LocateRegistry
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
public class RegistryImpl extends java.rmi.server.RemoteServer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        implements Registry
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /* indicate compatibility with JDK 1.1.x version of class */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private static final long serialVersionUID = 4666870661827494597L;
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    79
    private Hashtable<String, Remote> bindings
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    80
        = new Hashtable<String, Remote>(101);
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    81
    private static Hashtable<InetAddress, InetAddress> allowedAccessCache
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    82
        = new Hashtable<InetAddress, InetAddress>(3);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    private static RegistryImpl registry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    private static ObjID id = new ObjID(ObjID.REGISTRY_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private static ResourceBundle resources = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * Construct a new RegistryImpl on the specified port with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * given custom socket factory pair.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    public RegistryImpl(int port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                        RMIClientSocketFactory csf,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                        RMIServerSocketFactory ssf)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        throws RemoteException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        LiveRef lref = new LiveRef(id, port, csf, ssf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        setup(new UnicastServerRef2(lref));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * Construct a new RegistryImpl on the specified port.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    public RegistryImpl(int port)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        throws RemoteException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        LiveRef lref = new LiveRef(id, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        setup(new UnicastServerRef(lref));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * Create the export the object using the parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * <code>uref</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    private void setup(UnicastServerRef uref)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        throws RemoteException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        /* Server ref must be created and assigned before remote
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
         * object 'this' can be exported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        ref = uref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        uref.exportObject(this, null, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * Returns the remote object for specified name in the registry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * @exception RemoteException If remote operation failed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * @exception NotBound If name is not currently bound.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    public Remote lookup(String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        throws RemoteException, NotBoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        synchronized (bindings) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   134
            Remote obj = bindings.get(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            if (obj == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                throw new NotBoundException(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            return obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * Binds the name to the specified remote object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @exception RemoteException If remote operation failed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @exception AlreadyBoundException If name is already bound.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    public void bind(String name, Remote obj)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        throws RemoteException, AlreadyBoundException, AccessException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        checkAccess("Registry.bind");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        synchronized (bindings) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   151
            Remote curr = bindings.get(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            if (curr != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                throw new AlreadyBoundException(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            bindings.put(name, obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * Unbind the name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @exception RemoteException If remote operation failed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @exception NotBound If name is not currently bound.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    public void unbind(String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        throws RemoteException, NotBoundException, AccessException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        checkAccess("Registry.unbind");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        synchronized (bindings) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   168
            Remote obj = bindings.get(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            if (obj == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                throw new NotBoundException(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            bindings.remove(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
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
     * Rebind the name to a new object, replaces any existing binding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @exception RemoteException If remote operation failed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    public void rebind(String name, Remote obj)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        throws RemoteException, AccessException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        checkAccess("Registry.rebind");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        bindings.put(name, obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * Returns an enumeration of the names in the registry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * @exception RemoteException If remote operation failed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    public String[] list()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        throws RemoteException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        String[] names;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        synchronized (bindings) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            int i = bindings.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            names = new String[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            Enumeration enum_ = bindings.keys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            while ((--i) >= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                names[i] = (String)enum_.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        return names;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * Check that the caller has access to perform indicated operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * The client must be on same the same host as this server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    public static void checkAccess(String op) throws AccessException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
             * Get client host that this registry operation was made from.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            final String clientHostName = getClientHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            InetAddress clientHost;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            try {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   218
                clientHost = java.security.AccessController.doPrivileged(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   219
                    new java.security.PrivilegedExceptionAction<InetAddress>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   220
                        public InetAddress run()
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                            throws java.net.UnknownHostException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                            return InetAddress.getByName(clientHostName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                    });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            } catch (PrivilegedActionException pae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                throw (java.net.UnknownHostException) pae.getException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            // if client not yet seen, make sure client allowed access
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            if (allowedAccessCache.get(clientHost) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                if (clientHost.isAnyLocalAddress()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                    throw new AccessException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                        "Registry." + op + " disallowed; origin unknown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                    final InetAddress finalClientHost = clientHost;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                    java.security.AccessController.doPrivileged(
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   242
                        new java.security.PrivilegedExceptionAction<Void>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   243
                            public Void run() throws java.io.IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                                 * if a ServerSocket can be bound to the client's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                                 * address then that address must be local
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                                (new ServerSocket(0, 10, finalClientHost)).close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                                allowedAccessCache.put(finalClientHost,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                                                       finalClientHost);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                    });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                } catch (PrivilegedActionException pae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                    // must have been an IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                    throw new AccessException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                        "Registry." + op + " disallowed; origin " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                        clientHost + " is non-local host");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        } catch (ServerNotActiveException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
             * Local call from this VM: allow access.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        } catch (java.net.UnknownHostException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            throw new AccessException("Registry." + op +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                                      " disallowed; origin is unknown host");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    public static ObjID getID() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        return id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * Retrieves text resources from the locale-specific properties file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    private static String getTextResource(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        if (resources == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                resources = ResourceBundle.getBundle(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                    "sun.rmi.registry.resources.rmiregistry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            } catch (MissingResourceException mre) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            if (resources == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                // throwing an Error is a bit extreme, methinks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                return ("[missing resource file: " + key + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        String val = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            val = resources.getString(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        } catch (MissingResourceException mre) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        if (val == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            return ("[missing resource: " + key + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            return (val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * Main program to start a registry. <br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * The port number can be specified on the command line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    public static void main(String args[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        // Create and install the security manager if one is not installed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        // already.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        if (System.getSecurityManager() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            System.setSecurityManager(new RMISecurityManager());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
             * Fix bugid 4147561: When JDK tools are executed, the value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
             * the CLASSPATH environment variable for the shell in which they
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
             * were invoked is no longer incorporated into the application
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
             * class path; CLASSPATH's only effect is to be the value of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
             * system property "env.class.path".  To preserve the previous
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
             * (JDK1.1 and JDK1.2beta3) behavior of this tool, however, its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
             * CLASSPATH should still be considered when resolving classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
             * being unmarshalled.  To effect this old behavior, a class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
             * loader that loads from the file path specified in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
             * "env.class.path" property is created and set to be the context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
             * class loader before the remote object is exported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            String envcp = System.getProperty("env.class.path");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            if (envcp == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                envcp = ".";            // preserve old default behavior
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            URL[] urls = sun.misc.URLClassPath.pathToURLs(envcp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            ClassLoader cl = new URLClassLoader(urls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
10913
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   338
            String codebaseProperty = null;
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   339
            String prop = java.security.AccessController.doPrivileged(
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   340
                new GetPropertyAction("java.rmi.server.codebase"));
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   341
            if (prop != null && prop.trim().length() > 0) {
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   342
                codebaseProperty = prop;
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   343
            }
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   344
            URL[] codebaseURLs = null;
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   345
            if (codebaseProperty != null) {
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   346
                codebaseURLs = sun.misc.URLClassPath.pathToURLs(codebaseProperty);
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   347
            } else {
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   348
                codebaseURLs = new URL[0];
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   349
            }
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   350
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
             * Fix bugid 4242317: Classes defined by this class loader should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
             * be annotated with the value of the "java.rmi.server.codebase"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
             * property, not the "file:" URLs for the CLASSPATH elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            sun.rmi.server.LoaderHandler.registerCodebaseLoader(cl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            Thread.currentThread().setContextClassLoader(cl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
10913
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   360
            final int regPort = (args.length >= 1) ? Integer.parseInt(args[0])
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   361
                                                   : Registry.REGISTRY_PORT;
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   362
            try {
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   363
                registry = AccessController.doPrivileged(
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   364
                    new PrivilegedExceptionAction<RegistryImpl>() {
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   365
                        public RegistryImpl run() throws RemoteException {
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   366
                            return new RegistryImpl(regPort);
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   367
                        }
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   368
                    }, getAccessControlContext(codebaseURLs));
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   369
            } catch (PrivilegedActionException ex) {
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   370
                throw (RemoteException) ex.getException();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            }
10913
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   372
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            // prevent registry from exiting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                    Thread.sleep(Long.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        } catch (NumberFormatException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            System.err.println(MessageFormat.format(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                getTextResource("rmiregistry.port.badnumber"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                args[0] ));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            System.err.println(MessageFormat.format(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                getTextResource("rmiregistry.usage"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                "rmiregistry" ));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    }
10913
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   392
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   393
    /**
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   394
     * Generates an AccessControlContext from several URLs.
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   395
     * The approach used here is taken from the similar method
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   396
     * getAccessControlContext() in the sun.applet.AppletPanel class.
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   397
     */
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   398
    private static AccessControlContext getAccessControlContext(URL[] urls) {
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   399
        // begin with permissions granted to all code in current policy
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   400
        PermissionCollection perms = AccessController.doPrivileged(
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   401
            new java.security.PrivilegedAction<PermissionCollection>() {
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   402
                public PermissionCollection run() {
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   403
                    CodeSource codesource = new CodeSource(null,
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   404
                        (java.security.cert.Certificate[]) null);
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   405
                    Policy p = java.security.Policy.getPolicy();
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   406
                    if (p != null) {
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   407
                        return p.getPermissions(codesource);
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   408
                    } else {
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   409
                        return new Permissions();
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   410
                    }
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   411
                }
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   412
            });
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   413
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   414
        /*
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   415
         * Anyone can connect to the registry and the registry can connect
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   416
         * to and possibly download stubs from anywhere. Downloaded stubs and
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   417
         * related classes themselves are more tightly limited by RMI.
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   418
         */
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   419
        perms.add(new SocketPermission("*", "connect,accept"));
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   420
10914
da696f9a3be6 7092186: adjust package access in rmiregistry
smarks
parents: 10913
diff changeset
   421
        perms.add(new RuntimePermission("accessClassInPackage.sun.*"));
da696f9a3be6 7092186: adjust package access in rmiregistry
smarks
parents: 10913
diff changeset
   422
10913
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   423
        // add permissions required to load from codebase URL path
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   424
        LoaderHandler.addPermissionsForURLs(urls, perms, false);
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   425
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   426
        /*
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   427
         * Create an AccessControlContext that consists of a single
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   428
         * protection domain with only the permissions calculated above.
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   429
         */
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   430
        ProtectionDomain pd = new ProtectionDomain(
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   431
            new CodeSource((urls.length > 0 ? urls[0] : null),
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   432
                (java.security.cert.Certificate[]) null),
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   433
            perms);
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   434
        return new AccessControlContext(new ProtectionDomain[] { pd });
e1acf4473704 7083012: fix for RMI Registry
smarks
parents: 5506
diff changeset
   435
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
}