001: /*
002: * Copyright 2006 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
007: * BI Platform. The Initial Developer is Pentaho Corporation.
008: *
009: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
011: * the license for the specific language governing your rights and limitations.
012: *
013: * @created Mar 18, 2005
014: * @author Marc Batchelor
015: *
016: */
017:
018: package org.pentaho.util;
019:
020: import org.apache.commons.logging.Log;
021: import org.apache.commons.logging.LogFactory;
022: import org.safehaus.uuid.UUID;
023: import org.safehaus.uuid.UUIDGenerator;
024: import org.pentaho.messages.Messages;
025:
026: public class UUIDUtil {
027: private static Log log = LogFactory.getLog(UUIDUtil.class);
028:
029: static boolean nativeInitialized = false;
030:
031: static UUIDGenerator ug;
032:
033: static org.safehaus.uuid.EthernetAddress eAddr;
034:
035: static {
036: // Try loading the EthernetAddress library. If this fails, then fallback
037: // to
038: // using another method for generating UUID's.
039: /*
040: * This is always going to fail at the moment try {
041: * System.loadLibrary("EthernetAddress"); //$NON-NLS-1$
042: * nativeInitialized = true; } catch (Throwable t) { //
043: * log.warn(Messages.getErrorString("UUIDUtil.ERROR_0001_LOADING_ETHERNET_ADDRESS") );
044: * //$NON-NLS-1$ //$NON-NLS-2$ // Ignore for now. }
045: */
046: ug = UUIDGenerator.getInstance();
047: if (nativeInitialized) {
048: try {
049: com.ccg.net.ethernet.EthernetAddress ea = com.ccg.net.ethernet.EthernetAddress
050: .getPrimaryAdapter();
051: eAddr = new org.safehaus.uuid.EthernetAddress(ea
052: .getBytes());
053: } catch (Exception ex) {
054: log
055: .error(
056: Messages
057: .getErrorString("UUIDUtil.ERROR_0002_GET_MAC_ADDR"), ex); //$NON-NLS-1$
058: } catch (UnsatisfiedLinkError ule) {
059: log
060: .error(
061: Messages
062: .getErrorString("UUIDUtil.ERROR_0002_GET_MAC_ADDR"), ule); //$NON-NLS-1$
063: nativeInitialized = false;
064: }
065: }
066:
067: /*
068: * Add support for running in clustered environments. In this way, the MAC address of the
069: * running server can be added to the environment with a -DMAC_ADDRESS=00:50:56:C0:00:01
070: */
071: if (eAddr == null) {
072: String macAddr = System.getProperty("MAC_ADDRESS"); //$NON-NLS-1$
073: if (macAddr != null) {
074: // On Windows machines, people would be inclined to get the MAC
075: // address with ipconfig /all. The format of this would be
076: // something like 00-50-56-C0-00-08. So, replace '-' with ':' before
077: // creating the address.
078: //
079: macAddr = macAddr.replace('-', ':');
080: eAddr = new org.safehaus.uuid.EthernetAddress(macAddr);
081: }
082: }
083:
084: if (eAddr == null) {
085: // Still don't have an Ethernet Address - generate a dummy one.
086: eAddr = ug.getDummyAddress();
087: }
088:
089: // Generate a UUID to make sure everything is running OK.
090: UUID olduuId = ug.generateTimeBasedUUID(eAddr);
091: if (olduuId == null) {
092: log
093: .error(Messages
094: .getErrorString("UUIDUtil.ERROR_0003_GENERATEFAILED")); //$NON-NLS-1$
095: }
096:
097: }
098:
099: public static String getUUIDAsString() {
100: return getUUID().toString();
101: }
102:
103: public static UUID getUUID() {
104: UUID uuId = ug.generateTimeBasedUUID(eAddr);
105: // while (uuId.toString().equals(olduuId.toString())) {
106: // uuId = ug.generateTimeBasedUUID(eAddr);
107: // }
108: // olduuId = uuId;
109: return uuId;
110: }
111:
112: }
|