001: package org.wings.header;
002:
003: import org.wings.SFrame;
004: import org.wings.session.SessionManager;
005: import org.wings.util.SessionLocal;
006:
007: import java.util.*;
008:
009: /**
010: * A class to manage headers which should be appended to all frames within a wingS session.
011: */
012: public final class SessionHeaders {
013:
014: protected static final SessionLocal<SessionHeaders> INSTANCE = new SessionLocal<SessionHeaders>() {
015: protected SessionHeaders initialValue() {
016: return new SessionHeaders();
017: }
018: };
019:
020: protected final List<Header> headerList = new ArrayList<Header>();
021: protected final Map<Header, Integer> linkCounts = new HashMap<Header, Integer>();
022:
023: /**
024: * Session local manager class.
025: */
026: private SessionHeaders() {
027: }
028:
029: /**
030: * @return The session local instance of this class.
031: */
032: public static SessionHeaders getInstance() {
033: return INSTANCE.get();
034: }
035:
036: /**
037: * @return The headers currently attached to all frames inside this wingS session.
038: */
039: public List<Header> getHeaders() {
040: return Collections.unmodifiableList(headerList);
041: }
042:
043: public void registerHeaders(List<? extends Header> headers) {
044: registerHeaders(headerList.size(), headers);
045: }
046:
047: public void registerHeader(Header header) {
048: registerHeader(headerList.size(), header);
049: }
050:
051: public void registerHeaders(int index,
052: List<? extends Header> headers) {
053: for (Header header : headers) {
054: registerHeader(index++, header);
055: }
056: }
057:
058: public void registerHeader(int index, Header header) {
059: if (header == null)
060: throw new IllegalArgumentException(
061: "Header must not be null!");
062:
063: if (getLinkCount(header) == 0) {
064: Set<SFrame> frames = SessionManager.getSession()
065: .getFrames();
066: for (SFrame frame : frames) {
067: (frame).addHeader(header);
068: }
069: headerList.add(index, header);
070: }
071: incrementLinkCount(header);
072: }
073:
074: public void deregisterHeaders(List<? extends Header> headers) {
075: for (Header header : headers) {
076: deregisterHeader(header);
077: }
078: }
079:
080: public void deregisterHeader(Header header) {
081: if (header == null)
082: throw new IllegalArgumentException(
083: "Header must not be null!");
084:
085: decrementLinkCount(header);
086:
087: if (getLinkCount(header) == 0) {
088: Set<SFrame> frames = SessionManager.getSession()
089: .getFrames();
090: for (SFrame frame : frames) {
091: (frame).removeHeader(header);
092: }
093: headerList.remove(header);
094: }
095: }
096:
097: protected int getLinkCount(Header header) {
098: Integer linkCount = linkCounts.get(header);
099: if (linkCount == null) {
100: return 0;
101: } else {
102: return linkCount.intValue();
103: }
104: }
105:
106: protected void incrementLinkCount(Header header) {
107: linkCounts.put(header, new Integer(getLinkCount(header) + 1));
108: }
109:
110: protected void decrementLinkCount(Header header) {
111: linkCounts
112: .put(
113: header,
114: new Integer(
115: getLinkCount(header) > 0 ? getLinkCount(header) - 1
116: : 0));
117: }
118:
119: }
|