Class MemoryUserDatabase

java.lang.Object
org.apache.catalina.users.MemoryUserDatabase
All Implemented Interfaces:
UserDatabase

public class MemoryUserDatabase extends Object implements UserDatabase
Concrete implementation of UserDatabase that loads all defined users, groups, and roles into an in-memory data structure, and uses a specified XML file for its persistent storage.

This class is thread-safe.

This class does not enforce what, in an RDBMS, would be called referential integrity. Concurrent modifications may result in inconsistent data such as a User retaining a reference to a Role that has been removed from the database.

Since:
4.1
Author:
Craig R. McClanahan
  • Field Details

    • groups

      protected final Map<String,Group> groups
      The set of Groups defined in this database, keyed by group name.
    • id

      protected final String id
      The unique global identifier of this user database.
    • pathname

      protected String pathname
      The relative (to catalina.base) or absolute pathname to the XML file in which we will save our persistent information.
    • pathnameOld

      protected String pathnameOld
      The relative or absolute pathname to the file in which our old information is stored while renaming is in progress.
    • pathnameNew

      protected String pathnameNew
      The relative or absolute pathname of the file in which we write our new information prior to renaming.
    • readonly

      protected boolean readonly
      A flag, indicating if the user database is read only.
    • roles

      protected final Map<String,Role> roles
      The set of Roles defined in this database, keyed by role name.
    • users

      protected final Map<String,User> users
      The set of Users defined in this database, keyed by user name.
  • Constructor Details

    • MemoryUserDatabase

      public MemoryUserDatabase()
      Create a new instance with default values.
    • MemoryUserDatabase

      public MemoryUserDatabase(String id)
      Create a new instance with the specified values.
      Parameters:
      id - Unique global identifier of this user database
  • Method Details

    • getGroups

      public Iterator<Group> getGroups()
      Specified by:
      getGroups in interface UserDatabase
      Returns:
      the set of Groups defined in this user database.
    • getId

      public String getId()
      Specified by:
      getId in interface UserDatabase
      Returns:
      the unique global identifier of this user database.
    • getPathname

      public String getPathname()
      Returns:
      the relative or absolute pathname to the persistent storage file.
    • setPathname

      public void setPathname(String pathname)
      Set the relative or absolute pathname to the persistent storage file.
      Parameters:
      pathname - The new pathname
    • getReadonly

      public boolean getReadonly()
      Returns:
      the readonly status of the user database
    • setReadonly

      public void setReadonly(boolean readonly)
      Setting the readonly status of the user database
      Parameters:
      readonly - the new status
    • getWatchSource

      public boolean getWatchSource()
    • setWatchSource

      public void setWatchSource(boolean watchSource)
    • getRoles

      public Iterator<Role> getRoles()
      Specified by:
      getRoles in interface UserDatabase
      Returns:
      the set of Roles defined in this user database.
    • getUsers

      public Iterator<User> getUsers()
      Specified by:
      getUsers in interface UserDatabase
      Returns:
      the set of Users defined in this user database.
    • close

      public void close() throws Exception
      Description copied from interface: UserDatabase
      Finalize access to this user database.
      Specified by:
      close in interface UserDatabase
      Throws:
      Exception - if any exception is thrown during closing
    • createGroup

      public Group createGroup(String groupname, String description)
      Description copied from interface: UserDatabase
      Create and return a new Group defined in this user database.
      Specified by:
      createGroup in interface UserDatabase
      Parameters:
      groupname - The group name of the new group (must be unique)
      description - The description of this group
      Returns:
      The new group
    • createRole

      public Role createRole(String rolename, String description)
      Description copied from interface: UserDatabase
      Create and return a new Role defined in this user database.
      Specified by:
      createRole in interface UserDatabase
      Parameters:
      rolename - The role name of the new role (must be unique)
      description - The description of this role
      Returns:
      The new role
    • createUser

      public User createUser(String username, String password, String fullName)
      Description copied from interface: UserDatabase
      Create and return a new User defined in this user database.
      Specified by:
      createUser in interface UserDatabase
      Parameters:
      username - The logon username of the new user (must be unique)
      password - The logon password of the new user
      fullName - The full name of the new user
      Returns:
      The new user
    • findGroup

      public Group findGroup(String groupname)
      Specified by:
      findGroup in interface UserDatabase
      Parameters:
      groupname - Name of the group to return
      Returns:
      the Group with the specified group name, if any; otherwise return null.
    • findRole

      public Role findRole(String rolename)
      Specified by:
      findRole in interface UserDatabase
      Parameters:
      rolename - Name of the role to return
      Returns:
      the Role with the specified role name, if any; otherwise return null.
    • findUser

      public User findUser(String username)
      Specified by:
      findUser in interface UserDatabase
      Parameters:
      username - Name of the user to return
      Returns:
      the User with the specified user name, if any; otherwise return null.
    • open

      public void open() throws Exception
      Description copied from interface: UserDatabase
      Initialize access to this user database.
      Specified by:
      open in interface UserDatabase
      Throws:
      Exception - if any exception is thrown during opening
    • removeGroup

      public void removeGroup(Group group)
      Description copied from interface: UserDatabase
      Remove the specified Group from this user database.
      Specified by:
      removeGroup in interface UserDatabase
      Parameters:
      group - The group to be removed
    • removeRole

      public void removeRole(Role role)
      Description copied from interface: UserDatabase
      Remove the specified Role from this user database.
      Specified by:
      removeRole in interface UserDatabase
      Parameters:
      role - The role to be removed
    • removeUser

      public void removeUser(User user)
      Description copied from interface: UserDatabase
      Remove the specified User from this user database.
      Specified by:
      removeUser in interface UserDatabase
      Parameters:
      user - The user to be removed
    • isWriteable

      @Deprecated public boolean isWriteable()
      Deprecated.
      Use isWritable(). This method will be removed in Tomcat 10.1.x onwards.
      Check for permissions to save this user database to persistent storage location.
      Returns:
      true if the database is writable
    • isWritable

      public boolean isWritable()
      Check for permissions to save this user database to persistent storage location.
      Returns:
      true if the database is writable
    • save

      public void save() throws Exception
      Description copied from interface: UserDatabase
      Save any updated information to the persistent storage location for this user database.
      Specified by:
      save in interface UserDatabase
      Throws:
      Exception - if any exception is thrown during saving
    • backgroundProcess

      public void backgroundProcess()
      Description copied from interface: UserDatabase
      Perform any background processing (e.g. checking for changes in persisted storage) required for the user database.
      Specified by:
      backgroundProcess in interface UserDatabase
    • toString

      public String toString()
      Overrides:
      toString in class Object