sessionId产生方法参考

tomcat

java 代码
  1. protected synchronized String generateSessionId() {   
  2.   
  3.         byte random[] = new byte[16];   
  4.   
  5.         // Render the result as a String of hexadecimal digits   
  6.         StringBuffer result = new StringBuffer();   
  7.         int resultLenBytes = 0;   
  8.         while (resultLenBytes < this.sessionIdLength) {   
  9.             getRandomBytes(random);   
  10.             random = getDigest().digest(random);   
  11.             for (int j = 0;   
  12.                     j < random.length && resultLenBytes < this.sessionIdLength;   
  13.                     j++) {   
  14.                 byte b1 = (byte) ((random[j] & 0xf0) >> 4);   
  15.                 byte b2 = (byte) (random[j] & 0x0f);   
  16.                 if (b1 < 10)   
  17.                     result.append((char) ('0' + b1));   
  18.                 else  
  19.                     result.append((char) ('A' + (b1 - 10)));   
  20.                 if (b2 < 10)   
  21.                     result.append((char) ('0' + b2));   
  22.                 else  
  23.                     result.append((char) ('A' + (b2 - 10)));   
  24.                 resultLenBytes++;   
  25.             }   
  26.         }   
  27.         return (result.toString());   
  28.   
  29.     }  

 

hibernate uuid:

java 代码
  1. public abstract class AbstractUUIDGenerator implements IdentifierGenerator {   
  2.   
  3.     private static final int IP;   
  4.     static {   
  5.         int ipadd;   
  6.         try {   
  7.             ipadd = BytesHelper.toInt( InetAddress.getLocalHost().getAddress() );   
  8.         }   
  9.         catch (Exception e) {   
  10.             ipadd = 0;   
  11.         }   
  12.         IP = ipadd;   
  13.     }   
  14.     private static short counter = (short0;   
  15.     private static final int JVM = (int) ( System.currentTimeMillis() >>> 8 );   
  16.   
  17.     public AbstractUUIDGenerator() {   
  18.     }   
  19.   
  20.     /**  
  21.      * Unique across JVMs on this machine (unless they load this class  
  22.      * in the same quater second - very unlikely)  
  23.      */  
  24.     protected int getJVM() {   
  25.         return JVM;   
  26.     }   
  27.   
  28.     /**  
  29.      * Unique in a millisecond for this JVM instance (unless there  
  30.      * are > Short.MAX_VALUE instances created in a millisecond)  
  31.      */  
  32.     protected short getCount() {   
  33.         synchronized(AbstractUUIDGenerator.class) {   
  34.             if (counter<0) counter=0;   
  35.             return counter++;   
  36.         }   
  37.     }   
  38.   
  39.     /**  
  40.      * Unique in a local network  
  41.      */  
  42.     protected int getIP() {   
  43.         return IP;   
  44.     }   
  45.   
  46.     /**  
  47.      * Unique down to millisecond  
  48.      */  
  49.     protected short getHiTime() {   
  50.         return (short) ( System.currentTimeMillis() >>> 32 );   
  51.     }   
  52.     protected int getLoTime() {   
  53.         return (int) System.currentTimeMillis();   
  54.     }   
  55.   
  56.   
  57. }   
  58.   
  59. public class UUIDHexGenerator extends AbstractUUIDGenerator implements Configurable {   
  60.   
  61.     private String sep = "";   
  62.   
  63.     protected String format(int intval) {   
  64.         String formatted = Integer.toHexString(intval);   
  65.         StringBuffer buf = new StringBuffer("00000000");   
  66.         buf.replace( 8-formatted.length(), 8, formatted );   
  67.         return buf.toString();   
  68.     }   
  69.   
  70.     protected String format(short shortval) {   
  71.         String formatted = Integer.toHexString(shortval);   
  72.         StringBuffer buf = new StringBuffer("0000");   
  73.         buf.replace( 4-formatted.length(), 4, formatted );   
  74.         return buf.toString();   
  75.     }   
  76.   
  77.     public Serializable generate(SessionImplementor session, Object obj) {   
  78.         return new StringBuffer(36)   
  79.             .append( format( getIP() ) ).append(sep)   
  80.             .append( format( getJVM() ) ).append(sep)   
  81.             .append( format( getHiTime() ) ).append(sep)   
  82.             .append( format( getLoTime() ) ).append(sep)   
  83.             .append( format( getCount() ) )   
  84.             .toString();   
  85.     }   
  86.   
  87.     public void configure(Type type, Properties params, Dialect d) {   
  88.         sep = PropertiesHelper.getString("separator", params, "");   
  89.     }   
  90.   
  91.     public static void main( String[] args ) throws Exception {   
  92.         Properties props = new Properties();   
  93.         props.setProperty("separator""/");   
  94.         IdentifierGenerator gen = new UUIDHexGenerator();   
  95.         ( (Configurable) gen ).configure(Hibernate.STRING, props, null);   
  96.         IdentifierGenerator gen2 = new UUIDHexGenerator();   
  97.         ( (Configurable) gen2 ).configure(Hibernate.STRING, props, null);   
  98.   
  99.         for ( int i=0; i<10; i++) {   
  100.             String id = (String) gen.generate(nullnull);   
  101.             System.out.println(id);   
  102.             String id2 = (String) gen2.generate(nullnull);   
  103.             System.out.println(id2);   
  104.         }   
  105.   
  106.     }   
  107.   
  108. }   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值