01: /*
02: * Created on Aug 5, 2005
03: */
04: package uk.org.ponder.rsf.state;
05:
06: import java.util.Date;
07: import java.util.Map;
08:
09: import uk.org.ponder.reflect.ReflectiveCache;
10: import uk.org.ponder.rsac.GlobalBeanAccessor;
11: import uk.org.ponder.util.Logger;
12:
13: /**
14: * The repository of all inter-request state in RSF. This is held in entries of
15: * TokenRequestState in a (probably very large) ConcurrentHashMap which is
16: * perpetually expired on a TTL basis. More rapid expiry may occur through
17: * explicit session-closing and wizard-ending procedures.
18: * <p>
19: * High-requirement applications would presumably reimplement this class to push
20: * this state into a database or some sort of clustered broadcast.
21: *
22: * NB!! Expiry not yet implemented! Do not use this class in production!
23: * @author Antranig Basman (antranig@caret.cam.ac.uk)
24: */
25: public class InMemoryTSH implements TokenStateHolder {
26:
27: // a map of String error token IDs to ErrorStateEntries.
28: // the idea is that this be a timeout cache which is cleared out every
29: // hour or so... all this functionality to be moved to another class.
30: private Map tokencache;
31:
32: private int expiryseconds;
33:
34: public void setReflectiveCache(ReflectiveCache reflectiveCache) {
35: //this.reflectiveCache = reflectiveCache;
36: reflectiveCache.getConcurrentMap(16);
37: }
38:
39: TokenState getTokenStateRaw(String tokenID) {
40: return (TokenState) tokencache.get(tokenID);
41: }
42:
43: // TODO: The plan is that this thread is JVM-wide, and InMemoryTSH
44: // are in charge of attaching and detaching their caches to it on init and
45: // destruction. Managing the necessary IPC will be an annoyance. ANDY!!
46: private void launchExpirralThread() {
47:
48: }
49:
50: /**
51: * Returns any TokenRequestState object with the specified ID.
52: *
53: * @return The required TRS object, or <code>null</code> if none is stored.
54: */
55: public Object getTokenState(String tokenID) {
56: TokenState state = getTokenStateRaw(tokenID);
57: return state == null ? null : state.payload;
58: }
59:
60: /** Stores the supplied TokenRequestState object in the repository */
61: public void putTokenState(String tokenID, Object payload) {
62:
63: TokenState trs = new TokenState();
64: trs.payload = payload;
65: trs.tokenID = tokenID;
66: Date current = new Date();
67: long forwardsecs = current.getTime() + expiryseconds * 1000;
68: trs.expiry = new Date(forwardsecs);
69: tokencache.put(trs.tokenID, trs);
70: }
71:
72: public void clearTokenState(String tokenID) {
73:
74: Logger.log
75: .info("Token state cleared from InMemoryTSH for token "
76: + tokenID);
77: tokencache.remove(tokenID);
78: }
79:
80: public void setExpirySeconds(int seconds) {
81: this .expiryseconds = seconds;
82: }
83:
84: public String getId() {
85: return null;
86: }
87:
88: }
|