01: /*
02: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com> and
03: * Steven Grimm <koreth[remove] at midwinter dot com>
04: * Distributed under the terms of either:
05: * - the common development and distribution license (CDDL), v1.0; or
06: * - the GNU Lesser General Public License, v2.1 or later
07: * $Id: SimpleSessionManagerFactory.java 3643 2007-01-12 15:29:45Z gbevin $
08: */
09: package com.uwyn.rife.authentication.sessionmanagers;
10:
11: import com.uwyn.rife.authentication.SessionManager;
12: import com.uwyn.rife.authentication.elements.exceptions.UnknownSessionManagerFactoryClassException;
13: import com.uwyn.rife.ioc.HierarchicalProperties;
14: import com.uwyn.rife.ioc.exceptions.MandatoryPropertyMissingException;
15: import com.uwyn.rife.ioc.exceptions.PropertyValueException;
16: import java.util.HashMap;
17:
18: /**
19: * Simple caching session manager factory. This keeps a cache of session manager
20: * instances by name. This is used, for example, to create
21: * {@link MemorySessions} objects; it may be used for any session manager that
22: * doesn't require configuration information at startup time.
23: * <p>
24: * Element properties used:
25: * <dl>
26: * <dt>{@value #PROPERTYNAME_MANAGER_CLASS}</dt>
27: * <dd>Name of the session manager class. If the class name is not fully
28: * qualified, the package name
29: * {@code com.uwyn.rife.authentication.sessionmanagers}
30: * will be assumed.</dd>
31: * <dt>{@value #PROPERTYNAME_MANAGER_ID}</dt>
32: * <dd>Unique ID for this session manager instance. Optional. Use this if you
33: * need different elements to maintain separate session stores.</dd>
34: * </dl>
35: * <p>
36: * If you need logic other than a simple "new" for your session managers,
37: * implement {@link SessionManagerFactory} instead.
38: *
39: * @author Steven Grimm (koreth[remove] at midwinter dot com)
40: * @author Geert Bevin (gbevin[remove] at uwyn dot com)
41: * @version $Revision: 3643 $
42: * @see SessionManager
43: * @see SessionManagerFactory
44: * @since 1.6
45: */
46: public class SimpleSessionManagerFactory implements
47: SessionManagerFactory {
48: /** Element property that specifies the ID for a session manager instance. */
49: public static final String PROPERTYNAME_MANAGER_ID = "sessionmanager_id";
50:
51: /** Element property that specifies the class name for session managers. */
52: public static final String PROPERTYNAME_MANAGER_CLASS = "sessionmanager_class";
53:
54: private static HashMap<String, SessionManager> mSessionManagers = new HashMap<String, SessionManager>();
55:
56: public SessionManager getManager(HierarchicalProperties properties)
57: throws PropertyValueException {
58: String className = properties
59: .getValueString(PROPERTYNAME_MANAGER_CLASS);
60: if (null == className) {
61: throw new MandatoryPropertyMissingException(
62: PROPERTYNAME_MANAGER_CLASS);
63: }
64: if (className.indexOf(".") < 0) {
65: className = SimpleSessionManagerFactory.class.getPackage()
66: .getName()
67: + "." + className;
68: }
69:
70: // Include the class name in the cache key so we don't give the caller
71: // an unexpected session manager class if there's an identifier
72: // collision (e.g. because the caller isn't specifying an ID
73: // explicitly.)
74: String identifier = className
75: + ":"
76: + properties
77: .getValueString(PROPERTYNAME_MANAGER_ID, "");
78:
79: SessionManager session_manager = mSessionManagers
80: .get(identifier);
81:
82: if (null == session_manager) {
83: try {
84: Class<SessionManager> clazz = (Class) Class
85: .forName(className);
86: session_manager = clazz.newInstance();
87: mSessionManagers.put(identifier, session_manager);
88: } catch (Exception e) {
89: throw new UnknownSessionManagerFactoryClassException(
90: className, e);
91: }
92: }
93:
94: return session_manager;
95: }
96: }
|