User: user57  
  Date: 02/02/14 22:14:23

  Added:       src/main/org/jboss/util/id GUID.java ID.java UID.java
                        VMID.java package.html
  Log:
   o importing the useful bits from the bliss cl.
   o plus some bits from server/*/util/* have been moved to util.jmx
  
  Revision  Changes    Path
  1.1                  jboss-common/src/main/org/jboss/util/id/GUID.java
  
  Index: GUID.java
  ===================================================================
  /***************************************
   *                                     *
   *  JBoss: The OpenSource J2EE WebOS   *
   *                                     *
   *  Distributable under LGPL license.  *
   *  See terms of license at gnu.org.   *
   *                                     *
   ***************************************/
  
  package org.jboss.util.id;
  
  /**
   * A globally unique identifier (globally across a cluster of virtual 
   * machines).
   *
   * <p>The identifier is composed of:
   * <ol>
   *    <li>The VMID for the virtual machine.</li>
   *    <li>A UID to provide uniqueness over a VMID.</li>
   * </ol>
   *
   * <pre>
   *    [ address ] - [ process id ] - [ time ] - [ counter ] - [ time ] - [ counter ]
   *                                   |------- UID --------|   |------- UID --------|
   *    |---------------------- VMID -----------------------|
   * </pre>
   *
   * @see VMID
   * @see UID
   *
   * @version <tt>$Revision: 1.1 $</tt>
   * @author  <a href="mailto:[EMAIL PROTECTED]";>Jason Dillon</a>
   */
  public class GUID
     implements ID
  {
     /** The virtual machine identifier */
     protected final VMID vmid;
  
     /** The unique identifier */
     protected final UID uid;
  
     /** The hash code of this GUID */
     protected final int hashCode;
  
     /**
      * Construct a new GUID.
      */
     public GUID() {
        this.vmid = VMID.getInstance();
        this.uid = new UID();
  
        // generate a hash code for this GUID
        int code = vmid.hashCode();
        code ^= uid.hashCode();
        hashCode = code;
     }
  
     /**
      * Copy a GUID.
      *
      * @param guid    GUID to copy.
      */
     protected GUID(final GUID guid) {
        this.vmid = guid.vmid;
        this.uid = guid.uid;
        this.hashCode = guid.hashCode;
     }
  
     /**
      * Get the VMID portion of this GUID.
      *
      * @return  The VMID portion of this GUID.
      */
     public final VMID getVMID() {
        return vmid;
     }
  
     /**
      * Get the UID portion of this GUID.
      *
      * @return  The UID portion of this GUID.
      */
     public final UID getUID() {
        return uid;
     }
     
     /**
      * Return a string representation of this GUID.
      *
      * @return  A string representation of this GUID.
      */
     public String toString() {
        return vmid.toString() + "-" + uid.toString();
     }
  
     /**
      * Return the hash code of this GUID.
      *
      * @return  The hash code of this GUID.
      */
     public int hashCode() {
        return hashCode;
     }
  
     /**
      * Check if the given object is equal to this GUID.
      *
      * <p>A GUID is equal to another GUID if the VMID and UID portions are
      *    equal.
      *
      * @param obj  Object to test equality with.
      * @return     True if object is equal to this GUID.
      */
     public boolean equals(final Object obj) {
        if (obj == this) return true;
  
        if (obj != null && obj.getClass() == getClass()) {
           GUID guid = (GUID)obj;
  
           return 
              guid.vmid.equals(vmid) &&
              guid.uid.equals(uid);
        }
  
        return false;
     }
  
     /**
      * Returns a copy of this GUID.
      *
      * @return  A copy of this GUID.
      */
     public Object clone() {
        try {
           return super.clone();
        }
        catch (CloneNotSupportedException e) {
           throw new InternalError();
        }
     }
  
     /**
      * Returns a GUID as a string.
      *
      * @return  GUID as a string.
      */
     public static String asString() {
        return new GUID().toString();
     }
  }
  
  
  
  1.1                  jboss-common/src/main/org/jboss/util/id/ID.java
  
  Index: ID.java
  ===================================================================
  /***************************************
   *                                     *
   *  JBoss: The OpenSource J2EE WebOS   *
   *                                     *
   *  Distributable under LGPL license.  *
   *  See terms of license at gnu.org.   *
   *                                     *
   ***************************************/
  
  package org.jboss.util.id;
  
  import java.io.Serializable;
  
  /**
   * A tagging interface for an identifier.
   *
   * @version <tt>$Revision: 1.1 $</tt>
   * @author  <a href="mailto:[EMAIL PROTECTED]";>Jason Dillon</a>
   */
  public interface ID
     extends Serializable, Cloneable
  {
     // empty
  }
  
  
  
  1.1                  jboss-common/src/main/org/jboss/util/id/UID.java
  
  Index: UID.java
  ===================================================================
  /***************************************
   *                                     *
   *  JBoss: The OpenSource J2EE WebOS   *
   *                                     *
   *  Distributable under LGPL license.  *
   *  See terms of license at gnu.org.   *
   *                                     *
   ***************************************/
  
  package org.jboss.util.id;
  
  import org.jboss.util.MuLong;
  
  /**
   * A unique identifier (uniqueness only guarantied inside of the virtual
   * machine in which it was created).
   *
   * <p>The identifier is composed of:
   * <ol>
   *    <li>A long generated from the current system time (in milliseconds).</li>
   *    <li>A long generated from a counter (which is the number of UID objects
   *        that have been created durring the life of the executing virtual
   *        machine).</li>
   * </ol>
   *
   * <pre>
   *    [ time ] - [ counter ]
   * </pre>
   *
   * <p>Numbers are converted to radix(Character.MAX_RADIX) when converting
   *    to strings.
   *
   * <p>This <i>should</i> provide adequate uniqueness for most purposes.
   *
   * @version <tt>$Revision: 1.1 $</tt>
   * @author  <a href="mailto:[EMAIL PROTECTED]";>Jason Dillon</a>
   */
  public class UID
     implements ID
  {
     /** A counter for generating identity values */
     protected static final MuLong COUNTER = new MuLong(0);
  
     /** The time portion of the UID */
     protected final long time;
  
     /** The identity portion of the UID */
     protected final long id;
  
     /**
      * Construct a new UID.
      */
     public UID() {
        time = System.currentTimeMillis();
        synchronized (COUNTER) {
           id = COUNTER.increment();
        }
     }
  
     /**
      * Copy a UID.
      */
     protected UID(final UID uid) {
        time = uid.time;
        id = uid.id;
     }
  
     /**
      * Get the time portion of this UID.
      *
      * @return  The time portion of this UID.
      */
     public final long getTime() {
        return time;
     }
  
     /**
      * Get the identity portion of this UID.
      *
      * @return  The identity portion of this UID.
      */
     public final long getID() {
        return id;
     }
  
     /**
      * Return a string representation of this UID.
      *
      * @return  A string representation of this UID.
      */
     public String toString() {
        return 
           Long.toString(time, Character.MAX_RADIX) +
           "-" + 
           Long.toString(id, Character.MAX_RADIX);
     }
  
     /**
      * Return the hash code of this UID.
      *
      * @return  The hash code of this UID.
      */
     public int hashCode() {
        return (int)((time ^ id) >> 32);
     }
  
     /**
      * Checks if the given object is equal to this UID.
      *
      * @param obj     Object to test equality with.
      * @return        True if object is equal to this UID.
      */
     public boolean equals(final Object obj) {
        if (obj == this) return true;
  
        if (obj != null && obj.getClass() == getClass()) {
           UID uid = (UID)obj;
  
           return 
              uid.time == time &&
              uid.id == id;
        }
  
        return false;
     }
  
     /**
      * Returns a copy of this UID.
      *
      * @return  A copy of this UID.
      */
     public Object clone() {
        try {
           return super.clone();
        }
        catch (CloneNotSupportedException e) {
           throw new InternalError();
        }
     }
  
     /**
      * Returns a UID as a string.
      *
      * @return  UID as a string.
      */
     public static String asString() {
        return new UID().toString();
     }
  }
  
  
  
  1.1                  jboss-common/src/main/org/jboss/util/id/VMID.java
  
  Index: VMID.java
  ===================================================================
  /***************************************
   *                                     *
   *  JBoss: The OpenSource J2EE WebOS   *
   *                                     *
   *  Distributable under LGPL license.  *
   *  See terms of license at gnu.org.   *
   *                                     *
   ***************************************/
  
  package org.jboss.util.id;
  
  import java.net.InetAddress;
  
  import java.security.AccessController;
  import java.security.PrivilegedAction;
  
  import org.jboss.util.Primitives;
  import org.jboss.util.HashCode;
  import org.jboss.util.platform.PID;
  
  /**
   * An object that uniquely identifies a virtual machine.
   *
   * <p>The identifier is composed of:
   * <ol>
   *    <li>The Internet address of the physical machine.</li>
   *    <li>The process identifier of the virtual machine.</li>
   *    <li>A UID to guarantee uniqness across multipule virtual
   *        machines on the same physical machine.</li>
   * </ol>
   *
   * <pre>
   *    [ address ] - [ process id ] - [ time ] - [ counter ]
   *                                   |------- UID --------|
   * </pre>
   *
   * <p>Numbers are converted to radix(Character.MAX_RADIX) when converting
   *    to strings.
   *
   * @see UID
   *
   * @version <tt>$Revision: 1.1 $</tt>
   * @author  <a href="mailto:[EMAIL PROTECTED]";>Jason Dillon</a>
   */
  public class VMID
     implements ID
  {
     /** The address of the current virtual machine */
     protected final byte[] address;
  
     /** The process identifier of the current virtual machine */
     protected final PID pid;
  
     /** A unique identifier to ensure uniqueness across the host machine */
     protected final UID uid;
  
     /** The hash code of this VMID */
     protected final int hashCode;
  
     /**
      * Construct a new VMID.
      *
      * @param address    The address of the current virtual machine.
      * @param pid        Process identifier.
      * @param uid        Unique identifier.
      *
      * @see #getInstance()  For getting a VMID instance reference.
      */
     protected VMID(final byte[] address, final PID pid, final UID uid) {
        this.address = address;
        this.pid = pid;
        this.uid = uid;
  
        // generate a hashCode for this VMID
        int code = pid.hashCode();
        code ^= uid.hashCode();
        code ^= HashCode.generate(address);
        hashCode = code;
     }
  
     /**
      * Copy a VMID.
      *
      * @param vmid    VMID to copy.
      */
     protected VMID(final VMID vmid) {
        this.address = vmid.address;
        this.pid = vmid.pid;
        this.uid = vmid.uid;
        this.hashCode = vmid.hashCode;
     }
  
     /**
      * Get the address portion of this VMID.
      *
      * @return  The address portion of this VMID.
      */
     public final byte[] getAddress() {
        return address;
     }
  
     /**
      * Get the process identifier portion of this VMID.
      *
      * @return  The process identifier portion of this VMID.
      */
     public final PID getProcessID() {
        return pid;
     }
  
     /**
      * Get the UID portion of this VMID.
      *
      * @return  The UID portion of this VMID.
      */
     public final UID getUID() {
        return uid;
     }
  
     /**
      * Return a string representation of this VMID.
      *
      * @return  A string representation of this VMID.
      */
     public String toString() {
        StringBuffer buff = new StringBuffer();
        
        for (int i=0; i<address.length; i++) {
           int n = (int) (address[i] & 0xFF);
           buff.append(Integer.toString(n, Character.MAX_RADIX));
        }
  
        buff.append("-").append(pid.toString(Character.MAX_RADIX));
        buff.append("-").append(uid);
  
        return buff.toString();
     }
  
     /**
      * Return the hash code of this VMID.
      *
      * @return  The hash code of this VMID.
      */
     public final int hashCode() {
        return hashCode;
     }
  
     /**
      * Check if the given object is equal to this VMID.
      *
      * <p>A VMID is equals to another VMID if the address,
      *    process identifer and UID portions are equal.
      *
      * @param obj     Object to test equality with.
      * @return        True if object is equals to this VMID.
      */
     public boolean equals(final Object obj) {
        if (obj == this) return true;
  
        if (obj != null && obj.getClass() == getClass()) {
           VMID vmid = (VMID)obj;
           return 
              Primitives.equals(vmid.address, address) &&
              vmid.pid.equals(pid) &&
              vmid.uid.equals(uid);
        }
  
        return false;
     }
  
     /**
      * Returns a copy of this VMID.
      *
      * @return  A copy of this VMID.
      */
     public Object clone() {
        try {
           return super.clone();
        }
        catch (CloneNotSupportedException e) {
           throw new InternalError();
        }
     }
  
     /**
      * Returns a VMID as a string.
      *
      * @return  VMID as a string.
      */
     public static String asString() {
        return getInstance().toString();
     }
  
  
     /////////////////////////////////////////////////////////////////////////
     //                            Instance Access                          //
     /////////////////////////////////////////////////////////////////////////
  
     /** The single instance of VMID for the running Virtual Machine */
     private static VMID instance = null;
  
     /**
      * Get the VMID for the current virtual machine.
      *
      * @return  Virtual machine identifier.
      *
      * @throws NestedError  Failed to create VMID instance.
      */
     public synchronized static VMID getInstance() {
        if (instance == null) {
           instance = create();
        }
        return instance;
     }
  
     /** 
      * The address used when conventional methods fail to return the address
      * of the current machine.
      */
     public static final byte[] UNKNOWN_HOST = { 0, 0, 0, 0 };
  
     /**
      * Return the current host internet address.
      */
     private static byte[] getHostAddress() {
        return (byte[]) AccessController.doPrivileged(new PrivilegedAction() {
              public Object run() {
                 try {
                    return InetAddress.getLocalHost().getAddress();
                 }
                 catch (Exception e) {
                    return UNKNOWN_HOST;
                 }
              }
           });
     }
  
     /**
      * Create the VMID for the current virtual mahcine.
      *
      * @return  Virtual machine identifer.
      */
     private static VMID create() {
        // get the local internet address for the current host
        byte[] address = getHostAddress();
           
        return new VMID(address, PID.getInstance(), new UID());
     }
  }
  
  
  
  1.1                  jboss-common/src/main/org/jboss/util/id/package.html
  
  Index: package.html
  ===================================================================
  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  <html>
    <head>
      <!-- $Id: package.html,v 1.1 2002/02/15 06:14:23 user57 Exp $ -->
      <!--
  
      JBoss: The OpenSource J2EE WebOS 
  
      Distributable under LGPL license.
      See terms of license at gnu.org.
  
      -->
    </head>
  
    <body bgcolor="white">
      <p>Unique and globally unique identifier classes.</p>
  
      <h2>Package Specification</h2>
      <ul>
        <li><a href="javascript: alert('not available')">Not Available</a>
      </ul>
        
      <h2>Related Documentation</h2>
      <ul>
        <li><a href="javascript: alert('not available')">Not Available</a>
      </ul>
  
      <h2>Package Status</h2>
      <ul>
        <li><font color="green"><b>STABLE</b></font>
      </ul>
  
      <h2>Todo</h2>
      <ul>
        <li>???
      </ul>
  
      <!-- Put @see and @since tags down here. -->
  
    </body>
  </html>
  
  
  

_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-development

Reply via email to