KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > utils > SessionUtils


1 /*
2  * Copyright 2002-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.axis.utils;
17
18 import org.apache.axis.components.logger.LogFactory;
19 import org.apache.commons.logging.Log;
20
21 import java.util.Random JavaDoc;
22
23 /**
24  * Code borrowed from AuthenticatorBase.java for generating a secure id's.
25  */

26 public class SessionUtils {
27
28     /** Field log */
29     protected static Log log = LogFactory.getLog(SessionUtils.class.getName());
30
31     /**
32      * The number of random bytes to include when generating a
33      * session identifier.
34      */

35     protected static final int SESSION_ID_BYTES = 16;
36
37     /**
38      * A random number generator to use when generating session identifiers.
39      */

40     protected static Random JavaDoc random = null;
41
42     /**
43      * The Java class name of the random number generator class to be used
44      * when generating session identifiers.
45      */

46     protected static String JavaDoc randomClass = "java.security.SecureRandom";
47
48     /**
49      * Host name/ip.
50      */

51     private static String JavaDoc thisHost = null;
52
53     /**
54      * Generate and return a new session identifier.
55      *
56      * @return a new session id
57      */

58     public static synchronized String JavaDoc generateSessionId() {
59         // Generate a byte array containing a session identifier
60
byte bytes[] = new byte[SESSION_ID_BYTES];
61
62         getRandom().nextBytes(bytes);
63
64         // Render the result as a String of hexadecimal digits
65
StringBuffer JavaDoc result = new StringBuffer JavaDoc();
66
67         for (int i = 0; i < bytes.length; i++) {
68             byte b1 = (byte) ((bytes[i] & 0xf0) >> 4);
69             byte b2 = (byte) (bytes[i] & 0x0f);
70
71             if (b1 < 10) {
72                 result.append((char) ('0' + b1));
73             } else {
74                 result.append((char) ('A' + (b1 - 10)));
75             }
76             if (b2 < 10) {
77                 result.append((char) ('0' + b2));
78             } else {
79                 result.append((char) ('A' + (b2 - 10)));
80             }
81         }
82         return (result.toString());
83     }
84
85     /**
86      * Generate and return a new session identifier.
87      *
88      * @return a new session.
89      */

90     public static synchronized Long JavaDoc generateSession() {
91         return new Long JavaDoc(getRandom().nextLong());
92     }
93
94     /**
95      * Return the random number generator instance we should use for
96      * generating session identifiers. If there is no such generator
97      * currently defined, construct and seed a new one.
98      *
99      * @return Random object
100      */

101     private static synchronized Random JavaDoc getRandom() {
102         if (random == null) {
103             try {
104                 Class JavaDoc clazz = Class.forName(randomClass);
105                 random = (Random JavaDoc) clazz.newInstance();
106             } catch (Exception JavaDoc e) {
107                 random = new java.util.Random JavaDoc();
108             }
109         }
110         return (random);
111     }
112
113 }
114
Popular Tags