standup.profiling
Class ProfileManager

java.lang.Object
  extended by standup.profiling.ProfileManager

public class ProfileManager
extends Object

The main class that should be used to create, retrieve, and save user profiles.

All methods in ProfileManager are static, so there is never a need to create an instantiation. It is the responsibility of the code that uses the ProfileManager to ensure that it has been initialized before usage. This is done by calling one of the various #initialize() methods.

The general usage of the ProfileManager is as follows:

  1. Initialize the ProfileManager by calling either initialize(), initialize(File), or #initialize(File, File, File).
  2. Create a user profile by calling createUser(String, Profileable).
  3. Set a Profileable object to use the appropriate profile by calling useProfile(String, Profileable).
  4. Save any modified profile by calling saveCurrentProfile(Profileable).

Author:
Ruli Manurung

Field Summary
private static String currentUserName
           
private static File defaultBaseDataDir
           
private static boolean initialized
          Indicates whether this ProfileManager has been initialized yet.
static String loginProfileName
           
private static File optionsPackageDirectory
           
private static File userRootDirectory
           
 
Constructor Summary
ProfileManager()
           
 
Method Summary
static boolean addFrontendLoginProfile(Profile loginFrontendProfile)
          Tries to create the 'login' user profile, and adds the given Profile, assumed to be a ProfileCloudFrontend as 'clouds.profile';
static String chooseProfileName(Component wholePane)
          A utility method that brings up a JOptionPane input dialog that can be used to select a profile name from the list of available profiles.
private static void copyCustomLexiconFromJAR(File userLexiconDirectory, String name)
          Copies the custom lexicon with the given name from within the STANDUP jar (from /standup/resources/xml) into the user's lexicon directory.
private static void copyCustomLexiconFromJAR(File userLexiconDirectory, String jarName, String newName)
          Copies the custom lexicon with the given jarName from within the STANDUP jar (from /standup/resources/xml) into the user's lexicon directory as newName.
private static void copyTopicFromJAR(File userLexiconDirectory, String name)
          Copies the topic with the given name from within the STANDUP jar (from /standup/resources/xml) into the user's lexicon directory.
private static void copyTopicFromJAR(File userLexiconDirectory, String jarName, String newName)
          Copies the topic with the given jarName from within the STANDUP jar (from /standup/resources/xml) into the user's lexicon directory as newName.
static boolean createDefaultLexicons(String username)
          Copies over various topics and custom lexicons from within the STANDUP jar (from /standup/resources/xml) into the given user's lexicon directory.
static void createUser(String name)
          Creates a user and logfile directory for the user with the given name.
static void createUser(String name, Profileable profileable)
          Creates a user directory for the user with the given name, and creates in it a default profile for the given Profileable.
static String getCurrentUserName()
          Returns the name of the 'current' user, i.e.
static File getDefaultOptionsPackage()
          Returns the default options package file.
static File getLexiconDirectory(String name)
          Returns the lexicon directory for the user with the given name.
static File getLogDirectory(String name)
          Returns the logfile directory for the user with the given name.
static List<String> getNamesList()
          Returns a List<String> of users for whom there are existing Profiles.
static File getOptionsDirectory()
          Returns the designated directory for options packages.
static File getUserDirectory(String name)
          Returns the profile directory for the user with the given name.
static void initialize()
          Sets up the necessary directories for loading and saving user profiles.
static void initialize(File baseDir)
          Sets up the necessary directories for loading and saving user profiles.
static void initialize(File userDir, File optionsDir)
          Sets up the necessary directories for loading and saving user profiles.
static boolean profileExists(String name, Profileable profileable)
          Returns true if the profile associated with the given Profileable for the user with the given name exists, false otherwise.
static boolean quickSaveCurrentProfile(Profileable profileable)
          Saves the profile associated with the given Profileable for the current user.
private static Profileable reflectionBackendGetLexicalComponents(Profileable assumedBackend)
           
private static Profileable reflectionFrontendGetBackend(Profileable assumedFrontend)
           
static boolean saveCurrentProfile(Profileable profileable)
          Saves the profile associated with the given Profileable for the current user, and any other auxiliary files that make up the profile.
