1 package be.bastard.mtracker.external;
2
3 import java.net.InetAddress;
4 import java.util.Enumeration;
5 import java.util.Locale;
6 import java.util.Properties;
7 import java.util.Random;
8
9 /***
10 * PlatformIndependentGuidGen.
11 * This file does not belong explicitely to the Media Tracker project.
12 * It is a id generator that was written by Todd Clark in 2004.
13 * Below is the original license information of the class.
14 * <pre>
15 * --------------------------------------------------------------------------------------
16 * Just a stand alone class that I posted to a forum
17 *
18 * Copyright (C) 2004 Todd Clark quirkware0@yahoo.com
19 *
20 * This library is free software; you can redistribute it and/or modify it
21 * under the terms of the GNU Lesser General Public License as published by the Free
22 * Software Foundation; either version 2.1 of the License, or (at your option)
23 * any later version.
24 *
25 * This library is distributed in the hope that it will be useful, but
26 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
27 * or FITfNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
28 * for more details.
29 *
30 * You should have received a copy of the GNU Lesser General Public License
31 * along with this program; if not, write to the Free Software
32 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
33 *
34 * If you make changes or improvements to the this class I ask that you you
35 * post them to a forum thread and email the improvements to quirkware0@yahoo.com
36 * and that you leave my name intact in the header comments.
37 *
38 * If you use this code in an application that you distribute. I require that you
39 * include my name in the credits of your application.
40 * --------------------------------------------------------------------------------------
41 * </pre>
42 */
43 public class PlatformIndependentGuidGen {
44 private static PlatformIndependentGuidGen _TheInstance = null;
45
46 private Random _RandomForSpace = null;
47
48 private Random _RandomForTime = null;
49
50 /***
51 * Note: the first time you call this function it is very expensive
52 *
53 * @return PlatformIndependentGuidGen the instace of the Guid generator
54 */
55 public static synchronized PlatformIndependentGuidGen getInstance() {
56 if (_TheInstance == null)
57 _TheInstance = new PlatformIndependentGuidGen();
58
59 return _TheInstance;
60 }
61
62 /***
63 *
64 * @return String a new Guid
65 */
66 public String genNewGuid() {
67 long uniqueInSpace = _RandomForSpace.nextLong();
68 long uniqueInTime = _RandomForTime.nextLong();
69
70 return Long.toHexString(uniqueInSpace) + "-"
71 + Long.toHexString(uniqueInTime);
72 }
73
74 private PlatformIndependentGuidGen() {
75 long uniqueSpaceSeed = createUniqueSpaceSeed();
76 long uniqueTimeSeed = System.currentTimeMillis();
77
78 _RandomForSpace = new Random(uniqueSpaceSeed);
79 _RandomForTime = new Random(uniqueTimeSeed);
80 }
81
82 /***
83 * Does the best job it can to produce a number that is dependent on the
84 * machine on which this code is running
85 *
86 * @return long
87 */
88 private long createUniqueSpaceSeed() {
89 String hashingString = "";
90
91
92
93 Properties systemProps = System.getProperties();
94 Enumeration systemPropKeys = systemProps.keys();
95 String systemPropKey = null;
96 Locale[] locales = Locale.getAvailableLocales();
97 int iCounter = 0;
98 Runtime runtime = Runtime.getRuntime();
99 InetAddress myAddress = null;
100
101
102
103 while (systemPropKeys.hasMoreElements()) {
104 systemPropKey = (String) systemPropKeys.nextElement();
105
106
107 hashingString = hashingString + " " + systemPropKey + " = "
108 + systemProps.getProperty(systemPropKey) + "\n";
109
110 }
111
112
113 hashingString = hashingString + "Cycle Test = " + cpuCycleTest() + "\n";
114
115
116
117 for (iCounter = 0; iCounter < locales.length; iCounter++) {
118 hashingString = hashingString + locales[iCounter].getCountry()
119 + " " + locales[iCounter].getDisplayCountry() + " "
120 + locales[iCounter].getDisplayName() + "\n";
121 }
122
123
124
125 hashingString = hashingString + "Available proc = "
126 + runtime.availableProcessors() + "\n";
127
128 hashingString = hashingString + "free memory = " + runtime.freeMemory()
129 + "\n";
130
131 hashingString = hashingString + "max memory = " + runtime.maxMemory()
132 + "\n";
133
134 hashingString = hashingString + "total memory = "
135 + runtime.totalMemory() + "\n";
136
137
138 try {
139 myAddress = InetAddress.getLocalHost();
140 hashingString = hashingString + "my IP address = "
141 + myAddress.toString();
142 } catch (Exception e) {
143
144 }
145
146
147
148 return hashingString.hashCode();
149 }
150
151 /***
152 * how high can we count in 10 msec
153 *
154 * @return long
155 */
156 private long cpuCycleTest() {
157 long returnValue = 0;
158 long startTime = System.currentTimeMillis();
159 long rightNow = System.currentTimeMillis();
160
161
162 while ((rightNow - startTime) < 10
163 && returnValue < (Long.MAX_VALUE - 10)) {
164 returnValue++;
165 rightNow = System.currentTimeMillis();
166 }
167
168 return returnValue;
169 }
170 }