Class DBLocalAcctMgr

java.lang.Object
  extended byDBLocalAcctMgr
All Implemented Interfaces:
AcctMgrSpec

public class DBLocalAcctMgr
extends Object
implements AcctMgrSpec

An implementation of the AcctMgrSpec interface that uses a postgres backend. TODO: Add invalidation support ala closeMgr(). TODO: Reconnection managing. TODO: Add AcctMgrSpec.init()/destroy() ? TODO: Better error handling. TODO: AcctMgrSpec.addUser() doctation += 'fails if user exists'.

Version:
$Revision: #12 $
Author:
Kevin Thompson [antiduh@csh.rit.edu]

Field Summary
private  ConfigMgr configMgr
          The ConfigMgr to get our configuration data from.
private  Connection conn
          The connection to the database.
private  boolean connected
          Whether or not the class has a valid connection to the database.
private  String db
          The database to connect to.
private  String host
          The database server's hostname.
private  String imapHost
          The IMAP host to authenticate users against.
private  DrinkLoggerSpec log
          The DrinkLoggerSpec to log information/errors/aborts to.
private  String pass
          The password for the database username
private  String user
          The username of the database user to authenticate as.
 
Constructor Summary
DBLocalAcctMgr(DrinkLoggerSpec inLog, ConfigMgr inConfigMgr)
          Pulls the configuration data (host, db, user, pass) from the given ConfigMgr object.
DBLocalAcctMgr(DrinkLoggerSpec log, String host, String db, String user, String pass)
          Construct the account manager by trying to connect to the db server.
 
Method Summary
 boolean addUser(String username, String password)
          Adds a user from the system.
 boolean addUser(String username, String password, int balance, boolean adminStatus)
          Adds a user to the database of known users using the supplied values to initialize the account.
 void closeMgr()
          Causes the account manager to invalidate itself and close all database connections.
 boolean decreaseUserBalance(String username, int amount)
          Decrements a users balance
 Vector dump()
          TODO: This is not a set anymore.
private  int execUpdate(String query)
          Executes an update of the database, eg INSERT, DELETE, UPDATE.
 int getUserBalance(String username)
          Queries the DB for the given users balance.
 boolean increaseUserBalance(String username, int amount)
          Updates the database with the given users balance.
private  boolean initialize(String host, String db, String user, String pass)
          Performs the common part of the class construction.
 boolean isSystemReady()
          Returns whether or not the database backend is ready to process requests.
 boolean isUserAdmin(String username)
          Tests to see if the given user is an administrator.
 boolean isValidUser(String username)
          Checks to see if the user is registered in the database.
 boolean isValidUser(String username, String password)
          Authenticates a user.
 void purgeDatabase()
          WARNING: USE WITH CAUTION.
 boolean removeUser(String username)
          Removes a user from the database.
 boolean setUserAdmin(String username, boolean admin)
          Updates a users admin status.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

conn

private Connection conn
The connection to the database. This persists throughout the life of the class, until closeMgr() is called and the class invalidates itself.


connected

private boolean connected
Whether or not the class has a valid connection to the database.


host

private String host
The database server's hostname.


db

private String db
The database to connect to.


user

private String user
The username of the database user to authenticate as.


pass

private String pass
The password for the database username


configMgr

private ConfigMgr configMgr
The ConfigMgr to get our configuration data from.


log

private DrinkLoggerSpec log
The DrinkLoggerSpec to log information/errors/aborts to.


imapHost

private String imapHost
The IMAP host to authenticate users against.

Constructor Detail

DBLocalAcctMgr

public DBLocalAcctMgr(DrinkLoggerSpec inLog,
                      ConfigMgr inConfigMgr)
Pulls the configuration data (host, db, user, pass) from the given ConfigMgr object.

Parameters:
inLog - Where to send log messages.
inConfigMgr - Where to get our runtime configuration data from.

DBLocalAcctMgr

public DBLocalAcctMgr(DrinkLoggerSpec log,
                      String host,
                      String db,
                      String user,
                      String pass)
Construct the account manager by trying to connect to the db server. If we are unable to connect, remember that the connection is dead. The ReconnectionMgr will try to reconnect, and the rest of class will play dead until the connection is estabilished. TODO: Remove or use.

Parameters:
log - The DrinkLoggerSpec we're to use for logging errors.
host - The host machine to connect to.
db - The database to open.
user - The username of the account to authenticate as.
pass - The password of the account to authenticate as.
Method Detail

initialize

private boolean initialize(String host,
                           String db,
                           String user,
                           String pass)
