View Javadoc
1 /* 2 * 3 * Copyright 2001 Sun Microsystems(TM), Inc. All Rights Reserved. 4 * 5 * The contents of this file are made available under and subject to the 6 * Research Use Rights of the Sun(TM) Community Source License v 3.0 (the 7 * "License"). Software distributed under the License is distributed on an "AS 8 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the 9 * License for the specific language governing rights and limitations under 10 * the License. 11 * 12 * Contributor(s): Sun Microsystems, Inc. 13 * 14 * $Header: /cvsroot/ebxmlrr/jaxr/src/com/sun/xml/registry/ebxml/util/UUIDFactory.java,v 1.2 2002/11/09 01:38:16 jasilva Exp $ 15 * 16 */ 17 package com.sun.xml.registry.ebxml.util; 18 19 import java.security.*; 20 21 /*** 22 * A <code>UUIDFactory</code> generates a UUID 23 */ 24 25 public class UUIDFactory { 26 /*** 27 * random number generator for UUID generation 28 */ 29 private final SecureRandom secRand = new SecureRandom(); 30 31 /*** 32 * 128-bit buffer for use with secRand 33 */ 34 private final byte[] secRandBuf16 = new byte[16]; 35 36 /*** 37 * 64-bit buffer for use with secRand 38 */ 39 private final byte[] secRandBuf8 = new byte[8]; 40 41 /*** 42 * @link 43 * @shapeType PatternLink 44 * @pattern Singleton 45 * @supplierRole Singleton factory 46 */ 47 /*# private UUIDFactory _uuidFactory; */ 48 private static UUIDFactory instance = null; 49 50 protected UUIDFactory() { 51 } 52 53 public UUID newUUID() { 54 secRand.nextBytes(secRandBuf16); 55 secRandBuf16[6] &= 0x0f; 56 secRandBuf16[6] |= 0x40; /* version 4 */ 57 secRandBuf16[8] &= 0x3f; 58 secRandBuf16[8] |= 0x80; /* IETF variant */ 59 secRandBuf16[10] |= 0x80; /* multicast bit */ 60 long mostSig = 0; 61 for (int i = 0; i < 8; i++) { 62 mostSig = (mostSig << 8) | (secRandBuf16[i] & 0xff); 63 } 64 long leastSig = 0; 65 for (int i = 8; i < 16; i++) { 66 leastSig = (leastSig << 8) | (secRandBuf16[i] & 0xff); 67 } 68 return new UUID(mostSig, leastSig); 69 } 70 71 public boolean isValidUUID(String uuid) { 72 boolean isValid = true; 73 74 //57d925e0-7ad2-4dc3-ace1-b8a4064abcc7 75 //012345678901234567890123456789012345 76 int len = uuid.length(); 77 String hexDigits = "0123456789abcdef"; 78 79 for (int i=0; i<len; i++) { 80 char c = uuid.charAt(i); 81 if ((i==8) || (i==13) || (i==18) || (i==23)) { 82 if (c != '-') { 83 isValid = false; 84 break; 85 } 86 } 87 else { 88 if (hexDigits.indexOf(c) == -1) { 89 isValid = false; 90 break; 91 } 92 } 93 } 94 95 return isValid; 96 } 97 98 public static void main(String[] args) { 99 UUIDFactory uf = UUIDFactory.getInstance(); 100 101 UUID id = uf.newUUID(); 102 System.out.println( "new UUID : " + id.toString() ); 103 } 104 105 public static UUIDFactory getInstance(){ 106 if (instance == null) { 107 synchronized(UUIDFactory.class) { 108 if (instance == null) { 109 instance = new UUIDFactory(); 110 } 111 } 112 } 113 return instance; 114 } 115 }

This page was automatically generated by Maven