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