Path: blob/master/src/java.rmi/share/classes/java/rmi/dgc/VMID.java
41159 views
/*1* Copyright (c) 1996, 2013, 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 java.rmi.dgc;2627import java.rmi.server.UID;28import java.security.SecureRandom;2930/**31* A VMID is a identifier that is unique across all Java virtual32* machines. VMIDs are used by the distributed garbage collector33* to identify client VMs.34*35* @author Ann Wollrath36* @author Peter Jones37*/38public final class VMID implements java.io.Serializable {39/** Array of bytes uniquely identifying this host */40private static final byte[] randomBytes;4142/**43* @serial array of bytes uniquely identifying host created on44*/45private byte[] addr;4647/**48* @serial unique identifier with respect to host created on49*/50private UID uid;5152/** indicate compatibility with JDK 1.1.x version of class */53private static final long serialVersionUID = -538642295484486218L;5455static {56// Generate 8 bytes of random data.57SecureRandom secureRandom = new SecureRandom();58byte bytes[] = new byte[8];59secureRandom.nextBytes(bytes);60randomBytes = bytes;61}6263/**64* Create a new VMID. Each new VMID returned from this constructor65* is unique for all Java virtual machines under the following66* conditions: a) the conditions for uniqueness for objects of67* the class <code>java.rmi.server.UID</code> are satisfied, and b) an68* address can be obtained for this host that is unique and constant69* for the lifetime of this object.70*/71public VMID() {72addr = randomBytes;73uid = new UID();74}7576/**77* Return true if an accurate address can be determined for this78* host. If false, reliable VMID cannot be generated from this host79* @return true if host address can be determined, false otherwise80* @deprecated81*/82@Deprecated83public static boolean isUnique() {84return true;85}8687/**88* Compute hash code for this VMID.89*/90public int hashCode() {91return uid.hashCode();92}9394/**95* Compare this VMID to another, and return true if they are the96* same identifier.97*/98public boolean equals(Object obj) {99if (obj instanceof VMID) {100VMID vmid = (VMID) obj;101if (!uid.equals(vmid.uid))102return false;103if ((addr == null) ^ (vmid.addr == null))104return false;105if (addr != null) {106if (addr.length != vmid.addr.length)107return false;108for (int i = 0; i < addr.length; ++ i)109if (addr[i] != vmid.addr[i])110return false;111}112return true;113} else {114return false;115}116}117118/**119* Return string representation of this VMID.120*/121public String toString() {122StringBuilder sb = new StringBuilder();123if (addr != null)124for (int i = 0; i < addr.length; ++ i) {125int x = addr[i] & 0xFF;126sb.append((x < 0x10 ? "0" : "") +127Integer.toString(x, 16));128}129sb.append(':');130sb.append(uid.toString());131return sb.toString();132}133}134135136