public class DatabaseUserStore extends AbstractModule implements ModifyableUserStore
A user store which stores users in a relational database. Needs a module implementing the DatabaseInterface to work. The user data is stored in three tables, which need to be created beforehand. Following sql script will generate the tables on MySQL, you may need to adjust it slightly for other database systems.
create table USERS( ID bigint AUTO_INCREMENT primary key, USERNAME varchar(256) not null collate utf8_bin ); create table USER_ROLES( USERID bigint not null references USERS(ID), ROLE varchar(256) not null collate utf8_bin ); create table USER_PROPS( USERID bigint not null references USERS(ID), PROPKEY varchar(256) not null collate utf8_bin, PROPVALUE varchar(2048) collate utf8_bin );
You may prefix each table with a prefix of your choosing. Specify it in the initialisation parameters with tablePrefix.
Any changes to the user store are immediately stored in the database when saveUser is called.
| Modifier and Type | Class and Description |
|---|---|
protected static class |
DatabaseUserStore.DBUser |
| Modifier and Type | Field and Description |
|---|---|
protected DatabaseInterface |
database |
private java.lang.Object |
modifyLock |
protected java.lang.String |
tablePrefix |
protected java.util.HashMap<java.lang.String,DatabaseUserStore.DBUser> |
users |
autoStart, isInitialized, isRunning, logging, loggingModule, moduleManager| Constructor and Description |
|---|
DatabaseUserStore() |
| Modifier and Type | Method and Description |
|---|---|
protected boolean |
commitDeleteUser(java.lang.String userName) |
protected DatabaseUserStore.DBUser |
commitNewUser(java.lang.String userName) |
protected boolean |
commitUser(DatabaseUserStore.DBUser user) |
boolean |
deleteUser(java.lang.String user)
Deletes a user from this user store.
|
protected void |
fetchUsers() |
java.util.Collection<User> |
findUsers(java.lang.String key,
java.lang.String value)
Finds users based on a property value.
|
java.util.Collection<User> |
getAllUsers()
Returns a collection of all users.
|
java.util.Collection<Module> |
getDependencies(ModuleManager manager)
Returns all the modules this module depends on.
|
User |
getUser(java.lang.String user)
Finds a user with a user name.
|
protected void |
handleSQLException(java.sql.SQLException sqle) |
void |
init(ModuleManager manager,
java.util.HashMap<java.lang.String,java.lang.Object> settings)
Initialises the module.
|
User |
newUser(java.lang.String user)
Creates a new user in the user store.
|
boolean |
saveUser(User user)
Saves a user which originates from this database.
|
void |
start(ModuleManager manager)
Starts the module.
|
void |
stop(ModuleManager manager)
Stops the module.
|
isInitialized, isRunning, requireLogging, toStringclone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitisInitialized, isRunningprotected java.lang.String tablePrefix
protected DatabaseInterface database
protected final java.util.HashMap<java.lang.String,DatabaseUserStore.DBUser> users
private final java.lang.Object modifyLock
protected void handleSQLException(java.sql.SQLException sqle)
throws UserStoreException
UserStoreExceptionpublic java.util.Collection<Module> getDependencies(ModuleManager manager) throws ModuleException
ModulegetDependencies in interface ModulegetDependencies in class AbstractModulemanager - The module manager handling this module.ModuleExceptionpublic void init(ModuleManager manager, java.util.HashMap<java.lang.String,java.lang.Object> settings) throws ModuleException
ModuleInitialises the module. After constructor, this is the first method called in the life cycle of a module. It should not perform anything time consuming or anything with notable outside side effects. It should only read the parameters and initialise the module so that it can later be started. Note that a module being initialised doesn't mean that it necessarily will ever be started.
A ModuleException may be thrown if something vital is missing from the parameters or they are not sensible. In some cases you may not want to throw an exception even if vital initialisation information is missing. If, for example, it is possible that the module is initialised in some other way between the init and the start method calls. A ModuleException may also be thrown at the start method if the module is still not initialised.
init in interface Moduleinit in class AbstractModulemanager - The module manager handling this module. You may keep a
reference to it if needed.ModuleExceptionpublic void start(ModuleManager manager) throws ModuleException
Modulestart in interface Modulestart in class AbstractModulemanager - The module manager handling this module.ModuleExceptionpublic void stop(ModuleManager manager)
Modulestop in interface Modulestop in class AbstractModulemanager - The module manager handling this module.public boolean deleteUser(java.lang.String user)
throws UserStoreException
ModifyableUserStoredeleteUser in interface ModifyableUserStoreuser - The user name of the user to delete.UserStoreExceptionpublic User newUser(java.lang.String user) throws UserStoreException
ModifyableUserStorenewUser in interface ModifyableUserStoreuser - The user name of the new user.UserStoreExceptionpublic boolean saveUser(User user) throws UserStoreException
ModifyableUserStoresaveUser in interface ModifyableUserStoreuser - The user object to save.UserStoreExceptionpublic java.util.Collection<User> findUsers(java.lang.String key, java.lang.String value) throws UserStoreException
UserStorefindUsers in interface UserStorekey - The property key used for the search.value - The value of the property.UserStoreExceptionprotected void fetchUsers()
throws UserStoreException
UserStoreExceptionprotected boolean commitUser(DatabaseUserStore.DBUser user) throws UserStoreException
UserStoreExceptionprotected boolean commitDeleteUser(java.lang.String userName)
throws UserStoreException
UserStoreExceptionprotected DatabaseUserStore.DBUser commitNewUser(java.lang.String userName) throws UserStoreException
UserStoreExceptionpublic java.util.Collection<User> getAllUsers() throws UserStoreException
UserStoregetAllUsers in interface UserStoreUserStoreExceptionpublic User getUser(java.lang.String user) throws UserStoreException
UserStoregetUser in interface UserStoreuser - The user name.UserStoreExceptionCopyright 2004-2015 Wandora Team