static void useProfile(String name, Profileable profileable)
          Syntactic sugar for the 'cascaded' useProfile(String, Profileable, boolean) method -- assumes 'first time' every time.
static void useProfile(String name, Profileable[] profileables)
          Syntactic sugar for the 'non-cascaded' useProfile(String, Profileable[], boolean) method -- assumes 'first time' every time.
static void useProfile(String name, Profileable[] profileables, boolean repeat)
          An alternative method for using a user profile.
static boolean useProfile(String name, Profileable[] profileables, Profile[] profiles, boolean repeat)
          Alternative method of using profiles, where the Profiles themselves are provided as parameters, instead of being read from disk.
static void useProfile(String name, Profileable profileable, boolean repeat)
          The preferred method for using a user profile.
static boolean userExists(String name)
          Returns true if the profile directory for the user with the given name exists, false otherwise.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

loginProfileName

public static final String loginProfileName
See Also:
Constant Field Values

defaultBaseDataDir

private static final File defaultBaseDataDir

userRootDirectory

private static File userRootDirectory

optionsPackageDirectory

private static File optionsPackageDirectory

currentUserName

private static String currentUserName

initialized

private static boolean initialized
Indicates whether this ProfileManager has been initialized yet.

See Also:
initialize(), initialize(File), #initialize(File, File, File)
Constructor Detail

ProfileManager

public ProfileManager()
Method Detail

initialize

public static void initialize()
                       throws ProfileException
Sets up the necessary directories for loading and saving user profiles.

This method uses the following defaults, where . is the directory where the standup.jar is located:

Throws:
ProfileException

initialize

public static void initialize(File baseDir)
                       throws ProfileException
Sets up the necessary directories for loading and saving user profiles.

This method uses the following defaults, where . is the directory specified by baseDir:

Parameters:
baseDir - the base directory
Throws:
ProfileException

initialize

public static void initialize(File userDir,
                              File optionsDir)
                       throws ProfileException
Sets up the necessary directories for loading and saving user profiles. If the given directories do not exist, it attempts to create them. If it fails to do so, it throws a ProfileException.

Does *NOT* create the default 'login profile' for CloudFrontend, so call addFrontendLoginProfile(Profile) if necessary.

Parameters:
userDir - the directory that contains the various user profile directories
optionsDir - the directory that contains the various user options packages
Throws:
ProfileException

createDefaultLexicons

public static boolean createDefaultLexicons(String username)
                                     throws ProfileException
Copies over various topics and custom lexicons from within the STANDUP jar (from /standup/resources/xml) into the given user's lexicon directory.

It creates:

NOTE: If it fails to find the 'full' resources in the jar, it will try to load the "jokeonly_" variants, i.e. subsets of the above resources that are intersected with the set of lexemes known to yield jokes at the schema level (see database construction document for more details).

Parameters:
username -
Returns:
Throws:
ProfileException

copyTopicFromJAR

private static void copyTopicFromJAR(File userLexiconDirectory,
                                     String name)
Copies the topic with the given name from within the STANDUP jar (from /standup/resources/xml) into the user's lexicon directory. If it can't find the file in the jar, it will try the "jokeonly_" variant, i.e. the version included in the joke generation API and STANDUP system jars.

Parameters:
userLexiconDirectory -
name -

copyTopicFromJAR

private static void copyTopicFromJAR(File userLexiconDirectory,
                                     String jarName,
                                     String newName)
Copies the topic with the given jarName from within the STANDUP jar (from /standup/resources/xml) into the user's lexicon directory as newName. If it can't find the file in the jar, it will try the "jokeonly_" variant, i.e. the version included in the joke generation API and STANDUP system jars.

Parameters:
userLexiconDirectory -
jarName -
newName -

copyCustomLexiconFromJAR

private static void copyCustomLexiconFromJAR(File userLexiconDirectory,
                                             String name)
Copies the custom lexicon with the given name from within the STANDUP jar (from /standup/resources/xml) into the user's lexicon directory. If it can't find the file in the jar, it will try the "jokeonly_" variant, i.e. the version included in the joke generation API and STANDUP system jars.

