KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > j2ee > blueprints > clientstate > SessionPasswordStrategy


1 /* Copyright 2004 Sun Microsystems, Inc. All rights reserved. You may not modify, use, reproduce, or distribute this software except in compliance with the terms of the License at:
2  http://developer.sun.com/berkeley_license.html
3  $Id: SessionPasswordStrategy.java,v 1.1 2005/05/17 05:23:51 gmurray71 Exp $ */

4
5 package com.sun.j2ee.blueprints.clientstate;
6
7 import javax.servlet.http.HttpSession JavaDoc;
8 /**
9  * This strategy generates a password randomly and stores it in the session.
10  * This strategy works in a clustered as well as a non-clustered environment
11  * the session is migrated whenever a server goes down.
12  *
13  * @author Inderjeet Singh
14  */

15 public class SessionPasswordStrategy implements PasswordStrategy {
16     
17     public static final String JavaDoc DEFAULT_SESSION_KEY_FOR_PASSWORD = "com.sun.j2ee.blueprints.clientside-state.password-key";
18     public static final int DEFAULT_PASSWORD_LENGTH = 24;
19     
20     /** @param session HttpSession used to store the password
21      * Uses default values for password length as well as session key to store password
22      * @see DEFAULT_SESSION_KEY_FOR_PASSWORD
23      * @see DEFAULT_PASSWORD_LENGTH
24
25      */

26     public SessionPasswordStrategy(HttpSession JavaDoc session) {
27         this(session, DEFAULT_PASSWORD_LENGTH, DEFAULT_SESSION_KEY_FOR_PASSWORD);
28     }
29     
30     /**
31      * @param session HttpSession used to store the password
32      * @param passwordLength The desired length of the randomly-generated password
33      * @param sessionKeyForPassword The key used to store the password.
34      */

35     public SessionPasswordStrategy(HttpSession JavaDoc session, int passwordLength, String JavaDoc sessionKeyForPassword) {
36         this.session = session;
37         this.passwordLength = passwordLength;
38         this.sessionKeyForPassword = sessionKeyForPassword;
39     }
40     
41     /** implementation of the PasswordStrategy method. */
42     public String JavaDoc getPassword() {
43         String JavaDoc password = (String JavaDoc) session.getAttribute(sessionKeyForPassword);
44         if (password == null) {
45             password = (String JavaDoc) ByteArrayGuard.getRandomString(passwordLength);
46             session.setAttribute(sessionKeyForPassword, password);
47         }
48         return password;
49     }
50     
51     private HttpSession JavaDoc session;
52     private String JavaDoc sessionKeyForPassword;
53     private int passwordLength;
54 }
55
Popular Tags