001: /*
002: * Token.java
003: *
004: * Brazil project web application Framework,
005: * export version: 1.1
006: * Copyright (c) 1998-1999 Sun Microsystems, Inc.
007: *
008: * Sun Public License Notice
009: *
010: * The contents of this file are subject to the Sun Public License Version
011: * 1.0 (the "License"). You may not use this file except in compliance with
012: * the License. A copy of the License is included as the file "license.terms",
013: * and also available at http://www.sun.com/
014: *
015: * The Original Code is from:
016: * Brazil project web application Framework release 1.1.
017: * The Initial Developer of the Original Code is: suhler.
018: * Portions created by suhler are Copyright (C) Sun Microsystems, Inc.
019: * All Rights Reserved.
020: *
021: * Contributor(s): suhler.
022: *
023: * Version: 1.4
024: * Created by suhler on 98/09/24
025: * Last modified by suhler on 99/03/30 09:29:24
026: */
027:
028: package sunlabs.brazil.handler;
029:
030: import java.util.Hashtable;
031: import java.util.StringTokenizer;
032: import java.util.Vector;
033:
034: /**
035: * Manage supplier.net session tokens.
036: * Each token represents a "credentials lease" for a sun.net session.
037: * Used by {@link SunNetAuthHandler}
038: */
039:
040: public class Token {
041: static Hashtable tokens; // where to store the tokens
042: long ctime; // token creation time
043: long atime; // time of last use
044: long now; // The current time
045: int uses; // number of times token is used
046: String id; // ID for this token (e.g. the smart card serial #)
047: Vector roles; // The roles affiliated with this token
048: Object arg; // client data pointer
049:
050: /**
051: * Initialize the hash table to hold all of the tokens
052: */
053:
054: static {
055: tokens = new Hashtable();
056: }
057:
058: /**
059: * Create a new token, put it in the hash table .
060: * @param cookie The reference for this token (e.g. web cookie)
061: * This is called by getToken.
062: */
063:
064: private Token(String cookie) {
065: now = System.currentTimeMillis();
066: ctime = now;
067: id = null;
068: roles = null;
069: uses = 0;
070: arg = null;
071: tokens.put(cookie, this );
072: }
073:
074: /**
075: * Find the token assosiated with this cookie.
076: * Create a new one if it doesn't exist
077: */
078:
079: public static Token getToken(String cookie) {
080: Token result = (Token) tokens.get(cookie);
081: if (result == null) {
082: result = new Token(cookie);
083: }
084: result.atime = result.now;
085: result.now = System.currentTimeMillis();
086: result.uses++;
087: return result;
088: }
089:
090: /**
091: * Check to see if a token exists
092: * @return true if there was a token
093: */
094:
095: public static boolean haveToken(String cookie) {
096: return tokens.containsKey(cookie);
097: }
098:
099: /**
100: * Remove the token associated with this cookie
101: * @return true if there was a token to remove
102: */
103:
104: public static boolean removeToken(String cookie) {
105: return (cookie != null && tokens.remove(cookie) != null);
106: }
107:
108: /**
109: * Set the id and roles associated with this token
110: * @param id The unique identifier for this token
111: * @param roles The white space delimited set of roles
112: */
113:
114: public void setToken(String id, String roles) {
115: this .id = id;
116: StringTokenizer st = new StringTokenizer(roles);
117: this .roles = new Vector(st.countTokens());
118: for (int i = 0; i < this .roles.capacity(); i++) {
119: this .roles.addElement(st.nextToken());
120: }
121: }
122:
123: /**
124: * Get the unique id associated with this token.
125: */
126:
127: public String getId() {
128: return this .id;
129: }
130:
131: /**
132: * Get the roles associated with this token
133: */
134:
135: public Vector getRoles() {
136: return this .roles;
137: }
138:
139: /**
140: * Get the number of uses of this token
141: */
142:
143: public int getUses() {
144: return this .uses;
145: }
146:
147: /**
148: * Get the total age of this token in seconds
149: */
150:
151: public int getAge() {
152: return (int) ((this .now - this .ctime) / 1000);
153: }
154:
155: /**
156: * Get the idle time of this token in seconds
157: */
158:
159: public int getIdle() {
160: return (int) ((this .now - this .atime) / 1000);
161: }
162:
163: /**
164: * Set the client data
165: */
166:
167: public void setArg(Object arg) {
168: this .arg = arg;
169: }
170:
171: /**
172: * Get the client data
173: */
174:
175: public Object getArg() {
176: return this .arg;
177: }
178:
179: /**
180: * Print a prettier version of this instance
181: */
182:
183: public String toString() {
184: String list = "";
185: return "times: " + ctime / 1000 + " - " + atime / 1000 + " - "
186: + now / 1000 + " total tokens: " + tokens.size()
187: + " uses: " + uses + " id: " + id + " roles:" + roles;
188: }
189: }
|