/* this belongs to the WebRaveler package */ package webraveler.ravel; import java.io.*; import java.util.*; /** * A user for Web Raveler * * @author The Web Raveler Development Squad * @version 1.0 */ public class RavelerUser { /* variables */ public String username; public int realmCount; public boolean isLoggedIn; public Vector installedPlugins; public Vector activePlugins; public Vector groups; public Hashtable preferences; /* keys are names of plugins, values are plugin attribute hashes */ public boolean ravelOn; /* constructors */ /** * */ public RavelerUser() { realmCount = 0; isLoggedIn = false; ravelOn = true; } //RavelerUser() /** * */ public RavelerUser(String username) { this.username = username; preferences = new Hashtable(); realmCount = 0; isLoggedIn = false; activePlugins = new Vector(); ravelOn = true; //... } //RavelerUser(String) /* methods */ /** * Add the user to the given group, and return a success * value. * * @param groupName a String which is the name of the group * @return a boolean success indicator */ public boolean addToGroup(String groupName) { return true; } //addToGroups(String) /** * Get the groups associated with the current user. * * @return a linked list, a list of groups names */ public Vector getGroups() { return groups; } //getGroups() /** * Add a installed plugin to the user plugin Stack * * @param pluginName a string containing the name of the plugin to be added to the vector. * @return a boolean success indicator */ public void addInstalledPlugin(String pluginName) { } //addInstalledPlugin(RavelerPlugin) /** *Add an active plugin to the plugin vector * * @param pluginName a string containing the name of the plugin to be added to the vector. * @return a boolean success indicator */ public void addActivePlugin(String pluginName) throws RavelerException { if (!activePlugins.contains(pluginName)) { activePlugins.add(pluginName); } // if else throw new RavelerException(pluginName+" is already activated."); } // addActivePlugin(RavelerPlugin) /** * Removes an active plugin from the plugin vector * * @param pluginName a string containing the name of the plugin to be added to the vector. * @return a boolean success indicator */ public void removeActivePlugin(String pluginName) throws RavelerException { if (activePlugins.contains(pluginName)) { activePlugins.remove(pluginName); } // if else throw new RavelerException(pluginName+" is not activated."); } // removeActivePlugin(RavelerPlugin) /** * Get the installed plugins * * @return a list of the installed names. */ public Vector getInstalledPlugins(){ return installedPlugins; } // getInstalledPlugins() /** * Get the active plugins * * @return a vector of all the active plugins. */ public Vector getActivePlugins(){ return activePlugins; } // getActivePlugins() /** * Sets the information for the current user and the named plugin. Returns * false if the plugin already had that attribute set in its hash. * * @param pluginName a string containing the name of the plugin to be modified * @param attribute a string that names the attribute * @param value a string that gives the deisired value of the named attribute. * @return a boolean success value */ public void setAttribute(String pluginName, String attribute, String value) throws RavelerException { /* if the plugin is already in the prefs hash, and the attribute is already * set for this plugin, report an error*/ if (preferences.containsKey(pluginName)) { Hashtable pluginPref = (Hashtable) preferences.get(pluginName); if (!pluginPref.containsKey(attribute)) { /* if the plugin is in the prefs hash, but the attribute isn't set, set it to the * new value */ pluginPref.put(attribute,value); } //if else { String message = "Unable to set the "+attribute+" attribute for "+pluginName; message += " : The attribute already exists in "+username+"'s preferences."; throw new RavelerException(message); } //else } else { /* if the plugin isn't in the prefs hash, put it there, then set the attribute to * the given value */ Hashtable pluginPref = new Hashtable(); pluginPref.put(attribute,value); preferences.put(pluginName,new Hashtable()); } //else } //setAttribute(String,String,String) /** * Gets information for a partcular attribute for the current user and the named plugin. * * @param pluginName a string that names the plugin * @param attribute a string that names the attribute * @return a String which is the attribute value (or null if none exists) */ public String getAttribute(String pluginName, String attribute) throws RavelerException{ String message; /* Check if the user's preferences contain preferences for the given plugin */ if (preferences.containsKey(pluginName)) { Hashtable pluginPref = (Hashtable) preferences.get(pluginName); /* Check if the plugin's preferences contain the given attribute */ if (pluginPref.containsKey(attribute)) /* Get the value for the attribute */ return (String) pluginPref.get(attribute); else { /* The attribute does not exist in the hash - report error */ message = "Unable to get the "+attribute+" attribute for "+pluginName; message += " : The attribute does not exist in "+username+"'s preferences."; throw new RavelerException(message); } //else } //if else { /* No preferences for the plugin exist in the hash - report error */ message = "Unable to get the "+attribute+" attribute for "+pluginName; message += " : "+username+"'s preferences contain no information for "+pluginName; throw new RavelerException(message); } } //getAttribute(String,String) /** * Updates an existing attribute * * @param pluginName a string that names the plugin * @param attribute a string that names the attribute * @param value a string that gives the deisired value of the named attribute. * @return a boolean success value */ public void modifyAttribute(String pluginName, String attribute, String value) throws RavelerException { String message; if( preferences.containsKey(pluginName) ) { /* if the plugin is already in the prefs hash, and the attribute isn't already * set for this plugin, return false */ Hashtable pluginPref = (Hashtable) preferences.get(pluginName); /* Check if the attribute is in the plugin's preferences */ if (pluginPref.containsKey(attribute)) { /* Set the attribute to the new value */ pluginPref.put(attribute,value); } //if else { /* The attribute does not exist in the hash - report error */ message = "Unable to modify the "+attribute+" attribute for "+pluginName; message += " : The attribute does not exist in "+username+"'s preferences."; throw new RavelerException(message); } //else } //if else { /* No preferences for the plugin exist in the hash - report error */ message = "Unable to get the "+attribute+" attribute for "+pluginName; message += " : "+username+"'s preferences contain no information for "+pluginName; throw new RavelerException(message); } //else } //modifiyAttribute(String,String,String) /** * Save the user's preferences to the disk, returning a boolean * success indicator. * * @return a boolean success value */ public void savePreferences() throws RavelerException { try { FileOutputStream outFileStream = new FileOutputStream(Raveler.WEBRAVELER_HOME + "/data/" + username+".dat"); ObjectOutputStream objOut = new ObjectOutputStream(outFileStream); objOut.writeObject(preferences); objOut.writeObject(activePlugins); objOut.flush(); objOut.close(); } catch(IOException ioe) { throw new RavelerException("Unable to write the user's preferences to disk"); } //catch } //savePreferences() /** * Read in the user's preferences from the disk, returning a boolean * success indicator. * * @return a boolean success value */ public void readPreferences() throws RavelerException{ try{ FileInputStream fileInput = new FileInputStream(Raveler.WEBRAVELER_HOME + "/data/" + username+".dat"); ObjectInputStream objInput = new ObjectInputStream(fileInput); preferences = (Hashtable) objInput.readObject(); activePlugins = (Vector) objInput.readObject(); objInput.close(); } catch (Exception e) { throw new RavelerException("Unable to read the user's preferences to disk"); } //catch } //readPreferences() /** * Sets the status of the given plugin to the default * * @param pluginName a string containing the name of the plugin * @return a boolean success value */ public void setDefaultStatus(String pluginName) throws RavelerException { if (preferences.containsKey(pluginName)) { /* Set the status to the default "disabled" for the plugin */ Hashtable pluginPref = (Hashtable) preferences.get(pluginName); if (pluginPref.containsKey("status")) { pluginPref.remove("status"); pluginPref.put("status","disabled"); } //if else pluginPref.put("status","disabled"); } //if else { /* No preferences for the plugin exist in the hash - report error */ String message = "Unable to set the default status for "+pluginName; message += ": "+username+"'s preferences contain no information for "+pluginName; throw new RavelerException(message); } //else } //setDefaultStatus(String) /** * Returns the current status of the given plugin * * @param pluginName a string containing the name of the plugin * @return a String containing the current status of the plugin */ public String getStatus(String pluginName) throws RavelerException { if (preferences.containsKey(pluginName)) { /* Get the status */ Hashtable pluginPref = (Hashtable) preferences.get(pluginName); return (String) pluginPref.get("status"); } //if else { String message = "Unable to get the status for "+pluginName; message += ": "+username+"'s preferences contain no information for "+pluginName; throw new RavelerException(message); } //else } //getStatus /** * Sets the status of the given plugin * * @param pluginName a string containing the name of the plugin * @return a boolean success value */ public void setStatus(String pluginName, String status) throws RavelerException { if (preferences.containsKey(pluginName)) { /* Set the status */ Hashtable pluginPref = (Hashtable) preferences.get(pluginName); pluginPref.remove("status"); pluginPref.put("status",status); } //if else { /* No preferences for the plugin exist in the hash - report error */ String message = "Unable to set the status for "+pluginName; message += ": "+username+"'s preferences contain no information for "+pluginName; throw new RavelerException(message); } //else } //getStatus }