FileDocCategorySizeDatePackage
UserDB.javaAPI DocExample2520Wed Jun 16 21:54:58 BST 2004None

UserDB

public 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
users
The in-memory copy of the data
protected static UserDB
singleton
The 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 voidaddUser(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 voiddeleteUser(java.lang.String nick)

public static UserDBgetInstance()
"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 UsergetUser(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.ListgetUserList()
Get the list of users.

		return users;
	
public abstract voidsetPassword(java.lang.String nick, java.lang.String newPass)