home *** CD-ROM | disk | FTP | other *** search
/ Australian Personal Computer 2000 November / APCD25112k.iso / feature / webserv / SAMBAR43.ZIP / _SETUP.1 / javaeng.jar / com / sambar / javaeng / SambarSession.java < prev    next >
Encoding:
Java Source  |  2000-04-03  |  7.0 KB  |  252 lines

  1. /*
  2. **         Sambar Server JAVA Engine Session Implementation
  3. **
  4. **        Confidential Property of Tod Sambar
  5. **        (c) Copyright Tod Sambar 1999
  6. **        All rights reserved.
  7. */
  8. package com.sambar.javaeng;
  9.  
  10. import java.util.*;
  11. import javax.servlet.http.*;
  12.  
  13. public class SambarSession implements HttpSession 
  14. {
  15.     protected long             creationTime;
  16.     protected long             lastAccessTime;
  17.     protected int            inactiveInterval;
  18.     protected String         id;
  19.     private boolean         isNew;
  20.     private boolean         valid;
  21.     private Hashtable         sessionData;
  22.     private SambarContext     scontext;
  23.  
  24.     public SambarSession(String id, SambarContext scontext, int sessionTimeout) 
  25.     {
  26.         this.valid = true;
  27.         this.id = id;
  28.         this.scontext = scontext;
  29.         this.sessionData = new Hashtable();
  30.         this.creationTime = System.currentTimeMillis();
  31.         this.lastAccessTime = creationTime;
  32.         this.isNew = true;
  33.         this.inactiveInterval = sessionTimeout;
  34.     }
  35.  
  36.     /*
  37.     ** Returns the identifier assigned to this session. An HttpSession's
  38.     ** identifier is a unique string that is created and maintained by
  39.     ** HttpSessionContext.
  40.     */
  41.     public synchronized String getId() 
  42.     {
  43.         checkState();
  44.         return id;
  45.     }
  46.  
  47.     public int getMaxInactiveInterval() 
  48.     {
  49.         return inactiveInterval;
  50.     }
  51.  
  52.     public void setMaxInactiveInterval(int inactiveInterval) 
  53.     {
  54.         this.inactiveInterval = inactiveInterval;
  55.     }
  56.  
  57.     /*
  58.     ** Returns the context in which this session is bound.
  59.     */
  60.     public synchronized HttpSessionContext getSessionContext() 
  61.     {
  62.         checkState();
  63.         return scontext;
  64.     }
  65.  
  66.     /*
  67.     ** Returns the time at which this session representation was created,
  68.     ** in milliseconds since midnight, January 1, 1970 UTC.
  69.     */
  70.     public synchronized long getCreationTime() 
  71.     {
  72.         checkState();
  73.         return creationTime;
  74.     }
  75.  
  76.     /*
  77.     ** Returns the last time the client sent a request carrying the identifier
  78.     ** assigned to the session. Time is expressed
  79.     ** as milliseconds since midnight, January 1,
  80.     ** 1970 UTC.
  81.     ** Application level operations, such as getting or setting a value
  82.     ** associated with the session, does not affect the access time.
  83.     */
  84.     public synchronized long getLastAccessedTime() 
  85.     {
  86.         checkState();
  87.         return lastAccessTime;
  88.     }
  89.  
  90.     /*
  91.     ** Causes this representation of the session to be invalidated and removed
  92.     ** from its context.
  93.     */
  94.     public synchronized void invalidate() 
  95.     {
  96.         checkState();
  97.         valid = false;
  98.  
  99.         // deliver HttpSessionBindingEvent to all values that want it
  100.         Enumeration namesEnum;
  101.         Object name, value;
  102.  
  103.         namesEnum = sessionData.keys();
  104.         while (namesEnum.hasMoreElements()) 
  105.         {
  106.             name = namesEnum.nextElement();
  107.             value = sessionData.get(name);
  108.             if (value instanceof HttpSessionBindingListener) 
  109.             {
  110.                 HttpSessionBindingListener listener =
  111.                     (HttpSessionBindingListener)value;
  112.                 listener.valueUnbound(new HttpSessionBindingEvent(this,
  113.                     (String) name));
  114.             }
  115.         }
  116.  
  117.         scontext.removeSession(this);
  118.     }
  119.  
  120.     /*
  121.     ** Binds the specified object into the session's application layer data
  122.     ** with the given name.  Any existing binding with the same name is
  123.     ** replaced.  New (or existing) values that implement the
  124.     ** HttpSessionBindingListener interface will call its
  125.     ** valueBound() method.
  126.     */
  127.     public synchronized void putValue(String name, Object value) 
  128.     {
  129.         checkState();
  130.         removeValue(name);
  131.         sessionData.put(name, value);
  132.         if (value instanceof HttpSessionBindingListener) 
  133.         {
  134.             HttpSessionBindingListener listener =
  135.                 (HttpSessionBindingListener)value;
  136.             listener.valueBound(new HttpSessionBindingEvent(this, name));
  137.         }
  138.     }
  139.  
  140.     /*
  141.     ** Returns the object bound to the given name in the session's
  142.     ** application layer data.  Returns null if there is no such binding.
  143.     */
  144.     public synchronized Object getValue(String name) 
  145.     {
  146.         checkState();
  147.         return sessionData.get(name);
  148.     }
  149.  
  150.     /*
  151.     ** Removes the object bound to the given name in the session's
  152.     ** application layer data.  Does nothing if there is no object
  153.     ** bound to the given name.  The value that implements the
  154.     ** HttpSessionBindingListener interface will call its
  155.     ** valueUnbound() method.
  156.     */
  157.     public synchronized void removeValue(String name) 
  158.     {
  159.         checkState();
  160.         Object value = sessionData.remove(name);
  161.         if (value instanceof HttpSessionBindingListener) 
  162.         {
  163.             HttpSessionBindingListener listener =
  164.                 (HttpSessionBindingListener)value;
  165.             listener.valueUnbound(new HttpSessionBindingEvent(this, name));
  166.         }
  167.     }
  168.  
  169.     /*
  170.     ** Returns an array of the names of all the application layer
  171.     ** data objects bound into the session. For example, if you want to delete
  172.     ** all of the data objects bound into the session, use this method to
  173.     ** obtain their names.
  174.     */
  175.     public synchronized String[] getValueNames() 
  176.     {
  177.         checkState();
  178.         Vector buf = new Vector();
  179.         Enumeration namesEnum = sessionData.keys();
  180.         while (namesEnum.hasMoreElements())
  181.             buf.addElement(namesEnum.nextElement());
  182.  
  183.         String[] names = new String[buf.size()];
  184.         buf.copyInto(names);
  185.         return names;
  186.     }
  187.  
  188.     /*
  189.     ** A session is considered to be "new" if it has been created by the server,
  190.     ** but the client has not yet acknowledged joining the session. For example,
  191.     ** if the server supported only cookie-based sessions and the client had
  192.     ** completely disabled the use of cookies, then calls to
  193.     ** HttpServletRequest.getSession() would
  194.     ** always return "new" sessions.
  195.     */
  196.     public synchronized boolean isNew() 
  197.     {
  198.         checkState();
  199.         return isNew;
  200.     }
  201.  
  202.     /*
  203.     ** Has the session been invalidated.
  204.     */
  205.     public boolean isValid() 
  206.     {
  207.         return valid;
  208.     }
  209.  
  210.     /*
  211.     ** Tells the session that it has been accessed
  212.     */
  213.     public synchronized void access() 
  214.     {
  215.         lastAccessTime = System.currentTimeMillis();
  216.         isNew = false;
  217.     }
  218.  
  219.     /*
  220.     ** Throws an IllegalStateException when the session is no longer valid.
  221.     */
  222.     private void checkState() 
  223.     {
  224.         if (!valid)
  225.         {
  226.             throw new IllegalStateException("Session " + id + 
  227.                 " has been invalidated.");
  228.         }
  229.     }
  230.  
  231.     public Object getAttribute(String name)
  232.     {
  233.         return getValue(name);
  234.     }
  235.  
  236.     public Enumeration getAttributeNames() 
  237.     {
  238.         checkState();
  239.         return sessionData.keys();
  240.     }
  241.  
  242.     public void setAttribute(String name, Object object) 
  243.     {
  244.         putValue(name, object);
  245.     }
  246.  
  247.     public void removeAttribute(String name) 
  248.     {
  249.         removeValue(name);
  250.     }
  251. }
  252.