001: /*
002: * Created on 22 Jul 2006
003: */
004: package uk.org.ponder.rsf.state.scope;
005:
006: import java.util.Map;
007:
008: import org.springframework.beans.factory.BeanNameAware;
009:
010: import uk.org.ponder.rsf.state.TokenStateHolder;
011: import uk.org.ponder.stringutil.StringList;
012:
013: /** Manages a set of beans within a named scope. The beans will be
014: * automatically preserved to the nominated TokenStateHolder at the end of
015: * every action request, and restored at the beginning of every request.
016: *
017: * The beans in storage will expire on the natural expiry of the TSH, or on
018: * triggering of destruction through one of the <code>destroy</code> methods
019: * on this bean, whichever is sooner.
020: * @author Antranig Basman (antranig@caret.cam.ac.uk)
021: *
022: */
023:
024: public class ScopedBeanManager implements BeanDestroyer, BeanNameAware {
025: public static final char SPEC_SPLIT_CHAR = '|';
026: private String scopeName;
027: private String copyPreservingBeans;
028:
029: private TokenStateHolder tokenStateHolder;
030: private Map destroyed;
031: private boolean exclusive;
032: private boolean alwaysPreserve;
033:
034: private StringList copyPreservingBeanList;
035: private StringList targetPreservingKeyList;
036: private BeanDestroyer destroyer;
037:
038: public void setDestroyedScopeMap(Map destroyed) {
039: this .destroyed = destroyed;
040: }
041:
042: public void setBeanDestroyer(BeanDestroyer destroyer) {
043: this .destroyer = destroyer;
044: }
045:
046: public void destroy() {
047: destroyer.destroy();
048:
049: }
050:
051: public void setScopeName(String scopeName) {
052: this .scopeName = scopeName;
053: }
054:
055: public String getScopeName() {
056: return scopeName;
057: }
058:
059: /** Set to <code>true</code> if multiple requests are to be prevented
060: * from accessing this scope simultaneously. A second request trying to
061: * access the same scope will block in its alterationWrapper until
062: * the first has concluded.
063: */
064: public void setExclusive(boolean exclusive) {
065: this .exclusive = exclusive;
066: }
067:
068: public boolean getExclusive() {
069: return exclusive;
070: }
071:
072: /** Set to <code>true</code> if this is a "bad" kind of scope (with the
073: * potential to violate HTTP semantics) that requires to be preserved
074: * on <code>RENDER</code> cycles as well as <code>ACTION</code> cycles.
075: */
076: public void setAlwaysPreserve(boolean alwaysPreserve) {
077: this .alwaysPreserve = alwaysPreserve;
078: }
079:
080: public boolean getAlwaysPreserve() {
081: return alwaysPreserve;
082: }
083:
084: public void setCopyPreservingBeans(String copyPreservingBeans) {
085: StringList specs = StringList.fromString(copyPreservingBeans);
086: if (copyPreservingBeans.indexOf(SPEC_SPLIT_CHAR) != -1) {
087: copyPreservingBeanList = new StringList();
088: targetPreservingKeyList = new StringList();
089: for (int i = 0; i < specs.size(); ++i) {
090: String spec = specs.stringAt(i);
091: int splitpos = spec.indexOf(SPEC_SPLIT_CHAR);
092: String bean = null, key = null;
093: if (splitpos == -1) {
094: bean = key = spec;
095: } else {
096: bean = spec.substring(0, splitpos);
097: key = spec.substring(splitpos + 1);
098: }
099: copyPreservingBeanList.add(bean);
100: targetPreservingKeyList.add(key);
101: }
102: } else {
103: copyPreservingBeanList = specs;
104: }
105: }
106:
107: public StringList getCopyPreservingBeanList() {
108: return copyPreservingBeanList;
109: }
110:
111: public StringList getTargetPreservingKeyList() {
112: return targetPreservingKeyList;
113: }
114:
115: public void setTokenStateHolder(TokenStateHolder tokenStateHolder) {
116: this .tokenStateHolder = tokenStateHolder;
117: }
118:
119: public TokenStateHolder getTokenStateHolder() {
120: return tokenStateHolder;
121: }
122:
123: public void setBeanName(String name) {
124: scopeName = name;
125: }
126:
127: }
|