/* * Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package sun.jvmstat.monitor; import java.net.*; /** * An abstraction that identifies a target host and communications * protocol. The HostIdentifier, or hostid, provides a convenient string * representation of the information needed to locate and communicate with * a target host. The string, based on a {@link URI}, may specify the * the communications protocol, host name, and protocol specific information * for a target host. The format for a HostIdentifier string is: *
* [protocol:][[//]hostname][:port][/servername] ** There are actually no required components of this string, as a null string * is interpreted to mean a local connection to the local host and is equivalent * to the string local://localhost. The components of the * HostIdentifier are: *
protocol - The communications protocol. If omitted, * and a hostname is not specified, then default local protocol, * local:, is assumed. If the protocol is omitted and a * hostname is specified then the default remote protocol, * rmi: is assumed. *
hostname - The hostname. If omitted, then * localhost is assumed. If the protocol is also omitted, * then default local protocol local: is also assumed. * If the hostname is not omitted but the protocol is omitted, * then the default remote protocol, rmi: is assumed. *
port - The port for the communications protocol. * Treatment of the port parameter is implementation * (protocol) specific. It is unused by the default local protocol, * local:. For the default remote protocol, rmi:, * port indicates the port number of the rmiregistry * on the target host and defaults to port 1099. *
servername - The treatment of the Path, Query, and * Fragment components of the HostIdentifier are implementation * (protocol) dependent. These components are ignored by the * default local protocol, local:. For the default remote * protocol, rmi, the Path component is interpreted as * the name of the RMI remote object. The Query component may * contain an access mode specifier ?mode= specifying * "r" or "rw" access (write access currently * ignored). The Fragment part is ignored. *
* All HostIdentifier objects are represented as absolute, hierarchical URIs. * The constructors accept relative URIs, but these will generally be * transformed into an absolute URI specifying a default protocol. A * HostIdentifier differs from a URI in that certain contractions and * illicit syntactical constructions are allowed. The following are all * valid HostIdentifier strings: * *
< null > - transformed into "//localhost"
localhost - transformed into "//localhost"
hostname - transformed into "//hostname"
hostname:port - transformed into "//hostname:port"
proto:hostname - transformed into "proto://hostname"
proto:hostname:port - transformed into * "proto://hostname:port"
proto://hostname:port
* Specified components of the unresolved VmIdentifier take precedence * over their HostIdentifier counterparts. For example, if the VmIdentifier * indicates 1234@hostname:2099 and the HostIdentifier indicates * rmi://hostname:1099/, then the resolved VmIdentifier will * be rmi://1234@hostname:2099. Any component not explicitly * specified or assumed by the HostIdentifier, will remain unresolved in * resolved VmIdentifier. *
* A VmIdentifier specifying a file: scheme (protocol), is * not changed in any way by this method. * * @param vmid the unresolved VmIdentifier. * @return VmIdentifier - the resolved VmIdentifier. If vmid was resolved * on entry to this method, then the returned * VmIdentifier will be equal, but not identical, to * vmid. */ public VmIdentifier resolve(VmIdentifier vmid) throws URISyntaxException, MonitorException { String scheme = vmid.getScheme(); String host = vmid.getHost(); String authority = vmid.getAuthority(); if ((scheme != null) && (scheme.compareTo("file") == 0)) { // don't attempt to resolve a file based VmIdentifier. return vmid; } if ((host != null) && (host.compareTo(authority) == 0)) { /* * this condition occurs when the VmIdentifier specifies only * the authority (i.e. an lvmid), and not a host name. */ host = null; } if (scheme == null) { scheme = getScheme(); } URI nuri = null; StringBuffer sb = new StringBuffer(); sb.append(scheme).append("://"); String userInfo = vmid.getUserInfo(); if (userInfo != null) { sb.append(userInfo); } else { sb.append(vmid.getAuthority()); } if (host == null) { host = getHost(); } sb.append("@").append(host); int port = vmid.getPort(); if (port == -1) { port = getPort(); } if (port != -1) { sb.append(":").append(port); } String path = vmid.getPath(); if ((path == null) || (path.length() == 0)) { path = getPath(); } if ((path != null) && (path.length() > 0)) { sb.append(path); } String query = vmid.getQuery(); if (query == null) { query = getQuery(); } if (query != null) { sb.append("?").append(query); } String fragment = vmid.getFragment(); if (fragment == null) { fragment = getFragment(); } if (fragment != null) { sb.append("#").append(fragment); } String s = sb.toString(); return new VmIdentifier(s); } /** * Return the Scheme, or protocol, portion of this HostIdentifier. * * @return String - the scheme for this HostIdentifier. * @see URI#getScheme() */ public String getScheme() { return uri.isAbsolute() ? uri.getScheme() : null; } /** * Return the Scheme Specific Part of this HostIdentifier. * * @return String - the scheme specific part for this HostIdentifier. * @see URI#getSchemeSpecificPart() */ public String getSchemeSpecificPart() { return uri.getSchemeSpecificPart(); } /** * Return the User Info part of this HostIdentifier. * * @return String - the user info part for this HostIdentifier. * @see URI#getUserInfo() */ public String getUserInfo() { return uri.getUserInfo(); } /** * Return the Host part of this HostIdentifier. * * @return String - the host part for this HostIdentifier, or * "localhost" if the URI.getHost() returns null. * @see URI#getUserInfo() */ public String getHost() { return (uri.getHost() == null) ? "localhost" : uri.getHost(); } /** * Return the Port for of this HostIdentifier. * * @return String - the port for this HostIdentifier * @see URI#getPort() */ public int getPort() { return uri.getPort(); } /** * Return the Path part of this HostIdentifier. * * @return String - the path part for this HostIdentifier. * @see URI#getPath() */ public String getPath() { return uri.getPath(); } /** * Return the Query part of this HostIdentifier. * * @return String - the query part for this HostIdentifier. * @see URI#getQuery() */ public String getQuery() { return uri.getQuery(); } /** * Return the Fragment part of this HostIdentifier. * * @return String - the fragment part for this HostIdentifier. * @see URI#getFragment() */ public String getFragment() { return uri.getFragment(); } /** * Return the mode indicated in this HostIdentifier. * * @return String - the mode string. If no mode is specified, then "r" * is returned. otherwise, the specified mode is returned. */ public String getMode() { String query = getQuery(); if (query != null) { String[] queryArgs = query.split("\\+"); for (int i = 0; i < queryArgs.length; i++) { if (queryArgs[i].startsWith("mode=")) { int index = queryArgs[i].indexOf('='); return queryArgs[i].substring(index+1); } } } return "r"; } /** * Return the URI associated with the HostIdentifier. * * @return URI - the URI. * @see URI */ public URI getURI() { return uri; } /** * Return the hash code for this HostIdentifier. The hash code is * identical to the hash code for the contained URI. * * @return int - the hashcode. * @see URI#hashCode() */ public int hashCode() { return uri.hashCode(); } /** * Test for quality with other objects. * * @param object the object to be test for equality. * @return boolean - returns true if the given object is of type * HostIdentifier and its URI field is equal to this * object's URI field. Otherwise, returns false. * * @see URI#equals(Object) */ public boolean equals(Object object) { if (object == this) { return true; } if (!(object instanceof HostIdentifier)) { return false; } return uri.equals(((HostIdentifier)object).uri); } /** * Convert to a string representation. Conversion is identical to * calling getURI().toString(). This may change in a future release. * * @return String - a String representation of the HostIdentifier. * * @see URI#toString() */ public String toString() { return uri.toString(); } }