001: /*
002: * Activator.java May 2004
003: *
004: * Copyright (C) 2004, Niall Gallagher <niallg@users.sf.net>
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: * GNU Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General
016: * Public License along with this library; if not, write to the
017: * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
018: * Boston, MA 02111-1307 USA
019: */
020:
021: package simple.http.session;
022:
023: import simple.util.lease.Lease;
024: import simple.util.lease.LeaseException;
025: import simple.util.net.Cookie;
026: import simple.http.State;
027:
028: /**
029: * The <code>Activator</code> object is responsible for creating
030: * and retrieving <code>Session</code> objects. This deals with
031: * <code>Module</code> objects, which it uses to create sessions
032: * and renew the leases for the sessions. To ensure maximum
033: * flexibility in renewing leases for the system this makes use
034: * of a <code>Maintainer</code> component. This component is
035: * used to renew the lease for a session once that session has
036: * been referenced. If the session lease cannot be renewed this
037: * will attempt to create a new one.
038: *
039: * @author Niall Gallagher
040: *
041: * @see simple.http.session.Maintainer
042: * @see simple.http.session.Identifier
043: */
044: final class Activator {
045:
046: /**
047: * This is used to manage session modules for the system.
048: */
049: private Registry registry;
050:
051: /**
052: * This is used to renew the leases for session modules.
053: */
054: private Maintainer manager;
055:
056: /**
057: * This is used to identify the session reference cookie.
058: */
059: private Identifier lookup;
060:
061: /**
062: * Constructor for the <code>Activator</code> object. This is
063: * used to create a session management system that will lease
064: * the sessions created for a peroid of time. Each time this
065: * instance is used to activate the session the lease is
066: * renewed for the same fixed peroid of time.
067: */
068: public Activator() {
069: this (new Registry());
070: }
071:
072: /**
073: * Constructor for the <code>Activator</code> object. This is
074: * used to create a session management system that will lease
075: * the sessions created for a peroid of time. Each time this
076: * instance is used to activate the session the lease is
077: * renewed for the same fixed peroid of time.
078: *
079: * @param registry this is used to manage session modules
080: */
081: private Activator(Registry registry) {
082: this .manager = MaintainerFactory.getInstance();
083: this .lookup = IdentifierFactory.getInstance();
084: this .registry = registry;
085: }
086:
087: /**
088: * This will either retrieve an active session or create a
089: * new one. The <code>Session</code> object is created if the
090: * <code>State</code> does not have a reference to an active
091: * session. If a session has expired and the session reference
092: * is provided, this will create a new session using the
093: * existing reference. Once a <code>Session</code> has been
094: * created a <code>Cookie</code> is set with its reference.
095: *
096: * @param state contains all the cookies with a request
097: *
098: * @return returns an active <code>Session</code> object
099: */
100: public Session activate(State state) {
101: return activate(state, null);
102: }
103:
104: /**
105: * This will either retrieve an active session or create a
106: * new one. The <code>Session</code> object is created if the
107: * <code>State</code> does not have a reference to an active
108: * session. If a session has expired and the session reference
109: * is provided, this will create a new session using the
110: * existing reference. Once a <code>Session</code> has been
111: * created a <code>Cookie</code> is set with its reference.
112: *
113: * @param state contains all the cookies with a request
114: * @param data this is used to initialize the storage object
115: *
116: * @return returns an active <code>Session</code> object
117: */
118: public Session activate(State state, Object data) {
119: return activate(lookup.getIdentity(state), data);
120: }
121:
122: /**
123: * This will either retrieve an active session or create a
124: * new one. The <code>Session</code> object is created if the
125: * issued <code>Cookie</code> is not a reference to an active
126: * session. If a session has expired the session reference
127: * provided is used to reference the new session.
128: *
129: * @param cookie the cookie used as the session reference
130: * @param data this is used to initialize the storage object
131: *
132: * @return returns an active <code>Session</code> object
133: */
134: private Session activate(Cookie cookie, Object data) {
135: try {
136: Module module = retrieve(cookie, data);
137: return new Delegate(module, cookie);
138: } catch (LeaseException e) {
139: return null;
140: }
141: }
142:
143: /**
144: * This will retrieve or create a <code>Module</code> object
145: * to represent a <code>Session</code>. The module is created
146: * if the issued <code>Cookie</code> is not a reference to an
147: * active session. Once the module is created the lease for
148: * it is renewed using the <code>Maintainer</code> object.
149: *
150: * @param cookie the cookie used as the session reference
151: * @param data this is used to initialize the storage object
152: *
153: * @return returns an active <code>Module</code> object
154: */
155: private Module retrieve(Cookie cookie, Object data)
156: throws LeaseException {
157: Module module = registry.lookup(cookie, data);
158: try {
159: Lease lease = module.getLease();
160: Store store = module.getStore();
161:
162: manager.renew(lease, store);
163: } catch (LeaseException e) {
164: throw new LeaseException("Lease expired");
165: }
166: return module;
167: }
168: }
|