0N/A/*
2362N/A * Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.jvmstat.monitor;
0N/A
0N/Aimport java.net.*;
0N/A
0N/A/**
0N/A * An abstraction that identifies a target host and communications
0N/A * protocol. The HostIdentifier, or hostid, provides a convenient string
0N/A * representation of the information needed to locate and communicate with
0N/A * a target host. The string, based on a {@link URI}, may specify the
0N/A * the communications protocol, host name, and protocol specific information
0N/A * for a target host. The format for a HostIdentifier string is:
0N/A * <pre>
0N/A * [<I>protocol</I>:][[<I>//</I>]<I>hostname</I>][<I>:port</I>][<I>/servername</I>]
0N/A * </pre>
0N/A * There are actually no required components of this string, as a null string
0N/A * is interpreted to mean a local connection to the local host and is equivalent
0N/A * to the string <em>local://localhost</em>. The components of the
0N/A * HostIdentifier are:
0N/A * <ul>
0N/A * <li><p><tt>protocol</tt> - The communications protocol. If omitted,
0N/A * and a hostname is not specified, then default local protocol,
0N/A * <em>local:</em>, is assumed. If the protocol is omitted and a
0N/A * hostname is specified then the default remote protocol,
0N/A * <em>rmi:</em> is assumed.
0N/A * </p></li>
0N/A * <li><p><tt>hostname</tt> - The hostname. If omitted, then
0N/A * <em>localhost</em> is assumed. If the protocol is also omitted,
0N/A * then default local protocol <em>local:</em> is also assumed.
0N/A * If the hostname is not omitted but the protocol is omitted,
0N/A * then the default remote protocol, <em>rmi:</em> is assumed.
0N/A * </p></li>
0N/A * <li><p><tt>port</tt> - The port for the communications protocol.
0N/A * Treatment of the <tt>port</tt> parameter is implementation
0N/A * (protocol) specific. It is unused by the default local protocol,
0N/A * <em>local:</em>. For the default remote protocol, <em>rmi:</em>,
0N/A * <tt>port</tt> indicates the port number of the <em>rmiregistry</em>
0N/A * on the target host and defaults to port 1099.
0N/A * </p></li>
0N/A * <li><p><tt>servername</tt> - The treatment of the Path, Query, and
0N/A * Fragment components of the HostIdentifier are implementation
0N/A * (protocol) dependent. These components are ignored by the
0N/A * default local protocol, <em>local:</em>. For the default remote
0N/A * protocol, <em>rmi</em>, the Path component is interpreted as
0N/A * the name of the RMI remote object. The Query component may
0N/A * contain an access mode specifier <em>?mode=</em> specifying
0N/A * <em>"r"</em> or <em>"rw"</em> access (write access currently
0N/A * ignored). The Fragment part is ignored.
0N/A * </p></li>
0N/A * </ul>
0N/A * <p>
0N/A * All HostIdentifier objects are represented as absolute, hierarchical URIs.
0N/A * The constructors accept relative URIs, but these will generally be
0N/A * transformed into an absolute URI specifying a default protocol. A
0N/A * HostIdentifier differs from a URI in that certain contractions and
0N/A * illicit syntactical constructions are allowed. The following are all
0N/A * valid HostIdentifier strings:
0N/A *
0N/A * <ul>
0N/A * <li><p>&lt null &gt - transformed into "//localhost"</p></li>
0N/A * <li><p>localhost - transformed into "//localhost"</p></li>
0N/A * <li><p>hostname - transformed into "//hostname"</p></li>
0N/A * <li><p>hostname:port - transformed into "//hostname:port"</p></li>
0N/A * <li><p>proto:hostname - transformed into "proto://hostname"</p></li>
0N/A * <li><p>proto:hostname:port - transformed into
0N/A * "proto://hostname:port"</p></li>
0N/A * <li><p>proto://hostname:port</p></li>
0N/A * </ul>
0N/A * </p>
0N/A *
0N/A * @see URI
0N/A * @see VmIdentifier
0N/A *
0N/A * @author Brian Doherty
0N/A * @since 1.5
0N/A */
0N/Apublic class HostIdentifier {
0N/A private URI uri;
0N/A
0N/A /**
0N/A * creates a canonical representation of the uriString. This method
0N/A * performs certain translations depending on the type of URI generated
0N/A * by the string.
0N/A */
0N/A private URI canonicalize(String uriString) throws URISyntaxException {
0N/A if ((uriString == null) || (uriString.compareTo("localhost") == 0)) {
0N/A uriString = "//localhost";
0N/A return new URI(uriString);
0N/A }
0N/A
0N/A URI u = new URI(uriString);
0N/A
0N/A if (u.isAbsolute()) {
0N/A if (u.isOpaque()) {
0N/A /*
0N/A * this code is here to deal with a special case. For ease of
0N/A * use, we'd like to be able to handle the case where the user
0N/A * specifies hostname:port, not requiring the scheme part.
0N/A * This introduces some subtleties.
0N/A * hostname:port - scheme = hostname
0N/A * - schemespecificpart = port
0N/A * - hostname = null
0N/A * - userinfo=null
0N/A * however, someone could also enter scheme:hostname:port and
0N/A * get into this code. the strategy is to consider this
0N/A * syntax illegal and provide some code to defend against it.
0N/A * Basically, we test that the string contains only one ":"
0N/A * and that the ssp is numeric. If we get two colons, we will
0N/A * attempt to insert the "//" after the first colon and then
0N/A * try to create a URI from the resulting string.
0N/A */
0N/A String scheme = u.getScheme();
0N/A String ssp = u.getSchemeSpecificPart();
0N/A String frag = u.getFragment();
0N/A URI u2 = null;
0N/A
0N/A int c1index = uriString.indexOf(":");
0N/A int c2index = uriString.lastIndexOf(":");
0N/A if (c2index != c1index) {
0N/A /*
0N/A * this is the scheme:hostname:port case. Attempt to
0N/A * transform this to scheme://hostname:port. If a path
0N/A * part is part of the original strings, it will be
0N/A * included in the SchemeSpecificPart. however, the
0N/A * fragment part must be handled separately.
0N/A */
0N/A if (frag == null) {
0N/A u2 = new URI(scheme + "://" + ssp);
0N/A } else {
0N/A u2 = new URI(scheme + "://" + ssp + "#" + frag);
0N/A }
0N/A return u2;
0N/A }
0N/A /*
0N/A * here we have the <string>:<string> case, possibly with
0N/A * optional path and fragment components. we assume that
0N/A * the part following the colon is a number. we don't check
0N/A * this condition here as it will get detected later anyway.
0N/A */
0N/A u2 = new URI("//" + uriString);
0N/A return u2;
0N/A } else {
0N/A return u;
0N/A }
0N/A } else {
0N/A /*
0N/A * This is the case where we were given a hostname followed
0N/A * by a path part, fragment part, or both a path and fragment
0N/A * part. The key here is that no scheme part was specified.
0N/A * For this case, if the scheme specific part does not begin
0N/A * with "//", then we prefix the "//" to the given string and
0N/A * attempt to create a URI from the resulting string.
0N/A */
0N/A String ssp = u.getSchemeSpecificPart();
0N/A if (ssp.startsWith("//")) {
0N/A return u;
0N/A } else {
0N/A return new URI("//" + uriString);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Create a HostIdentifier instance from a string value.
0N/A *
0N/A * @param uriString a string representing a target host. The syntax of
0N/A * the string must conform to the rules specified in the
0N/A * class documentation.
0N/A *
0N/A * @throws URISyntaxException Thrown when the uriString or its canonical
0N/A * form is poorly formed. This exception may
0N/A * get encapsulated into a MonitorException in
0N/A * a future version.
0N/A *
0N/A */
0N/A public HostIdentifier(String uriString) throws URISyntaxException {
0N/A uri = canonicalize(uriString);
0N/A }
0N/A
0N/A /**
0N/A * Create a HostIdentifier instance from component parts of a URI.
0N/A *
0N/A * @param scheme the {@link URI#getScheme} component of a URI.
0N/A * @param authority the {@link URI#getAuthority} component of a URI.
0N/A * @param path the {@link URI#getPath} component of a URI.
0N/A * @param query the {@link URI#getQuery} component of a URI.
0N/A * @param fragment the {@link URI#getFragment} component of a URI.
0N/A *
0N/A * @throws URISyntaxException Thrown when the uriString or its canonical
0N/A * form is poorly formed. This exception may
0N/A * get encapsulated into a MonitorException in
0N/A * a future version.
0N/A * @see URI
0N/A */
0N/A public HostIdentifier(String scheme, String authority, String path,
0N/A String query, String fragment)
0N/A throws URISyntaxException {
0N/A uri = new URI(scheme, authority, path, query, fragment);
0N/A }
0N/A
0N/A /**
0N/A * Create a HostIdentifier instance from a VmIdentifier.
0N/A *
0N/A * The necessary components of the VmIdentifier are extracted and
0N/A * reassembled into a HostIdentifier. If a "file:" scheme (protocol)
0N/A * is specified, the the returned HostIdentifier will always be
0N/A * equivalent to HostIdentifier("file://localhost").
0N/A *
0N/A * @param vmid the VmIdentifier use to construct the HostIdentifier.
0N/A */
0N/A public HostIdentifier(VmIdentifier vmid) {
0N/A /*
0N/A * Extract all components of the VmIdentifier URI except the
0N/A * user-info part of the authority (the lvmid).
0N/A */
0N/A StringBuilder sb = new StringBuilder();
0N/A String scheme = vmid.getScheme();
0N/A String host = vmid.getHost();
0N/A String authority = vmid.getAuthority();
0N/A
0N/A // check for 'file:' VmIdentifiers and handled as a special case.
0N/A if ((scheme != null) && (scheme.compareTo("file") == 0)) {
0N/A try {
0N/A uri = new URI("file://localhost");
0N/A } catch (URISyntaxException e) { };
0N/A return;
0N/A }
0N/A
0N/A if ((host != null) && (host.compareTo(authority) == 0)) {
0N/A /*
0N/A * this condition occurs when the VmIdentifier specifies only
0N/A * the authority (i.e. the lvmid ), and not a host name.
0N/A */
0N/A host = null;
0N/A }
0N/A
0N/A if (scheme == null) {
0N/A if (host == null) {
0N/A scheme = "local"; // default local scheme
0N/A } else {
0N/A /*
0N/A * rmi is the default remote scheme. if the VmIdentifier
0N/A * specifies some other protocol, this default is overridden.
0N/A */
0N/A scheme = "rmi";
0N/A }
0N/A }
0N/A
0N/A sb.append(scheme).append("://");
0N/A
0N/A if (host == null) {
0N/A sb.append("localhost"); // default host name
0N/A } else {
0N/A sb.append(host);
0N/A }
0N/A
0N/A int port = vmid.getPort();
0N/A if (port != -1) {
0N/A sb.append(":").append(port);
0N/A }
0N/A
0N/A String path = vmid.getPath();
0N/A if ((path != null) && (path.length() != 0)) {
0N/A sb.append(path);
0N/A }
0N/A
0N/A String query = vmid.getQuery();
0N/A if (query != null) {
0N/A sb.append("?").append(query);
0N/A }
0N/A
0N/A String frag = vmid.getFragment();
0N/A if (frag != null) {
0N/A sb.append("#").append(frag);
0N/A }
0N/A
0N/A try {
0N/A uri = new URI(sb.toString());
0N/A } catch (URISyntaxException e) {
0N/A // shouldn't happen, as we were passed a valid VmIdentifier
0N/A throw new RuntimeException("Internal Error", e);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Resolve a VmIdentifier with this HostIdentifier. A VmIdentifier, such
0N/A * as <em>1234</em> or <em>1234@hostname</em> or any other string that
0N/A * omits certain components of the URI string may be valid, but is certainly
0N/A * incomplete. They are missing critical information for identifying the
0N/A * the communications protocol, target host, or other parameters. A
0N/A * VmIdentifier of this form is considered <em>unresolved</em>. This method
0N/A * uses components of the HostIdentifier to resolve the missing components
0N/A * of the VmIdentifier.
0N/A * <p>
0N/A * Specified components of the unresolved VmIdentifier take precedence
0N/A * over their HostIdentifier counterparts. For example, if the VmIdentifier
0N/A * indicates <em>1234@hostname:2099</em> and the HostIdentifier indicates
0N/A * <em>rmi://hostname:1099/</em>, then the resolved VmIdentifier will
0N/A * be <em>rmi://1234@hostname:2099</em>. Any component not explicitly
0N/A * specified or assumed by the HostIdentifier, will remain unresolved in
0N/A * resolved VmIdentifier.
0N/A * <p>
0N/A * A VmIdentifier specifying a <em>file:</em> scheme (protocol), is
0N/A * not changed in any way by this method.
0N/A *
0N/A * @param vmid the unresolved VmIdentifier.
0N/A * @return VmIdentifier - the resolved VmIdentifier. If vmid was resolved
0N/A * on entry to this method, then the returned
0N/A * VmIdentifier will be equal, but not identical, to
0N/A * vmid.
0N/A */
0N/A public VmIdentifier resolve(VmIdentifier vmid)
0N/A throws URISyntaxException, MonitorException {
0N/A String scheme = vmid.getScheme();
0N/A String host = vmid.getHost();
0N/A String authority = vmid.getAuthority();
0N/A
0N/A if ((scheme != null) && (scheme.compareTo("file") == 0)) {
0N/A // don't attempt to resolve a file based VmIdentifier.
0N/A return vmid;
0N/A }
0N/A
0N/A if ((host != null) && (host.compareTo(authority) == 0)) {
0N/A /*
0N/A * this condition occurs when the VmIdentifier specifies only
0N/A * the authority (i.e. an lvmid), and not a host name.
0N/A */
0N/A host = null;
0N/A }
0N/A
0N/A if (scheme == null) {
0N/A scheme = getScheme();
0N/A }
0N/A
0N/A URI nuri = null;
0N/A
0N/A StringBuffer sb = new StringBuffer();
0N/A
0N/A sb.append(scheme).append("://");
0N/A
0N/A String userInfo = vmid.getUserInfo();
0N/A if (userInfo != null) {
0N/A sb.append(userInfo);
0N/A } else {
0N/A sb.append(vmid.getAuthority());
0N/A }
0N/A
0N/A if (host == null) {
0N/A host = getHost();
0N/A }
0N/A sb.append("@").append(host);
0N/A
0N/A int port = vmid.getPort();
0N/A if (port == -1) {
0N/A port = getPort();
0N/A }
0N/A
0N/A if (port != -1) {
0N/A sb.append(":").append(port);
0N/A }
0N/A
0N/A String path = vmid.getPath();
0N/A if ((path == null) || (path.length() == 0)) {
0N/A path = getPath();
0N/A }
0N/A
0N/A if ((path != null) && (path.length() > 0)) {
0N/A sb.append(path);
0N/A }
0N/A
0N/A String query = vmid.getQuery();
0N/A if (query == null) {
0N/A query = getQuery();
0N/A }
0N/A if (query != null) {
0N/A sb.append("?").append(query);
0N/A }
0N/A
0N/A String fragment = vmid.getFragment();
0N/A if (fragment == null) {
0N/A fragment = getFragment();
0N/A }
0N/A if (fragment != null) {
0N/A sb.append("#").append(fragment);
0N/A }
0N/A
0N/A String s = sb.toString();
0N/A return new VmIdentifier(s);
0N/A }
0N/A
0N/A /**
0N/A * Return the Scheme, or protocol, portion of this HostIdentifier.
0N/A *
0N/A * @return String - the scheme for this HostIdentifier.
0N/A * @see URI#getScheme()
0N/A */
0N/A public String getScheme() {
0N/A return uri.isAbsolute() ? uri.getScheme() : null;
0N/A }
0N/A
0N/A /**
0N/A * Return the Scheme Specific Part of this HostIdentifier.
0N/A *
0N/A * @return String - the scheme specific part for this HostIdentifier.
0N/A * @see URI#getSchemeSpecificPart()
0N/A */
0N/A public String getSchemeSpecificPart() {
0N/A return uri.getSchemeSpecificPart();
0N/A }
0N/A
0N/A /**
0N/A * Return the User Info part of this HostIdentifier.
0N/A *
0N/A * @return String - the user info part for this HostIdentifier.
0N/A * @see URI#getUserInfo()
0N/A */
0N/A public String getUserInfo() {
0N/A return uri.getUserInfo();
0N/A }
0N/A
0N/A /**
0N/A * Return the Host part of this HostIdentifier.
0N/A *
0N/A * @return String - the host part for this HostIdentifier, or
0N/A * "localhost" if the URI.getHost() returns null.
0N/A * @see URI#getUserInfo()
0N/A */
0N/A public String getHost() {
0N/A return (uri.getHost() == null) ? "localhost" : uri.getHost();
0N/A }
0N/A
0N/A /**
0N/A * Return the Port for of this HostIdentifier.
0N/A *
0N/A * @return String - the port for this HostIdentifier
0N/A * @see URI#getPort()
0N/A */
0N/A public int getPort() {
0N/A return uri.getPort();
0N/A }
0N/A
0N/A /**
0N/A * Return the Path part of this HostIdentifier.
0N/A *
0N/A * @return String - the path part for this HostIdentifier.
0N/A * @see URI#getPath()
0N/A */
0N/A public String getPath() {
0N/A return uri.getPath();
0N/A }
0N/A
0N/A /**
0N/A * Return the Query part of this HostIdentifier.
0N/A *
0N/A * @return String - the query part for this HostIdentifier.
0N/A * @see URI#getQuery()
0N/A */
0N/A public String getQuery() {
0N/A return uri.getQuery();
0N/A }
0N/A
0N/A /**
0N/A * Return the Fragment part of this HostIdentifier.
0N/A *
0N/A * @return String - the fragment part for this HostIdentifier.
0N/A * @see URI#getFragment()
0N/A */
0N/A public String getFragment() {
0N/A return uri.getFragment();
0N/A }
0N/A
0N/A /**
0N/A * Return the mode indicated in this HostIdentifier.
0N/A *
0N/A * @return String - the mode string. If no mode is specified, then "r"
0N/A * is returned. otherwise, the specified mode is returned.
0N/A */
0N/A public String getMode() {
0N/A String query = getQuery();
0N/A if (query != null) {
0N/A String[] queryArgs = query.split("\\+");
0N/A for (int i = 0; i < queryArgs.length; i++) {
0N/A if (queryArgs[i].startsWith("mode=")) {
0N/A int index = queryArgs[i].indexOf('=');
0N/A return queryArgs[i].substring(index+1);
0N/A }
0N/A }
0N/A }
0N/A return "r";
0N/A }
0N/A
0N/A /**
0N/A * Return the URI associated with the HostIdentifier.
0N/A *
0N/A * @return URI - the URI.
0N/A * @see URI
0N/A */
0N/A public URI getURI() {
0N/A return uri;
0N/A }
0N/A
0N/A /**
0N/A * Return the hash code for this HostIdentifier. The hash code is
0N/A * identical to the hash code for the contained URI.
0N/A *
0N/A * @return int - the hashcode.
0N/A * @see URI#hashCode()
0N/A */
0N/A public int hashCode() {
0N/A return uri.hashCode();
0N/A }
0N/A
0N/A /**
0N/A * Test for quality with other objects.
0N/A *
0N/A * @param object the object to be test for equality.
0N/A * @return boolean - returns true if the given object is of type
0N/A * HostIdentifier and its URI field is equal to this
0N/A * object's URI field. Otherwise, returns false.
0N/A *
0N/A * @see URI#equals(Object)
0N/A */
0N/A public boolean equals(Object object) {
0N/A if (object == this) {
0N/A return true;
0N/A }
0N/A if (!(object instanceof HostIdentifier)) {
0N/A return false;
0N/A }
0N/A return uri.equals(((HostIdentifier)object).uri);
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Convert to a string representation. Conversion is identical to
0N/A * calling getURI().toString(). This may change in a future release.
0N/A *
0N/A * @return String - a String representation of the HostIdentifier.
0N/A *
0N/A * @see URI#toString()
0N/A */
0N/A public String toString() {
0N/A return uri.toString();
0N/A }
0N/A}