sessionId产生方法参考

Posted on

sessionId产生方法参考 - balaschen - JavaEye技术网站

首页 新闻 论坛 问答 博客 招聘 更多 ▼

专栏 文摘 圈子 搜索

您还未登录 ! 我的应用 登录 注册

balaschen

永久域名 http://balaschen.javaeye.com/

rad rail plugin | Spring Hibernate SessionFactory配置

2007-05-28

sessionId产生方法参考

tomcat java 代码

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

hibernate uuid: java 代码

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

发表评论

您还没有登录,请登录后发表评论(快捷键 Alt+S / Ctrl+Enter)

balaschen的博客

balaschen

搜索本博客

最近访客 >>更多访客

wangzhongjie的博客

wangzhongjie

yqwan的博客

yqwan icecityman的博客

icecityman

chilongxph的博客

chilongxph

博客分类

最近加入圈子

最新评论

声明:JavaEye文章版权属于作者,受法律保护。没有作者书面许可不得转载。若作者同意转载,必须以超链接形式标明文章原始出处和作者。 © 2003-2009 JavaEye.com. All rights reserved. 上海炯耐计算机软件有限公司 [ 沪ICP备05023328号 ]

希望本站内容对您有点用处,有什么疑问或建议请在后面留言评论
转载请注明作者(RobinChia)和出处 It so life ,请勿用于任何商业用途