netbattle.database.registry
Class DatabaseImpl

java.lang.Object
  extended by netbattle.database.registry.AccountRegistry
      extended by netbattle.database.registry.DatabaseImpl

public class DatabaseImpl
extends AccountRegistry

This class implements an AccountRegistry using a relational database (MySQL by default). This is the preferred implementation to use, but if a database is unavailable, a file-based implementation is also available.

See Also:
FileImpl

Field Summary
 
Fields inherited from class netbattle.database.registry.AccountRegistry
LEVEL_ADMIN, LEVEL_MOD, LEVEL_USER
 
Constructor Summary
DatabaseImpl()
           
 
Method Summary
 boolean addNewUser(java.lang.String user, java.lang.String password)
          Add a new user to the database.
 boolean banUser(java.lang.String user, long date)
          Set the date upon which a user becomes unbanned.
 boolean banUser(java.lang.String user, java.lang.String ip, long date)
          Set the date upon which a user becomes unbanned.
 void close()
          Close this AccountRegistry.
 boolean connect(java.lang.String host, java.lang.String database, java.lang.String user, java.lang.String password)
          Connect to a MySql database on this server.
 java.lang.String getIpAddress(java.lang.String user)
          Get the IP Address of a particular user.
 long getIpUnbanDate(java.lang.String ip)
          Return the date upon which an IP address becomes unbanned.
 java.lang.String getPassword(java.lang.String user)
          Get the SHA-1 hash of a user's password.
 int getUserLevel(java.lang.String user)
          Get the level of a user.
 UserTableMessage getUserList()
          Get a list of all users in this registry.
 java.util.List getUsersByIp(java.lang.String ip)
          Get a List of users with a given IP Address.
 long getUserUnbanDate(java.lang.String user)
          Determine when a user becomes unbanned.
 boolean isConnected()
          Determine whether are are connected and if the connection is active.
 boolean setUserLevel(java.lang.String user, int level)
          Set the level of a user.
 
Methods inherited from class netbattle.database.registry.AccountRegistry
finalize, getInstance, setInstance
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DatabaseImpl

public DatabaseImpl()
Method Detail

isConnected

public boolean isConnected()
Determine whether are are connected and if the connection is active. If the connection is inactive then we will attempt to reopen it because it probably closed due to inactivity.


connect

public boolean connect(java.lang.String host,
                       java.lang.String database,
                       java.lang.String user,
                       java.lang.String password)
Connect to a MySql database on this server. The user running the Shoddy Battle server must have sufficient privileges to perform the SQL executed in the AccountRegistry class.


close

public void close()
Close this AccountRegistry.

Specified by:
close in class AccountRegistry

getPassword

public java.lang.String getPassword(java.lang.String user)
Get the SHA-1 hash of a user's password.

Specified by:
getPassword in class AccountRegistry
Returns:
the hash or null if the user does not exist

getUserList

public UserTableMessage getUserList()
Get a list of all users in this registry.

Specified by:
getUserList in class AccountRegistry

addNewUser

public boolean addNewUser(java.lang.String user,
                          java.lang.String password)
Add a new user to the database.

Specified by:
addNewUser in class AccountRegistry
Parameters:
user - the user name of the user
password - an SHA-1 hash of the user's password
Returns:
whether the user was added successfully

getUserUnbanDate

public long getUserUnbanDate(java.lang.String user)
Determine when a user becomes unbanned.

Specified by:
getUserUnbanDate in class AccountRegistry
Returns:
a UNIX timestamp of the unban date

getIpUnbanDate

public long getIpUnbanDate(java.lang.String ip)
Return the date upon which an IP address becomes unbanned.

Specified by:
getIpUnbanDate in class AccountRegistry

banUser

public boolean banUser(java.lang.String user,
                       long date)
Set the date upon which a user becomes unbanned.

Specified by:
banUser in class AccountRegistry

banUser

public boolean banUser(java.lang.String user,
                       java.lang.String ip,
                       long date)
Set the date upon which a user becomes unbanned.

Specified by:
banUser in class AccountRegistry

getUserLevel

public int getUserLevel(java.lang.String user)
Get the level of a user.

Specified by:
getUserLevel in class AccountRegistry

getIpAddress

public java.lang.String getIpAddress(java.lang.String user)
Get the IP Address of a particular user.

Specified by:
getIpAddress in class AccountRegistry

getUsersByIp

public java.util.List getUsersByIp(java.lang.String ip)
Get a List of users with a given IP Address.

Specified by:
getUsersByIp in class AccountRegistry

setUserLevel

public boolean setUserLevel(java.lang.String user,
                            int level)
Set the level of a user.

Specified by:
setUserLevel in class AccountRegistry