KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis2 > util > SessionUtils


1 /*
2  * Copyright 2004,2005 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.axis2.util;
17
18 import java.util.Random JavaDoc;
19
20 /**
21  * Code borrowed from AuthenticatorBase.java for generating a secure id's.
22  */

23 public class SessionUtils {
24
25     /**
26      * The number of random bytes to include when generating a
27      * session identifier.
28      */

29     protected static final int SESSION_ID_BYTES = 16;
30
31     /**
32      * A random number generator to use when generating session identifiers.
33      */

34     protected static Random JavaDoc random = null;
35
36     /**
37      * The Java class name of the random number generator class to be used
38      * when generating session identifiers.
39      */

40     protected static String JavaDoc randomClass = "java.security.SecureRandom";
41
42     /**
43      * Host name/ip.
44      */

45     private static String JavaDoc thisHost = null;
46
47     /**
48      * Generate and return a new session identifier.
49      *
50      * @return a new session id
51      */

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

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

95     private static synchronized Random JavaDoc getRandom() {
96         if (random == null) {
97             try {
98                 Class JavaDoc clazz = Class.forName(randomClass);
99                 random = (Random JavaDoc) clazz.newInstance();
100             } catch (Exception JavaDoc e) {
101                 random = new java.util.Random JavaDoc();
102             }
103         }
104         return (random);
105     }
106
107 }
108
109
Popular Tags