Class FileSessionDataStore

  • All Implemented Interfaces:
    SessionDataMap, SessionDataStore, org.eclipse.jetty.util.component.Container, org.eclipse.jetty.util.component.Destroyable, org.eclipse.jetty.util.component.Dumpable, org.eclipse.jetty.util.component.Dumpable.DumpableContainer, org.eclipse.jetty.util.component.LifeCycle

    @ManagedObject
    public class FileSessionDataStore
    extends AbstractSessionDataStore
    FileSessionDataStore A file-based store of session data.
    • Nested Class Summary

      • Nested classes/interfaces inherited from class org.eclipse.jetty.util.component.AbstractLifeCycle

        org.eclipse.jetty.util.component.AbstractLifeCycle.AbstractLifeCycleListener
      • Nested classes/interfaces inherited from interface org.eclipse.jetty.util.component.Container

        org.eclipse.jetty.util.component.Container.InheritedListener, org.eclipse.jetty.util.component.Container.Listener
      • Nested classes/interfaces inherited from interface org.eclipse.jetty.util.component.Dumpable

        org.eclipse.jetty.util.component.Dumpable.DumpableContainer
      • Nested classes/interfaces inherited from interface org.eclipse.jetty.util.component.LifeCycle

        org.eclipse.jetty.util.component.LifeCycle.Listener
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean delete​(java.lang.String id)
      Delete a session
      boolean deleteFile​(java.lang.String filename)
      Delete the file associated with a session
      java.util.Set<java.lang.String> doGetExpired​(java.util.Set<java.lang.String> candidates)
      Check to see which sessions have expired.
      SessionData doLoad​(java.lang.String id)
      Load the session from persistent store.
      protected void doStart()  
      protected void doStop()  
      void doStore​(java.lang.String id, SessionData data, long lastSaveTime)
      Store the session data persistently.
      boolean exists​(java.lang.String id)
      Test if data exists for a given session id.
      protected java.lang.String getContextFromFilename​(java.lang.String filename)  
      protected long getExpiryFromFilename​(java.lang.String filename)  
      protected java.lang.String getIdFromFilename​(java.lang.String filename)  
      protected java.lang.String getIdWithContext​(java.lang.String id)
      Get the session id with its context.
      protected java.lang.String getIdWithContextAndExpiry​(SessionData data)
      Get the session id with its context and its expiry time
      protected java.lang.String getIdWithContextFromFilename​(java.lang.String filename)
      Extract the session id and context from the filename
      java.io.File getStoreDir()  
      void initialize​(SessionContext context)
      Initialize this data map for the given context.
      void initializeStore()
      Read the names of the existing session files and build a map of fully qualified session ids (ie with context) to filename.
      boolean isDeleteUnrestorableFiles()  
      protected boolean isOurContextSessionFilename​(java.lang.String filename)
      Check if the filename matches our session pattern and is a session for our context.
      boolean isPassivating()
      True if this type of datastore will passivate session objects
      protected boolean isSessionFilename​(java.lang.String filename)
      Check if the filename is a session filename.
      protected SessionData load​(java.io.InputStream is, java.lang.String expectedId)
      Load the session data from a file.
      protected void save​(java.io.OutputStream os, java.lang.String id, SessionData data)
      Save the session data.
      void setDeleteUnrestorableFiles​(boolean deleteUnrestorableFiles)  
      void setStoreDir​(java.io.File storeDir)  
      void sweepDisk()
      Check all session files that do not belong to this context and remove any that expired long ago (ie at least 5 gracePeriods ago).
      void sweepFile​(long now, java.nio.file.Path p)
      Check to see if the expiry on the file is very old, and delete the file if so.
      java.lang.String toString()  
      • Methods inherited from class org.eclipse.jetty.util.component.ContainerLifeCycle

        addBean, addBean, addEventListener, addManaged, contains, destroy, dump, dump, dump, dump, dump, dumpBeans, dumpObject, dumpObjects, dumpStdErr, dumpThis, getBean, getBeans, getBeans, getContainedBeans, getContainedBeans, isAuto, isManaged, isUnmanaged, manage, removeBean, removeBeans, removeEventListener, setBeans, setStopTimeout, start, stop, unmanage, updateBean, updateBean, updateBeans
      • Methods inherited from class org.eclipse.jetty.util.component.AbstractLifeCycle

        addLifeCycleListener, getState, getState, getStopTimeout, isFailed, isRunning, isStarted, isStarting, isStopped, isStopping, removeLifeCycleListener, start, stop
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface org.eclipse.jetty.util.component.Dumpable

        dumpSelf
      • Methods inherited from interface org.eclipse.jetty.util.component.Dumpable.DumpableContainer

        isDumpable
      • Methods inherited from interface org.eclipse.jetty.util.component.LifeCycle

        addLifeCycleListener, isFailed, isRunning, isStarted, isStarting, isStopped, isStopping, removeLifeCycleListener, start, stop
    • Field Detail

      • _storeDir

        protected java.io.File _storeDir
      • _deleteUnrestorableFiles

        protected boolean _deleteUnrestorableFiles
      • _sessionFileMap

        protected java.util.Map<java.lang.String,​java.lang.String> _sessionFileMap
      • _contextString

        protected java.lang.String _contextString
      • _lastSweepTime

        protected long _lastSweepTime
    • Constructor Detail

      • FileSessionDataStore

        public FileSessionDataStore()
    • Method Detail

      • initialize

        public void initialize​(SessionContext context)
                        throws java.lang.Exception
        Description copied from interface: SessionDataMap
        Initialize this data map for the given context. A SessionDataMap can only be used by one context(/session manager).
        Specified by:
        initialize in interface SessionDataMap
        Overrides:
        initialize in class AbstractSessionDataStore
        Parameters:
        context - context associated
        Throws:
        java.lang.Exception - if unable to initialize the
      • doStop

        protected void doStop()
                       throws java.lang.Exception
        Overrides:
        doStop in class org.eclipse.jetty.util.component.ContainerLifeCycle
        Throws:
        java.lang.Exception
      • getStoreDir

        @ManagedAttribute(value="dir where sessions are stored",
                          readonly=true)
        public java.io.File getStoreDir()
      • setStoreDir

        public void setStoreDir​(java.io.File storeDir)
      • isDeleteUnrestorableFiles

        public boolean isDeleteUnrestorableFiles()
      • setDeleteUnrestorableFiles

        public void setDeleteUnrestorableFiles​(boolean deleteUnrestorableFiles)
      • delete

        public boolean delete​(java.lang.String id)
                       throws java.lang.Exception
        Delete a session
        Parameters:
        id - session id
        Returns:
        true if the session was deleted
        Throws:
        java.lang.Exception - if unable to delete session data
      • deleteFile

        public boolean deleteFile​(java.lang.String filename)
                           throws java.lang.Exception
        Delete the file associated with a session
        Parameters:
        filename - name of the file containing the session's information
        Returns:
        true if file was deleted, false otherwise
        Throws:
        java.lang.Exception - indicating delete failure
      • doGetExpired

        public java.util.Set<java.lang.String> doGetExpired​(java.util.Set<java.lang.String> candidates)
        Check to see which sessions have expired.
        Specified by:
        doGetExpired in class AbstractSessionDataStore
        Parameters:
        candidates - the set of session ids that the SessionCache believes have expired
        Returns:
        the complete set of sessions that have expired, including those that are not currently loaded into the SessionCache
      • sweepDisk

        public void sweepDisk()
        Check all session files that do not belong to this context and remove any that expired long ago (ie at least 5 gracePeriods ago).
      • sweepFile

        public void sweepFile​(long now,
                              java.nio.file.Path p)
        Check to see if the expiry on the file is very old, and delete the file if so. "Old" means that it expired at least 5 gracePeriods ago. The session can belong to any context.
        Parameters:
        now - the time now in msec
        p - the file to check
      • doLoad

        public SessionData doLoad​(java.lang.String id)
                           throws java.lang.Exception
        Description copied from class: AbstractSessionDataStore
        Load the session from persistent store.
        Specified by:
        doLoad in class AbstractSessionDataStore
        Parameters:
        id - the id of the session to load
        Returns:
        the re-inflated session
        Throws:
        java.lang.Exception - if unable to load the session
      • doStore

        public void doStore​(java.lang.String id,
                            SessionData data,
                            long lastSaveTime)
                     throws java.lang.Exception
        Description copied from class: AbstractSessionDataStore
        Store the session data persistently.
        Specified by:
        doStore in class AbstractSessionDataStore
        Parameters:
        id - identity of session to store
        data - info of the session
        lastSaveTime - time of previous save or 0 if never saved
        Throws:
        java.lang.Exception - if unable to store data
      • initializeStore

        public void initializeStore()
                             throws java.lang.Exception
        Read the names of the existing session files and build a map of fully qualified session ids (ie with context) to filename. If there is more than one file for the same session, only the most recently modified will be kept and the rest deleted. At the same time, any files - for any context - that expired a long time ago will be cleaned up.
        Throws:
        java.lang.Exception - if storeDir doesn't exist, isn't readable/writeable or contains 2 files with the same lastmodify time for the same session. Throws IOException if the lastmodifytimes can't be read.
      • isPassivating

        @ManagedAttribute(value="are sessions serialized by this store",
                          readonly=true)
        public boolean isPassivating()
        Description copied from interface: SessionDataStore
        True if this type of datastore will passivate session objects
        Returns:
        true if this store can passivate sessions, false otherwise
      • exists

        public boolean exists​(java.lang.String id)
                       throws java.lang.Exception
        Description copied from interface: SessionDataStore
        Test if data exists for a given session id.
        Parameters:
        id - Identity of session whose existence should be checked
        Returns:
        true if valid, non-expired session exists
        Throws:
        java.lang.Exception - if problem checking existence with persistence layer
      • save

        protected void save​(java.io.OutputStream os,
                            java.lang.String id,
                            SessionData data)
                     throws java.io.IOException
        Save the session data.
        Parameters:
        os - the output stream to save to
        id - identity of the session
        data - the info of the session
        Throws:
        java.io.IOException
      • getIdWithContext

        protected java.lang.String getIdWithContext​(java.lang.String id)
        Get the session id with its context.
        Parameters:
        id - identity of session
        Returns:
        the session id plus context
      • getIdWithContextAndExpiry

        protected java.lang.String getIdWithContextAndExpiry​(SessionData data)
        Get the session id with its context and its expiry time
        Returns:
        the session id plus context and expiry
      • getIdFromFilename

        protected java.lang.String getIdFromFilename​(java.lang.String filename)
      • getExpiryFromFilename

        protected long getExpiryFromFilename​(java.lang.String filename)
      • getContextFromFilename

        protected java.lang.String getContextFromFilename​(java.lang.String filename)
      • getIdWithContextFromFilename

        protected java.lang.String getIdWithContextFromFilename​(java.lang.String filename)
        Extract the session id and context from the filename
        Parameters:
        filename - the name of the file to use
        Returns:
        the session id plus context
      • isSessionFilename

        protected boolean isSessionFilename​(java.lang.String filename)
        Check if the filename is a session filename.
        Parameters:
        filename - the filename to check
        Returns:
        true if the filename has the correct filename format
      • isOurContextSessionFilename

        protected boolean isOurContextSessionFilename​(java.lang.String filename)
        Check if the filename matches our session pattern and is a session for our context.
        Parameters:
        filename - the filename to check
        Returns:
        true if the filename has the correct filename format and is for this context
      • load

        protected SessionData load​(java.io.InputStream is,
                                   java.lang.String expectedId)
                            throws java.lang.Exception
        Load the session data from a file.
        Parameters:
        is - file input stream containing session data
        expectedId - the id we've been told to load
        Returns:
        the session data
        Throws:
        java.lang.Exception