View Javadoc
1 package org.apache.turbine.util; 2 3 /* ==================================================================== 4 * The Apache Software License, Version 1.1 5 * 6 * Copyright (c) 2001 The Apache Software Foundation. All rights 7 * reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in 18 * the documentation and/or other materials provided with the 19 * distribution. 20 * 21 * 3. The end-user documentation included with the redistribution, 22 * if any, must include the following acknowledgment: 23 * "This product includes software developed by the 24 * Apache Software Foundation (http://www.apache.org/)." 25 * Alternately, this acknowledgment may appear in the software itself, 26 * if and wherever such third-party acknowledgments normally appear. 27 * 28 * 4. The names "Apache" and "Apache Software Foundation" and 29 * "Apache Turbine" must not be used to endorse or promote products 30 * derived from this software without prior written permission. For 31 * written permission, please contact apache@apache.org. 32 * 33 * 5. Products derived from this software may not be called "Apache", 34 * "Apache Turbine", nor may "Apache" appear in their name, without 35 * prior written permission of the Apache Software Foundation. 36 * 37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 48 * SUCH DAMAGE. 49 * ==================================================================== 50 * 51 * This software consists of voluntary contributions made by many 52 * individuals on behalf of the Apache Software Foundation. For more 53 * information on the Apache Software Foundation, please see 54 * <http://www.apache.org/>;. 55 */ 56 57 import java.util.Random; 58 59 /*** 60 * This class generates a unique 10+ character id. This is good for 61 * authenticating users or tracking users around. 62 * 63 * <p>This code was borrowed from Apache 64 * JServ.JServServletManager.java. It is what Apache JServ uses to 65 * generate session ids for users. Unfortunately, it was not included 66 * in Apache JServ as a class, so I had to create one here in order to 67 * use it. 68 * 69 * @author <a href="mailto:jon@clearink.com">Jon S. Stevens</a> 70 * @author <a href="mailto:neeme@one.lv">Neeme Praks</a> 71 * @version $Id: GenerateUniqueId.java,v 1.1.1.1 2001/08/16 05:09:39 jvanzyl Exp $ 72 */ 73 public class GenerateUniqueId 74 { 75 /* 76 * Create a suitable string for session identification. Use 77 * synchronized count and time to ensure uniqueness. Use random 78 * string to ensure the timestamp cannot be guessed by programmed 79 * attack. 80 * 81 * Format of id is <6 chars random><3 chars time><1+ char count> 82 */ 83 static private int session_count = 0; 84 static private long lastTimeVal = 0; 85 static private Random randomSource = new java.util.Random(); 86 87 // MAX_RADIX is 36 88 89 /* 90 * We want to have a random string with a length of 6 characters. 91 * Since we encode it BASE 36, we've to modulo it with the 92 * following value: 93 */ 94 public final static long maxRandomLen = 2176782336L; // 36 ** 6 95 96 /* 97 * The session identifier must be unique within the typical 98 * lifespan of a Session; the value can roll over after that. 3 99 * characters: (this means a roll over after over a day, which is 100 * much larger than a typical lifespan) 101 */ 102 public final static long maxSessionLifespanTics = 46656; // 36 ** 3 103 104 /* 105 * Millisecons between different tics. So this means that the 106 * 3-character time string has a new value every 2 seconds: 107 */ 108 public final static long ticDifference = 2000; 109 110 /*** 111 * Get the unique id. 112 * 113 * <p>NOTE: This must work together with 114 * get_jserv_session_balance() in jserv_balance.c 115 * 116 * @return A String with the new unique id. 117 */ 118 static synchronized public String getIdentifier () 119 { 120 StringBuffer sessionId = new StringBuffer(); 121 122 // Random value. 123 long n = randomSource.nextLong(); 124 if (n < 0) n = -n; 125 n %= maxRandomLen; 126 127 // Add maxLen to pad the leading characters with '0'; remove 128 // first digit with substring. 129 n += maxRandomLen; 130 sessionId.append (Long.toString(n, Character.MAX_RADIX) 131 .substring(1)); 132 133 long timeVal = (System.currentTimeMillis() / ticDifference); 134 135 // Cut. 136 timeVal %= maxSessionLifespanTics; 137 138 // Padding, see above. 139 timeVal += maxSessionLifespanTics; 140 141 sessionId.append (Long.toString (timeVal, Character.MAX_RADIX) 142 .substring(1)); 143 144 /* 145 * Make the string unique: append the session count since last 146 * time flip. 147 */ 148 149 // Count sessions only within tics. So the 'real' session 150 // count isn't exposed to the public. 151 if (lastTimeVal != timeVal) 152 { 153 lastTimeVal = timeVal; 154 session_count = 0; 155 } 156 sessionId.append (Long.toString (++session_count, 157 Character.MAX_RADIX)); 158 159 return sessionId.toString(); 160 } 161 162 /*** 163 * Get the unique id. 164 * 165 * @param jsIdent A String. 166 * @return A String with the new unique id. 167 */ 168 synchronized public String getIdentifier(String jsIdent) 169 { 170 if (jsIdent != null && jsIdent.length() > 0) 171 { 172 return getIdentifier()+"."+jsIdent; 173 } 174 return getIdentifier(); 175 } 176 177 /*** 178 * Simple test of the functionality. 179 * 180 * @param args A String[] with the command line arguments. 181 */ 182 public static void main(String[] args) 183 { 184 System.out.println(GenerateUniqueId.getIdentifier()); 185 System.out.println(GenerateUniqueId.getIdentifier()); 186 System.out.println(GenerateUniqueId.getIdentifier()); 187 System.out.println(GenerateUniqueId.getIdentifier()); 188 } 189 }

This page was automatically generated by Maven