View Javadoc

1   /********************************************************************************
2    *InternetCafe is a software solution that helps the management of Cybercafes 
3    * according with the ITALIAN DECREE LAW ON ANTI-TERROR MEASURES, 27 JULY 2005.
4    * Copyright (C) 2006  Guido Angelo Ingenito
5    
6    * This program is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU General Public License
8    * as published by the Free Software Foundation; either version 2
9    * of the License, or (at your option) any later version.
10   
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   * 
16   * You should have received a copy of the GNU General Public License
17   * along with this program; if not, write to the Free Software
18   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19   *******************************************************************************/
20  package base.jdbs;
21  
22  import java.math.BigInteger;
23  import java.security.SecureRandom;
24  
25  import org.apache.log4j.Logger;
26  
27  public class GUIDGenerator {
28  	
29  	private static final transient Logger logger = Logger.getLogger(GUIDGenerator.class.getName());
30  	
31  	/***The secure random to use when must be build a global unique identifier.**/
32  	private SecureRandom secureRandom;
33  	/***The GUIDGenerator instance according to the Singleton pattern.**/
34  	private static GUIDGenerator instance = null;
35  	
36  	
37  	/***
38  	 * @return Returns the instance.
39  	 */
40  	public static GUIDGenerator getInstance(){
41  		return instance!=null ? instance : (instance = new GUIDGenerator());
42  	}
43  	
44  	/***
45  	 * The default constructor.
46  	 */
47  	protected GUIDGenerator(){
48  		this.secureRandom = new SecureRandom();
49  		logger.debug(secureRandom.getProvider().getInfo());
50  	}
51  	
52  	/***
53  	 * This method returns a alphanumeric key that is unique to a millisecond. The uniqueness
54  	 * is increased by appending a random number to the key.
55  	 * @return The unique key.
56  	 */
57  	public String getAlphanumericKey() {
58  		// Append current time in millis to a hexadecimal representation
59  		// of a random number.
60  		String key = "" + System.currentTimeMillis() + Long.toHexString(secureRandom.nextInt());
61  		logger.debug("getAlphanumericKey(): "+key);
62  		return key;
63  	}
64  	
65  	/***
66  	 * This method returns a numeric key that is unique to a millisecond. The uniqueness
67  	 * is increased by appending a random number to the key.
68  	 * @return The unique key.
69  	 */
70  	public BigInteger getNumericKey() {
71  		// Append current time in millis to a hexadecimal representation
72  		// of a random number.
73  		String firstToken = ""+System.currentTimeMillis();
74  		Long secureToken = secureRandom.nextLong();
75  		String lastToken = secureToken > 0 ? ""+secureToken : ""+(-1 * secureToken);
76  		BigInteger key = new BigInteger(firstToken+lastToken);
77  		logger.debug("getNumericKey(): "+key);
78  		return key;
79  	}	
80  	
81  }