/** * Copyright 1999-2009 The Pegadi Team * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** * This class holds all data for a session. * * @author HÃ¥vard Wigtil <havardw at pvv.org> * @version $Revision$, $Date$ */ package org.pegadi.server; import java.util.Date; public class Session { /** * The default lenght for a session key. */ private static int KEY_LENGTH = 20; /** * The user ID this session is for. */ private String username; /** * The session key. */ private String key; /** * The session host. */ private String host; /** * The start time of the session. */ private Date start; /** * Create a new session. This will generate a new session key with a call * to the {@link #createKey createKey} method. */ public Session(String user, String host) { this.username = user; this.host = host; this.start = new Date(); this.key = createKey(user, host, start); } /** * Create a new session key. The key is KEY_LENGTH long * * @return A session key * @see #Session * @see #KEY_LENGTH * @param user * @param host * @param start */ private String createKey(String user, String host, Date start) { return user + host + start.getTime(); } /** * Compares two <code>Session</code> objects. The objects are equal if the * keys match, even if they have different users or hosts. * * @param obj The object to compare with. */ public boolean equals(Object obj) { return obj instanceof Session && key.equals(((Session) obj).getKey()); } /** * Returns the session key. * * @return The key. */ public String getKey() { return key; } /** * Returns the sessions user ID. * * @return The ID. */ public String getUsername() { return username; } /** * Returns the user's host. * * @return The host. */ public String getHost() { return host; } /** * Returns the start time for the session. * * @return The time. */ public Date getStart() { return start; } }