1 /*
2 Copyright (C) 2000 - 2007 Grid Systems, S.A.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2, as
6 published by the Free Software Foundation.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 */
17 package com.gridsystems.innergrid.kernel;
18
19 import java.lang.reflect.Method;
20 import java.net.InetAddress;
21 import java.net.NetworkInterface;
22
23 import org.safehaus.uuid.EthernetAddress;
24 import org.safehaus.uuid.UUIDGenerator;
25
26 /**
27 * UUID Generation utility class.
28 *
29 * @author Rodrigo Ruiz
30 */
31 public final class UUIDUtils {
32
33 /**
34 * Hidden constructor.
35 */
36 private UUIDUtils() { }
37
38 /**
39 * Internal generator instance.
40 */
41 private static final UUIDGenerator GEN = UUIDGenerator.getInstance();
42
43 /**
44 * Local MAC address. If none is available, a dummy broadcast random
45 * UUID is generated.
46 */
47 private static final EthernetAddress MAC_ADDRESS = findMacAddress();
48
49 /**
50 * Generates a time based UUID.
51 *
52 * @return An UUID
53 */
54 public static String generateTimeBasedUUID() {
55 return generateTimeBasedUUID(null);
56 }
57
58 /**
59 * Generates a time based UUID.
60 * <p>
61 * Time-based generation generates UUID using spatial and temporal
62 * uniqueness. Spatial uniqueness is derived from ethernet address
63 * (MAC, 802.1); temporal from system clock.
64 *
65 * @param macAddress The MAC address
66 * @return An UUID for the specified ethernet address
67 */
68 public static String generateTimeBasedUUID(byte[] macAddress) {
69 final EthernetAddress addr;
70
71 if (macAddress == null) {
72 addr = MAC_ADDRESS;
73 } else {
74 addr = new EthernetAddress(macAddress);
75 }
76
77 return GEN.generateTimeBasedUUID(addr).toString();
78 }
79
80 /**
81 * Gets the MAC address of this machine as a byte array.
82 *
83 * @return a 6-byte array containing the ethernet address of this machine
84 */
85 public static byte[] getMacAddress() {
86 return MAC_ADDRESS.toByteArray();
87 }
88
89 /**
90 * Gets the MAC address of this machine. If no MAC address is available,
91 * a dummy random broadcast address is generated.
92 *
93 * @return The MAC address
94 */
95 private static EthernetAddress findMacAddress() {
96 try {
97 // This code only works in Java 6
98 InetAddress localHost = InetAddress.getLocalHost();
99 NetworkInterface iface = NetworkInterface.getByInetAddress(localHost);
100 Method m = NetworkInterface.class.getMethod("getHardwareAddress");
101 return new EthernetAddress((byte[])m.invoke(iface));
102 } catch (Exception e) {
103 return GEN.getDummyAddress();
104 }
105 }
106 }