jdk/src/share/classes/java/rmi/dgc/VMID.java
author sherman
Tue, 30 Aug 2011 11:53:11 -0700
changeset 10419 12c063b39232
parent 5506 202f599c92aa
child 11117 b6e68b1344d4
permissions -rw-r--r--
7084245: Update usages of InternalError to use exception chaining Summary: to use new InternalError constructor with cause chainning Reviewed-by: alanb, ksrini, xuelei, neugens Contributed-by: sebastian.sickelmann@gmx.de
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     2
 * Copyright (c) 1996, 2008, 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.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.rmi.server.UID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * A VMID is a identifier that is unique across all Java virtual
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * machines.  VMIDs are used by the distributed garbage collector
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * to identify client VMs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * @author      Ann Wollrath
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @author      Peter Jones
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
public final class VMID implements java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    /** array of bytes uniquely identifying this host */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private static byte[] localAddr = computeAddressHash();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     * @serial array of bytes uniquely identifying host created on
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private byte[] addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * @serial unique identifier with respect to host created on
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private UID uid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /** indicate compatibility with JDK 1.1.x version of class */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private static final long serialVersionUID = -538642295484486218L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * Create a new VMID.  Each new VMID returned from this constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * is unique for all Java virtual machines under the following
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * conditions: a) the conditions for uniqueness for objects of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * the class <code>java.rmi.server.UID</code> are satisfied, and b) an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * address can be obtained for this host that is unique and constant
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * for the lifetime of this object.  <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    public VMID() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        addr = localAddr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        uid = new UID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * Return true if an accurate address can be determined for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * host.  If false, reliable VMID cannot be generated from this host
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * @return true if host address can be determined, false otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * @deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    public static boolean isUnique() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * Compute hash code for this VMID.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        return uid.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * Compare this VMID to another, and return true if they are the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * same identifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        if (obj instanceof VMID) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            VMID vmid = (VMID) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            if (!uid.equals(vmid.uid))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            if ((addr == null) ^ (vmid.addr == null))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            if (addr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                if (addr.length != vmid.addr.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                for (int i = 0; i < addr.length; ++ i)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                    if (addr[i] != vmid.addr[i])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * Return string representation of this VMID.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        StringBuffer result = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        if (addr != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            for (int i = 0; i < addr.length; ++ i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                int x = (int) (addr[i] & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                result.append((x < 0x10 ? "0" : "") +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                              Integer.toString(x, 16));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        result.append(':');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        result.append(uid.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        return result.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * Compute the hash an IP address.  The hash is the first 8 bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * of the SHA digest of the IP address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    private static byte[] computeAddressHash() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
         * Get the local host's IP address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
         */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   139
        byte[] addr = java.security.AccessController.doPrivileged(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   140
            new PrivilegedAction<byte[]>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   141
            public byte[] run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                    return InetAddress.getLocalHost().getAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                return new byte[] { 0, 0, 0, 0 };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        byte[] addrHash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        final int ADDR_HASH_LENGTH = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
             * Calculate message digest of IP address using SHA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            MessageDigest md = MessageDigest.getInstance("SHA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            ByteArrayOutputStream sink = new ByteArrayOutputStream(64);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            DataOutputStream out = new DataOutputStream(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                new DigestOutputStream(sink, md));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            out.write(addr, 0, addr.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            byte digest[] = md.digest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            int hashlength = Math.min(ADDR_HASH_LENGTH, digest.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            addrHash = new byte[hashlength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            System.arraycopy(digest, 0, addrHash, 0, hashlength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        } catch (IOException ignore) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            /* can't happen, but be deterministic anyway. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            addrHash = new byte[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        } catch (NoSuchAlgorithmException complain) {
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 5506
diff changeset
   173
            throw new InternalError(complain.toString(), complain);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        return addrHash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
}