home *** CD-ROM | disk | FTP | other *** search
Java Source | 2000-04-03 | 7.0 KB | 252 lines |
- /*
- ** Sambar Server JAVA Engine Session Implementation
- **
- ** Confidential Property of Tod Sambar
- ** (c) Copyright Tod Sambar 1999
- ** All rights reserved.
- */
- package com.sambar.javaeng;
-
- import java.util.*;
- import javax.servlet.http.*;
-
- public class SambarSession implements HttpSession
- {
- protected long creationTime;
- protected long lastAccessTime;
- protected int inactiveInterval;
- protected String id;
- private boolean isNew;
- private boolean valid;
- private Hashtable sessionData;
- private SambarContext scontext;
-
- public SambarSession(String id, SambarContext scontext, int sessionTimeout)
- {
- this.valid = true;
- this.id = id;
- this.scontext = scontext;
- this.sessionData = new Hashtable();
- this.creationTime = System.currentTimeMillis();
- this.lastAccessTime = creationTime;
- this.isNew = true;
- this.inactiveInterval = sessionTimeout;
- }
-
- /*
- ** Returns the identifier assigned to this session. An HttpSession's
- ** identifier is a unique string that is created and maintained by
- ** HttpSessionContext.
- */
- public synchronized String getId()
- {
- checkState();
- return id;
- }
-
- public int getMaxInactiveInterval()
- {
- return inactiveInterval;
- }
-
- public void setMaxInactiveInterval(int inactiveInterval)
- {
- this.inactiveInterval = inactiveInterval;
- }
-
- /*
- ** Returns the context in which this session is bound.
- */
- public synchronized HttpSessionContext getSessionContext()
- {
- checkState();
- return scontext;
- }
-
- /*
- ** Returns the time at which this session representation was created,
- ** in milliseconds since midnight, January 1, 1970 UTC.
- */
- public synchronized long getCreationTime()
- {
- checkState();
- return creationTime;
- }
-
- /*
- ** Returns the last time the client sent a request carrying the identifier
- ** assigned to the session. Time is expressed
- ** as milliseconds since midnight, January 1,
- ** 1970 UTC.
- ** Application level operations, such as getting or setting a value
- ** associated with the session, does not affect the access time.
- */
- public synchronized long getLastAccessedTime()
- {
- checkState();
- return lastAccessTime;
- }
-
- /*
- ** Causes this representation of the session to be invalidated and removed
- ** from its context.
- */
- public synchronized void invalidate()
- {
- checkState();
- valid = false;
-
- // deliver HttpSessionBindingEvent to all values that want it
- Enumeration namesEnum;
- Object name, value;
-
- namesEnum = sessionData.keys();
- while (namesEnum.hasMoreElements())
- {
- name = namesEnum.nextElement();
- value = sessionData.get(name);
- if (value instanceof HttpSessionBindingListener)
- {
- HttpSessionBindingListener listener =
- (HttpSessionBindingListener)value;
- listener.valueUnbound(new HttpSessionBindingEvent(this,
- (String) name));
- }
- }
-
- scontext.removeSession(this);
- }
-
- /*
- ** Binds the specified object into the session's application layer data
- ** with the given name. Any existing binding with the same name is
- ** replaced. New (or existing) values that implement the
- ** HttpSessionBindingListener interface will call its
- ** valueBound() method.
- */
- public synchronized void putValue(String name, Object value)
- {
- checkState();
- removeValue(name);
- sessionData.put(name, value);
- if (value instanceof HttpSessionBindingListener)
- {
- HttpSessionBindingListener listener =
- (HttpSessionBindingListener)value;
- listener.valueBound(new HttpSessionBindingEvent(this, name));
- }
- }
-
- /*
- ** Returns the object bound to the given name in the session's
- ** application layer data. Returns null if there is no such binding.
- */
- public synchronized Object getValue(String name)
- {
- checkState();
- return sessionData.get(name);
- }
-
- /*
- ** Removes the object bound to the given name in the session's
- ** application layer data. Does nothing if there is no object
- ** bound to the given name. The value that implements the
- ** HttpSessionBindingListener interface will call its
- ** valueUnbound() method.
- */
- public synchronized void removeValue(String name)
- {
- checkState();
- Object value = sessionData.remove(name);
- if (value instanceof HttpSessionBindingListener)
- {
- HttpSessionBindingListener listener =
- (HttpSessionBindingListener)value;
- listener.valueUnbound(new HttpSessionBindingEvent(this, name));
- }
- }
-
- /*
- ** Returns an array of the names of all the application layer
- ** data objects bound into the session. For example, if you want to delete
- ** all of the data objects bound into the session, use this method to
- ** obtain their names.
- */
- public synchronized String[] getValueNames()
- {
- checkState();
- Vector buf = new Vector();
- Enumeration namesEnum = sessionData.keys();
- while (namesEnum.hasMoreElements())
- buf.addElement(namesEnum.nextElement());
-
- String[] names = new String[buf.size()];
- buf.copyInto(names);
- return names;
- }
-
- /*
- ** A session is considered to be "new" if it has been created by the server,
- ** but the client has not yet acknowledged joining the session. For example,
- ** if the server supported only cookie-based sessions and the client had
- ** completely disabled the use of cookies, then calls to
- ** HttpServletRequest.getSession() would
- ** always return "new" sessions.
- */
- public synchronized boolean isNew()
- {
- checkState();
- return isNew;
- }
-
- /*
- ** Has the session been invalidated.
- */
- public boolean isValid()
- {
- return valid;
- }
-
- /*
- ** Tells the session that it has been accessed
- */
- public synchronized void access()
- {
- lastAccessTime = System.currentTimeMillis();
- isNew = false;
- }
-
- /*
- ** Throws an IllegalStateException when the session is no longer valid.
- */
- private void checkState()
- {
- if (!valid)
- {
- throw new IllegalStateException("Session " + id +
- " has been invalidated.");
- }
- }
-
- public Object getAttribute(String name)
- {
- return getValue(name);
- }
-
- public Enumeration getAttributeNames()
- {
- checkState();
- return sessionData.keys();
- }
-
- public void setAttribute(String name, Object object)
- {
- putValue(name, object);
- }
-
- public void removeAttribute(String name)
- {
- removeValue(name);
- }
- }
-