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.io.File;
 
 
 
 public class HashedSession extends AbstractSession
 {
     private static final Logger LOG = Log.getLogger(HashedSession.class);
 
     private final HashSessionManager _hashSessionManager;

    
Whether the session has been saved because it has been deemed idle; in which case its attribute map will have been saved and cleared.
 
     private transient boolean _idled = false;

    
Whether there has already been an attempt to save this session which has failed. If there has, there will be no more save attempts for this session. This is to stop the logs being flooded with errors due to serialization failures that are most likely caused by user data stored in the session that is not serializable.
 
     private transient boolean _saveFailed = false;
 
     /* ------------------------------------------------------------- */
     protected HashedSession(HashSessionManager hashSessionManagerHttpServletRequest request)
     {
         super(hashSessionManager,request);
          = hashSessionManager;
     }
 
     /* ------------------------------------------------------------- */
     protected HashedSession(HashSessionManager hashSessionManagerlong createdlong accessedString clusterId)
     {
         super(hashSessionManager,createdaccessedclusterId);
          = hashSessionManager;
     }
 
     /* ------------------------------------------------------------- */
     protected void checkValid()
     {
         if (.!=0)
             deIdle();
         super.checkValid();
     }
     
     /* ------------------------------------------------------------- */
     @Override
     public void setMaxInactiveInterval(int secs)
     {
         super.setMaxInactiveInterval(secs);
             .setScavengePeriod((secs+9)/10);
     }
 
     /* ------------------------------------------------------------ */
     @Override
     protected void doInvalidate()
     throws IllegalStateException
     {
         super.doInvalidate();
         
         // Remove from the disk
         if (.!=null && getId()!=null)
         {
             String id=getId();
             File f = new File(.id);
             f.delete();
         }
     }
    /* ------------------------------------------------------------ */
    synchronized void save(boolean reactivate)
    throws Exception
    {
        // Only idle the session if not already idled and no previous save/idle has failed
        if (!isIdled() && !)
        {
            if (.isDebugEnabled())
                .debug("Saving {} {}",super.getId(),reactivate);
            File file = null;
            FileOutputStream fos = null;
            
            try
            {
                file = new File(.super.getId());
                if (file.exists())
                    file.delete();
                file.createNewFile();
                fos = new FileOutputStream(file);
                willPassivate();
                save(fos);
                if (reactivate)
                    didActivate();
                else
                    clearAttributes();
            }
            catch (Exception e)
            {
                saveFailed(); // We won't try again for this session
                if (fos != null)
                {
                    // Must not leave the file open if the saving failed
                    IO.close(fos);
                    // No point keeping the file if we didn't save the whole session
                    file.delete();
                    throw e;
                }
            }
        }
    }
    /* ------------------------------------------------------------ */
    public synchronized void save(OutputStream os)  throws IOException 
    {
        DataOutputStream out = new DataOutputStream(os);
        out.writeUTF(getClusterId());
        out.writeUTF(getNodeId());
        out.writeLong(getCreationTime());
        out.writeLong(getAccessed());
        
        /* Don't write these out, as they don't make sense to store because they
         * either they cannot be true or their value will be restored in the 
         * Session constructor.
         */
        //out.writeBoolean(_invalid);
        //out.writeBoolean(_doInvalidate);
        //out.writeLong(_maxIdleMs);
        //out.writeBoolean( _newSession);
        out.writeInt(getRequests());
        out.writeInt(getAttributes());
        ObjectOutputStream oos = new ObjectOutputStream(out);
        Enumeration<Stringe=getAttributeNames();
        while(e.hasMoreElements())
        {
            String key=e.nextElement();
            oos.writeUTF(key);
            oos.writeObject(doGet(key));
        }
        oos.close();
    }
    /* ------------------------------------------------------------ */
    public synchronized void deIdle()
    {
        if (isIdled())
        {
            // Access now to prevent race with idling period
            access(System.currentTimeMillis());
            if (.isDebugEnabled())
                .debug("De-idling " + super.getId());
            FileInputStream fis = null;
            try
            {
                File file = new File(.super.getId());
                if (!file.exists() || !file.canRead())
                    throw new FileNotFoundException(file.getName());
                fis = new FileInputStream(file);
                 = false;
                .restoreSession(fisthis);
                didActivate();
                
                // If we are doing period saves, then there is no point deleting at this point 
                if (. == 0)
                    file.delete();
            }
            catch (Exception e)
            {
                .warn("Problem de-idling session " + super.getId(), e);
                IO.close(fis);
                invalidate();
            }
        }
    }
    
    /* ------------------------------------------------------------ */
    
Idle the session to reduce session memory footprint. The session is idled by persisting it, then clearing the session values attribute map and finally setting it to an idled state.
    public synchronized void idle()
    throws Exception
    {
        save(false);
         = true;
    }
    
    /* ------------------------------------------------------------ */
    public synchronized boolean isIdled()
    {
      return ;
    }
    /* ------------------------------------------------------------ */
    public synchronized boolean isSaveFailed()
    {
        return ;
    }
    /* ------------------------------------------------------------ */
    public synchronized void saveFailed()
    {
         = true;
    }
New to GrepCode? Check out our FAQ X