|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--org.chwf.registry.UserRegistry
A class for caching user-specific data. It provides an abstraction layer
for components that can run both in single-user applications (such as test
applications) and multi-user applications (such as a web application).
Single-user application should initialize a global registry by using the
GlobalUserRegistry.init()
method:
GlobalUserRegistry.init();
Multi-user applications should cache their registry in the user's session
and transfer them to this class at the beginning of each thread of execution
using the initLocalRegistry(UserRegistry)
. This method stores the
registry in a ThreadLocal
variable, so that it can be retrieved
anywhere in the thread of execution. This local registry should be released
when the thread of execution is complete using the
releaseLocalRegistry()
method:
// A servlet example: UserRegistry registry = (UserRegistry) session.getAttribute("UserRegistry"); UserRegistry.initLocalRegistry(registy); // Do work ... UserRegistry.releaseLocalRegistry();
Releasing the local registry is important because multi-user applications
often use thread-pooling, and failing to release the registry might mean that
the ThreadLocal
variable could bleed between users. Failure to
release local registries could lead to memory leaks and weird errors.
The various initialization methods are protected
, so that the
user registry can only be initialized by subclasses. For each application,
you should create a subclass of the UserRegistry
class with the
correct initializations for that application. See GlobalUserRegistry
and ServletData
for examples. The registry
may be initialized in one way for production and another for testing.
Whether the application is using global or local registries, data inside the registry is manipulated in the same way:
UserRegistry.setRegistryEntry("My data", value); Object data = UserRegistry.getRegistryEntry("My data"); UserRegistry.removeRegistryEntry("My data");
Ideally, you should create encapsulating classes that prevent direct access to the user registry. You should use a good naming convention (based on class names) to minimize name collisions for information stored in the registry.
public class UserLocale { private static final String REGISTRY_NAME = UserLocale.class.getName(); public Locale getLocale() { return (Locale) UserRegistry.getRegistryEntry(UserLocale.REGISTRY_NAME); } public void setLocale(Locale locale) { UserRegistry.overwriteRegistryEntry(UserLocale.REGISTRY_NAME, locale); } }
Finally, because the registry is global and global data is bad, you should use it sparingly.
Constructor Summary | |
UserRegistry()
|
Method Summary | |
static UserRegistry |
getRegistry()
Retrieve the registry. |
static java.lang.Object |
getRegistryEntry(java.lang.String name)
Get registry entry value. |
static java.lang.Object |
getSingleton(java.lang.Class cls)
Get class-based, user-specific singleton for the given class, creating a new one if none exists. |
static java.lang.Object |
getSingleton(java.lang.String cls)
Get class-based, user-specific singleton for the given class, creating a new one if none exists. |
protected static void |
initGlobalRegistry(UserRegistry registry)
Initialize a global registry. |
protected static void |
initLocalRegistry(UserRegistry registry)
Initialize a local registry. |
static boolean |
isInitialized()
Returns true if the registry is initialized. |
static java.lang.Object |
overwriteRegistryEntry(java.lang.String name,
java.lang.Object value)
Overwrite registry entry. |
protected static void |
releaseGlobalRegistry()
Release the global registry. |
protected static void |
releaseLocalRegistry()
Release the local registry. |
static void |
releaseSingleton(java.lang.Class cls)
Release registry singleton for the given class. |
static java.lang.Object |
removeRegistryEntry(java.lang.String name)
Remove and return registry entry. |
static void |
setRegistryEntry(java.lang.String name,
java.lang.Object value)
Set registry entry value. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
public UserRegistry()
Method Detail |
public static boolean isInitialized()
public static UserRegistry getRegistry() throws RegistryException
CustomRegistry custom = (CustomRegistry) UserRegistry.getRegistry(); custom.getCustomData();
RegistryException
- If the registry is not initialized.public static java.lang.Object getRegistryEntry(java.lang.String name) throws RegistryException
name
- The registry entry name.RegistryException
- If the registry is not initialized.public static void setRegistryEntry(java.lang.String name, java.lang.Object value) throws RegistryException
name
- The registry entry name.value
- The registry entry value.RegistryException
- If the registry is not initialized or an
existing value is overwritten.public static java.lang.Object overwriteRegistryEntry(java.lang.String name, java.lang.Object value) throws RegistryException
name
- The registry entry name.value
- The registry entry value.RegistryException
- If the registry is not initialized.public static java.lang.Object removeRegistryEntry(java.lang.String name) throws RegistryException
name
- The registry entry name.RegistryException
- If the registry is not initialized.public static java.lang.Object getSingleton(java.lang.Class cls) throws RegistryException
cls
- The class for the singleton.RegistryException
- If the registry is not initialized or
singleton creation fails.public static java.lang.Object getSingleton(java.lang.String cls) throws RegistryException
cls
- The class for the singleton as a String.RegistryException
- If the registry is not initialized or
singleton creation fails.public static void releaseSingleton(java.lang.Class cls) throws RegistryException
cls
- The class for the singleton.RegistryException
- If the registry is not initialized.protected static void initLocalRegistry(UserRegistry registry)
registry
- The registry.protected static void releaseLocalRegistry()
protected static void initGlobalRegistry(UserRegistry registry)
registry
- The registry.protected static void releaseGlobalRegistry()
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |