KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > util > UUIDUtil


1 /*
2  * Copyright 2006 Pentaho Corporation. All rights reserved.
3  * This software was developed by Pentaho Corporation and is provided under the terms
4  * of the Mozilla Public License, Version 1.1, or any later version. You may not use
5  * this file except in compliance with the license. If you need a copy of the license,
6  * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
7  * BI Platform. The Initial Developer is Pentaho Corporation.
8  *
9  * Software distributed under the Mozilla Public License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
11  * the license for the specific language governing your rights and limitations.
12  *
13  * @created Mar 18, 2005
14  * @author Marc Batchelor
15  *
16  */

17
18 package org.pentaho.util;
19
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22 import org.safehaus.uuid.UUID;
23 import org.safehaus.uuid.UUIDGenerator;
24 import org.pentaho.messages.Messages;
25
26 public class UUIDUtil {
27     private static Log log = LogFactory.getLog(UUIDUtil.class);
28
29     static boolean nativeInitialized = false;
30
31     static UUIDGenerator ug;
32
33     static org.safehaus.uuid.EthernetAddress eAddr;
34
35     static {
36         // Try loading the EthernetAddress library. If this fails, then fallback
37
// to
38
// using another method for generating UUID's.
39
/*
40          * This is always going to fail at the moment try {
41          * System.loadLibrary("EthernetAddress"); //$NON-NLS-1$
42          * nativeInitialized = true; } catch (Throwable t) { //
43          * log.warn(Messages.getErrorString("UUIDUtil.ERROR_0001_LOADING_ETHERNET_ADDRESS") );
44          * //$NON-NLS-1$ //$NON-NLS-2$ // Ignore for now. }
45          */

46         ug = UUIDGenerator.getInstance();
47         if (nativeInitialized) {
48             try {
49                 com.ccg.net.ethernet.EthernetAddress ea = com.ccg.net.ethernet.EthernetAddress.getPrimaryAdapter();
50                 eAddr = new org.safehaus.uuid.EthernetAddress(ea.getBytes());
51             } catch (Exception JavaDoc ex) {
52                 log.error(Messages.getErrorString("UUIDUtil.ERROR_0002_GET_MAC_ADDR"), ex); //$NON-NLS-1$
53
} catch (UnsatisfiedLinkError JavaDoc ule) {
54                 log.error(Messages.getErrorString("UUIDUtil.ERROR_0002_GET_MAC_ADDR"), ule); //$NON-NLS-1$
55
nativeInitialized = false;
56             }
57         }
58
59         /*
60          * Add support for running in clustered environments. In this way, the MAC address of the
61          * running server can be added to the environment with a -DMAC_ADDRESS=00:50:56:C0:00:01
62          */

63         if (eAddr == null) {
64           String JavaDoc macAddr = System.getProperty("MAC_ADDRESS"); //$NON-NLS-1$
65
if (macAddr != null) {
66             // On Windows machines, people would be inclined to get the MAC
67
// address with ipconfig /all. The format of this would be
68
// something like 00-50-56-C0-00-08. So, replace '-' with ':' before
69
// creating the address.
70
//
71
macAddr = macAddr.replace('-', ':');
72             eAddr = new org.safehaus.uuid.EthernetAddress(macAddr);
73           }
74         }
75
76         if (eAddr == null) {
77             // Still don't have an Ethernet Address - generate a dummy one.
78
eAddr = ug.getDummyAddress();
79         }
80
81         // Generate a UUID to make sure everything is running OK.
82
UUID olduuId = ug.generateTimeBasedUUID(eAddr);
83         if (olduuId == null) {
84           log.error(Messages.getErrorString("UUIDUtil.ERROR_0003_GENERATEFAILED")); //$NON-NLS-1$
85
}
86
87     }
88
89     public static String JavaDoc getUUIDAsString() {
90         return getUUID().toString();
91     }
92
93     public static UUID getUUID() {
94         UUID uuId = ug.generateTimeBasedUUID(eAddr);
95 // while (uuId.toString().equals(olduuId.toString())) {
96
// uuId = ug.generateTimeBasedUUID(eAddr);
97
// }
98
// olduuId = uuId;
99
return uuId;
100     }
101
102 }
103
Popular Tags