FileDocCategorySizeDatePackage
UserDB.javaAPI DocExample2713Fri Oct 12 20:02:16 BST 2001jabadot

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 ArrayList
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();
		} catch (ClassNotFoundException ex) {
			System.err.println("Unable to instantiate UserDB singleton " + 
				dbClass + " (" + ex.toString() + ")");
			throw new IllegalArgumentException(ex.toString());
		} 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 jabadot.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.ArrayListgetUserList()
Get the list of users.

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