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;
 
 
 
 
 public abstract class AbstractSessionIdManager extends AbstractLifeCycle implements SessionIdManager
 {
     private static final Logger LOG = Log.getLogger(AbstractSessionIdManager.class);
 
     private final static String __NEW_SESSION_ID="org.eclipse.jetty.server.newSessionId";  
     
     protected Random _random;
     protected boolean _weakRandom;
     protected String _workerName;
     
     /* ------------------------------------------------------------ */
     public AbstractSessionIdManager()
     {
     }
     
     /* ------------------------------------------------------------ */
     public AbstractSessionIdManager(Random random)
     {
         =random;
     }
 
 
     /* ------------------------------------------------------------ */
    
Get the workname. If set, the workername is dot appended to the session ID and can be used to assist session affinity in a load balancer.

Returns:
String or null
 
     public String getWorkerName()
     {
         return ;
     }
 
     /* ------------------------------------------------------------ */
    
Set the workname. If set, the workername is dot appended to the session ID and can be used to assist session affinity in a load balancer.

Parameters:
workerName
 
     public void setWorkerName(String workerName)
     {
         if (workerName.contains("."))
             throw new IllegalArgumentException("Name cannot contain '.'");
         =workerName;
     }
 
     /* ------------------------------------------------------------ */
     public Random getRandom()
     {
         return ;
     }
 
     /* ------------------------------------------------------------ */
     public synchronized void setRandom(Random random)
     {
         =random;
         =false;
     }
     
     /* ------------------------------------------------------------ */
    
 
     public String newSessionId(HttpServletRequest requestlong created)
     {
        synchronized (this)
        {
            if (request!=null)
            {
                // A requested session ID can only be used if it is in use already.
                String requested_id=request.getRequestedSessionId();
                if (requested_id!=null)
                {
                    String cluster_id=getClusterId(requested_id);
                    if (idInUse(cluster_id))
                        return cluster_id;
                }
                // Else reuse any new session ID already defined for this request.
                String new_id=(String)request.getAttribute();
                if (new_id!=null&&idInUse(new_id))
                    return new_id;
            }
            
            // pick a new unique ID!
            String id=null;
            while (id==null||id.length()==0||idInUse(id))
            {
                long r0=
                ?(hashCode()^Runtime.getRuntime().freeMemory()^.nextInt()^(((long)request.hashCode())<<32))
                :.nextLong();
                if (r0<0)
                    r0=-r0;
                long r1=
                ?(hashCode()^Runtime.getRuntime().freeMemory()^.nextInt()^(((long)request.hashCode())<<32))
                :.nextLong();
                if (r1<0)
                    r1=-r1;
                id=Long.toString(r0,36)+Long.toString(r1,36);
                
                //add in the id of the node to ensure unique id across cluster
                //NOTE this is different to the node suffix which denotes which node the request was received on
                if (!=null)
                    id= + id;
            }
            request.setAttribute(,id);
            return id;
        }
    }
    /* ------------------------------------------------------------ */
    @Override
    protected void doStart() throws Exception
    {
       initRandom();
    }
    
    /* ------------------------------------------------------------ */
    @Override
    protected void doStop() throws Exception
    {
    }
    
    /* ------------------------------------------------------------ */
    
Set up a random number generator for the sessionids. By preference, use a SecureRandom but allow to be injected.
    public void initRandom ()
    {
        if (==null)
        {
            try
            {
                =new SecureRandom();
            }
            catch (Exception e)
            {
                .warn("Could not generate SecureRandom for session-id randomness",e);
                =new Random();
                =true;
            }
        }
        else
            .setSeed(.nextLong()^System.currentTimeMillis()^hashCode()^Runtime.getRuntime().freeMemory()); 
    }
    
    
New to GrepCode? Check out our FAQ X