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