Parameters:
userLexiconDirectory -
name -

copyCustomLexiconFromJAR

private static void copyCustomLexiconFromJAR(File userLexiconDirectory,
                                             String jarName,
                                             String newName)
Copies the custom lexicon with the given jarName from within the STANDUP jar (from /standup/resources/xml) into the user's lexicon directory as newName. If it can't find the file in the jar, it will try the "jokeonly_" variant, i.e. the version included in the joke generation API and STANDUP system jars.

Parameters:
userLexiconDirectory -
jarName -
newName -

addFrontendLoginProfile

public static boolean addFrontendLoginProfile(Profile loginFrontendProfile)
                                       throws ProfileException
Tries to create the 'login' user profile, and adds the given Profile, assumed to be a ProfileCloudFrontend as 'clouds.profile';

Parameters:
loginFrontendProfile -
Returns:
true if successful, or false if the login profile is found to already exist
Throws:
ProfileException

getOptionsDirectory

public static File getOptionsDirectory()
                                throws ProfileException
Returns the designated directory for options packages.

Returns:
Throws:
ProfileException

getDefaultOptionsPackage

public static File getDefaultOptionsPackage()
                                     throws ProfileException
Returns the default options package file.

Returns:
Throws:
ProfileException

getUserDirectory

public static File getUserDirectory(String name)
                             throws ProfileException
Returns the profile directory for the user with the given name.

Parameters:
name -
Returns:
Throws:
ProfileException

getLogDirectory

public static File getLogDirectory(String name)
                            throws ProfileException
Returns the logfile directory for the user with the given name.

Parameters:
name -
Returns:
Throws:
ProfileException

getLexiconDirectory

public static File getLexiconDirectory(String name)
                                throws ProfileException
Returns the lexicon directory for the user with the given name.

Parameters:
name -
Returns:
Throws:
ProfileException

userExists

public static boolean userExists(String name)
                          throws ProfileException
Returns true if the profile directory for the user with the given name exists, false otherwise.

Parameters:
name -
Returns:
Throws:
ProfileException

profileExists

public static boolean profileExists(String name,
                                    Profileable profileable)
                             throws ProfileException
Returns true if the profile associated with the given Profileable for the user with the given name exists, false otherwise.

Parameters:
name -
profileable -
Returns:
Throws:
ProfileException

getNamesList

public static List<String> getNamesList()
                                 throws ProfileException
Returns a List<String> of users for whom there are existing Profiles.

Returns:
Throws:
ProfileException

getCurrentUserName

public static String getCurrentUserName()
Returns the name of the 'current' user, i.e. the user for whom the last #useProfile method was called.

Returns:

createUser

public static void createUser(String name)
                       throws ProfileException
Creates a user and logfile directory for the user with the given name.

Parameters:
name - the name of the user for whom the profile directory is being created
Throws:
ProfileException

reflectionFrontendGetBackend

private static Profileable reflectionFrontendGetBackend(Profileable assumedFrontend)

reflectionBackendGetLexicalComponents

private static Profileable reflectionBackendGetLexicalComponents(Profileable assumedBackend)

createUser

public static void createUser(String name,
                              Profileable profileable)
                       throws ProfileException
Creates a user directory for the user with the given name, and creates in it a default profile for the given Profileable. Additionally, it performs a 'cascading' creation of default profiles with the following dependencies:
                 CloudFrontend -> Backend -> LexicalComponents
 
For example, given a CloudFrontend, it will create a profile for CloudFrontend, Backend, and LexicalComponents.

