/*
* JBoss, the OpenSource EJB server
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.ejb.plugins.keygenerator.uuid;

import org.jboss.ejb.plugins.keygenerator.KeyGenerator;

import java.net.InetAddress;
import java.security.SecureRandom;


/**
 * The implementation of UUID key generator
 * based on the algorithm from Floyd Marinescu's EJB Design Patterns.
 *
 * @author <a href="mailto:loubyansky@ukr.net">Alex Loubyansky</a>
 *
 * @version $Revision: 1.1.2.2 $
 */
public class UUIDKeyGenerator
   implements KeyGenerator
{

   // Attributes ---------------------------------------------------

   /** secure random to provide nonrepeating seed */
   SecureRandom seeder;

   /** cached middle value */
   private String midValue;

   // Constructor --------------------------------------------------

   public UUIDKeyGenerator()
      throws Exception
   {
      // cache the middle part for UUID

      StringBuffer buffer = new StringBuffer( 16 );

      // construct host part of the uuid (8 hex digits)
      byte[] addr = InetAddress.getLocalHost().getAddress();
      buffer.append( toHex( toInt(addr), 8 ) );

      // append the hash code for this object (8 hex digits)
      buffer.append( toHex( System.identityHashCode(this), 8 ) );

      // set up midValue
      midValue = buffer.toString();

      // load up the randomizer
      seeder = new SecureRandom();
      int node = seeder.nextInt();
   }

   // KeyGenerator implementation ----------------------------------

   public Object generateKey()
   {
      StringBuffer buffer = new StringBuffer( 32 );

      // append current time as unsigned int value
      buffer.append(toHex((int)(System.currentTimeMillis() & 0xFFFFFFFF), 8));

      // append cached midValue
      buffer.append( midValue );

      // append the next random int
      buffer.append( toHex( seeder.nextInt(), 8 ) );

      // return the result
      return buffer.toString();
   }

   // Private ------------------------------------------------------

   /**
    * Converts int value to string hex representation
    */
   private String toHex( int value, int length )
   {
      // hex digits
      char[] hexDigits =
         { '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' };

      StringBuffer buffer = new StringBuffer( length );
      int shift = (length - 1) << 2;
      int i = -1;
      while( ++i < length )
      {
         buffer.append( hexDigits[(value >> shift) & 0x0000000F] );
         value <<= 4;
      }
      return buffer.toString();
   }

   /**
    * Constructs int value from byte array
    */
   private static int toInt( byte[] bytes )
   {
      int value = 0;
      int i = -1;
      while( ++i < bytes.length )
      {
         value <<= 8;
         int b = bytes[ i ] & 0xff;
         value |= b;
      }
      return value;
   }
}
