001 /* VMID.java -- The object ID, unique between all virtual machines. 002 Copyright (c) 1996, 1997, 1998, 1999, 2006 Free Software Foundation, Inc. 003 004 This file is part of GNU Classpath. 005 006 GNU Classpath is free software; you can redistribute it and/or modify 007 it under the terms of the GNU General Public License as published by 008 the Free Software Foundation; either version 2, or (at your option) 009 any later version. 010 011 GNU Classpath is distributed in the hope that it will be useful, but 012 WITHOUT ANY WARRANTY; without even the implied warranty of 013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 General Public License for more details. 015 016 You should have received a copy of the GNU General Public License 017 along with GNU Classpath; see the file COPYING. If not, write to the 018 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 019 02110-1301 USA. 020 021 Linking this library statically or dynamically with other modules is 022 making a combined work based on this library. Thus, the terms and 023 conditions of the GNU General Public License cover the whole 024 combination. 025 026 As a special exception, the copyright holders of this library give you 027 permission to link this library with independent modules to produce an 028 executable, regardless of the license terms of these independent 029 modules, and to copy and distribute the resulting executable under 030 terms of your choice, provided that you also meet, for each linked 031 independent module, the terms and conditions of the license of that 032 module. An independent module is a module which is not derived from 033 or based on this library. If you modify this library, you may extend 034 this exception to your version of the library, but you are not 035 obligated to do so. If you do not wish to do so, delete this 036 exception statement from your version. */ 037 038 package java.rmi.dgc; 039 040 import java.io.Serializable; 041 import java.net.InetAddress; 042 import java.net.UnknownHostException; 043 import java.rmi.server.UID; 044 import java.util.Arrays; 045 046 /** 047 * An identifier that is unique accross the all virtual machines. This class is 048 * used by distributed garbage collector to identify the virtual machine of 049 * the client, but may also be used in various other cases, when such identifier 050 * is required. This class separately stores and transfers the host IP 051 * address, but will try to do its best also for the case if it failed to 052 * determine it. The alternative algorithms are used in {@link UID} that is 053 * part of this class. The VMID's, created on the same host, but in the two 054 * separately (parallely) running virtual machines are different. 055 */ 056 public final class VMID implements Serializable 057 { 058 /** 059 * Use SVUID for interoperability. 060 */ 061 static final long serialVersionUID = -538642295484486218L; 062 063 /** 064 * If true, the IP of this host can ve reliably determined. 065 */ 066 static boolean areWeUnique; 067 068 /** 069 * The IP address of the local host. 070 */ 071 static byte[] localAddr; 072 073 /** 074 * The IP address of the local host. 075 */ 076 private byte[] addr; 077 078 /** 079 * The cached hash code. 080 */ 081 transient int hash; 082 083 /** 084 * The UID of this VMID. 085 */ 086 private UID uid; 087 088 static 089 { 090 // This "local host" value usually indicates that the local 091 // IP address cannot be reliably determined. 092 byte[] localHost = new byte[] { 127, 0, 0, 1 }; 093 094 try 095 { 096 localAddr = InetAddress.getLocalHost().getAddress(); 097 areWeUnique = !Arrays.equals(localHost, localAddr); 098 } 099 catch (UnknownHostException uhex) 100 { 101 localAddr = localHost; 102 areWeUnique = false; 103 } 104 } 105 106 /** 107 * Create the new VMID. All VMID's are unique accross tha all virtual 108 * machines. 109 */ 110 public VMID() 111 { 112 addr = localAddr; 113 uid = new UID(); 114 } 115 116 /** 117 * Return true if it is possible to get the accurate address of this host. 118 * If false is returned, the created VMID's are less reliable, but the 119 * starting time and possibly the memory allocation are also taken into 120 * consideration in the incorporated UID. Hence the VMID's, created on the 121 * different virtual machines, still should be different. 122 * 123 * @deprecated VMID's are more or less always reliable. 124 * 125 * @return false if the local host ip address is 127.0.0.1 or unknown, 126 * true otherwise. 127 */ 128 public static boolean isUnique () 129 { 130 return areWeUnique; 131 } 132 133 /** 134 * Get the hash code of this VMID. 135 */ 136 public int hashCode () 137 { 138 if (hash==0) 139 { 140 for (int i = 0; i < localAddr.length; i++) 141 hash += addr[i]; 142 hash = hash ^ uid.hashCode(); 143 } 144 return hash; 145 } 146 147 /** 148 * Returns true if the passed parameter is also VMID and it is equal to this 149 * VMID. The VMID should only be equal to itself (also if the passed value is 150 * another instance, cloned by serialization). 151 */ 152 public boolean equals(Object obj) 153 { 154 if (obj instanceof VMID) 155 { 156 VMID other = (VMID) obj; 157 158 // The UID's are compared faster than arrays. 159 return uid.equals(other.uid) && Arrays.equals(addr, other.addr); 160 } 161 else 162 return false; 163 164 } 165 166 /** 167 * Get the string representation of this VMID. 168 */ 169 public String toString () 170 { 171 StringBuffer buf = new StringBuffer ("[VMID: "); 172 173 for (int i = 0; i < addr.length; i++) 174 { 175 if (i > 0) 176 { 177 buf.append ("."); 178 } 179 180 buf.append (Integer.toString (addr [i])); 181 } 182 183 buf.append (" "); 184 buf.append (uid.toString ()); 185 buf.append ("]"); 186 187 return buf.toString(); 188 } 189 }