/*
* SessionObject.java
*
*
* Copyright (c) 2003 Rimfaxe ApS (www.rimfaxe.com).
* All rights reserved.
*
* This package is written by Lars Andersen <lars@rimfaxe.com>
* and licensed by Rimfaxe ApS.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by Rimfaxe ApS
(www.rimfaxe.com)"
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "Rimfaxe", "Rimfaxe Software", "Lars Andersen" and
* "Rimfaxe WebServer" must not be used to endorse or promote products
* derived from this software without prior written permission. For written
* permission, please contact info@rimfaxe.com
*
* 5. Products derived from this software may not be called "Rimfaxe"
* nor may "Rimfaxe" appear in their names without prior written
* permission of the Rimfaxe ApS.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/
package com.rimfaxe.webserver.servletapi.session;
import java.util.*;
import com.rimfaxe.webserver.servletapi.RimfaxeHttpSession;
import com.rimfaxe.webserver.VirtualHost;
/**
*
* @author Lars Andersen
*/
public class SessionStore
{
HashMap hmap = new HashMap();
//ArrayList arraylist = new ArrayList();
VirtualHost vhost;
int cookiecount = 0;
/** Creates a new instance of SessionObject */
public SessionStore(VirtualHost vhost)
{
this.vhost = vhost;
}
public void removeSessionObject(String id)
{
hmap.remove(id);
}
public int getSize() { return hmap.size(); }
public String getNewCookieValue()
{
com.rimfaxe.util.Calendar cal = new com.rimfaxe.util.Calendar();
cookiecount++;
String res = "rwsid"+cal.getTimeInMillisSpecial()+cookiecount;
if (cookiecount>9999999) cookiecount=0;
return res;
}
public void putSessionObject(String cookiestr, RimfaxeHttpSession session)
{
Object obj = hmap.get(cookiestr);
if (obj==null)
{
session.setID(cookiestr);
hmap.put(cookiestr, session);
//arraylist.add(session);
}
}
public synchronized RimfaxeHttpSession getSessionObject(String cookiestr)
{
RimfaxeHttpSession obj = (RimfaxeHttpSession) hmap.get(cookiestr);
if (obj!=null)
{
if (obj.hasExpired())
{
hmap.remove(cookiestr);
return null;
}
obj.setLastAccessedTime();
return obj;
}
// Once in a while, do some cleaning up!
cleanup();
//
return null;
}
int cleaning_interval = 10;
int clean = 0;
public void cleanup()
{
if (clean<cleaning_interval)
{
clean++;
return;
}
else
{
clean=0;
}
Iterator iter = hmap.keySet().iterator();
while (iter.hasNext())
{
String key = (String) iter.next();
RimfaxeHttpSession test_obj = (RimfaxeHttpSession) hmap.get(key);
if (test_obj.hasExpired()) hmap.remove(key);
}
System.out.println("Size of session store : "+hmap.size());
}
}