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