jdk/src/share/classes/java/rmi/dgc/VMID.java
author dmocek
Wed, 13 Mar 2013 17:24:34 -0700
changeset 18193 3812d4a4207a
parent 14342 8435a30053c1
child 21334 c60dfce46a77
permissions -rw-r--r--
8001033: Refactor network address handling in virtual machine identifiers Reviewed-by: smarks, hawtin, mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.rmi.dgc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * A VMID is a identifier that is unique across all Java virtual
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * machines.  VMIDs are used by the distributed garbage collector
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * to identify client VMs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * @author      Ann Wollrath
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * @author      Peter Jones
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
     * @serial array of bytes uniquely identifying host created on
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private byte[] addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * @serial unique identifier with respect to host created on
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private UID uid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    /** indicate compatibility with JDK 1.1.x version of class */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private static final long serialVersionUID = -538642295484486218L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * Create a new VMID.  Each new VMID returned from this constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * is unique for all Java virtual machines under the following
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * conditions: a) the conditions for uniqueness for objects of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * the class <code>java.rmi.server.UID</code> are satisfied, and b) an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * address can be obtained for this host that is unique and constant
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * for the lifetime of this object.  <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    public VMID() {
18193
3812d4a4207a 8001033: Refactor network address handling in virtual machine identifiers
dmocek
parents: 14342
diff changeset
    73
        addr = randomBytes;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        uid = new UID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * Return true if an accurate address can be determined for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * host.  If false, reliable VMID cannot be generated from this host
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * @return true if host address can be determined, false otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * @deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    public static boolean isUnique() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * Compute hash code for this VMID.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        return uid.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * Compare this VMID to another, and return true if they are the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * same identifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        if (obj instanceof VMID) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            VMID vmid = (VMID) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            if (!uid.equals(vmid.uid))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            if ((addr == null) ^ (vmid.addr == null))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            if (addr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                if (addr.length != vmid.addr.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                for (int i = 0; i < addr.length; ++ i)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                    if (addr[i] != vmid.addr[i])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * Return string representation of this VMID.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        StringBuffer result = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        if (addr != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                result.append((x < 0x10 ? "0" : "") +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                              Integer.toString(x, 16));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        result.append(':');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        result.append(uid.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        return result.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
}