author | michaelm |
Wed, 13 Nov 2019 09:09:41 +0000 | |
changeset 59047 | 6c78185c99d7 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
18193
3812d4a4207a
8001033: Refactor network address handling in virtual machine identifiers
dmocek
parents:
14342
diff
changeset
|
2 |
* Copyright (c) 1996, 2013, Oracle and/or its affiliates. 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.rmi.dgc; |
|
27 |
||
28 |
import java.rmi.server.UID; |
|
18193
3812d4a4207a
8001033: Refactor network address handling in virtual machine identifiers
dmocek
parents:
14342
diff
changeset
|
29 |
import java.security.SecureRandom; |
2 | 30 |
|
31 |
/** |
|
32 |
* A VMID is a identifier that is unique across all Java virtual |
|
33 |
* machines. VMIDs are used by the distributed garbage collector |
|
34 |
* to identify client VMs. |
|
35 |
* |
|
36 |
* @author Ann Wollrath |
|
37 |
* @author Peter Jones |
|
38 |
*/ |
|
39 |
public final class VMID implements java.io.Serializable { |
|
18193
3812d4a4207a
8001033: Refactor network address handling in virtual machine identifiers
dmocek
parents:
14342
diff
changeset
|
40 |
/** Array of bytes uniquely identifying this host */ |
3812d4a4207a
8001033: Refactor network address handling in virtual machine identifiers
dmocek
parents:
14342
diff
changeset
|
41 |
private static final byte[] randomBytes; |
2 | 42 |
|
43 |
/** |
|
44 |
* @serial array of bytes uniquely identifying host created on |
|
45 |
*/ |
|
46 |
private byte[] addr; |
|
47 |
||
48 |
/** |
|
49 |
* @serial unique identifier with respect to host created on |
|
50 |
*/ |
|
51 |
private UID uid; |
|
52 |
||
53 |
/** indicate compatibility with JDK 1.1.x version of class */ |
|
54 |
private static final long serialVersionUID = -538642295484486218L; |
|
55 |
||
18193
3812d4a4207a
8001033: Refactor network address handling in virtual machine identifiers
dmocek
parents:
14342
diff
changeset
|
56 |
static { |
3812d4a4207a
8001033: Refactor network address handling in virtual machine identifiers
dmocek
parents:
14342
diff
changeset
|
57 |
// Generate 8 bytes of random data. |
3812d4a4207a
8001033: Refactor network address handling in virtual machine identifiers
dmocek
parents:
14342
diff
changeset
|
58 |
SecureRandom secureRandom = new SecureRandom(); |
3812d4a4207a
8001033: Refactor network address handling in virtual machine identifiers
dmocek
parents:
14342
diff
changeset
|
59 |
byte bytes[] = new byte[8]; |
3812d4a4207a
8001033: Refactor network address handling in virtual machine identifiers
dmocek
parents:
14342
diff
changeset
|
60 |
secureRandom.nextBytes(bytes); |
3812d4a4207a
8001033: Refactor network address handling in virtual machine identifiers
dmocek
parents:
14342
diff
changeset
|
61 |
randomBytes = bytes; |
3812d4a4207a
8001033: Refactor network address handling in virtual machine identifiers
dmocek
parents:
14342
diff
changeset
|
62 |
} |
3812d4a4207a
8001033: Refactor network address handling in virtual machine identifiers
dmocek
parents:
14342
diff
changeset
|
63 |
|
2 | 64 |
/** |
65 |
* Create a new VMID. Each new VMID returned from this constructor |
|
66 |
* is unique for all Java virtual machines under the following |
|
67 |
* conditions: a) the conditions for uniqueness for objects of |
|
68 |
* the class <code>java.rmi.server.UID</code> are satisfied, and b) an |
|
69 |
* address can be obtained for this host that is unique and constant |
|
21334 | 70 |
* for the lifetime of this object. |
2 | 71 |
*/ |
72 |
public VMID() { |
|
18193
3812d4a4207a
8001033: Refactor network address handling in virtual machine identifiers
dmocek
parents:
14342
diff
changeset
|
73 |
addr = randomBytes; |
2 | 74 |
uid = new UID(); |
75 |
} |
|
76 |
||
77 |
/** |
|
78 |
* Return true if an accurate address can be determined for this |
|
79 |
* host. If false, reliable VMID cannot be generated from this host |
|
80 |
* @return true if host address can be determined, false otherwise |
|
81 |
* @deprecated |
|
82 |
*/ |
|
83 |
@Deprecated |
|
84 |
public static boolean isUnique() { |
|
85 |
return true; |
|
86 |
} |
|
87 |
||
88 |
/** |
|
89 |
* Compute hash code for this VMID. |
|
90 |
*/ |
|
91 |
public int hashCode() { |
|
92 |
return uid.hashCode(); |
|
93 |
} |
|
94 |
||
95 |
/** |
|
96 |
* Compare this VMID to another, and return true if they are the |
|
97 |
* same identifier. |
|
98 |
*/ |
|
99 |
public boolean equals(Object obj) { |
|
100 |
if (obj instanceof VMID) { |
|
101 |
VMID vmid = (VMID) obj; |
|
102 |
if (!uid.equals(vmid.uid)) |
|
103 |
return false; |
|
104 |
if ((addr == null) ^ (vmid.addr == null)) |
|
105 |
return false; |
|
106 |
if (addr != null) { |
|
107 |
if (addr.length != vmid.addr.length) |
|
108 |
return false; |
|
109 |
for (int i = 0; i < addr.length; ++ i) |
|
110 |
if (addr[i] != vmid.addr[i]) |
|
111 |
return false; |
|
112 |
} |
|
113 |
return true; |
|
114 |
} else { |
|
115 |
return false; |
|
116 |
} |
|
117 |
} |
|
118 |
||
119 |
/** |
|
120 |
* Return string representation of this VMID. |
|
121 |
*/ |
|
122 |
public String toString() { |
|
24969
afa6934dd8e8
8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents:
21334
diff
changeset
|
123 |
StringBuilder sb = new StringBuilder(); |
2 | 124 |
if (addr != null) |
125 |
for (int i = 0; i < addr.length; ++ i) { |
|
11117
b6e68b1344d4
7116404: Miscellaneous warnings (java.rmi.**, serialization, some core classes)
alanb
parents:
10419
diff
changeset
|
126 |
int x = addr[i] & 0xFF; |
24969
afa6934dd8e8
8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents:
21334
diff
changeset
|
127 |
sb.append((x < 0x10 ? "0" : "") + |
afa6934dd8e8
8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents:
21334
diff
changeset
|
128 |
Integer.toString(x, 16)); |
2 | 129 |
} |
24969
afa6934dd8e8
8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents:
21334
diff
changeset
|
130 |
sb.append(':'); |
afa6934dd8e8
8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents:
21334
diff
changeset
|
131 |
sb.append(uid.toString()); |
afa6934dd8e8
8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents:
21334
diff
changeset
|
132 |
return sb.toString(); |
2 | 133 |
} |
134 |
} |