Source for java.rmi.dgc.VMID

   1: /* VMID.java -- The object ID, unique between all virtual machines.
   2:    Copyright (c) 1996, 1997, 1998, 1999, 2006 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   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.
  10:  
  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.
  15: 
  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.
  20: 
  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.
  25: 
  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. */
  37: 
  38: package java.rmi.dgc;
  39: 
  40: import java.io.Serializable;
  41: import java.net.InetAddress;
  42: import java.net.UnknownHostException;
  43: import java.rmi.server.UID;
  44: import java.util.Arrays;
  45: 
  46: /**
  47:  * An identifier that is unique accross the all virtual machines. This class is
  48:  * used by distributed garbage collector to identify the virtual machine of
  49:  * the client, but may also be used in various other cases, when such identifier
  50:  * is required. This class separately stores and transfers the host IP 
  51:  * address, but will try to do its best also for the case if it failed to
  52:  * determine it. The alternative algorithms are used in {@link UID} that is 
  53:  * part of this class. The VMID's, created on the same host, but in the two
  54:  * separately (parallely) running virtual machines are different.
  55:  */
  56: public final class VMID    implements Serializable
  57: {
  58:   /**
  59:    * Use SVUID for interoperability.
  60:    */
  61:   static final long serialVersionUID = -538642295484486218L;
  62:   
  63:   /**
  64:    * If true, the IP of this host can ve reliably determined.
  65:    */
  66:   static boolean areWeUnique;
  67:   
  68:   /**
  69:    * The IP address of the local host.
  70:    */
  71:   static byte[] localAddr;
  72: 
  73:   /**
  74:    * The IP address of the local host.
  75:    */
  76:   private byte[] addr;
  77:   
  78:   /**
  79:    * The cached hash code.
  80:    */
  81:   transient int hash;
  82:   
  83:   /**
  84:    * The UID of this VMID.
  85:    */
  86:   private UID uid;
  87: 
  88:   static
  89:     {
  90:       // This "local host" value usually indicates that the local 
  91:       // IP address cannot be reliably determined.
  92:       byte[] localHost = new byte[] { 127, 0, 0, 1 };
  93: 
  94:       try
  95:         {
  96:           localAddr = InetAddress.getLocalHost().getAddress();
  97:           areWeUnique = !Arrays.equals(localHost, localAddr);
  98:         }
  99:       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: }