src/jdk.naming.rmi/share/classes/com/sun/jndi/rmi/registry/RegistryContext.java
author prappo
Mon, 19 Mar 2018 12:24:07 +0000
branchhttp-client-branch
changeset 56319 bb9e25a8ea04
parent 47216 71c04702a3d5
permissions -rw-r--r--
http-client-branch: (HttpClient) faulty assertion, some tests fail
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
44534
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 43208
diff changeset
     2
 * Copyright (c) 1999, 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: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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 com.sun.jndi.rmi.registry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Properties;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.rmi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.rmi.server.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.rmi.registry.Registry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.rmi.registry.LocateRegistry;
43208
abc45540077d 8158997: JNDI Protocols Switch
vinnie
parents: 28114
diff changeset
    35
import java.security.AccessController;
abc45540077d 8158997: JNDI Protocols Switch
vinnie
parents: 28114
diff changeset
    36
import java.security.PrivilegedAction;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.naming.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.naming.spi.NamingManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * A RegistryContext is a context representing a remote RMI registry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @author Scott Seligman
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
public class RegistryContext implements Context, Referenceable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 7668
diff changeset
    51
    private Hashtable<String, Object> environment;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private Registry registry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private String host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private int port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private static final NameParser nameParser = new AtomicNameParser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private static final String SOCKET_FACTORY = "com.sun.jndi.rmi.factory.socket";
43208
abc45540077d 8158997: JNDI Protocols Switch
vinnie
parents: 28114
diff changeset
    57
    /**
abc45540077d 8158997: JNDI Protocols Switch
vinnie
parents: 28114
diff changeset
    58
     * Determines whether classes may be loaded from an arbitrary URL code base.
abc45540077d 8158997: JNDI Protocols Switch
vinnie
parents: 28114
diff changeset
    59
     */
abc45540077d 8158997: JNDI Protocols Switch
vinnie
parents: 28114
diff changeset
    60
    static final boolean trustURLCodebase;
