libjava/ChangeLog:
[official-gcc.git] / libjava / classpath / java / rmi / dgc / VMID.java
blob22b737367df34a7a2c0dba128ad633cfa8c35e27
1 /* VMID.java -- The object ID, unique between all virtual machines.
2 Copyright (c) 1996, 1997, 1998, 1999, 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package java.rmi.dgc;
40 import gnu.java.lang.CPStringBuilder;
42 import java.io.Serializable;
43 import java.net.InetAddress;
44 import java.net.UnknownHostException;
45 import java.rmi.server.UID;
46 import java.util.Arrays;
48 /**
49 * An identifier that is unique accross the all virtual machines. This class is
50 * used by distributed garbage collector to identify the virtual machine of
51 * the client, but may also be used in various other cases, when such identifier
52 * is required. This class separately stores and transfers the host IP
53 * address, but will try to do its best also for the case if it failed to
54 * determine it. The alternative algorithms are used in {@link UID} that is
55 * part of this class. The VMID's, created on the same host, but in the two
56 * separately (parallely) running virtual machines are different.
58 public final class VMID implements Serializable
60 /**
61 * Use SVUID for interoperability.
63 static final long serialVersionUID = -538642295484486218L;
65 /**
66 * If true, the IP of this host can ve reliably determined.
68 static boolean areWeUnique;
70 /**
71 * The IP address of the local host.
73 static byte[] localAddr;
75 /**
76 * The IP address of the local host.
78 private byte[] addr;
80 /**
81 * The cached hash code.
83 transient int hash;
85 /**
86 * The UID of this VMID.
88 private UID uid;
90 static
92 // This "local host" value usually indicates that the local
93 // IP address cannot be reliably determined.
94 byte[] localHost = new byte[] { 127, 0, 0, 1 };
96 try
98 localAddr = InetAddress.getLocalHost().getAddress();
99 areWeUnique = !Arrays.equals(localHost, localAddr);
101 catch (UnknownHostException uhex)
103 localAddr = localHost;
104 areWeUnique = false;
109 * Create the new VMID. All VMID's are unique accross tha all virtual
110 * machines.
112 public VMID()
114 addr = localAddr;
115 uid = new UID();
119 * Return true if it is possible to get the accurate address of this host.
120 * If false is returned, the created VMID's are less reliable, but the
121 * starting time and possibly the memory allocation are also taken into
122 * consideration in the incorporated UID. Hence the VMID's, created on the
123 * different virtual machines, still should be different.
125 * @deprecated VMID's are more or less always reliable.
127 * @return false if the local host ip address is 127.0.0.1 or unknown,
128 * true otherwise.
130 public static boolean isUnique ()
132 return areWeUnique;
136 * Get the hash code of this VMID.
138 public int hashCode ()
140 if (hash==0)
142 for (int i = 0; i < localAddr.length; i++)
143 hash += addr[i];
144 hash = hash ^ uid.hashCode();
146 return hash;
150 * Returns true if the passed parameter is also VMID and it is equal to this
151 * VMID. The VMID should only be equal to itself (also if the passed value is
152 * another instance, cloned by serialization).
154 public boolean equals(Object obj)
156 if (obj instanceof VMID)
158 VMID other = (VMID) obj;
160 // The UID's are compared faster than arrays.
161 return uid.equals(other.uid) && Arrays.equals(addr, other.addr);
163 else
164 return false;
169 * Get the string representation of this VMID.
171 public String toString ()
173 CPStringBuilder buf = new CPStringBuilder ("[VMID: ");
175 for (int i = 0; i < addr.length; i++)
177 if (i > 0)
179 buf.append (".");
182 buf.append (Integer.toString (addr [i]));
185 buf.append (" ");
186 buf.append (uid.toString ());
187 buf.append ("]");
189 return buf.toString();