Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  //
  //  ========================================================================
  //  Copyright (c) 1995-2012 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.Map;
 import java.util.Set;
 
 
 
 /* ------------------------------------------------------------ */
HashSessionIdManager. An in-memory implementation of the session ID manager.
 
 {
     private final Map<StringSet<WeakReference<HttpSession>>> _sessions = new HashMap<StringSet<WeakReference<HttpSession>>>();
 
     /* ------------------------------------------------------------ */
     public HashSessionIdManager()
     {
     }
 
     /* ------------------------------------------------------------ */
     public HashSessionIdManager(Random random)
     {
         super(random);
     }
 
     /* ------------------------------------------------------------ */
    

Returns:
Collection of String session IDs
 
     public Collection<StringgetSessions()
     {
         return Collections.unmodifiableCollection(.keySet());
     }
     
     /* ------------------------------------------------------------ */
    

Returns:
Collection of Sessions for the passed session ID
 
     public Collection<HttpSessiongetSession(String id)
     {
         ArrayList<HttpSessionsessions = new ArrayList<HttpSession>();
         Set<WeakReference<HttpSession>> refs =.get(id);
         if (refs!=null)
         {
             for (WeakReference<HttpSessionrefrefs)
             {
                 HttpSession session = ref.get();
                 if (session!=null)
                     sessions.add(session);
             }
         }
         return sessions;
     }
     /* ------------------------------------------------------------ */
    
Get the session ID with any worker ID.

Parameters:
clusterId
request
Returns:
sessionId plus any worker ID.
 
     public String getNodeId(String clusterId,HttpServletRequest request
     {
         // used in Ajp13Parser
         String worker=request==null?null:(String)request.getAttribute("org.eclipse.jetty.ajp.JVMRoute");
         if (worker!=null
             return clusterId+'.'+worker
         
         if (!=null
             return clusterId+'.'+;
       
        return clusterId;
    }
    /* ------------------------------------------------------------ */
    
Get the session ID without any worker ID.

Parameters:
nodeId the node id
Returns:
sessionId without any worker ID.
    public String getClusterId(String nodeId
    {
        int dot=nodeId.lastIndexOf('.');
        return (dot>0)?nodeId.substring(0,dot):nodeId;
    }
    
    /* ------------------------------------------------------------ */
    @Override
    protected void doStart() throws Exception
    {        
        super.doStart();
    }
    /* ------------------------------------------------------------ */
    @Override
    protected void doStop() throws Exception
    {
        .clear(); 
        super.doStop();
    }
    /* ------------------------------------------------------------ */
    
    public boolean idInUse(String id)
    {
        synchronized (this)
        {
            return .containsKey(id);
        }
    }
    /* ------------------------------------------------------------ */
    
    public void addSession(HttpSession session)
    {
        String id = getClusterId(session.getId());
        WeakReference<HttpSessionref = new WeakReference<HttpSession>(session);
        
        synchronized (this)
        {
            Set<WeakReference<HttpSession>> sessions = .get(id);
            if (sessions==null)
            {
                sessions=new HashSet<WeakReference<HttpSession>>();
                .put(id,sessions);
            }
            sessions.add(ref);
        }
    }
    /* ------------------------------------------------------------ */
    
    public void removeSession(HttpSession session)
    {
        String id = getClusterId(session.getId());
        
        synchronized (this)
        {
            Collection<WeakReference<HttpSession>> sessions = .get(id);
            if (sessions!=null)
            {
                for (Iterator<WeakReference<HttpSession>> iter = sessions.iterator(); iter.hasNext();)
                {
                    WeakReference<HttpSessionref = iter.next();
                    HttpSession s=ref.get();
                    if (s==null)
                    {
                        iter.remove();
                        continue;
                    }
                    if (s==session)
                    {
                        iter.remove();
                        break;
                    }
                }
                if (sessions.isEmpty())
                    .remove(id);
            }
        }
    }
    /* ------------------------------------------------------------ */
    
    public void invalidateAll(String id)
    {
        Collection<WeakReference<HttpSession>> sessions;
        synchronized (this)
        {
            sessions = .remove(id);
        }
        
        if (sessions!=null)
        {
            for (WeakReference<HttpSessionrefsessions)
            {
                AbstractSession session=(AbstractSession)ref.get();
                if (session!=null && session.isValid())
                    session.invalidate();
            }
            sessions.clear();
        }
    }
New to GrepCode? Check out our FAQ X