001: /*
002: Mdarad-Toolobox is a collection of tools for Architected RAD
003: (Rapid Application Development) based on an MDA approach.
004: The toolbox contains frameworks and generators for many environments
005: (JAVA, J2EE, Hibernate, .NET, C++, etc.) which allow to generate
006: applications from a design Model
007: Copyright (C) 2004-2005 Elapse Technologies Inc.
008:
009: This library is free software; you can redistribute it and/or
010: modify it under the terms of the GNU General Public
011: License as published by the Free Software Foundation; either
012: version 2.1 of the License, or (at your option) any later version.
013:
014: This library is distributed in the hope that it will be useful,
015: but WITHOUT ANY WARRANTY; without even the implied warranty of
016: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: General Public License for more details.
018:
019: You should have received a copy of the GNU General Public
020: License along with this library; if not, write to the Free Software
021: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
022: */
023:
024: package org.mdarad.framework.util;
025:
026: import org.mdarad.framework.util.GUIDGenerationException;
027:
028: import java.net.UnknownHostException;
029: import java.net.InetAddress;
030:
031: /**
032: * Created by IntelliJ IDEA.
033: * User: François Eric
034: * Date: 2005-02-01
035: * Time: 14:21:46
036: * To change this template use File | Settings | File Templates.
037: */
038: public class GUIDGenerator {
039:
040: /**
041: * Cached per JVM server IP.
042: */
043: private static String hexServerIP = null;
044:
045: // initialise the secure random instance
046: private static final java.security.SecureRandom seeder = new java.security.SecureRandom();
047:
048: /**
049: * A 32 byte GUID generator (Globally Unique ID). These artificial keys SHOULD <strong>NOT </strong> be seen by the user,
050: * not even touched by the DBA but with very rare exceptions, just manipulated by the database and the programs.
051: * <p/>
052: */
053: public static final String generateGUID(Object o)
054: throws GUIDGenerationException {
055: StringBuffer tmpBuffer = new StringBuffer(16);
056: if (hexServerIP == null) {
057: InetAddress localInetAddress = null;
058:
059: try {
060: // get the inet address
061: localInetAddress = InetAddress.getLocalHost();
062: byte serverIP[] = localInetAddress.getAddress();
063: hexServerIP = hexFormat(getInt(serverIP), 8);
064: } catch (UnknownHostException e) {
065: throw new GUIDGenerationException(
066: "Could not generate GUID, for unable to reach localhost",
067: e);
068: }
069: }
070: String hashcode = hexFormat(System.identityHashCode(o), 8);
071: tmpBuffer.append(hexServerIP);
072: tmpBuffer.append(hashcode);
073:
074: long timeNow = System.currentTimeMillis();
075: int timeLow = (int) timeNow & 0xFFFFFFFF;
076: int node = seeder.nextInt();
077:
078: StringBuffer guid = new StringBuffer(32);
079: guid.append(hexFormat(timeLow, 8));
080: guid.append(tmpBuffer.toString());
081: guid.append(hexFormat(node, 8));
082: return guid.toString();
083: }
084:
085: private static int getInt(byte bytes[]) {
086: int i = 0;
087: int j = 24;
088: for (int k = 0; j >= 0; k++) {
089: int l = bytes[k] & 0xff;
090: i += l << j;
091: j -= 8;
092: }
093: return i;
094: }
095:
096: private static String hexFormat(int i, int j) {
097: String s = Integer.toHexString(i);
098: return padHex(s, j) + s;
099: }
100:
101: private static String padHex(String s, int i) {
102: StringBuffer tmpBuffer = new StringBuffer();
103: if (s.length() < i) {
104: for (int j = 0; j < i - s.length(); j++) {
105: tmpBuffer.append('0');
106: }
107: }
108: return tmpBuffer.toString();
109: }
110:
111: }
|