author | xdono |
Wed, 02 Jul 2008 12:55:45 -0700 | |
changeset 715 | f16baef3a20e |
parent 526 | 61ba2d5ea9da |
child 1153 | 6b88c071a015 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
715 | 2 |
* Copyright 2002-2008 Sun Microsystems, Inc. All Rights Reserved. |
2 | 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. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
*/ |
|
25 |
||
26 |
||
27 |
package javax.management.remote; |
|
28 |
||
29 |
||
30 |
import com.sun.jmx.remote.util.ClassLogger; |
|
31 |
import com.sun.jmx.remote.util.EnvHelp; |
|
32 |
||
38
dea12bbfb498
6670375: Missing unit test for 6607114 (Make JMXServiceURL reconstructible)
emcmanus
parents:
36
diff
changeset
|
33 |
import java.beans.ConstructorProperties; |
2 | 34 |
import java.io.Serializable; |
35 |
import java.net.InetAddress; |
|
36 |
import java.net.MalformedURLException; |
|
37 |
import java.net.UnknownHostException; |
|
38 |
import java.util.BitSet; |
|
39 |
import java.util.StringTokenizer; |
|
40 |
||
41 |
/** |
|
42 |
* <p>The address of a JMX API connector server. Instances of this class |
|
43 |
* are immutable.</p> |
|
44 |
* |
|
45 |
* <p>The address is an <em>Abstract Service URL</em> for SLP, as |
|
46 |
* defined in RFC 2609 and amended by RFC 3111. It must look like |
|
47 |
* this:</p> |
|
48 |
* |
|
49 |
* <blockquote> |
|
50 |
* |
|
51 |
* <code>service:jmx:<em>protocol</em>:<em>sap</em></code> |
|
52 |
* |
|
53 |
* </blockquote> |
|
54 |
* |
|
55 |
* <p>Here, <code><em>protocol</em></code> is the transport |
|
56 |
* protocol to be used to connect to the connector server. It is |
|
57 |
* a string of one or more ASCII characters, each of which is a |
|
58 |
* letter, a digit, or one of the characters <code>+</code> or |
|
59 |
* <code>-</code>. The first character must be a letter. |
|
60 |
* Uppercase letters are converted into lowercase ones.</p> |
|
61 |
* |
|
62 |
* <p><code><em>sap</em></code> is the address at which the connector |
|
63 |
* server is found. This address uses a subset of the syntax defined |
|
64 |
* by RFC 2609 for IP-based protocols. It is a subset because the |
|
65 |
* <code>user@host</code> syntax is not supported.</p> |
|
66 |
* |
|
67 |
* <p>The other syntaxes defined by RFC 2609 are not currently |
|
68 |
* supported by this class.</p> |
|
69 |
* |
|
70 |
* <p>The supported syntax is:</p> |
|
71 |
* |
|
72 |
* <blockquote> |
|
73 |
* |
|
74 |
* <code>//<em>[host[</em>:<em>port]][url-path]</em></code> |
|
75 |
* |
|
76 |
* </blockquote> |
|
77 |
* |
|
78 |
* <p>Square brackets <code>[]</code> indicate optional parts of |
|
79 |
* the address. Not all protocols will recognize all optional |
|
80 |
* parts.</p> |
|
81 |
* |
|
82 |
* <p>The <code><em>host</em></code> is a host name, an IPv4 numeric |
|
83 |
* host address, or an IPv6 numeric address enclosed in square |
|
84 |
* brackets.</p> |
|
85 |
* |
|
86 |
* <p>The <code><em>port</em></code> is a decimal port number. 0 |
|
87 |
* means a default or anonymous port, depending on the protocol.</p> |
|
88 |
* |
|
89 |
* <p>The <code><em>host</em></code> and <code><em>port</em></code> |
|
90 |
* can be omitted. The <code><em>port</em></code> cannot be supplied |
|
91 |
* without a <code><em>host</em></code>.</p> |
|
92 |
* |
|
93 |
* <p>The <code><em>url-path</em></code>, if any, begins with a slash |
|
94 |
* (<code>/</code>) or a semicolon (<code>;</code>) and continues to |
|
95 |
* the end of the address. It can contain attributes using the |
|
96 |
* semicolon syntax specified in RFC 2609. Those attributes are not |
|
97 |
* parsed by this class and incorrect attribute syntax is not |
|
98 |
* detected.</p> |
|
99 |
* |
|
100 |
* <p>Although it is legal according to RFC 2609 to have a |
|
101 |
* <code><em>url-path</em></code> that begins with a semicolon, not |
|
102 |
* all implementations of SLP allow it, so it is recommended to avoid |
|
103 |
* that syntax.</p> |
|
104 |
* |
|
105 |
* <p>Case is not significant in the initial |
|
106 |
* <code>service:jmx:<em>protocol</em></code> string or in the host |
|
107 |
* part of the address. Depending on the protocol, case can be |
|
108 |
* significant in the <code><em>url-path</em></code>.</p> |
|
109 |
* |
|
110 |
* @see <a |
|
111 |
* href="ftp://ftp.rfc-editor.org/in-notes/rfc2609.txt">RFC 2609, |
|
112 |
* "Service Templates and <code>Service:</code> Schemes"</a> |
|
113 |
* @see <a |
|
114 |
* href="ftp://ftp.rfc-editor.org/in-notes/rfc3111.txt">RFC 3111, |
|
115 |
* "Service Location Protocol Modifications for IPv6"</a> |
|
116 |
* |
|
117 |
* @since 1.5 |
|
118 |
*/ |
|
119 |
public class JMXServiceURL implements Serializable { |
|
120 |
||
121 |
private static final long serialVersionUID = 8173364409860779292L; |
|
122 |
||
123 |
/** |
|
124 |
* <p>Constructs a <code>JMXServiceURL</code> by parsing a Service URL |
|
125 |
* string.</p> |
|
126 |
* |
|
127 |
* @param serviceURL the URL string to be parsed. |
|
128 |
* |
|
129 |
* @exception NullPointerException if <code>serviceURL</code> is |
|
130 |
* null. |
|
131 |
* |
|
132 |
* @exception MalformedURLException if <code>serviceURL</code> |
|
133 |
* does not conform to the syntax for an Abstract Service URL or |
|
134 |
* if it is not a valid name for a JMX Remote API service. A |
|
135 |
* <code>JMXServiceURL</code> must begin with the string |
|
136 |
* <code>"service:jmx:"</code> (case-insensitive). It must not |
|
137 |
* contain any characters that are not printable ASCII characters. |
|
138 |
*/ |
|
139 |
public JMXServiceURL(String serviceURL) throws MalformedURLException { |
|
140 |
final int serviceURLLength = serviceURL.length(); |
|
141 |
||
142 |
/* Check that there are no non-ASCII characters in the URL, |
|
143 |
following RFC 2609. */ |
|
144 |
for (int i = 0; i < serviceURLLength; i++) { |
|
145 |
char c = serviceURL.charAt(i); |
|
146 |
if (c < 32 || c >= 127) { |
|
147 |
throw new MalformedURLException("Service URL contains " + |
|
148 |
"non-ASCII character 0x" + |
|
149 |
Integer.toHexString(c)); |
|
150 |
} |
|
151 |
} |
|
152 |
||
153 |
// Parse the required prefix |
|
154 |
final String requiredPrefix = "service:jmx:"; |
|
155 |
final int requiredPrefixLength = requiredPrefix.length(); |
|
156 |
if (!serviceURL.regionMatches(true, // ignore case |
|
157 |
0, // serviceURL offset |
|
158 |
requiredPrefix, |
|
159 |
0, // requiredPrefix offset |
|
160 |
requiredPrefixLength)) { |
|
161 |
throw new MalformedURLException("Service URL must start with " + |
|
162 |
requiredPrefix); |
|
163 |
} |
|
164 |
||
165 |
// Parse the protocol name |
|
166 |
final int protoStart = requiredPrefixLength; |
|
167 |
final int protoEnd = indexOf(serviceURL, ':', protoStart); |
|
168 |
this.protocol = |
|
169 |
serviceURL.substring(protoStart, protoEnd).toLowerCase(); |
|
170 |
||
171 |
if (!serviceURL.regionMatches(protoEnd, "://", 0, 3)) { |
|
172 |
throw new MalformedURLException("Missing \"://\" after " + |
|
173 |
"protocol name"); |
|
174 |
} |
|
175 |
||
176 |
// Parse the host name |
|
177 |
final int hostStart = protoEnd + 3; |
|
178 |
final int hostEnd; |
|
179 |
if (hostStart < serviceURLLength |
|
180 |
&& serviceURL.charAt(hostStart) == '[') { |
|
181 |
hostEnd = serviceURL.indexOf(']', hostStart) + 1; |
|
182 |
if (hostEnd == 0) |
|
183 |
throw new MalformedURLException("Bad host name: [ without ]"); |
|
184 |
this.host = serviceURL.substring(hostStart + 1, hostEnd - 1); |
|
185 |
if (!isNumericIPv6Address(this.host)) { |
|
186 |
throw new MalformedURLException("Address inside [...] must " + |
|
187 |
"be numeric IPv6 address"); |
|
188 |
} |
|
189 |
} else { |
|
190 |
hostEnd = |
|
191 |
indexOfFirstNotInSet(serviceURL, hostNameBitSet, hostStart); |
|
192 |
this.host = serviceURL.substring(hostStart, hostEnd); |
|
193 |
} |
|
194 |
||
195 |
// Parse the port number |
|
196 |
final int portEnd; |
|
197 |
if (hostEnd < serviceURLLength && serviceURL.charAt(hostEnd) == ':') { |
|
198 |
if (this.host.length() == 0) { |
|
199 |
throw new MalformedURLException("Cannot give port number " + |
|
200 |
"without host name"); |
|
201 |
} |
|
202 |
final int portStart = hostEnd + 1; |
|
203 |
portEnd = |
|
204 |
indexOfFirstNotInSet(serviceURL, numericBitSet, portStart); |
|
205 |
final String portString = serviceURL.substring(portStart, portEnd); |
|
206 |
try { |
|
207 |
this.port = Integer.parseInt(portString); |
|
208 |
} catch (NumberFormatException e) { |
|
209 |
throw new MalformedURLException("Bad port number: \"" + |
|
210 |
portString + "\": " + e); |
|
211 |
} |
|
212 |
} else { |
|
213 |
portEnd = hostEnd; |
|
214 |
this.port = 0; |
|
215 |
} |
|
216 |
||
217 |
// Parse the URL path |
|
218 |
final int urlPathStart = portEnd; |
|
219 |
if (urlPathStart < serviceURLLength) |
|
220 |
this.urlPath = serviceURL.substring(urlPathStart); |
|
221 |
else |
|
222 |
this.urlPath = ""; |
|
223 |
||
224 |
validate(); |
|
225 |
} |
|
226 |
||
227 |
/** |
|
228 |
* <p>Constructs a <code>JMXServiceURL</code> with the given protocol, |
|
229 |
* host, and port. This constructor is equivalent to |
|
230 |
* {@link #JMXServiceURL(String, String, int, String) |
|
231 |
* JMXServiceURL(protocol, host, port, null)}.</p> |
|
232 |
* |
|
233 |
* @param protocol the protocol part of the URL. If null, defaults |
|
234 |
* to <code>jmxmp</code>. |
|
235 |
* |
|
236 |
* @param host the host part of the URL. If null, defaults to the |
|
237 |
* local host name, as determined by |
|
238 |
* <code>InetAddress.getLocalHost().getHostName()</code>. If it |
|
239 |
* is a numeric IPv6 address, it can optionally be enclosed in |
|
240 |
* square brackets <code>[]</code>. |
|
241 |
* |
|
242 |
* @param port the port part of the URL. |
|
243 |
* |
|
244 |
* @exception MalformedURLException if one of the parts is |
|
245 |
* syntactically incorrect, or if <code>host</code> is null and it |
|
246 |
* is not possible to find the local host name, or if |
|
247 |
* <code>port</code> is negative. |
|
248 |
*/ |
|
249 |
public JMXServiceURL(String protocol, String host, int port) |
|
250 |
throws MalformedURLException { |
|
251 |
this(protocol, host, port, null); |
|
252 |
} |
|
253 |
||
254 |
/** |
|
255 |
* <p>Constructs a <code>JMXServiceURL</code> with the given parts. |
|
256 |
* |
|
257 |
* @param protocol the protocol part of the URL. If null, defaults |
|
258 |
* to <code>jmxmp</code>. |
|
259 |
* |
|
260 |
* @param host the host part of the URL. If null, defaults to the |
|
261 |
* local host name, as determined by |
|
262 |
* <code>InetAddress.getLocalHost().getHostName()</code>. If it |
|
263 |
* is a numeric IPv6 address, it can optionally be enclosed in |
|
264 |
* square brackets <code>[]</code>. |
|
265 |
* |
|
266 |
* @param port the port part of the URL. |
|
267 |
* |
|
268 |
* @param urlPath the URL path part of the URL. If null, defaults to |
|
269 |
* the empty string. |
|
270 |
* |
|
271 |
* @exception MalformedURLException if one of the parts is |
|
272 |
* syntactically incorrect, or if <code>host</code> is null and it |
|
273 |
* is not possible to find the local host name, or if |
|
274 |
* <code>port</code> is negative. |
|
275 |
*/ |
|
36
86095028f162
6607114: Make JMXServiceURL reconstructible in MXBeans
emcmanus
parents:
2
diff
changeset
|
276 |
@ConstructorProperties({"protocol", "host", "port", "URLPath"}) |
2 | 277 |
public JMXServiceURL(String protocol, String host, int port, |
278 |
String urlPath) |
|
279 |
throws MalformedURLException { |
|
280 |
if (protocol == null) |
|
281 |
protocol = "jmxmp"; |
|
282 |
||
283 |
if (host == null) { |
|
284 |
InetAddress local; |
|
285 |
try { |
|
286 |
local = InetAddress.getLocalHost(); |
|
287 |
} catch (UnknownHostException e) { |
|
288 |
throw new MalformedURLException("Local host name unknown: " + |
|
289 |
e); |
|
290 |
} |
|
291 |
||
292 |
host = local.getHostName(); |
|
293 |
||
294 |
/* We might have a hostname that violates DNS naming |
|
295 |
rules, for example that contains an `_'. While we |
|
296 |
could be strict and throw an exception, this is rather |
|
297 |
user-hostile. Instead we use its numerical IP address. |
|
298 |
We can only reasonably do this for the host==null case. |
|
299 |
If we're given an explicit host name that is illegal we |
|
300 |
have to reject it. (Bug 5057532.) */ |
|
301 |
try { |
|
302 |
validateHost(host); |
|
303 |
} catch (MalformedURLException e) { |
|
304 |
if (logger.fineOn()) { |
|
305 |
logger.fine("JMXServiceURL", |
|
306 |
"Replacing illegal local host name " + |
|
307 |
host + " with numeric IP address " + |
|
308 |
"(see RFC 1034)", e); |
|
309 |
} |
|
310 |
host = local.getHostAddress(); |
|
311 |
/* Use the numeric address, which could be either IPv4 |
|
312 |
or IPv6. validateHost will accept either. */ |
|
313 |
} |
|
314 |
} |
|
315 |
||
316 |
if (host.startsWith("[")) { |
|
317 |
if (!host.endsWith("]")) { |
|
318 |
throw new MalformedURLException("Host starts with [ but " + |
|
319 |
"does not end with ]"); |
|
320 |
} |
|
321 |
host = host.substring(1, host.length() - 1); |
|
322 |
if (!isNumericIPv6Address(host)) { |
|
323 |
throw new MalformedURLException("Address inside [...] must " + |
|
324 |
"be numeric IPv6 address"); |
|
325 |
} |
|
326 |
if (host.startsWith("[")) |
|
327 |
throw new MalformedURLException("More than one [[...]]"); |
|
328 |
} |
|
329 |
||
330 |
this.protocol = protocol.toLowerCase(); |
|
331 |
this.host = host; |
|
332 |
this.port = port; |
|
333 |
||
334 |
if (urlPath == null) |
|
335 |
urlPath = ""; |
|
336 |
this.urlPath = urlPath; |
|
337 |
||
338 |
validate(); |
|
339 |
} |
|
340 |
||
341 |
private void validate() throws MalformedURLException { |
|
342 |
||
343 |
// Check protocol |
|
344 |
||
345 |
final int protoEnd = indexOfFirstNotInSet(protocol, protocolBitSet, 0); |
|
346 |
if (protoEnd == 0 || protoEnd < protocol.length() |
|
347 |
|| !alphaBitSet.get(protocol.charAt(0))) { |
|
348 |
throw new MalformedURLException("Missing or invalid protocol " + |
|
349 |
"name: \"" + protocol + "\""); |
|
350 |
} |
|
351 |
||
352 |
// Check host |
|
353 |
||
354 |
validateHost(); |
|
355 |
||
356 |
// Check port |
|
357 |
||
358 |
if (port < 0) |
|
359 |
throw new MalformedURLException("Bad port: " + port); |
|
360 |
||
361 |
// Check URL path |
|
362 |
||
363 |
if (urlPath.length() > 0) { |
|
364 |
if (!urlPath.startsWith("/") && !urlPath.startsWith(";")) |
|
365 |
throw new MalformedURLException("Bad URL path: " + urlPath); |
|
366 |
} |
|
367 |
} |
|
368 |
||
369 |
private void validateHost() throws MalformedURLException { |
|
370 |
if (host.length() == 0) { |
|
371 |
if (port != 0) { |
|
372 |
throw new MalformedURLException("Cannot give port number " + |
|
373 |
"without host name"); |
|
374 |
} |
|
375 |
return; |
|
376 |
} |
|
377 |
||
378 |
validateHost(host); |
|
379 |
} |
|
380 |
||
381 |
private static void validateHost(String h) |
|
382 |
throws MalformedURLException { |
|
383 |
||
384 |
if (isNumericIPv6Address(h)) { |
|
385 |
/* We assume J2SE >= 1.4 here. Otherwise you can't |
|
386 |
use the address anyway. We can't call |
|
387 |
InetAddress.getByName without checking for a |
|
388 |
numeric IPv6 address, because we mustn't try to do |
|
389 |
a DNS lookup in case the address is not actually |
|
390 |
numeric. */ |
|
391 |
try { |
|
392 |
InetAddress.getByName(h); |
|
393 |
} catch (Exception e) { |
|
394 |
/* We should really catch UnknownHostException |
|
395 |
here, but a bug in JDK 1.4 causes it to throw |
|
396 |
ArrayIndexOutOfBoundsException, e.g. if the |
|
397 |
string is ":". */ |
|
398 |
MalformedURLException bad = |
|
399 |
new MalformedURLException("Bad IPv6 address: " + h); |
|
400 |
EnvHelp.initCause(bad, e); |
|
401 |
throw bad; |
|
402 |
} |
|
403 |
} else { |
|
404 |
/* Tiny state machine to check valid host name. This |
|
405 |
checks the hostname grammar from RFC 1034 (DNS), |
|
406 |
page 11. A hostname is a dot-separated list of one |
|
407 |
or more labels, where each label consists of |
|
408 |
letters, numbers, or hyphens. A label cannot begin |
|
409 |
or end with a hyphen. Empty hostnames are not |
|
410 |
allowed. Note that numeric IPv4 addresses are a |
|
411 |
special case of this grammar. |
|
412 |
||
413 |
The state is entirely captured by the last |
|
414 |
character seen, with a virtual `.' preceding the |
|
415 |
name. We represent any alphanumeric character by |
|
416 |
`a'. |
|
417 |
||
418 |
We need a special hack to check, as required by the |
|
419 |
RFC 2609 (SLP) grammar, that the last component of |
|
420 |
the hostname begins with a letter. Respecting the |
|
421 |
intent of the RFC, we only do this if there is more |
|
422 |
than one component. If your local hostname begins |
|
423 |
with a digit, we don't reject it. */ |
|
424 |
final int hostLen = h.length(); |
|
425 |
char lastc = '.'; |
|
426 |
boolean sawDot = false; |
|
427 |
char componentStart = 0; |
|
428 |
||
429 |
loop: |
|
430 |
for (int i = 0; i < hostLen; i++) { |
|
431 |
char c = h.charAt(i); |
|
432 |
boolean isAlphaNumeric = alphaNumericBitSet.get(c); |
|
433 |
if (lastc == '.') |
|
434 |
componentStart = c; |
|
435 |
if (isAlphaNumeric) |
|
436 |
lastc = 'a'; |
|
437 |
else if (c == '-') { |
|
438 |
if (lastc == '.') |
|
439 |
break; // will throw exception |
|
440 |
lastc = '-'; |
|
441 |
} else if (c == '.') { |
|
442 |
sawDot = true; |
|
443 |
if (lastc != 'a') |
|
444 |
break; // will throw exception |
|
445 |
lastc = '.'; |
|
446 |
} else { |
|
447 |
lastc = '.'; // will throw exception |
|
448 |
break; |
|
449 |
} |
|
450 |
} |
|
451 |
||
452 |
try { |
|
453 |
if (lastc != 'a') |
|
454 |
throw randomException; |
|
455 |
if (sawDot && !alphaBitSet.get(componentStart)) { |
|
456 |
/* Must be a numeric IPv4 address. In addition to |
|
457 |
the explicitly-thrown exceptions, we can get |
|
458 |
NoSuchElementException from the calls to |
|
459 |
tok.nextToken and NumberFormatException from |
|
460 |
the call to Integer.parseInt. Using exceptions |
|
461 |
for control flow this way is a bit evil but it |
|
462 |
does simplify things enormously. */ |
|
463 |
StringTokenizer tok = new StringTokenizer(h, ".", true); |
|
464 |
for (int i = 0; i < 4; i++) { |
|
465 |
String ns = tok.nextToken(); |
|
466 |
int n = Integer.parseInt(ns); |
|
467 |
if (n < 0 || n > 255) |
|
468 |
throw randomException; |
|
469 |
if (i < 3 && !tok.nextToken().equals(".")) |
|
470 |
throw randomException; |
|
471 |
} |
|
472 |
if (tok.hasMoreTokens()) |
|
473 |
throw randomException; |
|
474 |
} |
|
475 |
} catch (Exception e) { |
|
476 |
throw new MalformedURLException("Bad host: \"" + h + "\""); |
|
477 |
} |
|
478 |
} |
|
479 |
} |
|
480 |
||
481 |
private static final Exception randomException = new Exception(); |
|
482 |
||
483 |
||
484 |
/** |
|
485 |
* <p>The protocol part of the Service URL. |
|
486 |
* |
|
487 |
* @return the protocol part of the Service URL. This is never null. |
|
488 |
*/ |
|
489 |
public String getProtocol() { |
|
490 |
return protocol; |
|
491 |
} |
|
492 |
||
493 |
/** |
|
494 |
* <p>The host part of the Service URL. If the Service URL was |
|
495 |
* constructed with the constructor that takes a URL string |
|
496 |
* parameter, the result is the substring specifying the host in |
|
497 |
* that URL. If the Service URL was constructed with a |
|
498 |
* constructor that takes a separate host parameter, the result is |
|
499 |
* the string that was specified. If that string was null, the |
|
500 |
* result is |
|
501 |
* <code>InetAddress.getLocalHost().getHostName()</code>.</p> |
|
502 |
* |
|
503 |
* <p>In either case, if the host was specified using the |
|
504 |
* <code>[...]</code> syntax for numeric IPv6 addresses, the |
|
505 |
* square brackets are not included in the return value here.</p> |
|
506 |
* |
|
507 |
* @return the host part of the Service URL. This is never null. |
|
508 |
*/ |
|
509 |
public String getHost() { |
|
510 |
return host; |
|
511 |
} |
|
512 |
||
513 |
/** |
|
514 |
* <p>The port of the Service URL. If no port was |
|
515 |
* specified, the returned value is 0.</p> |
|
516 |
* |
|
517 |
* @return the port of the Service URL, or 0 if none. |
|
518 |
*/ |
|
519 |
public int getPort() { |
|
520 |
return port; |
|
521 |
} |
|
522 |
||
523 |
/** |
|
524 |
* <p>The URL Path part of the Service URL. This is an empty |
|
525 |
* string, or a string beginning with a slash (<code>/</code>), or |
|
526 |
* a string beginning with a semicolon (<code>;</code>). |
|
527 |
* |
|
528 |
* @return the URL Path part of the Service URL. This is never |
|
529 |
* null. |
|
530 |
*/ |
|
531 |
public String getURLPath() { |
|
532 |
return urlPath; |
|
533 |
} |
|
534 |
||
535 |
/** |
|
536 |
* <p>The string representation of this Service URL. If the value |
|
537 |
* returned by this method is supplied to the |
|
538 |
* <code>JMXServiceURL</code> constructor, the resultant object is |
|
539 |
* equal to this one.</p> |
|
540 |
* |
|
541 |
* <p>The <code><em>host</em></code> part of the returned string |
|
542 |
* is the value returned by {@link #getHost()}. If that value |
|
543 |
* specifies a numeric IPv6 address, it is surrounded by square |
|
544 |
* brackets <code>[]</code>.</p> |
|
545 |
* |
|
546 |
* <p>The <code><em>port</em></code> part of the returned string |
|
547 |
* is the value returned by {@link #getPort()} in its shortest |
|
548 |
* decimal form. If the value is zero, it is omitted.</p> |
|
549 |
* |
|
550 |
* @return the string representation of this Service URL. |
|
551 |
*/ |
|
552 |
public String toString() { |
|
553 |
/* We don't bother synchronizing the access to toString. At worst, |
|
554 |
n threads will independently compute and store the same value. */ |
|
555 |
if (toString != null) |
|
556 |
return toString; |
|
557 |
StringBuilder buf = new StringBuilder("service:jmx:"); |
|
558 |
buf.append(getProtocol()).append("://"); |
|
559 |
final String getHost = getHost(); |
|
560 |
if (isNumericIPv6Address(getHost)) |
|
561 |
buf.append('[').append(getHost).append(']'); |
|
562 |
else |
|
563 |
buf.append(getHost); |
|
564 |
final int getPort = getPort(); |
|
565 |
if (getPort != 0) |
|
566 |
buf.append(':').append(getPort); |
|
567 |
buf.append(getURLPath()); |
|
568 |
toString = buf.toString(); |
|
569 |
return toString; |
|
570 |
} |
|
571 |
||
572 |
/** |
|
573 |
* <p>Indicates whether some other object is equal to this one. |
|
574 |
* This method returns true if and only if <code>obj</code> is an |
|
575 |
* instance of <code>JMXServiceURL</code> whose {@link |
|
576 |
* #getProtocol()}, {@link #getHost()}, {@link #getPort()}, and |
|
577 |
* {@link #getURLPath()} methods return the same values as for |
|
578 |
* this object. The values for {@link #getProtocol()} and {@link |
|
579 |
* #getHost()} can differ in case without affecting equality. |
|
580 |
* |
|
581 |
* @param obj the reference object with which to compare. |
|
582 |
* |
|
583 |
* @return <code>true</code> if this object is the same as the |
|
584 |
* <code>obj</code> argument; <code>false</code> otherwise. |
|
585 |
*/ |
|
586 |
public boolean equals(Object obj) { |
|
587 |
if (!(obj instanceof JMXServiceURL)) |
|
588 |
return false; |
|
589 |
JMXServiceURL u = (JMXServiceURL) obj; |
|
590 |
return |
|
591 |
(u.getProtocol().equalsIgnoreCase(getProtocol()) && |
|
592 |
u.getHost().equalsIgnoreCase(getHost()) && |
|
593 |
u.getPort() == getPort() && |
|
594 |
u.getURLPath().equals(getURLPath())); |
|
595 |
} |
|
596 |
||
597 |
public int hashCode() { |
|
598 |
return toString().hashCode(); |
|
599 |
} |
|
600 |
||
601 |
/* True if this string, assumed to be a valid argument to |
|
602 |
* InetAddress.getByName, is a numeric IPv6 address. |
|
603 |
*/ |
|
604 |
private static boolean isNumericIPv6Address(String s) { |
|
605 |
// address contains colon if and only if it's a numeric IPv6 address |
|
606 |
return (s.indexOf(':') >= 0); |
|
607 |
} |
|
608 |
||
609 |
// like String.indexOf but returns string length not -1 if not present |
|
610 |
private static int indexOf(String s, char c, int fromIndex) { |
|
611 |
int index = s.indexOf(c, fromIndex); |
|
612 |
if (index < 0) |
|
613 |
return s.length(); |
|
614 |
else |
|
615 |
return index; |
|
616 |
} |
|
617 |
||
618 |
private static int indexOfFirstNotInSet(String s, BitSet set, |
|
619 |
int fromIndex) { |
|
620 |
final int slen = s.length(); |
|
621 |
int i = fromIndex; |
|
622 |
while (true) { |
|
623 |
if (i >= slen) |
|
624 |
break; |
|
625 |
char c = s.charAt(i); |
|
626 |
if (c >= 128) |
|
627 |
break; // not ASCII |
|
628 |
if (!set.get(c)) |
|
629 |
break; |
|
630 |
i++; |
|
631 |
} |
|
632 |
return i; |
|
633 |
} |
|
634 |
||
635 |
private final static BitSet alphaBitSet = new BitSet(128); |
|
636 |
private final static BitSet numericBitSet = new BitSet(128); |
|
637 |
private final static BitSet alphaNumericBitSet = new BitSet(128); |
|
638 |
private final static BitSet protocolBitSet = new BitSet(128); |
|
639 |
private final static BitSet hostNameBitSet = new BitSet(128); |
|
640 |
static { |
|
641 |
/* J2SE 1.4 adds lots of handy methods to BitSet that would |
|
642 |
allow us to simplify here, e.g. by not writing loops, but |
|
643 |
we want to work on J2SE 1.3 too. */ |
|
644 |
||
645 |
for (char c = '0'; c <= '9'; c++) |
|
646 |
numericBitSet.set(c); |
|
647 |
||
648 |
for (char c = 'A'; c <= 'Z'; c++) |
|
649 |
alphaBitSet.set(c); |
|
650 |
for (char c = 'a'; c <= 'z'; c++) |
|
651 |
alphaBitSet.set(c); |
|
652 |
||
653 |
alphaNumericBitSet.or(alphaBitSet); |
|
654 |
alphaNumericBitSet.or(numericBitSet); |
|
655 |
||
656 |
protocolBitSet.or(alphaNumericBitSet); |
|
657 |
protocolBitSet.set('+'); |
|
658 |
protocolBitSet.set('-'); |
|
659 |
||
660 |
hostNameBitSet.or(alphaNumericBitSet); |
|
661 |
hostNameBitSet.set('-'); |
|
662 |
hostNameBitSet.set('.'); |
|
663 |
} |
|
664 |
||
665 |
/** |
|
666 |
* The value returned by {@link #getProtocol()}. |
|
667 |
*/ |
|
668 |
private final String protocol; |
|
669 |
||
670 |
/** |
|
671 |
* The value returned by {@link #getHost()}. |
|
672 |
*/ |
|
673 |
private final String host; |
|
674 |
||
675 |
/** |
|
676 |
* The value returned by {@link #getPort()}. |
|
677 |
*/ |
|
678 |
private final int port; |
|
679 |
||
680 |
/** |
|
681 |
* The value returned by {@link #getURLPath()}. |
|
682 |
*/ |
|
683 |
private final String urlPath; |
|
684 |
||
685 |
/** |
|
686 |
* Cached result of {@link #toString()}. |
|
687 |
*/ |
|
688 |
private transient String toString; |
|
689 |
||
690 |
private static final ClassLogger logger = |
|
691 |
new ClassLogger("javax.management.remote.misc", "JMXServiceURL"); |
|
692 |
} |