Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  //
  //  ========================================================================
  //  Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
  //  ------------------------------------------------------------------------
  //  All rights reserved. This program and the accompanying materials
  //  are made available under the terms of the Eclipse Public License v1.0
  //  and Apache License v2.0 which accompanies this distribution.
  //
  //      The Eclipse Public License is available at
 //      http://www.eclipse.org/legal/epl-v10.html
 //
 //      The Apache License v2.0 is available at
 //      http://www.opensource.org/licenses/apache2.0.php
 //
 //  You may elect to redistribute this code under either of these licenses.
 //  ========================================================================
 //
 
 package org.eclipse.jetty.server.session;
 
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
 

Implements javax.servlet.http.HttpSession from the javax.servlet package.

 
 @SuppressWarnings("deprecation")
 public abstract class AbstractSession implements AbstractSessionManager.SessionIf
 {
     final static Logger LOG = .;
     
     private final AbstractSessionManager _manager;
     private final String _clusterId// ID unique within cluster
     private final String _nodeId;    // ID unique within node
     private final Map<String,Object_attributes=new HashMap<StringObject>();
     private boolean _idChanged;
     private final long _created;
     private long _cookieSet;
     private long _accessed;         // the time of the last access
     private long _lastAccessed;     // the time of the last access excluding this one
     private boolean _invalid;
     private boolean _doInvalidate;
     private long _maxIdleMs;
     private boolean _newSession;
     private int _requests;
 
 
     
     /* ------------------------------------------------------------- */
     protected AbstractSession(AbstractSessionManager abstractSessionManagerHttpServletRequest request)
     {
          = abstractSessionManager;
         
         =true;
         =System.currentTimeMillis();
         =;
         =;
         =1;
         if (.isDebugEnabled())
             .debug("new session & id "++" "+);
     }
 
     /* ------------------------------------------------------------- */
     protected AbstractSession(AbstractSessionManager abstractSessionManagerlong createdlong accessedString clusterId)
     {
          = abstractSessionManager;
         =created;
         =clusterId;
         =accessed;
         =accessed;
         =1;
         if (.isDebugEnabled())
             .debug("new session "++" "+);
    }
    
    /* ------------------------------------------------------------- */
    
asserts that the session is valid
    protected void checkValid() throws IllegalStateException
    {
        if ()
            throw new IllegalStateException();
    }
    
    /* ------------------------------------------------------------- */
    public AbstractSession getSession()
    {
        return this;
    }
    /* ------------------------------------------------------------- */
    public long getAccessed()
    {
        synchronized (this)
        {
            return ;
        }
    }
    
    /* ------------------------------------------------------------ */
    public Object getAttribute(String name)
    {
        synchronized (this)
        {
            checkValid();
            return .get(name);
        }
    }
    
    /* ------------------------------------------------------------ */
    public int getAttributes()
    {
        synchronized (this)
        {
            checkValid();
            return .size();
        }
    }
    /* ------------------------------------------------------------ */
    @SuppressWarnings({ "unchecked" })
    {
        synchronized (this)
        {
            checkValid();
            List<Stringnames===null?.:new ArrayList<String>(.keySet());
            return Collections.enumeration(names);
        }
    }
    
    /* ------------------------------------------------------------ */
    public Set<StringgetNames()
    {
        synchronized (this)
        { 
            return new HashSet<String>(.keySet());
        }
    }
    /* ------------------------------------------------------------- */
    public long getCookieSetTime()
    {
        return ;
    }
    /* ------------------------------------------------------------- */
    public long getCreationTime() throws IllegalStateException
    {
        return ;
    }
    /* ------------------------------------------------------------ */
    public String getId() throws IllegalStateException
    {
    }
    /* ------------------------------------------------------------- */
    public String getNodeId()
    {
        return ;
    }
    /* ------------------------------------------------------------- */
    public String getClusterId()
    {
        return ;
    }
    /* ------------------------------------------------------------- */
    public long getLastAccessedTime() throws IllegalStateException
    {
        checkValid();
        return ;
    }
    
    /* ------------------------------------------------------------- */
    public void setLastAccessedTime(long time)
    {
         = time;
    }
    /* ------------------------------------------------------------- */
    public int getMaxInactiveInterval()
    {
        checkValid();
        return (int)(/1000);
    }
    /* ------------------------------------------------------------ */
    /*
     * @see javax.servlet.http.HttpSession#getServletContext()
     */
    {
        return .;
    }
    /* ------------------------------------------------------------- */
    @Deprecated
    {
        checkValid();
    }
    /* ------------------------------------------------------------- */
    

Deprecated:
As of Version 2.2, this method is replaced by getAttribute(java.lang.String)
    @Deprecated
    public Object getValue(String namethrows IllegalStateException
    {
        return getAttribute(name);
    }
    /* ------------------------------------------------------------- */
    

Deprecated:
As of Version 2.2, this method is replaced by getAttributeNames()
    @Deprecated
    public String[] getValueNames() throws IllegalStateException
    {
        synchronized(this)
        {
            checkValid();
            if (==null)
                return new String[0];
            String[] a=new String[.size()];
            return (String[]).keySet().toArray(a);
        }
    }
    
    /* ------------------------------------------------------------ */
    protected  Map<String,ObjectgetAttributeMap ()
    {
        return ;
    }
    
    /* ------------------------------------------------------------ */
    protected void addAttributes(Map<String,Objectmap)
    {
        .putAll(map);
    }
    /* ------------------------------------------------------------ */
    protected boolean access(long time)
    {
        synchronized(this)
        {
            if ()
                return false;
            =false;
            =;
            =time;
            if (>0 && >0 &&  +  < time
            {
                invalidate();
                return false;
            }
            ++;
            return true;
        }
    }
    /* ------------------------------------------------------------ */
    protected void complete()
    {
        synchronized(this)
        {
            --;
            if ( && <=0  )
                doInvalidate();
        }
    }
    /* ------------------------------------------------------------- */
    protected void timeout() throws IllegalStateException
    {
        // remove session from context and invalidate other sessions with same ID.
        .removeSession(this,true);
        // Notify listeners and unbind values
        boolean do_invalidate=false;
        synchronized (this)
        {
            if (!)
            {
                if (<=0)
                    do_invalidate=true;
                else
                    =true;
            }
        }
        if (do_invalidate)
            doInvalidate();
    }
    /* ------------------------------------------------------------- */
    public void invalidate() throws IllegalStateException
    {
        // remove session from context and invalidate other sessions with same ID.
        .removeSession(this,true);
        doInvalidate();
    }
    /* ------------------------------------------------------------- */
    protected void doInvalidate() throws IllegalStateException
    {
        try
        {
            .debug("invalidate {}",);
            if (isValid())
                clearAttributes();
        }
        finally
        {
            synchronized (this)
            {
                // mark as invalid
                =true;
            }
        }
    }
    /* ------------------------------------------------------------- */
    public void clearAttributes() 
    {
        while (!=null && .size()>0)
        {
            ArrayList<Stringkeys;
            synchronized(this)
            {
                keys=new ArrayList<String>(.keySet());
            }
            Iterator<Stringiter=keys.iterator();
            while (iter.hasNext())
            {
                String key=(String)iter.next();
                Object value;
                synchronized(this)
                {
                    value=doPutOrRemove(key,null);
                }
                unbindValue(key,value);
                .doSessionAttributeListeners(this,key,value,null);
            }
        } 
        if (!=null)
            .clear();
    }
    
    /* ------------------------------------------------------------- */
    public boolean isIdChanged()
    {
        return ;
    }
    /* ------------------------------------------------------------- */
    public boolean isNew() throws IllegalStateException
    {
        checkValid();
        return ;
    }
    /* ------------------------------------------------------------- */
    

Deprecated:
As of Version 2.2, this method is replaced by setAttribute(java.lang.String,java.lang.Object)
    @Deprecated
    public void putValue(java.lang.String namejava.lang.Object valuethrows IllegalStateException
    {
        setAttribute(name,value);
    }
    /* ------------------------------------------------------------ */
    public void removeAttribute(String name)
    {
        setAttribute(name,null);
    }
    /* ------------------------------------------------------------- */
    

Deprecated:
As of Version 2.2, this method is replaced by removeAttribute(java.lang.String)
    @Deprecated
    public void removeValue(java.lang.String namethrows IllegalStateException
    {
        removeAttribute(name);
    }
    /* ------------------------------------------------------------ */
    protected Object doPutOrRemove(String nameObject value)
    {
        return value==null?.remove(name):.put(name,value);
    }
    /* ------------------------------------------------------------ */
    protected Object doGet(String name)
    {
        return .get(name);
    }
    
    /* ------------------------------------------------------------ */
    public void setAttribute(String nameObject value)
    {
        Object old=null;
        synchronized (this)
        {
            checkValid();
            old=doPutOrRemove(name,value);
        }
        
        if (value==null || !value.equals(old))
        {
            if (old!=null)
                unbindValue(name,old);
            if (value!=null)
                bindValue(name,value);
            .doSessionAttributeListeners(this,name,old,value);
            
        }
    }
    /* ------------------------------------------------------------- */
    public void setIdChanged(boolean changed)
    {
        =changed;
    }
    /* ------------------------------------------------------------- */
    public void setMaxInactiveInterval(int secs)
    {
        =(long)secs*1000L;
    }
    /* ------------------------------------------------------------- */
    @Override
    public String toString()
    {
        return this.getClass().getName()+":"+getId()+"@"+hashCode();
    }
    /* ------------------------------------------------------------- */
    
If value implements HttpSessionBindingListener, call valueBound()
    public void bindValue(java.lang.String nameObject value)
    {
        if (value!=null&&value instanceof HttpSessionBindingListener)
            ((HttpSessionBindingListener)value).valueBound(new HttpSessionBindingEvent(this,name));
    }
    /* ------------------------------------------------------------ */
    public boolean isValid()
    {
        return !;
    }
    /* ------------------------------------------------------------- */
    protected void cookieSet()
    {
        synchronized (this)
        {
            =;
        }
    }
    /* ------------------------------------------------------------ */
    public int getRequests()
    {
        synchronized (this)
        {
            return ;
        }
    }
    /* ------------------------------------------------------------ */
    public void setRequests(int requests)
    {
        synchronized (this)
        {
            =requests;
        }
    }
    
    /* ------------------------------------------------------------- */
    
If value implements HttpSessionBindingListener, call valueUnbound()
    public void unbindValue(java.lang.String nameObject value)
    {
        if (value!=null&&value instanceof HttpSessionBindingListener)
            ((HttpSessionBindingListener)value).valueUnbound(new HttpSessionBindingEvent(this,name));
    }
    /* ------------------------------------------------------------- */
    public void willPassivate()
    {
        synchronized(this)
        {
            HttpSessionEvent event = new HttpSessionEvent(this);
            for (Iterator<Objectiter = .values().iterator(); iter.hasNext();)
            {
                Object value = iter.next();
                if (value instanceof HttpSessionActivationListener)
                {
                    HttpSessionActivationListener listener = (HttpSessionActivationListenervalue;
                    listener.sessionWillPassivate(event);
                }
            }
        }
    }
    /* ------------------------------------------------------------- */
    public void didActivate()
    {
        synchronized(this)
        {
            HttpSessionEvent event = new HttpSessionEvent(this);
            for (Iterator<Objectiter = .values().iterator(); iter.hasNext();)
            {
                Object value = iter.next();
                if (value instanceof HttpSessionActivationListener)
                {
                    HttpSessionActivationListener listener = (HttpSessionActivationListenervalue;
                    listener.sessionDidActivate(event);
                }
            }
        }
    }
    
    
New to GrepCode? Check out our FAQ X