UserDBpublic abstract class UserDB extends Object A base for several "database" accessors for User objects.
We use a Singleton access method for efficiency and to enforce
single access on the database, which means we can keep an in-memory
copy (in an ArrayList) perfectly in synch with the database.
Of course this really should be an EJB(!). But then JabaDot
would need a full EJB server to run, not just Tomcat and idb. |
Fields Summary |
---|
protected List | usersThe in-memory copy of the data | protected static UserDB | singletonThe only instance of this class. |
Constructors Summary |
---|
protected UserDB()In some subclasses the constructor will probably load the database,
while in others it may defer this until getUserList().
// String dbClass = null;
try {
//dbClass = "jabadot.UserDBJDBC";
//singleton = (UserDB)Class.forName(dbClass).newInstance();
singleton = new UserDBJDBC();
} catch (Exception ex) {
System.err.println(
"Unexpected exception: Unable to initialize UserDB singleton");
ex.printStackTrace(System.err);
throw new IllegalArgumentException(ex.toString());
}
users = new ArrayList();
|
Methods Summary |
---|
public synchronized void | addUser(User nu)
// Add it to the in-memory list
users.add(nu);
// Add it to the on-disk version
// N.B. - must be done in subclass.
| public abstract void | deleteUser(java.lang.String nick)
| public static UserDB | getInstance()"factory" method to get an instance, which will always be
the Singleton.
if (singleton == null)
throw new IllegalStateException(
"UserDB initialization failed (singleton was null)");
return singleton;
| public User | getUser(java.lang.String nick)Get the User object for a given nickname
Iterator it = users.iterator();
while (it.hasNext()) {
User u = (User)it.next();
if (u.getName().equals(nick))
return u;
}
return null;
| public java.util.List | getUserList()Get the list of users.
return users;
| public abstract void | setPassword(java.lang.String nick, java.lang.String newPass)
|
|