Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/jvmstat/monitor/HostIdentifier.java
38918 views
/*1* Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.jvmstat.monitor;2627import java.net.*;2829/**30* An abstraction that identifies a target host and communications31* protocol. The HostIdentifier, or hostid, provides a convenient string32* representation of the information needed to locate and communicate with33* a target host. The string, based on a {@link URI}, may specify the34* the communications protocol, host name, and protocol specific information35* for a target host. The format for a HostIdentifier string is:36* <pre>37* [<I>protocol</I>:][[<I>//</I>]<I>hostname</I>][<I>:port</I>][<I>/servername</I>]38* </pre>39* There are actually no required components of this string, as a null string40* is interpreted to mean a local connection to the local host and is equivalent41* to the string <em>local://localhost</em>. The components of the42* HostIdentifier are:43* <ul>44* <li><p><tt>protocol</tt> - The communications protocol. If omitted,45* and a hostname is not specified, then default local protocol,46* <em>local:</em>, is assumed. If the protocol is omitted and a47* hostname is specified then the default remote protocol,48* <em>rmi:</em> is assumed.49* </p></li>50* <li><p><tt>hostname</tt> - The hostname. If omitted, then51* <em>localhost</em> is assumed. If the protocol is also omitted,52* then default local protocol <em>local:</em> is also assumed.53* If the hostname is not omitted but the protocol is omitted,54* then the default remote protocol, <em>rmi:</em> is assumed.55* </p></li>56* <li><p><tt>port</tt> - The port for the communications protocol.57* Treatment of the <tt>port</tt> parameter is implementation58* (protocol) specific. It is unused by the default local protocol,59* <em>local:</em>. For the default remote protocol, <em>rmi:</em>,60* <tt>port</tt> indicates the port number of the <em>rmiregistry</em>61* on the target host and defaults to port 1099.62* </p></li>63* <li><p><tt>servername</tt> - The treatment of the Path, Query, and64* Fragment components of the HostIdentifier are implementation65* (protocol) dependent. These components are ignored by the66* default local protocol, <em>local:</em>. For the default remote67* protocol, <em>rmi</em>, the Path component is interpreted as68* the name of the RMI remote object. The Query component may69* contain an access mode specifier <em>?mode=</em> specifying70* <em>"r"</em> or <em>"rw"</em> access (write access currently71* ignored). The Fragment part is ignored.72* </p></li>73* </ul>74* <p>75* All HostIdentifier objects are represented as absolute, hierarchical URIs.76* The constructors accept relative URIs, but these will generally be77* transformed into an absolute URI specifying a default protocol. A78* HostIdentifier differs from a URI in that certain contractions and79* illicit syntactical constructions are allowed. The following are all80* valid HostIdentifier strings:81*82* <ul>83* <li><p>< null > - transformed into "//localhost"</p></li>84* <li><p>localhost - transformed into "//localhost"</p></li>85* <li><p>hostname - transformed into "//hostname"</p></li>86* <li><p>hostname:port - transformed into "//hostname:port"</p></li>87* <li><p>proto:hostname - transformed into "proto://hostname"</p></li>88* <li><p>proto:hostname:port - transformed into89* "proto://hostname:port"</p></li>90* <li><p>proto://hostname:port</p></li>91* </ul>92* </p>93*94* @see URI95* @see VmIdentifier96*97* @author Brian Doherty98* @since 1.599*/100public class HostIdentifier {101private URI uri;102103/**104* creates a canonical representation of the uriString. This method105* performs certain translations depending on the type of URI generated106* by the string.107*/108private URI canonicalize(String uriString) throws URISyntaxException {109if ((uriString == null) || (uriString.compareTo("localhost") == 0)) {110uriString = "//localhost";111return new URI(uriString);112}113114URI u = new URI(uriString);115116if (u.isAbsolute()) {117if (u.isOpaque()) {118/*119* this code is here to deal with a special case. For ease of120* use, we'd like to be able to handle the case where the user121* specifies hostname:port, not requiring the scheme part.122* This introduces some subtleties.123* hostname:port - scheme = hostname124* - schemespecificpart = port125* - hostname = null126* - userinfo=null127* however, someone could also enter scheme:hostname:port and128* get into this code. the strategy is to consider this129* syntax illegal and provide some code to defend against it.130* Basically, we test that the string contains only one ":"131* and that the ssp is numeric. If we get two colons, we will132* attempt to insert the "//" after the first colon and then133* try to create a URI from the resulting string.134*/135String scheme = u.getScheme();136String ssp = u.getSchemeSpecificPart();137String frag = u.getFragment();138URI u2 = null;139140int c1index = uriString.indexOf(":");141int c2index = uriString.lastIndexOf(":");142if (c2index != c1index) {143/*144* this is the scheme:hostname:port case. Attempt to145* transform this to scheme://hostname:port. If a path146* part is part of the original strings, it will be147* included in the SchemeSpecificPart. however, the148* fragment part must be handled separately.149*/150if (frag == null) {151u2 = new URI(scheme + "://" + ssp);152} else {153u2 = new URI(scheme + "://" + ssp + "#" + frag);154}155return u2;156}157/*158* here we have the <string>:<string> case, possibly with159* optional path and fragment components. we assume that160* the part following the colon is a number. we don't check161* this condition here as it will get detected later anyway.162*/163u2 = new URI("//" + uriString);164return u2;165} else {166return u;167}168} else {169/*170* This is the case where we were given a hostname followed171* by a path part, fragment part, or both a path and fragment172* part. The key here is that no scheme part was specified.173* For this case, if the scheme specific part does not begin174* with "//", then we prefix the "//" to the given string and175* attempt to create a URI from the resulting string.176*/177String ssp = u.getSchemeSpecificPart();178if (ssp.startsWith("//")) {179return u;180} else {181return new URI("//" + uriString);182}183}184}185186/**187* Create a HostIdentifier instance from a string value.188*189* @param uriString a string representing a target host. The syntax of190* the string must conform to the rules specified in the191* class documentation.192*193* @throws URISyntaxException Thrown when the uriString or its canonical194* form is poorly formed. This exception may195* get encapsulated into a MonitorException in196* a future version.197*198*/199public HostIdentifier(String uriString) throws URISyntaxException {200uri = canonicalize(uriString);201}202203/**204* Create a HostIdentifier instance from component parts of a URI.205*206* @param scheme the {@link URI#getScheme} component of a URI.207* @param authority the {@link URI#getAuthority} component of a URI.208* @param path the {@link URI#getPath} component of a URI.209* @param query the {@link URI#getQuery} component of a URI.210* @param fragment the {@link URI#getFragment} component of a URI.211*212* @throws URISyntaxException Thrown when the uriString or its canonical213* form is poorly formed. This exception may214* get encapsulated into a MonitorException in215* a future version.216* @see URI217*/218public HostIdentifier(String scheme, String authority, String path,219String query, String fragment)220throws URISyntaxException {221uri = new URI(scheme, authority, path, query, fragment);222}223224/**225* Create a HostIdentifier instance from a VmIdentifier.226*227* The necessary components of the VmIdentifier are extracted and228* reassembled into a HostIdentifier. If a "file:" scheme (protocol)229* is specified, the the returned HostIdentifier will always be230* equivalent to HostIdentifier("file://localhost").231*232* @param vmid the VmIdentifier use to construct the HostIdentifier.233*/234public HostIdentifier(VmIdentifier vmid) {235/*236* Extract all components of the VmIdentifier URI except the237* user-info part of the authority (the lvmid).238*/239StringBuilder sb = new StringBuilder();240String scheme = vmid.getScheme();241String host = vmid.getHost();242String authority = vmid.getAuthority();243244// check for 'file:' VmIdentifiers and handled as a special case.245if ((scheme != null) && (scheme.compareTo("file") == 0)) {246try {247uri = new URI("file://localhost");248} catch (URISyntaxException e) { };249return;250}251252if ((host != null) && (host.compareTo(authority) == 0)) {253/*254* this condition occurs when the VmIdentifier specifies only255* the authority (i.e. the lvmid ), and not a host name.256*/257host = null;258}259260if (scheme == null) {261if (host == null) {262scheme = "local"; // default local scheme263} else {264/*265* rmi is the default remote scheme. if the VmIdentifier266* specifies some other protocol, this default is overridden.267*/268scheme = "rmi";269}270}271272sb.append(scheme).append("://");273274if (host == null) {275sb.append("localhost"); // default host name276} else {277sb.append(host);278}279280int port = vmid.getPort();281if (port != -1) {282sb.append(":").append(port);283}284285String path = vmid.getPath();286if ((path != null) && (path.length() != 0)) {287sb.append(path);288}289290String query = vmid.getQuery();291if (query != null) {292sb.append("?").append(query);293}294295String frag = vmid.getFragment();296if (frag != null) {297sb.append("#").append(frag);298}299300try {301uri = new URI(sb.toString());302} catch (URISyntaxException e) {303// shouldn't happen, as we were passed a valid VmIdentifier304throw new RuntimeException("Internal Error", e);305}306}307308/**309* Resolve a VmIdentifier with this HostIdentifier. A VmIdentifier, such310* as <em>1234</em> or <em>1234@hostname</em> or any other string that311* omits certain components of the URI string may be valid, but is certainly312* incomplete. They are missing critical information for identifying the313* the communications protocol, target host, or other parameters. A314* VmIdentifier of this form is considered <em>unresolved</em>. This method315* uses components of the HostIdentifier to resolve the missing components316* of the VmIdentifier.317* <p>318* Specified components of the unresolved VmIdentifier take precedence319* over their HostIdentifier counterparts. For example, if the VmIdentifier320* indicates <em>1234@hostname:2099</em> and the HostIdentifier indicates321* <em>rmi://hostname:1099/</em>, then the resolved VmIdentifier will322* be <em>rmi://1234@hostname:2099</em>. Any component not explicitly323* specified or assumed by the HostIdentifier, will remain unresolved in324* resolved VmIdentifier.325* <p>326* A VmIdentifier specifying a <em>file:</em> scheme (protocol), is327* not changed in any way by this method.328*329* @param vmid the unresolved VmIdentifier.330* @return VmIdentifier - the resolved VmIdentifier. If vmid was resolved331* on entry to this method, then the returned332* VmIdentifier will be equal, but not identical, to333* vmid.334*/335public VmIdentifier resolve(VmIdentifier vmid)336throws URISyntaxException, MonitorException {337String scheme = vmid.getScheme();338String host = vmid.getHost();339String authority = vmid.getAuthority();340341if ((scheme != null) && (scheme.compareTo("file") == 0)) {342// don't attempt to resolve a file based VmIdentifier.343return vmid;344}345346if ((host != null) && (host.compareTo(authority) == 0)) {347/*348* this condition occurs when the VmIdentifier specifies only349* the authority (i.e. an lvmid), and not a host name.350*/351host = null;352}353354if (scheme == null) {355scheme = getScheme();356}357358URI nuri = null;359360StringBuffer sb = new StringBuffer();361362sb.append(scheme).append("://");363364String userInfo = vmid.getUserInfo();365if (userInfo != null) {366sb.append(userInfo);367} else {368sb.append(vmid.getAuthority());369}370371if (host == null) {372host = getHost();373}374sb.append("@").append(host);375376int port = vmid.getPort();377if (port == -1) {378port = getPort();379}380381if (port != -1) {382sb.append(":").append(port);383}384385String path = vmid.getPath();386if ((path == null) || (path.length() == 0)) {387path = getPath();388}389390if ((path != null) && (path.length() > 0)) {391sb.append(path);392}393394String query = vmid.getQuery();395if (query == null) {396query = getQuery();397}398if (query != null) {399sb.append("?").append(query);400}401402String fragment = vmid.getFragment();403if (fragment == null) {404fragment = getFragment();405}406if (fragment != null) {407sb.append("#").append(fragment);408}409410String s = sb.toString();411return new VmIdentifier(s);412}413414/**415* Return the Scheme, or protocol, portion of this HostIdentifier.416*417* @return String - the scheme for this HostIdentifier.418* @see URI#getScheme()419*/420public String getScheme() {421return uri.isAbsolute() ? uri.getScheme() : null;422}423424/**425* Return the Scheme Specific Part of this HostIdentifier.426*427* @return String - the scheme specific part for this HostIdentifier.428* @see URI#getSchemeSpecificPart()429*/430public String getSchemeSpecificPart() {431return uri.getSchemeSpecificPart();432}433434/**435* Return the User Info part of this HostIdentifier.436*437* @return String - the user info part for this HostIdentifier.438* @see URI#getUserInfo()439*/440public String getUserInfo() {441return uri.getUserInfo();442}443444/**445* Return the Host part of this HostIdentifier.446*447* @return String - the host part for this HostIdentifier, or448* "localhost" if the URI.getHost() returns null.449* @see URI#getUserInfo()450*/451public String getHost() {452return (uri.getHost() == null) ? "localhost" : uri.getHost();453}454455/**456* Return the Port for of this HostIdentifier.457*458* @return String - the port for this HostIdentifier459* @see URI#getPort()460*/461public int getPort() {462return uri.getPort();463}464465/**466* Return the Path part of this HostIdentifier.467*468* @return String - the path part for this HostIdentifier.469* @see URI#getPath()470*/471public String getPath() {472return uri.getPath();473}474475/**476* Return the Query part of this HostIdentifier.477*478* @return String - the query part for this HostIdentifier.479* @see URI#getQuery()480*/481public String getQuery() {482return uri.getQuery();483}484485/**486* Return the Fragment part of this HostIdentifier.487*488* @return String - the fragment part for this HostIdentifier.489* @see URI#getFragment()490*/491public String getFragment() {492return uri.getFragment();493}494495/**496* Return the mode indicated in this HostIdentifier.497*498* @return String - the mode string. If no mode is specified, then "r"499* is returned. otherwise, the specified mode is returned.500*/501public String getMode() {502String query = getQuery();503if (query != null) {504String[] queryArgs = query.split("\\+");505for (int i = 0; i < queryArgs.length; i++) {506if (queryArgs[i].startsWith("mode=")) {507int index = queryArgs[i].indexOf('=');508return queryArgs[i].substring(index+1);509}510}511}512return "r";513}514515/**516* Return the URI associated with the HostIdentifier.517*518* @return URI - the URI.519* @see URI520*/521public URI getURI() {522return uri;523}524525/**526* Return the hash code for this HostIdentifier. The hash code is527* identical to the hash code for the contained URI.528*529* @return int - the hashcode.530* @see URI#hashCode()531*/532public int hashCode() {533return uri.hashCode();534}535536/**537* Test for quality with other objects.538*539* @param object the object to be test for equality.540* @return boolean - returns true if the given object is of type541* HostIdentifier and its URI field is equal to this542* object's URI field. Otherwise, returns false.543*544* @see URI#equals(Object)545*/546public boolean equals(Object object) {547if (object == this) {548return true;549}550if (!(object instanceof HostIdentifier)) {551return false;552}553return uri.equals(((HostIdentifier)object).uri);554}555556557/**558* Convert to a string representation. Conversion is identical to559* calling getURI().toString(). This may change in a future release.560*561* @return String - a String representation of the HostIdentifier.562*563* @see URI#toString()564*/565public String toString() {566return uri.toString();567}568}569570571