0N/A/*
6276N/A * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.rmi.dgc;
0N/A
0N/Aimport java.rmi.server.UID;
6276N/Aimport java.security.SecureRandom;
0N/A
0N/A/**
0N/A * A VMID is a identifier that is unique across all Java virtual
0N/A * machines. VMIDs are used by the distributed garbage collector
0N/A * to identify client VMs.
0N/A *
0N/A * @author Ann Wollrath
0N/A * @author Peter Jones
0N/A */
0N/Apublic final class VMID implements java.io.Serializable {
6276N/A /** Array of bytes uniquely identifying this host */
6276N/A private static final byte[] randomBytes;
0N/A
0N/A /**
0N/A * @serial array of bytes uniquely identifying host created on
0N/A */
0N/A private byte[] addr;
0N/A
0N/A /**
0N/A * @serial unique identifier with respect to host created on
0N/A */
0N/A private UID uid;
0N/A
0N/A /** indicate compatibility with JDK 1.1.x version of class */
0N/A private static final long serialVersionUID = -538642295484486218L;
0N/A
6276N/A static {
6276N/A // Generate 8 bytes of random data.
6276N/A SecureRandom secureRandom = new SecureRandom();
6276N/A byte bytes[] = new byte[8];
6276N/A secureRandom.nextBytes(bytes);
6276N/A randomBytes = bytes;
6276N/A }
6276N/A
0N/A /**
0N/A * Create a new VMID. Each new VMID returned from this constructor
0N/A * is unique for all Java virtual machines under the following
0N/A * conditions: a) the conditions for uniqueness for objects of
0N/A * the class <code>java.rmi.server.UID</code> are satisfied, and b) an
0N/A * address can be obtained for this host that is unique and constant
0N/A * for the lifetime of this object. <p>
0N/A */
0N/A public VMID() {
6276N/A addr = randomBytes;
0N/A uid = new UID();
0N/A }
0N/A
0N/A /**
0N/A * Return true if an accurate address can be determined for this
0N/A * host. If false, reliable VMID cannot be generated from this host
0N/A * @return true if host address can be determined, false otherwise
0N/A * @deprecated
0N/A */
0N/A @Deprecated
0N/A public static boolean isUnique() {
0N/A return true;
0N/A }
0N/A
0N/A /**
0N/A * Compute hash code for this VMID.
0N/A */
0N/A public int hashCode() {
0N/A return uid.hashCode();
0N/A }
0N/A
0N/A /**
0N/A * Compare this VMID to another, and return true if they are the
0N/A * same identifier.
0N/A */
0N/A public boolean equals(Object obj) {
0N/A if (obj instanceof VMID) {
0N/A VMID vmid = (VMID) obj;
0N/A if (!uid.equals(vmid.uid))
0N/A return false;
0N/A if ((addr == null) ^ (vmid.addr == null))
0N/A return false;
0N/A if (addr != null) {
0N/A if (addr.length != vmid.addr.length)
0N/A return false;
0N/A for (int i = 0; i < addr.length; ++ i)
0N/A if (addr[i] != vmid.addr[i])
0N/A return false;
0N/A }
0N/A return true;
0N/A } else {
0N/A return false;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Return string representation of this VMID.
0N/A */
0N/A public String toString() {
0N/A StringBuffer result = new StringBuffer();
0N/A if (addr != null)
0N/A for (int i = 0; i < addr.length; ++ i) {
0N/A int x = (int) (addr[i] & 0xFF);
0N/A result.append((x < 0x10 ? "0" : "") +
0N/A Integer.toString(x, 16));
0N/A }
0N/A result.append(':');
0N/A result.append(uid.toString());
0N/A return result.toString();
0N/A }
0N/A}