PageRenderTime 58ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/test/files/presentation/akka/src/com/eaio/uuid/UUIDGen.java

https://github.com/mkneissl/scala
Java | 364 lines | 200 code | 45 blank | 119 comment | 42 complexity | 413adfd7c87818345f9a20efacb2c92a MD5 | raw file
  1. /*
  2. * UUIDGen.java
  3. *
  4. * Created on 09.08.2003.
  5. *
  6. * eaio: UUID - an implementation of the UUID specification
  7. * Copyright (c) 2003-2009 Johann Burkard (jb@eaio.com) http://eaio.com.
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a
  10. * copy of this software and associated documentation files (the "Software"),
  11. * to deal in the Software without restriction, including without limitation
  12. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  13. * and/or sell copies of the Software, and to permit persons to whom the
  14. * Software is furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included
  17. * in all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  20. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  21. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  22. * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  23. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  24. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  25. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. *
  27. */
  28. package com.eaio.uuid;
  29. import java.io.BufferedReader;
  30. import java.io.File;
  31. import java.io.IOException;
  32. import java.io.InputStreamReader;
  33. import java.net.InetAddress;
  34. import java.net.InterfaceAddress;
  35. import java.net.NetworkInterface;
  36. import java.net.SocketException;
  37. import java.net.UnknownHostException;
  38. import java.util.Enumeration;
  39. import com.eaio.util.lang.Hex;
  40. /**
  41. * This class contains methods to generate UUID fields. These methods have been
  42. * refactored out of {@link com.eaio.uuid.UUID}.
  43. * <p>
  44. * Starting with version 2, this implementation tries to obtain the MAC address
  45. * of the network card. Under Microsoft Windows, the <code>ifconfig</code>
  46. * command is used which may pop up a command window in Java Virtual Machines
  47. * prior to 1.4 once this class is initialized. The command window is closed
  48. * automatically.
  49. * <p>
  50. * The MAC address code has been tested extensively in Microsoft Windows,
  51. * Linux, Solaris 8, HP-UX 11, but should work in MacOS X and BSDs, too.
  52. * <p>
  53. * If you use JDK 6 or later, the code in {@link InterfaceAddress} will be used.
  54. *
  55. * @see <a href="http://johannburkard.de/software/uuid/">UUID</a>
  56. * @author <a href="mailto:jb@eaio.de">Johann Burkard</a>
  57. * @version $Id: UUIDGen.java 2914 2010-04-23 11:35:00Z johann $
  58. * @see com.eaio.uuid.UUID
  59. */
  60. public final class UUIDGen {
  61. /**
  62. * No instances needed.
  63. */
  64. private UUIDGen() {
  65. super();
  66. }
  67. /**
  68. * The last time value. Used to remove duplicate UUIDs.
  69. */
  70. private static long lastTime = Long.MIN_VALUE;
  71. /**
  72. * The cached MAC address.
  73. */
  74. private static String macAddress = null;
  75. /**
  76. * The current clock and node value.
  77. */
  78. private static long clockSeqAndNode = 0x8000000000000000L;
  79. static {
  80. try {
  81. Class.forName("java.net.InterfaceAddress");
  82. macAddress = Class.forName(
  83. "com.eaio.uuid.UUIDGen$HardwareAddressLookup").newInstance().toString();
  84. }
  85. catch (ExceptionInInitializerError err) {
  86. // Ignored.
  87. }
  88. catch (ClassNotFoundException ex) {
  89. // Ignored.
  90. }
  91. catch (LinkageError err) {
  92. // Ignored.
  93. }
  94. catch (IllegalAccessException ex) {
  95. // Ignored.
  96. }
  97. catch (InstantiationException ex) {
  98. // Ignored.
  99. }
  100. catch (SecurityException ex) {
  101. // Ignored.
  102. }
  103. if (macAddress == null) {
  104. Process p = null;
  105. BufferedReader in = null;
  106. try {
  107. String osname = System.getProperty("os.name", "");
  108. if (osname.startsWith("Windows")) {
  109. p = Runtime.getRuntime().exec(
  110. new String[] { "ipconfig", "/all" }, null);
  111. }
  112. // Solaris code must appear before the generic code
  113. else if (osname.startsWith("Solaris")
  114. || osname.startsWith("SunOS")) {
  115. String hostName = getFirstLineOfCommand(
  116. "uname", "-n" );
  117. if (hostName != null) {
  118. p = Runtime.getRuntime().exec(
  119. new String[] { "/usr/sbin/arp", hostName },
  120. null);
  121. }
  122. }
  123. else if (new File("/usr/sbin/lanscan").exists()) {
  124. p = Runtime.getRuntime().exec(
  125. new String[] { "/usr/sbin/lanscan" }, null);
  126. }
  127. else if (new File("/sbin/ifconfig").exists()) {
  128. p = Runtime.getRuntime().exec(
  129. new String[] { "/sbin/ifconfig", "-a" }, null);
  130. }
  131. if (p != null) {
  132. in = new BufferedReader(new InputStreamReader(
  133. p.getInputStream()), 128);
  134. String l = null;
  135. while ((l = in.readLine()) != null) {
  136. macAddress = MACAddressParser.parse(l);
  137. if (macAddress != null
  138. && Hex.parseShort(macAddress) != 0xff) {
  139. break;
  140. }
  141. }
  142. }
  143. }
  144. catch (SecurityException ex) {
  145. // Ignore it.
  146. }
  147. catch (IOException ex) {
  148. // Ignore it.
  149. }
  150. finally {
  151. if (p != null) {
  152. if (in != null) {
  153. try {
  154. in.close();
  155. }
  156. catch (IOException ex) {
  157. // Ignore it.
  158. }
  159. }
  160. try {
  161. p.getErrorStream().close();
  162. }
  163. catch (IOException ex) {
  164. // Ignore it.
  165. }
  166. try {
  167. p.getOutputStream().close();
  168. }
  169. catch (IOException ex) {
  170. // Ignore it.
  171. }
  172. p.destroy();
  173. }
  174. }
  175. }
  176. if (macAddress != null) {
  177. clockSeqAndNode |= Hex.parseLong(macAddress);
  178. }
  179. else {
  180. try {
  181. byte[] local = InetAddress.getLocalHost().getAddress();
  182. clockSeqAndNode |= (local[0] << 24) & 0xFF000000L;
  183. clockSeqAndNode |= (local[1] << 16) & 0xFF0000;
  184. clockSeqAndNode |= (local[2] << 8) & 0xFF00;
  185. clockSeqAndNode |= local[3] & 0xFF;
  186. }
  187. catch (UnknownHostException ex) {
  188. clockSeqAndNode |= (long) (Math.random() * 0x7FFFFFFF);
  189. }
  190. }
  191. // Skip the clock sequence generation process and use random instead.
  192. clockSeqAndNode |= (long) (Math.random() * 0x3FFF) << 48;
  193. }
  194. /**
  195. * Returns the current clockSeqAndNode value.
  196. *
  197. * @return the clockSeqAndNode value
  198. * @see UUID#getClockSeqAndNode()
  199. */
  200. public static long getClockSeqAndNode() {
  201. return clockSeqAndNode;
  202. }
  203. /**
  204. * Generates a new time field. Each time field is unique and larger than the
  205. * previously generated time field.
  206. *
  207. * @return a new time value
  208. * @see UUID#getTime()
  209. */
  210. public static long newTime() {
  211. return createTime(System.currentTimeMillis());
  212. }
  213. /**
  214. * Creates a new time field from the given timestamp. Note that even identical
  215. * values of <code>currentTimeMillis</code> will produce different time fields.
  216. *
  217. * @param currentTimeMillis the timestamp
  218. * @return a new time value
  219. * @see UUID#getTime()
  220. */
  221. public static synchronized long createTime(long currentTimeMillis) {
  222. long time;
  223. // UTC time
  224. long timeMillis = (currentTimeMillis * 10000) + 0x01B21DD213814000L;
  225. if (timeMillis > lastTime) {
  226. lastTime = timeMillis;
  227. }
  228. else {
  229. timeMillis = ++lastTime;
  230. }
  231. // time low
  232. time = timeMillis << 32;
  233. // time mid
  234. time |= (timeMillis & 0xFFFF00000000L) >> 16;
  235. // time hi and version
  236. time |= 0x1000 | ((timeMillis >> 48) & 0x0FFF); // version 1
  237. return time;
  238. }
  239. /**
  240. * Returns the MAC address. Not guaranteed to return anything.
  241. *
  242. * @return the MAC address, may be <code>null</code>
  243. */
  244. public static String getMACAddress() {
  245. return macAddress;
  246. }
  247. /**
  248. * Returns the first line of the shell command.
  249. *
  250. * @param commands the commands to run
  251. * @return the first line of the command
  252. * @throws IOException
  253. */
  254. static String getFirstLineOfCommand(String... commands) throws IOException {
  255. Process p = null;
  256. BufferedReader reader = null;
  257. try {
  258. p = Runtime.getRuntime().exec(commands);
  259. reader = new BufferedReader(new InputStreamReader(
  260. p.getInputStream()), 128);
  261. return reader.readLine();
  262. }
  263. finally {
  264. if (p != null) {
  265. if (reader != null) {
  266. try {
  267. reader.close();
  268. }
  269. catch (IOException ex) {
  270. // Ignore it.
  271. }
  272. }
  273. try {
  274. p.getErrorStream().close();
  275. }
  276. catch (IOException ex) {
  277. // Ignore it.
  278. }
  279. try {
  280. p.getOutputStream().close();
  281. }
  282. catch (IOException ex) {
  283. // Ignore it.
  284. }
  285. p.destroy();
  286. }
  287. }
  288. }
  289. /**
  290. * Scans MAC addresses for good ones.
  291. */
  292. static class HardwareAddressLookup {
  293. /**
  294. * @see java.lang.Object#toString()
  295. */
  296. @Override
  297. public String toString() {
  298. String out = null;
  299. try {
  300. Enumeration<NetworkInterface> ifs = NetworkInterface.getNetworkInterfaces();
  301. if (ifs != null) {
  302. while (ifs.hasMoreElements()) {
  303. NetworkInterface iface = ifs.nextElement();
  304. byte[] hardware = iface.getHardwareAddress();
  305. if (hardware != null && hardware.length == 6
  306. && hardware[1] != (byte) 0xff) {
  307. out = Hex.append(new StringBuilder(36), hardware).toString();
  308. break;
  309. }
  310. }
  311. }
  312. }
  313. catch (SocketException ex) {
  314. // Ignore it.
  315. }
  316. return out;
  317. }
  318. }
  319. }