1 16 package org.apache.axis2.util; 17 18 import java.util.Random ; 19 20 23 public class SessionUtils { 24 25 29 protected static final int SESSION_ID_BYTES = 16; 30 31 34 protected static Random random = null; 35 36 40 protected static String randomClass = "java.security.SecureRandom"; 41 42 45 private static String thisHost = null; 46 47 52 public static synchronized String generateSessionId() { 53 byte bytes[] = new byte[SESSION_ID_BYTES]; 55 56 getRandom().nextBytes(bytes); 57 58 StringBuffer result = new StringBuffer (); 60 61 for (int i = 0; i < bytes.length; i++) { 62 byte b1 = (byte) ((bytes[i] & 0xf0) >> 4); 63 byte b2 = (byte) (bytes[i] & 0x0f); 64 65 if (b1 < 10) { 66 result.append((char) ('0' + b1)); 67 } else { 68 result.append((char) ('A' + (b1 - 10))); 69 } 70 if (b2 < 10) { 71 result.append((char) ('0' + b2)); 72 } else { 73 result.append((char) ('A' + (b2 - 10))); 74 } 75 } 76 return (result.toString()); 77 } 78 79 84 public static synchronized Long generateSession() { 85 return new Long (getRandom().nextLong()); 86 } 87 88 95 private static synchronized Random getRandom() { 96 if (random == null) { 97 try { 98 Class clazz = Class.forName(randomClass); 99 random = (Random ) clazz.newInstance(); 100 } catch (Exception e) { 101 random = new java.util.Random (); 102 } 103 } 104 return (random); 105 } 106 107 } 108 109 | Popular Tags |