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/ebxmlrr/src/share/com/sun/ebxml/registry/util/UUIDFactory.java,v 1.5 2002/11/09 04:59:23 jasilva Exp $ 15 * 16 */ 17 package com.sun.ebxml.registry.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 private static void printUsage() { 99 System.err.println("...UUIDFactory [help] cnt=<number of uuids required>"); 100 System.exit(-1); 101 } 102 103 public static void main(String[] args) { 104 int cnt = 1; 105 106 for (int i=0; i<args.length; i++) { 107 if (args[i].equalsIgnoreCase("help")) { 108 printUsage(); 109 } 110 else if (args[i].startsWith("cnt=")) { 111 cnt = Integer.parseInt(args[i].substring(4, args[i].length())); 112 } 113 else { 114 System.err.println("Unknown parameter: '" + args[i] + "' at position " + i); 115 if (i > 0) { 116 System.err.println("Last valid parameter was '" + args[i-1] + "'"); 117 } 118 printUsage(); 119 } 120 } 121 122 UUIDFactory uf = UUIDFactory.getInstance(); 123 124 for (int i=0; i<cnt; i++) { 125 UUID id = uf.newUUID(); 126 System.out.println( "new UUID : " + id.toString() ); 127 } 128 } 129 130 public static UUIDFactory getInstance(){ 131 if (instance == null) { 132 synchronized(UUIDFactory.class) { 133 if (instance == null) { 134 instance = new UUIDFactory(); 135 } 136 } 137 } 138 return instance; 139 } 140 }

This page was automatically generated by Maven