Performs the common part of the class construction.


closeMgr

public void closeMgr()
Causes the account manager to invalidate itself and close all database connections. TODO: Put this in the AcctMgrSpec?


addUser

public boolean addUser(String username,
                       String password)
Adds a user from the system. If the user is already registered, then this function will not attempt to add them, and will return false. The function will supply default values for account data (eg 0 credits).

Specified by:
addUser in interface AcctMgrSpec
Parameters:
username - The username of the user to be registered.
password - The password of the user.
Returns:
True if the operation was successful, false otherwise.
See Also:
LocalAcctMgr.isValidUser(String,String)

addUser

public boolean addUser(String username,
                       String password,
                       int balance,
                       boolean adminStatus)
Adds a user to the database of known users using the supplied values to initialize the account. TODO: Implement this

Specified by:
addUser in interface AcctMgrSpec
Parameters:
username - The username of the account to add.
password - The password for the new account.
balance - The accounts initial balance.
adminStatus - Whether or not the new user is to be an admin.
Returns:
true if the DB can be updated with the new user. Also, the command will pre-fail if the user already exists.

removeUser

public boolean removeUser(String username)
Removes a user from the database. Checks to see if the given username is null, zero-length, or does not refer to a known user. If those pass, then it procedes to delete the user from the database. If multiple records are deleted, then a warning is issued.

Specified by:
removeUser in interface AcctMgrSpec
Parameters:
username - The username of the account to delete
Returns:
True if the operation was successful, false otherwise

isValidUser

public boolean isValidUser(String username)
Checks to see if the user is registered in the database.

Specified by:
isValidUser in interface AcctMgrSpec
Parameters:
username - The user to verify
Returns:
true if the username is known to the Drink system.

increaseUserBalance

public boolean increaseUserBalance(String username,
                                   int amount)
Updates the database with the given users balance.

Specified by:
increaseUserBalance in interface AcctMgrSpec
Parameters:
username - The user whose account is to be incremented.
amount - The amount to increase the users account
Returns:
True if the operation was successful, false otherwise.
See Also:
AcctMgrSpec.decreaseUserBalance(String, int)

decreaseUserBalance

public boolean decreaseUserBalance(String username,
                                   int amount)
Decrements a users balance

Specified by:
decreaseUserBalance in interface AcctMgrSpec
Parameters:
username - The user whose account is to be decremented
amount - The amount, in credits, to decrement the users account.
Returns:
True if the operation was successful, false otherwise.
See Also:
AcctMgrSpec.increaseUserBalance(String, int)

getUserBalance

public int getUserBalance(String username)
Queries the DB for the given users balance. Returns 0 if the balance is not available.

Specified by:
getUserBalance in interface AcctMgrSpec
Parameters:
username - The username whose balance is desired
Returns:
The number of credits a user has on their account.

isValidUser

public boolean isValidUser(String username,
                           String password)
Authenticates a user. Currently, this authenticates against the CSH IMAP server.

Specified by:
isValidUser in interface AcctMgrSpec
Parameters:
username - The user to authenticate.
password - The users password.
Returns:
True if the username and password authenticate, false otherwise.

isUserAdmin

public boolean isUserAdmin(String username)
Tests to see if the given user is an administrator.

Specified by:
isUserAdmin in interface AcctMgrSpec
Parameters:
username - The user to be verified
Returns:
True if the user is an administrator, false otherwise.

setUserAdmin

public boolean setUserAdmin(String username,
                            boolean admin)
Updates a users admin status.

Specified by:
setUserAdmin in interface AcctMgrSpec
Parameters:
username - The user to edit
admin - The value to set the flag to.
Returns:
True if the operation was successful, false otherwise.

isSystemReady

public boolean isSystemReady()
Returns whether or not the database backend is ready to process requests.

Specified by:
isSystemReady in interface AcctMgrSpec
Returns:
True if the system is ready, false otherwise.

dump

public Vector dump()
TODO: This is not a set anymore. Returns a Set of all known usernames.

Specified by:
dump in interface AcctMgrSpec

purgeDatabase

public void purgeDatabase()
WARNING: USE WITH CAUTION. MAKE SURE YOU HAVE BACKUPS IF YOU USE THIS. Removes all users from the records. No accounts will be recognized after this. All data will be lost. TODO: Put this in the interface.

Specified by:
purgeDatabase in interface AcctMgrSpec

execUpdate

private int execUpdate(String query)
Executes an update of the database, eg INSERT, DELETE, UPDATE.

Returns:
The number of records inserted, deleted, or updated, or -1 if the update failed.