1 /*
   2  * Copyright (c) 1996, 2013, 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 java.rmi.dgc;
  27 
  28 import java.rmi.server.UID;
  29 import java.security.SecureRandom;
  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 {
  40     /** Array of bytes uniquely identifying this host */
  41     private static final byte[] randomBytes;
  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 
  56     static {
  57         // Generate 8 bytes of random data.
  58         SecureRandom secureRandom = new SecureRandom();
  59         byte bytes[] = new byte[8];
  60         secureRandom.nextBytes(bytes);
  61         randomBytes = bytes;
  62     }
  63 
  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
  70      * for the lifetime of this object.
  71      */
  72     public VMID() {
  73         addr = randomBytes;
  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() {
 123         StringBuilder sb = new StringBuilder();
 124         if (addr != null)
 125             for (int i = 0; i < addr.length; ++ i) {
 126                 int x = addr[i] & 0xFF;
 127                 sb.append(x < 0x10 ? "0" : "").append(Integer.toString(x, 16));
 128             }
 129         sb.append(':');
 130         sb.append(uid.toString());
 131         return sb.toString();
 132     }
 133 }