/* * Created on Feb 13, 2005 * * TODO To change the template for this generated file go to * Window - Preferences - Java - Code Style - Code Templates */ package webraveler.ravel; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.Enumeration; import java.util.Hashtable; /** * @author schmitzc * * TODO To change the template for this generated type comment go to * Window - Preferences - Java - Code Style - Code Templates */ public class UserStack { private Hashtable users; private Hashtable userPassHash; private Raveler ravel; /** * * @param ravel */ public UserStack (Raveler ravel){ this.ravel = ravel; users = new Hashtable(); userPassHash = new Hashtable(); gatherUsers(); } /** * Gets the password for the given username * * @param username a string containing the username * @return a string containing the password */ public String getUserPassword(String username) { return (String) userPassHash.get(username); } /** * Returns an Enumeration of all the current users * * @return */ public Enumeration getUsers(){ return users.keys(); } /** * Detemines whether a RavelerUser with the given username * is in the UserStack * * @param username a string containing the username * @return a boolean existence value */ public boolean userExists(String username) { return users.containsKey(username); } //userExists(String) /** * Gets the RavelerUser associated with the given username * * @param username a string containing the username * @return a RavelerUser associated with the given username */ public RavelerUser getUser(String username) throws RavelerException{ RavelerUser user = null; if (userExists(username)) { user = (RavelerUser) users.get(username); return user; } else throw new RavelerException("Unable to get the user: A RavelerUser with the given username does not exist."); } //getUser(String) /** * Creates a RavelerUser for the given username and adds it to the * Users vector * * @param username a string containing the username */ public void addUser(RavelerUser user) throws RavelerException { if (!users.containsKey(user.username)) { users.put(user.username,user); } // if else throw new RavelerException("UserStack: Unable to create a new user: A RavelerUser with the given username already exists."); } //createUser(String) /** * Deletes a user from the Users vector * * @param username a string containing the username */ public void deleteUser(String username) throws RavelerException { if (!users.containsKey(username)) { users.remove(username); } // if else throw new RavelerException("UserStack: Unable to delete the user: A RavelerUser with the given username does not exist."); } //deleteUser(String) /** * Fills the users hashtable with users from the users.txt file, * sets up the username/password hashtable, and * loads the plugin attributes for each user * */ private void gatherUsers() { // populate the users vector from the users.txt file (for now) ... then try // to load the attributes files for those users and the plugins we found // (so we don't have to do it later)... if we don't find an attributes file // for a plugin and a given user, load the default attributes for the user. // then, when the plugin runs, it merely has to fetch the latest attributes // from the user, not from the disk FileReader userFileReader = null; FileWriter userFileWriter; BufferedReader userFileBuffReader; try { userFileReader = new FileReader(Raveler.WEBRAVELER_HOME + "/data/users.txt"); } catch (FileNotFoundException fnfe) { try{ System.out.println("UserStack: No users.txt found. . . Attempting to create one."); userFileWriter = new FileWriter("users.txt"); userFileWriter.close(); /* not going to need this one for a while... just creating the file */ userFileReader = new FileReader("users.txt"); System.out.println("UserStack: Successfully created users.txt"); } catch(IOException ioe) { System.out.println("UserStack: Could neither open nor create users.txt in your WebRaveler2 directory. Exiting."); System.exit(1); } } // read in the usernames and passwords from the disk, and put 'em in [yet another] hash // table System.out.println("UserStack: Reading username/password file and populating hash table."); int colonIndex; String username; String password; String line; userFileBuffReader = new BufferedReader(userFileReader); try { while ((line = userFileBuffReader.readLine()) != null) { colonIndex = line.indexOf(":"); username = line.substring(0,colonIndex); password = line.substring(colonIndex+1,line.length()); userPassHash.put(username,password); RavelerUser currentUser = new RavelerUser(username); try { this.addUser(currentUser); /* add a new user object with this username to the hash table */ } catch (RavelerException re) { System.out.println(re.getMessage()); } try { currentUser.readPreferences(); } catch (RavelerException re) { // System.out.println("Raveler: Loading "+username+"'s preferences. Success? " + loaded); // save the empty preferences hash table to disk try { currentUser.savePreferences(); } catch (RavelerException rave) { System.out.println("UserStack: Couldn't save user preferences file to disk. Exiting."); System.exit(1); } //catch // System.out.println("Raveler: Saved blank user preferences to disk for user "+username+"."); } //catch // set up plugin attributes (using defaults as necessary) for each plugin in this // user's prefs hash for( Enumeration e=ravel.getPlugins() ; e.hasMoreElements() ; ) { RavelerPlugin plugin = (RavelerPlugin) e.nextElement(); /* if the plugin doesn't have an entry in the user's prefs hash, add the defaults * and set the default status of the plugin */ if ( ! currentUser.preferences.containsKey(plugin.name) ) { //System.out.println("Raveler: Adding defaults for "+plugin.name+" to preferences for user "+username+"."); plugin.setDefaultAttributes(); plugin.setDefaultStatus(); currentUser.preferences.put(plugin.name,plugin.attributes); } //if // System.out.println("Raveler: User "+username+" already has preferences for "+plugin.name+". Continuing."); } //for // System.out.print("Raveler: User "+username+"'s preferences loaded successfully. Saving any modifications. . . "); try { currentUser.savePreferences(); // System.out.println("["+saved+"]"); } catch (RavelerException re) { System.out.println("UserStack: Couldn't save user preferences file to disk. Exiting."); System.exit(1); } //catch }//while userFileReader.close(); } catch(IOException ioe) { ioe.printStackTrace(); } } //gatherUsers() /** * Saves a user's settings to disk * * @param username A string containing the username */ public void saveUserSettings(String username) throws RavelerException { /* Save the user preferences for the RavelerUser with the given username */ if (userExists(username)) { RavelerUser user = getUser(username); try { user.savePreferences(); } catch (RavelerException re) { throw new RavelerException(re.getMessage()); } //catch } // if else throw new RavelerException("Unable to save the user's preferences."); } //saveUserSettings(String) }