tomcat
java 代码
- protected synchronized String generateSessionId() {
- byte random[] = new byte[16];
- // Render the result as a String of hexadecimal digits
- StringBuffer result = new StringBuffer();
- int resultLenBytes = 0;
- while (resultLenBytes < this.sessionIdLength) {
- getRandomBytes(random);
- random = getDigest().digest(random);
- for (int j = 0;
- j < random.length && resultLenBytes < this.sessionIdLength;
- j++) {
- byte b1 = (byte) ((random[j] & 0xf0) >> 4);
- byte b2 = (byte) (random[j] & 0x0f);
- if (b1 < 10)
- result.append((char) ('0' + b1));
- else
- result.append((char) ('A' + (b1 - 10)));
- if (b2 < 10)
- result.append((char) ('0' + b2));
- else
- result.append((char) ('A' + (b2 - 10)));
- resultLenBytes++;
- }
- }
- return (result.toString());
- }
hibernate uuid:
java 代码
- public abstract class AbstractUUIDGenerator implements IdentifierGenerator {
- private static final int IP;
- static {
- int ipadd;
- try {
- ipadd = BytesHelper.toInt( InetAddress.getLocalHost().getAddress() );
- }
- catch (Exception e) {
- ipadd = 0;
- }
- IP = ipadd;
- }
- private static short counter = (short) 0;
- private static final int JVM = (int) ( System.currentTimeMillis() >>> 8 );
- public AbstractUUIDGenerator() {
- }
- /**
- * Unique across JVMs on this machine (unless they load this class
- * in the same quater second - very unlikely)
- */
- protected int getJVM() {
- return JVM;
- }
- /**
- * Unique in a millisecond for this JVM instance (unless there
- * are > Short.MAX_VALUE instances created in a millisecond)
- */
- protected short getCount() {
- synchronized(AbstractUUIDGenerator.class) {
- if (counter<0) counter=0;
- return counter++;
- }
- }
- /**
- * Unique in a local network
- */
- protected int getIP() {
- return IP;
- }
- /**
- * Unique down to millisecond
- */
- protected short getHiTime() {
- return (short) ( System.currentTimeMillis() >>> 32 );
- }
- protected int getLoTime() {
- return (int) System.currentTimeMillis();
- }
- }
- public class UUIDHexGenerator extends AbstractUUIDGenerator implements Configurable {
- private String sep = "";
- protected String format(int intval) {
- String formatted = Integer.toHexString(intval);
- StringBuffer buf = new StringBuffer("00000000");
- buf.replace( 8-formatted.length(), 8, formatted );
- return buf.toString();
- }
- protected String format(short shortval) {
- String formatted = Integer.toHexString(shortval);
- StringBuffer buf = new StringBuffer("0000");
- buf.replace( 4-formatted.length(), 4, formatted );
- return buf.toString();
- }
- public Serializable generate(SessionImplementor session, Object obj) {
- return new StringBuffer(36)
- .append( format( getIP() ) ).append(sep)
- .append( format( getJVM() ) ).append(sep)
- .append( format( getHiTime() ) ).append(sep)
- .append( format( getLoTime() ) ).append(sep)
- .append( format( getCount() ) )
- .toString();
- }
- public void configure(Type type, Properties params, Dialect d) {
- sep = PropertiesHelper.getString("separator", params, "");
- }
- public static void main( String[] args ) throws Exception {
- Properties props = new Properties();
- props.setProperty("separator", "/");
- IdentifierGenerator gen = new UUIDHexGenerator();
- ( (Configurable) gen ).configure(Hibernate.STRING, props, null);
- IdentifierGenerator gen2 = new UUIDHexGenerator();
- ( (Configurable) gen2 ).configure(Hibernate.STRING, props, null);
- for ( int i=0; i<10; i++) {
- String id = (String) gen.generate(null, null);
- System.out.println(id);
- String id2 = (String) gen2.generate(null, null);
- System.out.println(id2);
- }
- }
- }