abc45540077d 8158997: JNDI Protocols Switch
vinnie
parents: 28114
diff changeset
    61
    static {
abc45540077d 8158997: JNDI Protocols Switch
vinnie
parents: 28114
diff changeset
    62
        // System property to control whether classes may be loaded from an
abc45540077d 8158997: JNDI Protocols Switch
vinnie
parents: 28114
diff changeset
    63
        // arbitrary URL codebase
abc45540077d 8158997: JNDI Protocols Switch
vinnie
parents: 28114
diff changeset
    64
        PrivilegedAction<String> act = () -> System.getProperty(
abc45540077d 8158997: JNDI Protocols Switch
vinnie
parents: 28114
diff changeset
    65
            "com.sun.jndi.rmi.object.trustURLCodebase", "false");
abc45540077d 8158997: JNDI Protocols Switch
vinnie
parents: 28114
diff changeset
    66
        String trust = AccessController.doPrivileged(act);
abc45540077d 8158997: JNDI Protocols Switch
vinnie
parents: 28114
diff changeset
    67
        trustURLCodebase = "true".equalsIgnoreCase(trust);
abc45540077d 8158997: JNDI Protocols Switch
vinnie
parents: 28114
diff changeset
    68
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    Reference reference = null; // ref used to create this context, if any
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    // Environment property that, if set, indicates that a security
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    // manager should be installed (if none is already in place).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    public static final String SECURITY_MGR =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            "java.naming.rmi.security.manager";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * Returns a context for the registry at a given host and port.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * If "host" is null, uses default host.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * If "port" is non-positive, uses default port.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * Cloning of "env" is handled by caller; see comments within
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * RegistryContextFactory.getObjectInstance(), for example.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     */
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 7668
diff changeset
    84
    @SuppressWarnings("unchecked")
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 7668
diff changeset
    85
    public RegistryContext(String host, int port, Hashtable<?, ?> env)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            throws NamingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 7668
diff changeset
    88
        environment = (env == null)
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 7668
diff changeset
    89
                      ? new Hashtable<String, Object>(5)
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 7668
diff changeset
    90
                      : (Hashtable<String, Object>) env;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        if (environment.get(SECURITY_MGR) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            installSecurityMgr();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        // chop off '[' and ']' in an IPv6 literal address
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        if ((host != null) && (host.charAt(0) == '[')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            host = host.substring(1, host.length() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        RMIClientSocketFactory socketFactory =
6121
236dc8767612 6676075: RegistryContext (com.sun.jndi.url.rmi.rmiURLContext) coding problem
vinnie
parents: 5506
diff changeset
   101
                (RMIClientSocketFactory) environment.get(SOCKET_FACTORY);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        registry = getRegistry(host, port, socketFactory);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        this.host = host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        this.port = port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * Returns a clone of a registry context.  The context's private state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * is independent of the original's (so closing one context, for example,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * won't close the other).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    // %%% Alternatively, this could be done with a clone() method.
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 7668
diff changeset
   113
    @SuppressWarnings("unchecked") // clone()
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    RegistryContext(RegistryContext ctx) {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 7668
diff changeset
   115
        environment = (Hashtable<String, Object>)ctx.environment.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        registry = ctx.registry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        host = ctx.host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        port = ctx.port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        reference = ctx.reference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
44534
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 43208
diff changeset
   122
    @SuppressWarnings("deprecation")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    protected void finalize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    public Object lookup(Name name) throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        if (name.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            return (new RegistryContext(this));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        Remote obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            obj = registry.lookup(name.get(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        } catch (NotBoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            throw (new NameNotFoundException(name.get(0)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        } catch (RemoteException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            throw (NamingException)wrapRemoteException(e).fillInStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        return (decodeObject(obj, name.getPrefix(1)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    public Object lookup(String name) throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        return lookup(new CompositeName(name));
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
     * If the object to be bound is both Remote and Referenceable, binds the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * object itself, not its Reference.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    public void bind(Name name, Object obj) throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        if (name.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            throw (new InvalidNameException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                    "RegistryContext: Cannot bind empty name"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            registry.bind(name.get(0), encodeObject(obj, name.getPrefix(1)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        } catch (AlreadyBoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            NamingException ne = new NameAlreadyBoundException(name.get(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            ne.setRootCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            throw ne;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        } catch (RemoteException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            throw (NamingException)wrapRemoteException(e).fillInStackTrace();
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
    public void bind(String name, Object obj) throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        bind(new CompositeName(name), obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    public void rebind(Name name, Object obj) throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        if (name.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            throw (new InvalidNameException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                    "RegistryContext: Cannot rebind empty name"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            registry.rebind(name.get(0), encodeObject(obj, name.getPrefix(1)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        } catch (RemoteException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            throw (NamingException)wrapRemoteException(e).fillInStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    public void rebind(String name, Object obj) throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        rebind(new CompositeName(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
    public void unbind(Name name) throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        if (name.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            throw (new InvalidNameException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                    "RegistryContext: Cannot unbind empty name"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            registry.unbind(name.get(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        } catch (NotBoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            // method is idempotent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        } catch (RemoteException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            throw (NamingException)wrapRemoteException(e).fillInStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    public void unbind(String name) throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        unbind(new CompositeName(name));
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
     * Rename is implemented by this sequence of operations:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * lookup, bind, unbind.  The sequence is not performed atomically.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    public void rename(Name oldName, Name newName) throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        bind(newName, lookup(oldName));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        unbind(oldName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    public void rename(String name, String newName) throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        rename(new CompositeName(name), new CompositeName(newName));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 7668
diff changeset
   217
    public NamingEnumeration<NameClassPair> list(Name name) throws
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 7668
diff changeset
   218
            NamingException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        if (!name.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            throw (new InvalidNameException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                    "RegistryContext: can only list \"\""));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            String[] names = registry.list();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            return (new NameClassPairEnumeration(names));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        } catch (RemoteException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            throw (NamingException)wrapRemoteException(e).fillInStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 7668
diff changeset
   231
    public NamingEnumeration<NameClassPair> list(String name) throws
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 7668
diff changeset
   232
            NamingException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        return list(new CompositeName(name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 7668
diff changeset
   236
    public NamingEnumeration<Binding> listBindings(Name name)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            throws NamingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        if (!name.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            throw (new InvalidNameException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                    "RegistryContext: can only list \"\""));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            String[] names = registry.list();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            return (new BindingEnumeration(this, names));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        } catch (RemoteException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            throw (NamingException)wrapRemoteException(e).fillInStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 7668
diff changeset
   251
    public NamingEnumeration<Binding> listBindings(String name) throws
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 7668
diff changeset
   252
            NamingException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        return listBindings(new CompositeName(name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    public void destroySubcontext(Name name) throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        throw (new OperationNotSupportedException());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    public void destroySubcontext(String name) throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        throw (new OperationNotSupportedException());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    public Context createSubcontext(Name name) throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        throw (new OperationNotSupportedException());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    public Context createSubcontext(String name) throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        throw (new OperationNotSupportedException());
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 Object lookupLink(Name name) throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        return lookup(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    public Object lookupLink(String name) throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        return lookup(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    public NameParser getNameParser(Name name) throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        return nameParser;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    public NameParser getNameParser(String name) throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        return nameParser;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    public Name composeName(Name name, Name prefix) throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        Name result = (Name)prefix.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        return result.addAll(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    public String composeName(String name, String prefix)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            throws NamingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        return composeName(new CompositeName(name),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                           new CompositeName(prefix)).toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    public Object removeFromEnvironment(String propName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            throws NamingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        return environment.remove(propName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    public Object addToEnvironment(String propName, Object propVal)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            throws NamingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        if (propName.equals(SECURITY_MGR)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            installSecurityMgr();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        return environment.put(propName, propVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 7668
diff changeset
   315
    @SuppressWarnings("unchecked") // clone()
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 7668
diff changeset
   316
    public Hashtable<String, Object> getEnvironment() throws NamingException {
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 7668
diff changeset
   317
        return (Hashtable<String, Object>)environment.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    public void close() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        environment = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        registry = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        // &&& If we were caching registry connections, we would probably
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        // uncache this one now.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    public String getNameInNamespace() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        return ""; // Registry has an empty name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * Returns an RMI registry reference for this context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * If this context was created from a reference, that reference is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * returned.  Otherwise, an exception is thrown if the registry's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * host is "localhost" or the default (null).  Although this could
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * possibly make for a valid reference, it's far more likely to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * an easily made error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * @see RegistryContextFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    public Reference getReference() throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        if (reference != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            return (Reference)reference.clone();  // %%% clone the addrs too?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        if (host == null || host.equals("localhost")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            throw (new ConfigurationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                    "Cannot create a reference for an RMI registry whose " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                    "host was unspecified or specified as \"localhost\""));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        String url = "rmi://";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        // Enclose IPv6 literal address in '[' and ']'
24685
215fa91e1b4c 8044461: Cleanup new Boolean and single character strings
rriggs
parents: 10324
diff changeset
   354
        url = (host.indexOf(':') > -1) ? url + "[" + host + "]" :
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                                         url + host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        if (port > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            url += ":" + Integer.toString(port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        RefAddr addr = new StringRefAddr(RegistryContextFactory.ADDRESS_TYPE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                                         url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        return (new Reference(RegistryContext.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                              addr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                              RegistryContextFactory.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                              null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * Wrap a RemoteException inside a NamingException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    public static NamingException wrapRemoteException(RemoteException re) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        NamingException ne;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        if (re instanceof ConnectException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            ne = new ServiceUnavailableException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        } else if (re instanceof AccessException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            ne = new NoPermissionException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        } else if (re instanceof StubNotFoundException ||
28114
7ab051865dd2 8066642: Fix deprecation warnings in jdk.naming module
prappo
parents: 27936
diff changeset
   382
                   re instanceof UnknownHostException) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            ne = new ConfigurationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        } else if (re instanceof ExportException ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                   re instanceof ConnectIOException ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                   re instanceof MarshalException ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                   re instanceof UnmarshalException ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                   re instanceof NoSuchObjectException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            ne = new CommunicationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        } else if (re instanceof ServerException &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                   re.detail instanceof RemoteException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            ne = wrapRemoteException((RemoteException)re.detail);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            ne = new NamingException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        ne.setRootCause(re);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        return ne;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * Returns the registry at a given host, port and socket factory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * If "host" is null, uses default host.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * If "port" is non-positive, uses default port.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * If "socketFactory" is null, uses the default socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    private static Registry getRegistry(String host, int port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                RMIClientSocketFactory socketFactory)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            throws NamingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        // %%% We could cache registry connections here.  The transport layer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        // may already reuse connections.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            if (socketFactory == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                return LocateRegistry.getRegistry(host, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                return LocateRegistry.getRegistry(host, port, socketFactory);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        } catch (RemoteException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            throw (NamingException)wrapRemoteException(e).fillInStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * Attempts to install a security manager if none is currently in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * place.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    private static void installSecurityMgr() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        try {
28114
7ab051865dd2 8066642: Fix deprecation warnings in jdk.naming module
prappo
parents: 27936
diff changeset
   433
            System.setSecurityManager(new SecurityManager());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * Encodes an object prior to binding it in the registry.  First,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * NamingManager.getStateToBind() is invoked.  If the resulting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * object is Remote, it is returned.  If it is a Reference or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * Referenceable, the reference is wrapped in a Remote object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * Otherwise, an exception is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * @param name      The object's name relative to this context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    private Remote encodeObject(Object obj, Name name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            throws NamingException, RemoteException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        obj = NamingManager.getStateToBind(obj, name, this, environment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        if (obj instanceof Remote) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            return (Remote)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        if (obj instanceof Reference) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            return (new ReferenceWrapper((Reference)obj));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        if (obj instanceof Referenceable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            return (new ReferenceWrapper(((Referenceable)obj).getReference()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        throw (new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                "RegistryContext: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                "object to bind must be Remote, Reference, or Referenceable"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * Decodes an object that has been retrieved from the registry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * First, if the object is a RemoteReference, the Reference is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * unwrapped.  Then, NamingManager.getObjectInstance() is invoked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * @param name      The object's name relative to this context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    private Object decodeObject(Remote r, Name name) throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            Object obj = (r instanceof RemoteReference)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                        ? ((RemoteReference)r).getReference()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                        : (Object)r;
43208
abc45540077d 8158997: JNDI Protocols Switch
vinnie
parents: 28114
diff changeset
   478
abc45540077d 8158997: JNDI Protocols Switch
vinnie
parents: 28114
diff changeset
   479
            /*
abc45540077d 8158997: JNDI Protocols Switch
vinnie
parents: 28114
diff changeset
   480
             * Classes may only be loaded from an arbitrary URL codebase when
abc45540077d 8158997: JNDI Protocols Switch
vinnie
parents: 28114
diff changeset
   481
             * the system property com.sun.jndi.rmi.object.trustURLCodebase
abc45540077d 8158997: JNDI Protocols Switch
vinnie
parents: 28114
diff changeset
   482
             * has been set to "true".
abc45540077d 8158997: JNDI Protocols Switch
vinnie
parents: 28114
diff changeset
   483
             */
abc45540077d 8158997: JNDI Protocols Switch
vinnie
parents: 28114
diff changeset
   484
abc45540077d 8158997: JNDI Protocols Switch
vinnie
parents: 28114
diff changeset
   485
            // Use reference if possible
abc45540077d 8158997: JNDI Protocols Switch
vinnie
parents: 28114
diff changeset
   486
            Reference ref = null;
abc45540077d 8158997: JNDI Protocols Switch
vinnie
parents: 28114
diff changeset
   487
            if (obj instanceof Reference) {
abc45540077d 8158997: JNDI Protocols Switch
vinnie
parents: 28114
diff changeset
   488
                ref = (Reference) obj;
abc45540077d 8158997: JNDI Protocols Switch
vinnie
parents: 28114
diff changeset
   489
            } else if (obj instanceof Referenceable) {
abc45540077d 8158997: JNDI Protocols Switch
vinnie
parents: 28114
diff changeset
   490
                ref = ((Referenceable)(obj)).getReference();
abc45540077d 8158997: JNDI Protocols Switch
vinnie
parents: 28114
diff changeset
   491
            }
abc45540077d 8158997: JNDI Protocols Switch
vinnie
parents: 28114
diff changeset
   492
abc45540077d 8158997: JNDI Protocols Switch
vinnie
parents: 28114
diff changeset
   493
            if (ref != null && ref.getFactoryClassLocation() != null &&
abc45540077d 8158997: JNDI Protocols Switch
vinnie
parents: 28114
diff changeset
   494
                !trustURLCodebase) {
abc45540077d 8158997: JNDI Protocols Switch
vinnie
parents: 28114
diff changeset
   495
                throw new ConfigurationException(
abc45540077d 8158997: JNDI Protocols Switch
vinnie
parents: 28114
diff changeset
   496
                    "The object factory is untrusted. Set the system property" +
abc45540077d 8158997: JNDI Protocols Switch
vinnie
parents: 28114
diff changeset
   497
                    " 'com.sun.jndi.rmi.object.trustURLCodebase' to 'true'.");
abc45540077d 8158997: JNDI Protocols Switch
vinnie
parents: 28114
diff changeset
   498
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            return NamingManager.getObjectInstance(obj, name, this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                                                   environment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        } catch (NamingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        } catch (RemoteException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            throw (NamingException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                wrapRemoteException(e).fillInStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            NamingException ne = new NamingException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            ne.setRootCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            throw ne;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
 * A name parser for case-sensitive atomic names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
class AtomicNameParser implements NameParser {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    private static final Properties syntax = new Properties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    public Name parse(String name) throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        return (new CompoundName(name, syntax));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
/**
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 7668
diff changeset
   529
 * An enumeration of name / class-name pairs.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
 */
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 7668
diff changeset
   531
class NameClassPairEnumeration implements NamingEnumeration<NameClassPair> {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    private final String[] names;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    private int nextName;       // index into "names"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    NameClassPairEnumeration(String[] names) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        this.names = names;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        nextName = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    public boolean hasMore() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        return (nextName < names.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 7668
diff changeset
   544
    public NameClassPair next() throws NamingException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        if (!hasMore()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            throw (new java.util.NoSuchElementException());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        // Convert name to a one-element composite name, so embedded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        // meta-characters are properly escaped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        String name = names[nextName++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        Name cname = (new CompositeName()).add(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        NameClassPair ncp = new NameClassPair(cname.toString(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                                            "java.lang.Object");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        ncp.setNameInNamespace(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        return ncp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    public boolean hasMoreElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        return hasMore();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 7668
diff changeset
   562
    public NameClassPair nextElement() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            return next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        } catch (NamingException e) {   // should never happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            throw (new java.util.NoSuchElementException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                    "javax.naming.NamingException was thrown"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    public void close() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        nextName = names.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
 * An enumeration of Bindings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
 * The actual registry lookups are performed when next() is called.  It would
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
 * be nicer to defer this until the object (or its class name) is actually
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
 * requested.  The problem with that approach is that Binding.getObject()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
 * cannot throw NamingException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
 */
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 7668
diff changeset
   585
class BindingEnumeration implements NamingEnumeration<Binding> {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    private RegistryContext ctx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    private final String[] names;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    private int nextName;       // index into "names"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    BindingEnumeration(RegistryContext ctx, String[] names) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        // Clone ctx in case someone closes it before we're through.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        this.ctx = new RegistryContext(ctx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        this.names = names;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        nextName = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
44534
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 43208
diff changeset
   597
    @SuppressWarnings("deprecation")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
    protected void finalize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        ctx.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    public boolean hasMore() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        if (nextName >= names.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            ctx.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        return (nextName < names.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 7668
diff changeset
   609
    public Binding next() throws NamingException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        if (!hasMore()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            throw (new java.util.NoSuchElementException());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        // Convert name to a one-element composite name, so embedded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        // meta-characters are properly escaped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        String name = names[nextName++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        Name cname = (new CompositeName()).add(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        Object obj = ctx.lookup(cname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        String cnameStr = cname.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        Binding binding = new Binding(cnameStr, obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        binding.setNameInNamespace(cnameStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        return binding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
    public boolean hasMoreElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        return hasMore();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 7668
diff changeset
   629
    public Binding nextElement() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
            return next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        } catch (NamingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            throw (new java.util.NoSuchElementException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                    "javax.naming.NamingException was thrown"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
44534
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 43208
diff changeset
   638
    @SuppressWarnings("deprecation")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
    public void close () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        finalize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
}