jdk/src/jdk.jvmstat/share/classes/sun/jvmstat/monitor/HostIdentifier.java
author avstepan
Mon, 08 Jun 2015 16:37:53 +0400
changeset 31061 fead7d86d75f
parent 28059 e576535359cc
child 32210 958d823579c3
permissions -rw-r--r--
8081517: minor cleanup for docs Summary: fix some tidy and javadoc warnings/errors Reviewed-by: lancea, rriggs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.jvmstat.monitor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * An abstraction that identifies a target host and communications
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * protocol. The HostIdentifier, or hostid, provides a convenient string
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * representation of the information needed to locate and communicate with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * a target host. The string, based on a {@link URI}, may specify the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * the communications protocol, host name, and protocol specific information
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * for a target host. The format for a HostIdentifier string is:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *       [<I>protocol</I>:][[<I>//</I>]<I>hostname</I>][<I>:port</I>][<I>/servername</I>]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * There are actually no required components of this string, as a null string
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * is interpreted to mean a local connection to the local host and is equivalent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * to the string <em>local://localhost</em>. The components of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * HostIdentifier are:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *   <li><p><tt>protocol</tt> - The communications protocol. If omitted,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *          and a hostname is not specified, then default local protocol,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *          <em>local:</em>, is assumed. If the protocol is omitted and a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *          hostname is specified then the default remote protocol,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *          <em>rmi:</em> is assumed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *       </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *   <li><p><tt>hostname</tt> - The hostname. If omitted, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *          <em>localhost</em> is assumed. If the protocol is also omitted,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *          then default local protocol <em>local:</em> is also assumed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *          If the hostname is not omitted but the protocol is omitted,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *          then the default remote protocol, <em>rmi:</em> is assumed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *       </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *   <li><p><tt>port</tt> - The port for the communications protocol.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *          Treatment of the <tt>port</tt> parameter is implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *          (protocol) specific. It is unused by the default local protocol,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *          <em>local:</em>. For the default remote protocol, <em>rmi:</em>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *          <tt>port</tt> indicates the port number of the <em>rmiregistry</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *          on the target host and defaults to port 1099.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *       </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *   <li><p><tt>servername</tt> - The treatment of the Path, Query, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *          Fragment components of the HostIdentifier are implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *          (protocol) dependent. These components are ignored by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *          default local protocol, <em>local:</em>. For the default remote
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *          protocol, <em>rmi</em>, the Path component is interpreted as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *          the name of the RMI remote object. The Query component may
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *          contain an access mode specifier <em>?mode=</em> specifying
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *          <em>"r"</em> or <em>"rw"</em> access (write access currently
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *          ignored). The Fragment part is ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *       </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * All HostIdentifier objects are represented as absolute, hierarchical URIs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * The constructors accept relative URIs, but these will generally be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * transformed into an absolute URI specifying a default protocol. A
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * HostIdentifier differs from a URI in that certain contractions and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * illicit syntactical constructions are allowed. The following are all
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * valid HostIdentifier strings:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * <ul>
31061
fead7d86d75f 8081517: minor cleanup for docs
avstepan
parents: 28059
diff changeset
    84
 *   <li>{@code <null>} - transformed into "//localhost"</li>
fead7d86d75f 8081517: minor cleanup for docs
avstepan
parents: 28059
diff changeset
    85
 *   <li>localhost - transformed into "//localhost"</li>
fead7d86d75f 8081517: minor cleanup for docs
avstepan
parents: 28059
diff changeset
    86
 *   <li>hostname - transformed into "//hostname"</li>
fead7d86d75f 8081517: minor cleanup for docs
avstepan
parents: 28059
diff changeset
    87
 *   <li>hostname:port - transformed into "//hostname:port"</li>
fead7d86d75f 8081517: minor cleanup for docs
avstepan
parents: 28059
diff changeset
    88
 *   <li>proto:hostname - transformed into "proto://hostname"</li>
fead7d86d75f 8081517: minor cleanup for docs
avstepan
parents: 28059
diff changeset
    89
 *   <li>proto:hostname:port - transformed into
fead7d86d75f 8081517: minor cleanup for docs
avstepan
parents: 28059
diff changeset
    90
 *          "proto://hostname:port"</li>
fead7d86d75f 8081517: minor cleanup for docs
avstepan
parents: 28059
diff changeset
    91
 *   <li>proto://hostname:port</li>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * @see URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * @see VmIdentifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * @author Brian Doherty
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
public class HostIdentifier {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    private URI uri;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * creates a canonical representation of the uriString. This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * performs certain translations depending on the type of URI generated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * by the string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    private URI canonicalize(String uriString) throws URISyntaxException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        if ((uriString == null) || (uriString.compareTo("localhost") == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            uriString = "//localhost";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            return new URI(uriString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        URI u = new URI(uriString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        if (u.isAbsolute()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            if (u.isOpaque()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                 * this code is here to deal with a special case. For ease of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                 * use, we'd like to be able to handle the case where the user
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                 * specifies hostname:port, not requiring the scheme part.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                 * This introduces some subtleties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                 *     hostname:port - scheme = hostname
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                 *                   - schemespecificpart = port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                 *                   - hostname = null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                 *                   - userinfo=null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                 * however, someone could also enter scheme:hostname:port and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                 * get into this code. the strategy is to consider this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                 * syntax illegal and provide some code to defend against it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                 * Basically, we test that the string contains only one ":"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                 * and that the ssp is numeric. If we get two colons, we will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                 * attempt to insert the "//" after the first colon and then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                 * try to create a URI from the resulting string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                String scheme = u.getScheme();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                String ssp = u.getSchemeSpecificPart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                String frag = u.getFragment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                URI u2 = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
24685
215fa91e1b4c 8044461: Cleanup new Boolean and single character strings
rriggs
parents: 5506
diff changeset
   140
                int c1index = uriString.indexOf(':');
215fa91e1b4c 8044461: Cleanup new Boolean and single character strings
rriggs
parents: 5506
diff changeset
   141
                int c2index = uriString.lastIndexOf(':');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                if (c2index != c1index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                     * this is the scheme:hostname:port case. Attempt to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                     * transform this to scheme://hostname:port. If a path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                     * part is part of the original strings, it will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                     * included in the SchemeSpecificPart. however, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                     * fragment part must be handled separately.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                    if (frag == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                        u2 = new URI(scheme + "://" + ssp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                        u2 = new URI(scheme + "://" + ssp + "#" + frag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                    return u2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                 * here we have the <string>:<string> case, possibly with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                 * optional path and fragment components. we assume that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                 * the part following the colon is a number. we don't check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                 * this condition here as it will get detected later anyway.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                u2 = new URI("//" + uriString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                return u2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                return u;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
             * This is the case where we were given a hostname followed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
             * by a path part, fragment part, or both a path and fragment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
             * part. The key here is that no scheme part was specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
             * For this case, if the scheme specific part does not begin
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
             * with "//", then we prefix the "//" to the given string and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
             * attempt to create a URI from the resulting string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            String ssp = u.getSchemeSpecificPart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            if (ssp.startsWith("//")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                return u;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                return new URI("//" + uriString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * Create a HostIdentifier instance from a string value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * @param uriString a string representing a target host. The syntax of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     *                  the string must conform to the rules specified in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     *                  class documentation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * @throws URISyntaxException Thrown when the uriString or its canonical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     *                            form is poorly formed. This exception may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     *                            get encapsulated into a MonitorException in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     *                            a future version.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    public HostIdentifier(String uriString) throws URISyntaxException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        uri = canonicalize(uriString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * Create a HostIdentifier instance from component parts of a URI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * @param scheme the {@link URI#getScheme} component of a URI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * @param authority the {@link URI#getAuthority} component of a URI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * @param path the {@link URI#getPath} component of a URI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * @param query the {@link URI#getQuery} component of a URI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * @param fragment the {@link URI#getFragment} component of a URI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * @throws URISyntaxException Thrown when the uriString or its canonical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     *                            form is poorly formed. This exception may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     *                            get encapsulated into a MonitorException in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     *                            a future version.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * @see URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    public HostIdentifier(String scheme, String authority, String path,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                          String query, String fragment)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
           throws URISyntaxException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        uri = new URI(scheme, authority, path, query, fragment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * Create a HostIdentifier instance from a VmIdentifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * The necessary components of the VmIdentifier are extracted and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * reassembled into a HostIdentifier. If a "file:" scheme (protocol)
28059
e576535359cc 8067377: My hobby: caning, then then canning, the the can-can
martin
parents: 25859
diff changeset
   229
     * is specified, the returned HostIdentifier will always be
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * equivalent to HostIdentifier("file://localhost").
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * @param vmid the VmIdentifier use to construct the HostIdentifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    public HostIdentifier(VmIdentifier vmid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
         * Extract all components of the VmIdentifier URI except the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
         * user-info part of the authority (the lvmid).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        String scheme = vmid.getScheme();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        String host = vmid.getHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        String authority = vmid.getAuthority();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        // check for 'file:' VmIdentifiers and handled as a special case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        if ((scheme != null) && (scheme.compareTo("file") == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                uri = new URI("file://localhost");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            } catch (URISyntaxException e) { };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        if ((host != null) && (host.compareTo(authority) == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
             * this condition occurs when the VmIdentifier specifies only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
             * the authority (i.e. the lvmid ), and not a host name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            host = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        if (scheme == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            if (host == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                scheme = "local";            // default local scheme
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                 * rmi is the default remote scheme. if the VmIdentifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                 * specifies some other protocol, this default is overridden.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                scheme = "rmi";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        sb.append(scheme).append("://");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        if (host == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            sb.append("localhost");          // default host name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            sb.append(host);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        int port = vmid.getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        if (port != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            sb.append(":").append(port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        String path = vmid.getPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        if ((path != null) && (path.length() != 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            sb.append(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        String query = vmid.getQuery();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        if (query != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            sb.append("?").append(query);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        String frag = vmid.getFragment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        if (frag != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            sb.append("#").append(frag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
           uri = new URI(sb.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        } catch (URISyntaxException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
           // shouldn't happen, as we were passed a valid VmIdentifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
           throw new RuntimeException("Internal Error", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * Resolve a VmIdentifier with this HostIdentifier. A VmIdentifier, such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * as <em>1234</em> or <em>1234@hostname</em> or any other string that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * omits certain components of the URI string may be valid, but is certainly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * incomplete. They are missing critical information for identifying the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * the communications protocol, target host, or other parameters. A
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * VmIdentifier of this form is considered <em>unresolved</em>. This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * uses components of the HostIdentifier to resolve the missing components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * of the VmIdentifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * Specified components of the unresolved VmIdentifier take precedence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * over their HostIdentifier counterparts. For example, if the VmIdentifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * indicates <em>1234@hostname:2099</em> and the HostIdentifier indicates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * <em>rmi://hostname:1099/</em>, then the resolved VmIdentifier will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * be <em>rmi://1234@hostname:2099</em>. Any component not explicitly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * specified or assumed by the HostIdentifier, will remain unresolved in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * resolved VmIdentifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     *  <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * A VmIdentifier specifying a <em>file:</em> scheme (protocol), is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * not changed in any way by this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * @param vmid the unresolved VmIdentifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * @return VmIdentifier - the resolved VmIdentifier. If vmid was resolved
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     *                        on entry to this method, then the returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     *                        VmIdentifier will be equal, but not identical, to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     *                        vmid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    public VmIdentifier resolve(VmIdentifier vmid)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
           throws URISyntaxException, MonitorException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        String scheme = vmid.getScheme();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        String host = vmid.getHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        String authority = vmid.getAuthority();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        if ((scheme != null) && (scheme.compareTo("file") == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            // don't attempt to resolve a file based VmIdentifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            return vmid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        if ((host != null) && (host.compareTo(authority) == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
             * this condition occurs when the VmIdentifier specifies only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
             * the authority (i.e. an lvmid), and not a host name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            host = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        if (scheme == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            scheme = getScheme();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        URI nuri = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 24685
diff changeset
   360
        StringBuilder sb = new StringBuilder();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        sb.append(scheme).append("://");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        String userInfo = vmid.getUserInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        if (userInfo != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            sb.append(userInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            sb.append(vmid.getAuthority());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        if (host == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            host = getHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        sb.append("@").append(host);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        int port = vmid.getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        if (port == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            port = getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        if (port != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            sb.append(":").append(port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        String path = vmid.getPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        if ((path == null) || (path.length() == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            path = getPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        if ((path != null) && (path.length() > 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            sb.append(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        String query = vmid.getQuery();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        if (query == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            query = getQuery();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        if (query != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            sb.append("?").append(query);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        String fragment = vmid.getFragment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        if (fragment == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            fragment = getFragment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        if (fragment != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            sb.append("#").append(fragment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        String s = sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        return new VmIdentifier(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * Return the Scheme, or protocol, portion of this HostIdentifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * @return String - the scheme for this HostIdentifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * @see URI#getScheme()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    public String getScheme() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        return uri.isAbsolute() ? uri.getScheme() : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * Return the Scheme Specific Part of this HostIdentifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * @return String - the scheme specific part for this HostIdentifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * @see URI#getSchemeSpecificPart()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    public String getSchemeSpecificPart() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        return  uri.getSchemeSpecificPart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * Return the User Info part of this HostIdentifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * @return String - the user info part for this HostIdentifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * @see URI#getUserInfo()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    public String getUserInfo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        return uri.getUserInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * Return the Host part of this HostIdentifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * @return String - the host part for this HostIdentifier, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     *                  "localhost" if the URI.getHost() returns null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * @see URI#getUserInfo()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    public String getHost() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        return (uri.getHost() == null) ? "localhost" : uri.getHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * Return the Port for of this HostIdentifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * @return String - the port for this HostIdentifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * @see URI#getPort()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    public int getPort() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        return uri.getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * Return the Path part of this HostIdentifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * @return String - the path part for this HostIdentifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * @see URI#getPath()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    public String getPath() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        return uri.getPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * Return the Query part of this HostIdentifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * @return String - the query part for this HostIdentifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * @see URI#getQuery()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    public String getQuery() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        return uri.getQuery();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * Return the Fragment part of this HostIdentifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * @return String - the fragment part for this HostIdentifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * @see URI#getFragment()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    public String getFragment() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        return uri.getFragment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * Return the mode indicated in this HostIdentifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * @return String - the mode string. If no mode is specified, then "r"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     *                  is returned. otherwise, the specified mode is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    public String getMode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        String query = getQuery();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        if (query != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            String[] queryArgs = query.split("\\+");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            for (int i = 0; i < queryArgs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                if (queryArgs[i].startsWith("mode=")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                    int index = queryArgs[i].indexOf('=');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                    return queryArgs[i].substring(index+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        return "r";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * Return the URI associated with the HostIdentifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * @return URI - the URI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * @see URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    public URI getURI() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        return uri;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * Return the hash code for this HostIdentifier. The hash code is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * identical to the hash code for the contained URI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * @return int - the hashcode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * @see URI#hashCode()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        return uri.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * Test for quality with other objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * @param object the object to be test for equality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * @return boolean - returns true if the given object is of type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     *                   HostIdentifier and its URI field is equal to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     *                   object's URI field. Otherwise, returns false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * @see URI#equals(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    public boolean equals(Object object) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        if (object == this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        if (!(object instanceof HostIdentifier)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        return uri.equals(((HostIdentifier)object).uri);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * Convert to a string representation. Conversion is identical to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * calling getURI().toString(). This may change in a future release.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * @return String - a String representation of the HostIdentifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * @see URI#toString()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        return uri.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
}