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