001: /*
002: * This file is part of the Echo Web Application Framework (hereinafter "Echo").
003: * Copyright (C) 2002-2005 NextApp, Inc.
004: *
005: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
006: *
007: * The contents of this file are subject to the Mozilla Public License Version
008: * 1.1 (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: * http://www.mozilla.org/MPL/
011: *
012: * Software distributed under the License is distributed on an "AS IS" basis,
013: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
014: * for the specific language governing rights and limitations under the
015: * License.
016: *
017: * Alternatively, the contents of this file may be used under the terms of
018: * either the GNU General Public License Version 2 or later (the "GPL"), or
019: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
020: * in which case the provisions of the GPL or the LGPL are applicable instead
021: * of those above. If you wish to allow use of your version of this file only
022: * under the terms of either the GPL or the LGPL, and not to allow others to
023: * use your version of this file under the terms of the MPL, indicate your
024: * decision by deleting the provisions above and replace them with the notice
025: * and other provisions required by the GPL or the LGPL. If you do not delete
026: * the provisions above, a recipient may use your version of this file under
027: * the terms of any one of the MPL, the GPL or the LGPL.
028: */
029:
030: package echo2example.chatserver;
031:
032: import java.util.HashMap;
033: import java.util.Iterator;
034: import java.util.Map;
035:
036: /**
037: * A store of active users.
038: */
039: public class UserManager {
040:
041: /**
042: * User timeout (15min * 60sec/min * 1000ms/sec).
043: */
044: private static final int TIMEOUT_MS = 15 * 60 * 1000;
045:
046: /**
047: * User name to <code>User</code> mapping.
048: */
049: private Map userMap = new HashMap();
050:
051: /**
052: * Attempts to add a new user.
053: *
054: * @param userName the name of the user
055: * @return an authentication token, if the user name is available or
056: * null if it is not
057: */
058: public String add(String userName) {
059: // Ensure no leading/trailing spaces are present in user name.
060: if (userName.length() != userName.trim().length()) {
061: return null;
062: }
063:
064: // Ensure user name is at least two characters in length.
065: if (userName.length() < 2) {
066: return null;
067: }
068:
069: User user = null;
070: synchronized (userMap) {
071: // Ensure user name is available.
072: if (userMap.containsKey(userName)) {
073: return null;
074: }
075:
076: user = new User(userName);
077: userMap.put(userName, user);
078: }
079: return user.getAuthToken();
080: }
081:
082: /**
083: * Authenticates a user based on user name and an authentication token.
084: *
085: * @param userName the user's name
086: * @param authToken the authentication token
087: * @return true if the user is authenticated
088: */
089: public boolean authenticate(String userName, String authToken) {
090: User user = (User) userMap.get(userName);
091: if (user == null) {
092: return false;
093: }
094: return user.authenticate(authToken);
095: }
096:
097: /**
098: * Performs clean-up operations, i.e., removing stale users.
099: */
100: public void purge() {
101: long time = System.currentTimeMillis();
102: synchronized (userMap) {
103: Iterator userNameIt = userMap.keySet().iterator();
104: while (userNameIt.hasNext()) {
105: String userName = (String) userNameIt.next();
106: User user = (User) userMap.get(userName);
107: if (time - user.getLastActionTime() > TIMEOUT_MS) {
108: userNameIt.remove();
109: }
110: }
111: }
112: }
113:
114: /**
115: * Removes the specified user.
116: *
117: * @param userName the user's name
118: */
119: public void remove(String userName) {
120: synchronized (userMap) {
121: userMap.remove(userName);
122: }
123: }
124:
125: /**
126: * @see java.lang.Object#toString()
127: */
128: public String toString() {
129: StringBuffer out = new StringBuffer();
130: synchronized (userMap) {
131: Iterator it = userMap.keySet().iterator();
132: while (it.hasNext()) {
133: out.append(it.next());
134: if (it.hasNext()) {
135: out.append(", ");
136: }
137: }
138: }
139: return out.toString();
140: }
141: }
|