jdk/src/share/classes/javax/management/remote/JMXServiceURL.java
author xdono
Wed, 02 Jul 2008 12:55:45 -0700
changeset 715 f16baef3a20e
parent 526 61ba2d5ea9da
child 1153 6b88c071a015
permissions -rw-r--r--
6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 526
diff changeset
     2
 * Copyright 2002-2008 Sun Microsystems, Inc.  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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
package javax.management.remote;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import com.sun.jmx.remote.util.ClassLogger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import com.sun.jmx.remote.util.EnvHelp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
38
dea12bbfb498 6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents: 36
diff changeset
    33
import java.beans.ConstructorProperties;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.net.InetAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.net.MalformedURLException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.net.UnknownHostException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.BitSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.StringTokenizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <p>The address of a JMX API connector server.  Instances of this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * are immutable.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <p>The address is an <em>Abstract Service URL</em> for SLP, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * defined in RFC 2609 and amended by RFC 3111.  It must look like
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * this:</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <code>service:jmx:<em>protocol</em>:<em>sap</em></code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * <p>Here, <code><em>protocol</em></code> is the transport
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * protocol to be used to connect to the connector server.  It is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * a string of one or more ASCII characters, each of which is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * letter, a digit, or one of the characters <code>+</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * <code>-</code>.  The first character must be a letter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * Uppercase letters are converted into lowercase ones.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * <p><code><em>sap</em></code> is the address at which the connector
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * server is found.  This address uses a subset of the syntax defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * by RFC 2609 for IP-based protocols.  It is a subset because the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * <code>user@host</code> syntax is not supported.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * <p>The other syntaxes defined by RFC 2609 are not currently
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * supported by this class.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * <p>The supported syntax is:</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * <code>//<em>[host[</em>:<em>port]][url-path]</em></code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * <p>Square brackets <code>[]</code> indicate optional parts of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * the address.  Not all protocols will recognize all optional
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * parts.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * <p>The <code><em>host</em></code> is a host name, an IPv4 numeric
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * host address, or an IPv6 numeric address enclosed in square
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * brackets.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * <p>The <code><em>port</em></code> is a decimal port number.  0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * means a default or anonymous port, depending on the protocol.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * <p>The <code><em>host</em></code> and <code><em>port</em></code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * can be omitted.  The <code><em>port</em></code> cannot be supplied
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * without a <code><em>host</em></code>.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * <p>The <code><em>url-path</em></code>, if any, begins with a slash
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * (<code>/</code>) or a semicolon (<code>;</code>) and continues to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * the end of the address.  It can contain attributes using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * semicolon syntax specified in RFC 2609.  Those attributes are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * parsed by this class and incorrect attribute syntax is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * detected.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * <p>Although it is legal according to RFC 2609 to have a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * <code><em>url-path</em></code> that begins with a semicolon, not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * all implementations of SLP allow it, so it is recommended to avoid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * that syntax.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * <p>Case is not significant in the initial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * <code>service:jmx:<em>protocol</em></code> string or in the host
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * part of the address.  Depending on the protocol, case can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * significant in the <code><em>url-path</em></code>.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * @see <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * href="ftp://ftp.rfc-editor.org/in-notes/rfc2609.txt">RFC 2609,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * "Service Templates and <code>Service:</code> Schemes"</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * @see <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * href="ftp://ftp.rfc-editor.org/in-notes/rfc3111.txt">RFC 3111,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * "Service Location Protocol Modifications for IPv6"</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
public class JMXServiceURL implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    private static final long serialVersionUID = 8173364409860779292L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * <p>Constructs a <code>JMXServiceURL</code> by parsing a Service URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * string.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * @param serviceURL the URL string to be parsed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @exception NullPointerException if <code>serviceURL</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @exception MalformedURLException if <code>serviceURL</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * does not conform to the syntax for an Abstract Service URL or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * if it is not a valid name for a JMX Remote API service.  A
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * <code>JMXServiceURL</code> must begin with the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * <code>"service:jmx:"</code> (case-insensitive).  It must not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * contain any characters that are not printable ASCII characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    public JMXServiceURL(String serviceURL) throws MalformedURLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        final int serviceURLLength = serviceURL.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        /* Check that there are no non-ASCII characters in the URL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
           following RFC 2609.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        for (int i = 0; i < serviceURLLength; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            char c = serviceURL.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            if (c < 32 || c >= 127) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                throw new MalformedURLException("Service URL contains " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                                                "non-ASCII character 0x" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                                                Integer.toHexString(c));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        // Parse the required prefix
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        final String requiredPrefix = "service:jmx:";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        final int requiredPrefixLength = requiredPrefix.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        if (!serviceURL.regionMatches(true, // ignore case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                                      0,    // serviceURL offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                                      requiredPrefix,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                                      0,    // requiredPrefix offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                                      requiredPrefixLength)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            throw new MalformedURLException("Service URL must start with " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                                            requiredPrefix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        // Parse the protocol name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        final int protoStart = requiredPrefixLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        final int protoEnd = indexOf(serviceURL, ':', protoStart);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        this.protocol =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            serviceURL.substring(protoStart, protoEnd).toLowerCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        if (!serviceURL.regionMatches(protoEnd, "://", 0, 3)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            throw new MalformedURLException("Missing \"://\" after " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                                            "protocol name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        // Parse the host name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        final int hostStart = protoEnd + 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        final int hostEnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        if (hostStart < serviceURLLength
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            && serviceURL.charAt(hostStart) == '[') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            hostEnd = serviceURL.indexOf(']', hostStart) + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            if (hostEnd == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                throw new MalformedURLException("Bad host name: [ without ]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            this.host = serviceURL.substring(hostStart + 1, hostEnd - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            if (!isNumericIPv6Address(this.host)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                throw new MalformedURLException("Address inside [...] must " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                                                "be numeric IPv6 address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            hostEnd =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                indexOfFirstNotInSet(serviceURL, hostNameBitSet, hostStart);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            this.host = serviceURL.substring(hostStart, hostEnd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        // Parse the port number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        final int portEnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        if (hostEnd < serviceURLLength && serviceURL.charAt(hostEnd) == ':') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            if (this.host.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                throw new MalformedURLException("Cannot give port number " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                                                "without host name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            final int portStart = hostEnd + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            portEnd =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                indexOfFirstNotInSet(serviceURL, numericBitSet, portStart);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            final String portString = serviceURL.substring(portStart, portEnd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                this.port = Integer.parseInt(portString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            } catch (NumberFormatException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                throw new MalformedURLException("Bad port number: \"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                                                portString + "\": " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            portEnd = hostEnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            this.port = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        // Parse the URL path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        final int urlPathStart = portEnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        if (urlPathStart < serviceURLLength)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            this.urlPath = serviceURL.substring(urlPathStart);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            this.urlPath = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        validate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * <p>Constructs a <code>JMXServiceURL</code> with the given protocol,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * host, and port.  This constructor is equivalent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * {@link #JMXServiceURL(String, String, int, String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * JMXServiceURL(protocol, host, port, null)}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * @param protocol the protocol part of the URL.  If null, defaults
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * to <code>jmxmp</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * @param host the host part of the URL.  If null, defaults to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * local host name, as determined by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * <code>InetAddress.getLocalHost().getHostName()</code>.  If it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * is a numeric IPv6 address, it can optionally be enclosed in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * square brackets <code>[]</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * @param port the port part of the URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * @exception MalformedURLException if one of the parts is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * syntactically incorrect, or if <code>host</code> is null and it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * is not possible to find the local host name, or if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * <code>port</code> is negative.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    public JMXServiceURL(String protocol, String host, int port)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            throws MalformedURLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        this(protocol, host, port, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * <p>Constructs a <code>JMXServiceURL</code> with the given parts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * @param protocol the protocol part of the URL.  If null, defaults
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * to <code>jmxmp</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * @param host the host part of the URL.  If null, defaults to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * local host name, as determined by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * <code>InetAddress.getLocalHost().getHostName()</code>.  If it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * is a numeric IPv6 address, it can optionally be enclosed in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * square brackets <code>[]</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * @param port the port part of the URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * @param urlPath the URL path part of the URL.  If null, defaults to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * the empty string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * @exception MalformedURLException if one of the parts is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * syntactically incorrect, or if <code>host</code> is null and it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * is not possible to find the local host name, or if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * <code>port</code> is negative.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     */
36
86095028f162 6607114: Make JMXServiceURL reconstructible in MXBeans
emcmanus
parents: 2
diff changeset
   276
    @ConstructorProperties({"protocol", "host", "port", "URLPath"})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    public JMXServiceURL(String protocol, String host, int port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                         String urlPath)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            throws MalformedURLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        if (protocol == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            protocol = "jmxmp";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        if (host == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            InetAddress local;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                local = InetAddress.getLocalHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            } catch (UnknownHostException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                throw new MalformedURLException("Local host name unknown: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                                                e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            host = local.getHostName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            /* We might have a hostname that violates DNS naming
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
               rules, for example that contains an `_'.  While we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
               could be strict and throw an exception, this is rather
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
               user-hostile.  Instead we use its numerical IP address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
               We can only reasonably do this for the host==null case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
               If we're given an explicit host name that is illegal we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
               have to reject it.  (Bug 5057532.)  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                validateHost(host);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            } catch (MalformedURLException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                if (logger.fineOn()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                    logger.fine("JMXServiceURL",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                                "Replacing illegal local host name " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                                host + " with numeric IP address " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                                "(see RFC 1034)", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                host = local.getHostAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                /* Use the numeric address, which could be either IPv4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                   or IPv6.  validateHost will accept either.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        if (host.startsWith("[")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            if (!host.endsWith("]")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                throw new MalformedURLException("Host starts with [ but " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                                                "does not end with ]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            host = host.substring(1, host.length() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            if (!isNumericIPv6Address(host)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                throw new MalformedURLException("Address inside [...] must " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                                                "be numeric IPv6 address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            if (host.startsWith("["))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                throw new MalformedURLException("More than one [[...]]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        this.protocol = protocol.toLowerCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        this.host = host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        this.port = port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        if (urlPath == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            urlPath = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        this.urlPath = urlPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        validate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    private void validate() throws MalformedURLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        // Check protocol
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        final int protoEnd = indexOfFirstNotInSet(protocol, protocolBitSet, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        if (protoEnd == 0 || protoEnd < protocol.length()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            || !alphaBitSet.get(protocol.charAt(0))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            throw new MalformedURLException("Missing or invalid protocol " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                                            "name: \"" + protocol + "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        // Check host
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        validateHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        // Check port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        if (port < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            throw new MalformedURLException("Bad port: " + port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        // Check URL path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        if (urlPath.length() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            if (!urlPath.startsWith("/") && !urlPath.startsWith(";"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                throw new MalformedURLException("Bad URL path: " + urlPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    private void validateHost() throws MalformedURLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        if (host.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            if (port != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                throw new MalformedURLException("Cannot give port number " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                                                "without host name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        validateHost(host);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    private static void validateHost(String h)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            throws MalformedURLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        if (isNumericIPv6Address(h)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            /* We assume J2SE >= 1.4 here.  Otherwise you can't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
               use the address anyway.  We can't call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
               InetAddress.getByName without checking for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
               numeric IPv6 address, because we mustn't try to do
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
               a DNS lookup in case the address is not actually
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
               numeric.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                InetAddress.getByName(h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                /* We should really catch UnknownHostException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                   here, but a bug in JDK 1.4 causes it to throw
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                   ArrayIndexOutOfBoundsException, e.g. if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                   string is ":".  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                MalformedURLException bad =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                    new MalformedURLException("Bad IPv6 address: " + h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                EnvHelp.initCause(bad, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                throw bad;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            /* Tiny state machine to check valid host name.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
               checks the hostname grammar from RFC 1034 (DNS),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
               page 11.  A hostname is a dot-separated list of one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
               or more labels, where each label consists of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
               letters, numbers, or hyphens.  A label cannot begin
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
               or end with a hyphen.  Empty hostnames are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
               allowed.  Note that numeric IPv4 addresses are a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
               special case of this grammar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
               The state is entirely captured by the last
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
               character seen, with a virtual `.' preceding the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
               name.  We represent any alphanumeric character by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
               `a'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
               We need a special hack to check, as required by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
               RFC 2609 (SLP) grammar, that the last component of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
               the hostname begins with a letter.  Respecting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
               intent of the RFC, we only do this if there is more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
               than one component.  If your local hostname begins
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
               with a digit, we don't reject it.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            final int hostLen = h.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            char lastc = '.';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            boolean sawDot = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            char componentStart = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            loop:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            for (int i = 0; i < hostLen; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                char c = h.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                boolean isAlphaNumeric = alphaNumericBitSet.get(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                if (lastc == '.')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                    componentStart = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                if (isAlphaNumeric)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                    lastc = 'a';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                else if (c == '-') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                    if (lastc == '.')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                        break; // will throw exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                    lastc = '-';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                } else if (c == '.') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                    sawDot = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                    if (lastc != 'a')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                        break; // will throw exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                    lastc = '.';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                    lastc = '.'; // will throw exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                    break;
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
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                if (lastc != 'a')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                    throw randomException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                if (sawDot && !alphaBitSet.get(componentStart)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                    /* Must be a numeric IPv4 address.  In addition to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                       the explicitly-thrown exceptions, we can get
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                       NoSuchElementException from the calls to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                       tok.nextToken and NumberFormatException from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                       the call to Integer.parseInt.  Using exceptions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                       for control flow this way is a bit evil but it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                       does simplify things enormously.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                    StringTokenizer tok = new StringTokenizer(h, ".", true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                    for (int i = 0; i < 4; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                        String ns = tok.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                        int n = Integer.parseInt(ns);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                        if (n < 0 || n > 255)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                            throw randomException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                        if (i < 3 && !tok.nextToken().equals("."))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                            throw randomException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                    if (tok.hasMoreTokens())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                        throw randomException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                throw new MalformedURLException("Bad host: \"" + h + "\"");
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
    private static final Exception randomException = new Exception();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * <p>The protocol part of the Service URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * @return the protocol part of the Service URL.  This is never null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    public String getProtocol() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        return protocol;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * <p>The host part of the Service URL.  If the Service URL was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * constructed with the constructor that takes a URL string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * parameter, the result is the substring specifying the host in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * that URL.  If the Service URL was constructed with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * constructor that takes a separate host parameter, the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * the string that was specified.  If that string was null, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * <code>InetAddress.getLocalHost().getHostName()</code>.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * <p>In either case, if the host was specified using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * <code>[...]</code> syntax for numeric IPv6 addresses, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * square brackets are not included in the return value here.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * @return the host part of the Service URL.  This is never null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    public String getHost() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        return host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * <p>The port of the Service URL.  If no port was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * specified, the returned value is 0.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * @return the port of the Service URL, or 0 if none.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    public int getPort() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        return port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * <p>The URL Path part of the Service URL.  This is an empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * string, or a string beginning with a slash (<code>/</code>), or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * a string beginning with a semicolon (<code>;</code>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * @return the URL Path part of the Service URL.  This is never
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    public String getURLPath() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        return urlPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * <p>The string representation of this Service URL.  If the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * returned by this method is supplied to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * <code>JMXServiceURL</code> constructor, the resultant object is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * equal to this one.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * <p>The <code><em>host</em></code> part of the returned string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * is the value returned by {@link #getHost()}.  If that value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * specifies a numeric IPv6 address, it is surrounded by square
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * brackets <code>[]</code>.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     * <p>The <code><em>port</em></code> part of the returned string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * is the value returned by {@link #getPort()} in its shortest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * decimal form.  If the value is zero, it is omitted.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * @return the string representation of this Service URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        /* We don't bother synchronizing the access to toString.  At worst,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
           n threads will independently compute and store the same value.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        if (toString != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            return toString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        StringBuilder buf = new StringBuilder("service:jmx:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        buf.append(getProtocol()).append("://");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        final String getHost = getHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        if (isNumericIPv6Address(getHost))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            buf.append('[').append(getHost).append(']');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            buf.append(getHost);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        final int getPort = getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        if (getPort != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            buf.append(':').append(getPort);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        buf.append(getURLPath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        toString = buf.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        return toString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * <p>Indicates whether some other object is equal to this one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * This method returns true if and only if <code>obj</code> is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * instance of <code>JMXServiceURL</code> whose {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * #getProtocol()}, {@link #getHost()}, {@link #getPort()}, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * {@link #getURLPath()} methods return the same values as for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * this object.  The values for {@link #getProtocol()} and {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * #getHost()} can differ in case without affecting equality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * @param obj the reference object with which to compare.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * @return <code>true</code> if this object is the same as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * <code>obj</code> argument; <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        if (!(obj instanceof JMXServiceURL))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        JMXServiceURL u = (JMXServiceURL) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
            (u.getProtocol().equalsIgnoreCase(getProtocol()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
             u.getHost().equalsIgnoreCase(getHost()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
             u.getPort() == getPort() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
             u.getURLPath().equals(getURLPath()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        return toString().hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    /* True if this string, assumed to be a valid argument to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * InetAddress.getByName, is a numeric IPv6 address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    private static boolean isNumericIPv6Address(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        // address contains colon if and only if it's a numeric IPv6 address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        return (s.indexOf(':') >= 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    // like String.indexOf but returns string length not -1 if not present
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    private static int indexOf(String s, char c, int fromIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        int index = s.indexOf(c, fromIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        if (index < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            return s.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            return index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
    private static int indexOfFirstNotInSet(String s, BitSet set,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                                            int fromIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        final int slen = s.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        int i = fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
            if (i >= slen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            char c = s.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            if (c >= 128)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                break; // not ASCII
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
            if (!set.get(c))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
            i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
    private final static BitSet alphaBitSet = new BitSet(128);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
    private final static BitSet numericBitSet = new BitSet(128);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
    private final static BitSet alphaNumericBitSet = new BitSet(128);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
    private final static BitSet protocolBitSet = new BitSet(128);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
    private final static BitSet hostNameBitSet = new BitSet(128);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        /* J2SE 1.4 adds lots of handy methods to BitSet that would
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
           allow us to simplify here, e.g. by not writing loops, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
           we want to work on J2SE 1.3 too.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        for (char c = '0'; c <= '9'; c++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            numericBitSet.set(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        for (char c = 'A'; c <= 'Z'; c++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
            alphaBitSet.set(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        for (char c = 'a'; c <= 'z'; c++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
            alphaBitSet.set(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        alphaNumericBitSet.or(alphaBitSet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        alphaNumericBitSet.or(numericBitSet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        protocolBitSet.or(alphaNumericBitSet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        protocolBitSet.set('+');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        protocolBitSet.set('-');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        hostNameBitSet.or(alphaNumericBitSet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        hostNameBitSet.set('-');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        hostNameBitSet.set('.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * The value returned by {@link #getProtocol()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
    private final String protocol;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * The value returned by {@link #getHost()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
    private final String host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     * The value returned by {@link #getPort()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
    private final int port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * The value returned by {@link #getURLPath()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    private final String urlPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * Cached result of {@link #toString()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
    private transient String toString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
    private static final ClassLogger logger =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        new ClassLogger("javax.management.remote.misc", "JMXServiceURL");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
}