1 16 package org.apache.axis.utils; 17 18 import org.apache.axis.components.logger.LogFactory; 19 import org.apache.commons.logging.Log; 20 21 import java.util.Random ; 22 23 26 public class SessionUtils { 27 28 29 protected static Log log = LogFactory.getLog(SessionUtils.class.getName()); 30 31 35 protected static final int SESSION_ID_BYTES = 16; 36 37 40 protected static Random random = null; 41 42 46 protected static String randomClass = "java.security.SecureRandom"; 47 48 51 private static String thisHost = null; 52 53 58 public static synchronized String generateSessionId() { 59 byte bytes[] = new byte[SESSION_ID_BYTES]; 61 62 getRandom().nextBytes(bytes); 63 64 StringBuffer result = new StringBuffer (); 66 67 for (int i = 0; i < bytes.length; i++) { 68 byte b1 = (byte) ((bytes[i] & 0xf0) >> 4); 69 byte b2 = (byte) (bytes[i] & 0x0f); 70 71 if (b1 < 10) { 72 result.append((char) ('0' + b1)); 73 } else { 74 result.append((char) ('A' + (b1 - 10))); 75 } 76 if (b2 < 10) { 77 result.append((char) ('0' + b2)); 78 } else { 79 result.append((char) ('A' + (b2 - 10))); 80 } 81 } 82 return (result.toString()); 83 } 84 85 90 public static synchronized Long generateSession() { 91 return new Long (getRandom().nextLong()); 92 } 93 94 101 private static synchronized Random getRandom() { 102 if (random == null) { 103 try { 104 Class clazz = Class.forName(randomClass); 105 random = (Random ) clazz.newInstance(); 106 } catch (Exception e) { 107 random = new java.util.Random (); 108 } 109 } 110 return (random); 111 } 112 113 } 114 | Popular Tags |