jdk/src/share/classes/sun/rmi/registry/RegistryImpl.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 5506 202f599c92aa
child 10913 e1acf4473704
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
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;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.security.PrivilegedActionException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.text.MessageFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import sun.rmi.server.UnicastServerRef;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import sun.rmi.server.UnicastServerRef2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import sun.rmi.transport.LiveRef;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import sun.rmi.transport.ObjectTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import sun.rmi.transport.Target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * A "registry" exists on every node that allows RMI connections to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * servers on that node.  The registry on a particular node contains a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * transient database that maps names to remote objects.  When the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * node boots, the registry database is empty.  The names stored in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * registry are pure and are not parsed.  A service storing itself in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * the registry may want to prefix its name of the service by a package
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * name (although not required), to reduce name collisions in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * registry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * The LocateRegistry class is used to obtain registry for different hosts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * @see java.rmi.registry.LocateRegistry
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
public class RegistryImpl extends java.rmi.server.RemoteServer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        implements Registry
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /* indicate compatibility with JDK 1.1.x version of class */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private static final long serialVersionUID = 4666870661827494597L;
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    69
    private Hashtable<String, Remote> bindings
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    70
        = new Hashtable<String, Remote>(101);
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    71
    private static Hashtable<InetAddress, InetAddress> allowedAccessCache
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    72
        = new Hashtable<InetAddress, InetAddress>(3);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private static RegistryImpl registry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private static ObjID id = new ObjID(ObjID.REGISTRY_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private static ResourceBundle resources = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * Construct a new RegistryImpl on the specified port with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * given custom socket factory pair.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    public RegistryImpl(int port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                        RMIClientSocketFactory csf,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                        RMIServerSocketFactory ssf)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        throws RemoteException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        LiveRef lref = new LiveRef(id, port, csf, ssf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        setup(new UnicastServerRef2(lref));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * Construct a new RegistryImpl on the specified port.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    public RegistryImpl(int port)
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);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        setup(new UnicastServerRef(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
     * Create the export the object using the parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * <code>uref</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    private void setup(UnicastServerRef uref)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        throws RemoteException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        /* Server ref must be created and assigned before remote
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
         * object 'this' can be exported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        ref = uref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        uref.exportObject(this, null, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * Returns the remote object for specified name in the registry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @exception RemoteException If remote operation failed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @exception NotBound If name is not currently bound.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    public Remote lookup(String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        throws RemoteException, NotBoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        synchronized (bindings) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   124
            Remote obj = bindings.get(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            if (obj == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                throw new NotBoundException(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            return obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * Binds the name to the specified remote object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @exception RemoteException If remote operation failed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @exception AlreadyBoundException If name is already bound.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    public void bind(String name, Remote obj)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        throws RemoteException, AlreadyBoundException, AccessException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        checkAccess("Registry.bind");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        synchronized (bindings) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   141
            Remote curr = bindings.get(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            if (curr != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                throw new AlreadyBoundException(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            bindings.put(name, obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * Unbind the name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @exception RemoteException If remote operation failed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @exception NotBound If name is not currently bound.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    public void unbind(String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        throws RemoteException, NotBoundException, AccessException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        checkAccess("Registry.unbind");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        synchronized (bindings) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   158
            Remote obj = bindings.get(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            if (obj == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                throw new NotBoundException(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            bindings.remove(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * Rebind the name to a new object, replaces any existing binding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * @exception RemoteException If remote operation failed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    public void rebind(String name, Remote obj)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        throws RemoteException, AccessException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        checkAccess("Registry.rebind");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        bindings.put(name, obj);
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
     * Returns an enumeration of the names in the registry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * @exception RemoteException If remote operation failed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    public String[] list()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        throws RemoteException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        String[] names;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        synchronized (bindings) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            int i = bindings.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            names = new String[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            Enumeration enum_ = bindings.keys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            while ((--i) >= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                names[i] = (String)enum_.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        return names;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * Check that the caller has access to perform indicated operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * The client must be on same the same host as this server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    public static void checkAccess(String op) throws AccessException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
             * Get client host that this registry operation was made from.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            final String clientHostName = getClientHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            InetAddress clientHost;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            try {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   208
                clientHost = java.security.AccessController.doPrivileged(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   209
                    new java.security.PrivilegedExceptionAction<InetAddress>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   210
                        public InetAddress run()
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                            throws java.net.UnknownHostException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                            return InetAddress.getByName(clientHostName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                    });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            } catch (PrivilegedActionException pae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                throw (java.net.UnknownHostException) pae.getException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            // if client not yet seen, make sure client allowed access
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            if (allowedAccessCache.get(clientHost) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                if (clientHost.isAnyLocalAddress()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                    throw new AccessException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                        "Registry." + op + " disallowed; origin unknown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                    final InetAddress finalClientHost = clientHost;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                    java.security.AccessController.doPrivileged(
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   232
                        new java.security.PrivilegedExceptionAction<Void>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   233
                            public Void run() throws java.io.IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                                 * if a ServerSocket can be bound to the client's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                                 * address then that address must be local
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                                (new ServerSocket(0, 10, finalClientHost)).close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                                allowedAccessCache.put(finalClientHost,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                                                       finalClientHost);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                    });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                } catch (PrivilegedActionException pae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                    // must have been an IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                    throw new AccessException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                        "Registry." + op + " disallowed; origin " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                        clientHost + " is non-local host");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        } catch (ServerNotActiveException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
             * Local call from this VM: allow access.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        } catch (java.net.UnknownHostException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            throw new AccessException("Registry." + op +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                                      " disallowed; origin is unknown host");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    public static ObjID getID() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        return id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * Retrieves text resources from the locale-specific properties file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    private static String getTextResource(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        if (resources == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                resources = ResourceBundle.getBundle(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                    "sun.rmi.registry.resources.rmiregistry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            } catch (MissingResourceException mre) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            if (resources == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                // throwing an Error is a bit extreme, methinks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                return ("[missing resource file: " + key + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        String val = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            val = resources.getString(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        } catch (MissingResourceException mre) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        if (val == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            return ("[missing resource: " + key + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            return (val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * Main program to start a registry. <br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * The port number can be specified on the command line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    public static void main(String args[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        // Create and install the security manager if one is not installed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        // already.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        if (System.getSecurityManager() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            System.setSecurityManager(new RMISecurityManager());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
             * Fix bugid 4147561: When JDK tools are executed, the value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
             * the CLASSPATH environment variable for the shell in which they
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
             * were invoked is no longer incorporated into the application
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
             * class path; CLASSPATH's only effect is to be the value of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
             * system property "env.class.path".  To preserve the previous
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
             * (JDK1.1 and JDK1.2beta3) behavior of this tool, however, its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
             * CLASSPATH should still be considered when resolving classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
             * being unmarshalled.  To effect this old behavior, a class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
             * loader that loads from the file path specified in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
             * "env.class.path" property is created and set to be the context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
             * class loader before the remote object is exported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            String envcp = System.getProperty("env.class.path");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            if (envcp == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                envcp = ".";            // preserve old default behavior
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            URL[] urls = sun.misc.URLClassPath.pathToURLs(envcp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            ClassLoader cl = new URLClassLoader(urls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
             * Fix bugid 4242317: Classes defined by this class loader should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
             * be annotated with the value of the "java.rmi.server.codebase"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
             * property, not the "file:" URLs for the CLASSPATH elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            sun.rmi.server.LoaderHandler.registerCodebaseLoader(cl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            Thread.currentThread().setContextClassLoader(cl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            int regPort = Registry.REGISTRY_PORT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            if (args.length >= 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                regPort = Integer.parseInt(args[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            registry = new RegistryImpl(regPort);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            // prevent registry from exiting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                    Thread.sleep(Long.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        } catch (NumberFormatException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            System.err.println(MessageFormat.format(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                getTextResource("rmiregistry.port.badnumber"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                args[0] ));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            System.err.println(MessageFormat.format(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                getTextResource("rmiregistry.usage"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                "rmiregistry" ));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
}