Class Session

All Implemented Interfaces:
Serializable, AutoCloseable, ConcurrentMap<String,Object>, Map<String,Object>

public class Session extends ConcurrentHashMap<String,Object> implements AutoCloseable
Manages session data, allowing the storage and retrieval of serialized objects within a json file. The session data is stored in the specified location and automatically serialized and deserialized when starting or stopping a session.
Since:
3.3.0-SNAPSHOT
See Also:
  • Field Details

  • Constructor Details

    • Session

      public Session()
      Creates a new instance of Session without an active session.
  • Method Details

    • setExchange

      public void setExchange(@NotNull @NotNull BaseExchange exchange)
      Sets the BaseExchange instance for this session storage.
      Parameters:
      exchange - The exchange used to load the session.
      Throws:
      IllegalStateException - if the session file does not exist.
    • getAsType

      public <T> T getAsType(@NotNull @NotNull String key, @NotNull @NotNull Class<T> type)
      Retrieves the value associated with the specified key and attempts to cast it to the specified type.
      Type Parameters:
      T - The type parameter of the value.
      Parameters:
      key - The key for the value to retrieve.
      type - The expected type of the value.
      Returns:
      The value associated with the key, or null if not found or cannot be cast.
    • getAsType

      public <T> T getAsType(@NotNull @NotNull String key, T fallback, @NotNull @NotNull Class<T> type)
      Retrieves the value associated with the specified key and attempts to cast it to the specified type.
      Type Parameters:
      T - The type parameter of the value.
      Parameters:
      key - The key for the value to retrieve.
      fallback - The fallback value returned when the key does not exist.
      type - The expected type of the value.
      Returns:
      The value associated with the key, or null if not found or cannot be cast.
    • isType

      public <T> boolean isType(@NotNull @NotNull String key, @NotNull @NotNull Class<T> type)
      Checks if the value associated with the specified key is of the specified type.
      Type Parameters:
      T - The type parameter of the value.
      Parameters:
      key - The key for the value to check.
      type - The expected type of the value.
      Returns:
      true if the value exists and matches the type, otherwise false.
    • getExchange

      public BaseExchange getExchange()
      Retrieves the BaseExchange instance associated with this session.
      Returns:
      the associated BaseExchange instance, or null if none is set.
    • stopSession

      public void stopSession()
      Stops the current session and deletes the associated session file.
    • startSession

      public void startSession()
      Starts a new session and generates a session identifier.
      Throws:
      RuntimeException - if the secure random passphrase generation fails.
    • isSessionStarted

      public boolean isSessionStarted()
      Checks whether a session has been started.
      Returns:
      true if a session is active, otherwise false.
    • close

      public void close()
      Saves the session data to a persistent file. Automatically called when the session is closed.
      Specified by:
      close in interface AutoCloseable
    • getSessionInfo

      public SessionInfo getSessionInfo()
      Retrieves the SessionInfo associated with this session.
      Returns:
      the SessionInfo instance managing session metadata.
    • getSessionStorage

      public SessionStorage getSessionStorage()
      Retrieves the SessionStorage associated with this session.
      Returns:
      the SessionStorage instance managing session persistence.