UserDBTextpublic class UserDBText extends UserDB A trivial "database" for User objects, stored in a flat file.
Since this is exected to be used heavily, and to avoid the overhead
of re-reading the file, the "Singleton" Design Pattern is used
to ensure that there is only ever one instance of this class. |
Fields Summary |
---|
protected static final String | DEF_NAME | protected String | fileName | protected PrintWriter | pw |
Constructors Summary |
---|
protected UserDBText()
this(DEF_NAME);
| protected UserDBText(String fn)Constructor
super();
fileName = fn;
BufferedReader is = new BufferedReader(new FileReader(fn));
String line;
while ((line = is.readLine()) != null) {
//name:password:fullname:City:Prov:Country:privs
if (line.startsWith("#")) { // comment
continue;
}
StringTokenizer st =
new StringTokenizer(line, ":");
String nick = st.nextToken();
String pass = st.nextToken();
String full = st.nextToken();
String email = st.nextToken();
String city = st.nextToken();
String prov = st.nextToken();
String ctry = st.nextToken();
User u = new User(nick, pass, full, email,
city, prov, ctry);
String privs = st.nextToken();
if (privs.indexOf("A") != -1) {
u.setAdminPrivileged(true);
}
users.add(u);
}
|
Methods Summary |
---|
public synchronized void | addUser(User nu)
// Add it to the in-memory list
super.addUser(nu);
// Add it to the on-disk version
if (pw == null) {
pw = new PrintWriter(new FileWriter(fileName, true));
}
pw.println(toDB(nu));
// toDB returns: name:password:fullname:City:Prov:Country:privs
pw.flush();
| protected java.lang.String | toDB(User u)
// #name:password:fullName:email:City:Prov:Country:privs
char privs = '-";
if (adminPrivs)
privs = 'A";
else if (editPrivs)
privs = 'E";
return new StringBuffer()
.append(u.name).append(':")
.append(u.password).append(':")
.append(u.fullName).append(':")
.append(u.email).append(':")
.append(u.city).append(':")
.append(u.prov).append(':")
.append(u.country).append(':")
.append(u.privs)
.toString();
|
|