src/jdk.naming.rmi/share/classes/com/sun/jndi/rmi/registry/RegistryContextFactory.java
author mbaesken
Wed, 06 Nov 2019 14:04:07 +0100
changeset 58959 b7b170ba3ba9
parent 47216 71c04702a3d5
permissions -rw-r--r--
8233328: fix minimal VM build on Linux s390x Reviewed-by: lucy, mdoerr
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
     2
 * Copyright (c) 1999, 2011, 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.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.naming.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.naming.spi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import com.sun.jndi.url.rmi.rmiURLContextFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * A RegistryContextFactory takes an RMI registry reference, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * creates the corresponding RMI object or registry context.  In
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * addition, it serves as the initial context factory when using an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * RMI registry as an initial context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * When an initial context is being created, the environment
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * property "java.naming.provider.url" should contain the RMI URL of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * the appropriate registry.  Otherwise, the default URL "rmi:" is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * An RMI registry reference contains one or more StringRefAddrs of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * type "URL", each containing a single RMI URL.  Other addresses
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * are ignored.  Multiple URLs represent alternative addresses for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * same logical resource.  The order of the addresses is not significant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * @author Scott Seligman
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
public class RegistryContextFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        implements ObjectFactory, InitialContextFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * The type of each address in an RMI registry reference.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    public final static String ADDRESS_TYPE = "URL";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    public Context getInitialContext(Hashtable<?,?> env) throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        if (env != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            env = (Hashtable) env.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        return URLToContext(getInitCtxURL(env), env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    public Object getObjectInstance(Object ref, Name name, Context nameCtx,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                                    Hashtable<?,?> env)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            throws NamingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        if (!isRegistryRef(ref)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
         * No need to clone env here.  If getObjectInstance()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
         * returns something other than a RegistryContext (which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
         * happens if you're looking up an object bound in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
         * registry, as opposed to looking up the registry itself),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
         * then the context is GCed right away and there's no need to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
         * clone the environment.  If getObjectInstance() returns a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
         * RegistryContext, then it still goes through
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
         * GenericURLContext, which calls RegistryContext.lookup()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
         * with an empty name, which clones the environment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        Object obj = URLsToObject(getURLs((Reference)ref), env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        if (obj instanceof RegistryContext) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            RegistryContext ctx = (RegistryContext)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            ctx.reference = (Reference)ref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        return obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
    98
    private static Context URLToContext(String url, Hashtable<?,?> env)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            throws NamingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        rmiURLContextFactory factory = new rmiURLContextFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        Object obj = factory.getObjectInstance(url, null, null, env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        if (obj instanceof Context) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            return (Context)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            throw (new NotContextException(url));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   111
    private static Object URLsToObject(String[] urls, Hashtable<?,?> env)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            throws NamingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        rmiURLContextFactory factory = new rmiURLContextFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        return factory.getObjectInstance(urls, null, null, env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * Reads environment to find URL of initial context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * The default URL is "rmi:".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     */
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   122
    private static String getInitCtxURL(Hashtable<?,?> env) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        final String defaultURL = "rmi:";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        String url = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        if (env != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            url = (String)env.get(Context.PROVIDER_URL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        return ((url != null) ? url : defaultURL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * Returns true if argument is an RMI registry reference.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    private static boolean isRegistryRef(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        if (!(obj instanceof Reference)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        String thisClassName = RegistryContextFactory.class.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        Reference ref = (Reference)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        return thisClassName.equals(ref.getFactoryClassName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * Returns the URLs contained within an RMI registry reference.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    private static String[] getURLs(Reference ref) throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        int size = 0;   // number of URLs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        String[] urls = new String[ref.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   155
        Enumeration<RefAddr> addrs = ref.getAll();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        while (addrs.hasMoreElements()) {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   157
            RefAddr addr = addrs.nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            if ((addr instanceof StringRefAddr) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                addr.getType().equals(ADDRESS_TYPE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                urls[size++] = (String)addr.getContent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        if (size == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            throw (new ConfigurationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                    "Reference contains no valid addresses"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        // Trim URL array down to size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        if (size == ref.size()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            return urls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        String[] urls2 = new String[size];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        System.arraycopy(urls, 0, urls2, 0, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        return urls2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
}