src/jdk.naming.rmi/share/classes/com/sun/jndi/url/rmi/rmiURLContext.java
author mchung
Mon, 12 Feb 2018 11:40:19 -0800
changeset 48849 64a52906b71f
parent 47216 71c04702a3d5
permissions -rw-r--r--
8168682: jdk/test/java/lang/ClassLoader/forNameLeak/ClassForNameLeak.java fails with -Xcomp Reviewed-by: bchristi
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.url.rmi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.naming.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.naming.spi.ResolveResult;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import com.sun.jndi.toolkit.url.GenericURLContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import com.sun.jndi.rmi.registry.RegistryContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * An RMI URL context resolves names that are URLs of the form
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *   rmi://[host][:port][/[object]]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *   rmi:[/][object]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * If an object is specified, the URL resolves to the named object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * Otherwise, the URL resolves to the specified RMI registry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @author Scott Seligman
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
public class rmiURLContext extends GenericURLContext {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
    50
    public rmiURLContext(Hashtable<?,?> env) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        super(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * Resolves the registry portion of "url" to the corresponding
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * RMI registry, and returns the atomic object name as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * remaining name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     */
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
    59
    protected ResolveResult getRootURLContext(String url, Hashtable<?,?> env)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            throws NamingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        if (!url.startsWith("rmi:")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            throw (new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                    "rmiURLContext: name is not an RMI URL: " + url));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        // Parse the URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        String host = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        int port = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        String objName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        int i = 4;              // index into url, following the "rmi:"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        if (url.startsWith("//", i)) {          // parse "//host:port"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            i += 2;                             // skip past "//"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            int slash = url.indexOf('/', i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            if (slash < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                slash = url.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            if (url.startsWith("[", i)) {               // at IPv6 literal
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                int brac = url.indexOf(']', i + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                if (brac < 0 || brac > slash) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                    throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                        "rmiURLContext: name is an Invalid URL: " + url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                host = url.substring(i, brac + 1);      // include brackets
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                i = brac + 1;                           // skip past "[...]"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            } else {                                    // at host name or IPv4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                int colon = url.indexOf(':', i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                int hostEnd = (colon < 0 || colon > slash)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                    ? slash
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                    : colon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                if (i < hostEnd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                    host = url.substring(i, hostEnd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                i = hostEnd;                            // skip past host
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            if ((i + 1 < slash)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                if ( url.startsWith(":", i)) {       // parse port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                    i++;                             // skip past ":"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                    port = Integer.parseInt(url.substring(i, slash));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                    throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                        "rmiURLContext: name is an Invalid URL: " + url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            i = slash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        if ("".equals(host)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            host = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        if (url.startsWith("/", i)) {           // skip "/" before object name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        if (i < url.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            objName = url.substring(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        // Represent object name as empty or single-component composite name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        CompositeName remaining = new CompositeName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        if (objName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            remaining.add(objName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        // Debug
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        //System.out.println("host=" + host + " port=" + port +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        //                 " objName=" + remaining.toString() + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        // Create a registry context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        Context regCtx = new RegistryContext(host, port, env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        return (new ResolveResult(regCtx, remaining));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
}