This is a convenience method which is probably what is needed most of the time. For more specialized requirements (e.g. the CloudFrontend "login" profile, which doesn't have a joke generation profile), consider using #createUser(String) coupled with manual calling of Profileable#createProfile(File). NOTE: this method 'hardcodes' the dependency above and uses some rather hacky Java reflection methods to implement it without actually importing the relevant classes -- this is to ensure that the joke generator API works without needing standup.clouds, and the lexicon API works without needing standup.joke, etc. It makes quite a few assumptions about class names, e.g. "standup.clouds.CloudFrontend", "standup.joke.Backend*", and "standup.lexicon.LexicalComponents", etc.

Parameters:
name - the name of the user for whom the profile directory is being created
profileable - the Profileable for which all required Profiles will be created
Throws:
ProfileException

useProfile

public static void useProfile(String name,
                              Profileable profileable)
                       throws ProfileException
Syntactic sugar for the 'cascaded' useProfile(String, Profileable, boolean) method -- assumes 'first time' every time.

Parameters:
name -
profileable -
Throws:
ProfileException

useProfile

public static void useProfile(String name,
                              Profileable[] profileables)
                       throws ProfileException
Syntactic sugar for the 'non-cascaded' useProfile(String, Profileable[], boolean) method -- assumes 'first time' every time.

Parameters:
name -
profileables -
Throws:
ProfileException

useProfile

public static void useProfile(String name,
                              Profileable profileable,
                              boolean repeat)
                       throws ProfileException
The preferred method for using a user profile. Loads (and uses) the profile associated with the given Profileable found in the directory for the given user.

Additionally, it performs a 'cascading' use of required profiles with the following dependency:

                 CloudFrontend -> Backend -> LexicalComponents
 
For example, given a CloudFrontend cf, it will load the profile for cf.getBackend() and also cf.getBackend().getLexicalComponents().

The repeat parameter determines whether this is the first time useProfile is being called for the given user during this session (e.g. from the login screen) or not (e.g. after changing options in the control panel).

Finally, it sets the given user name as the 'current user' name.

This is a convenience method which is probably what is needed most of the time. For more specialized requirements (e.g. the CloudFrontend "login" profile, which doesn't have a joke generation profile), consider using #useProfile(String,Profileable[],boolean) instead. NOTE: this method 'hardcodes' the dependency above and uses some rather hacky Java reflection methods to implement it without actually importing the relevant classes -- this is to ensure that the joke generator API works without needing standup.clouds, and the lexicon API works without needing standup.joke, etc. It makes quite a few assumptions about class names, e.g. "standup.clouds.CloudFrontend", "standup.joke.Backend*", and "standup.lexicon.LexicalComponents", etc.

Parameters:
name -
profileable -
repeat -
Throws:
ProfileException

useProfile

public static void useProfile(String name,
                              Profileable[] profileables,
                              boolean repeat)
                       throws ProfileException
An alternative method for using a user profile. Loads (and uses) the profile associated with the given Profileables found in the directory for the given user, but does not do the 'cascading' use of dependent profiles that useProfile(String, Profileable, boolean) performs.

The repeat parameter determines whether this is the first time useProfile is being called for the given user during this session (e.g. from the login screen) or not (e.g. after changing options in the control panel).

Finally, it sets the given user name as the 'current user' name.

Parameters:
name -
profileables - an array of Profileables that are to use the user's profile
repeat -
Throws:
ProfileException

useProfile

public static boolean useProfile(String name,
                                 Profileable[] profileables,
                                 Profile[] profiles,
                                 boolean repeat)
                          throws ProfileException
Alternative method of using profiles, where the Profiles themselves are provided as parameters, instead of being read from disk. This is used, for example, by PlaybackTool, where the Profiles are obtained from the log file. It still checks, however, for the presence of the appropriate profile directories and files, and returns false if they don't exist.

Parameters:
name -
profileables -
profiles -
repeat -
Returns:
Throws:
ProfileException

saveCurrentProfile

public static boolean saveCurrentProfile(Profileable profileable)
                                  throws ProfileException
Saves the profile associated with the given Profileable for the current user, and any other auxiliary files that make up the profile.

Parameters:
profileable -
Returns:
Throws:
ProfileException

quickSaveCurrentProfile

public static boolean quickSaveCurrentProfile(Profileable profileable)
                                       throws ProfileException
Saves the profile associated with the given Profileable for the current user.

Parameters:
profileable -
Returns:
Throws:
ProfileException

chooseProfileName

public static String chooseProfileName(Component wholePane)
                                throws ProfileException
A utility method that brings up a JOptionPane input dialog that can be used to select a profile name from the list of available profiles.

Throws:
ProfileException