001/* 002 * Copyright 2005-2008 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.kuali.rice.kns.util; 017 018import java.security.MessageDigest; 019import java.security.NoSuchAlgorithmException; 020 021import org.apache.ojb.broker.util.GUID; 022 023/** 024 * 025 * This class wraps an OJB Guid so that it conforms to the format (and using the algorithm) described in 026 * RFC 4122 entitled " A Universally Unique IDentifier (UUID) URN Namespace" 027 */ 028public class Guid { 029 030 private final static char HYPHEN='-'; 031 private final static String DIGITS="0123456789ABCDEF"; 032 private String stringValue=null; 033 private GUID guid; 034 035 public Guid() { 036 037 guid = new GUID(); // This OJB class is deprecated; remove this line when we upgrade OJB 038 039 040 String guidString=guid.toString(); 041 // this is roughly the prefered way with the new OJB GUIDFactory: 042 // String guidString=org.apache.ojb.broker.util.GUIDFactory.next(); 043 044 045 MessageDigest sha; 046 try { 047 sha = MessageDigest.getInstance("SHA-1"); 048 } 049 catch (NoSuchAlgorithmException e) { 050 throw new RuntimeException(e); 051 } 052 sha.update(guidString.getBytes()); 053 byte[] hash=sha.digest(); 054 055 StringBuffer result=new StringBuffer(); 056 for (int i=0; i<hash.length; i++) { 057 result.append(toHex(hash[i])); 058 } 059 060 // hyphenate 061 for (int i=20; i>4; i-=4) { 062 result.insert(i,HYPHEN); 063 } 064 065 // truncate 066 result.delete(32,40); 067 stringValue=result.toString(); 068 } 069 070 public static String toHex(byte b) { 071 072 int ub=b<0?b+256:b; 073 074 StringBuffer result=new StringBuffer(2); 075 result.append(DIGITS.charAt(ub/16)); 076 result.append(DIGITS.charAt(ub%16)); 077 078 return result.toString(); 079 } 080 081 @Override 082 public String toString() { 083 return stringValue; 084 } 085 086}