001: /**
002: * $Id: ResponseBufferGroup.java,v 1.3 2004/03/03 19:31:28 batchu Exp $
003: * Copyright 2003 Sun Microsystems, Inc.
004: * All rights reserved.
005: * Use of this product is subject to license terms.
006: * Federal Acquisitions: Commercial Software -- Government Users
007: * Subject to Standard License Terms and Conditions.
008: *
009: * Sun, Sun Microsystems, the Sun logo, and Sun ONE are trademarks or
010: * registered trademarks of Sun Microsystems, Inc. in the
011: * United States and other countries.
012: */package com.sun.mobile.responsebuffer;
013:
014: import java.util.SortedMap;
015: import java.util.TreeMap;
016:
017: import com.aligo.util.Cache;
018:
019: /**
020: * ResponseBufferGroup holds a ResponseBufferEntry object. It also assigns a
021: * number to each response buffer entry created in this group and also
022: * remembers the request urls for the just invalidated buffer entries.
023: *
024: * The idea behind this grouping is that this would hold all the response_buffer
025: * entries and request urls for the recently invalidated entries for a particular
026: * SSOToken and so it is easier to apply the policies which limits the number
027: * ResponseBufferEntry objects the user associated with this SSOToken is allowed
028: * to have.
029: *
030: * No method in this class is public, they are all package private. The only
031: * consumers of this class are ResponseBufferService and ResponseBufferEntry
032: */
033: class ResponseBufferGroup {
034: /**
035: * This constructor is only called from ResponseBufferService.
036: *
037: * @see ResponseBufferService
038: */
039: ResponseBufferGroup(int max_num_invalidated_entry_urls) {
040: if (max_num_invalidated_entry_urls > 0) {
041: _max_invalidated_entry_url_list_size = max_num_invalidated_entry_urls;
042: _invalidated_entry_url_list = new TreeMap();
043: }
044: }
045:
046: /**
047: * This ResponseBufferGroup is valid as long as the SSOToken associated
048: * with this group is valid. The mapping between SSOToken and
049: * ResponseBufferGroup is managed by ResponseBufferService.
050: *
051: * @see ResponseBufferService
052: */
053: public synchronized boolean isValid() {
054: return (_is_valid);
055: }
056:
057: /**
058: * Invalidates the ResponseBufferEntry identified by the given response_buffer_entry_number
059: */
060: synchronized void invalidate(Integer entry_number) {
061: invalidate(getEntry(entry_number));
062: }
063:
064: /**
065: * Invalidates the given ResponseBufferEntry
066: */
067: synchronized void invalidate(ResponseBufferEntry entry) {
068: if (entry == null) {
069: return;
070: }
071:
072: entry.setInvalid();
073:
074: if (_invalidated_entry_url_list != null) {
075: /*
076: * Remove the oldest entry if necessary to make room
077: */
078: if (_invalidated_entry_url_list.size() >= _max_invalidated_entry_url_list_size) {
079: Object _lowest_entry_number = _invalidated_entry_url_list
080: .firstKey();
081: if (_lowest_entry_number != null) {
082: _invalidated_entry_url_list
083: .remove(_lowest_entry_number);
084: }
085: }
086:
087: // Add the request url for this entry to _invalidated_entry_url_list
088: _invalidated_entry_url_list.put(entry.getEntryNumber(),
089: entry.getRequestURL());
090: }
091: }
092:
093: /**
094: * Invalidate all the ResponseBufferEntries
095: */
096: synchronized void invalidate() {
097: if (!_is_valid) {
098: return;
099: }
100:
101: _is_valid = false;
102:
103: if (_entry != null) {
104: _entry.setInvalid();
105: }
106:
107: if (_invalidated_entry_url_list != null) {
108: _invalidated_entry_url_list.clear();
109: }
110: }
111:
112: /**
113: * Can be used to assign a number to some new ResponseBufferEntry
114: */
115: private synchronized Integer getNextEntryNumber() {
116: return (new Integer(++_counter));
117: }
118:
119: /**
120: * Creates a ResponseBufferEntry
121: */
122: synchronized ResponseBufferEntry createEntry(Cache cache) {
123: invalidate(_entry);
124:
125: _entry = new ResponseBufferEntry(this , getNextEntryNumber(),
126: cache);
127: return (_entry);
128: }
129:
130: /**
131: * Gets the ResponseBufferEntry for a given entry_number
132: */
133: synchronized ResponseBufferEntry getEntry(Integer entry_number) {
134: if ((entry_number == null)
135: || (_entry == null)
136: || (entry_number.compareTo(_entry.getEntryNumber()) != 0)) {
137: return (null);
138: }
139:
140: return (_entry);
141: }
142:
143: /**
144: * Gets the RequestURL which generated a ResponseBufferEntry for a given entry_number
145: */
146: synchronized String getRequestURL(Integer entry_number) {
147: ResponseBufferEntry entry = getEntry(entry_number);
148: if (entry != null) {
149: return (entry.getRequestURL());
150: }
151:
152: if (_invalidated_entry_url_list != null) {
153: return ((String) _invalidated_entry_url_list
154: .get(entry_number));
155: }
156:
157: return (null);
158: }
159:
160: /**
161: * Holds the latest ResponseBufferEntry in this group.
162: */
163: private ResponseBufferEntry _entry = null;
164:
165: /**
166: * Holds the request urls for some recently invalidated ResponseBufferEntries
167: */
168: private SortedMap _invalidated_entry_url_list = null;
169:
170: /**
171: * Holds the maximum number of request urls for the recently invalidated
172: * ResponseBufferEntries. The idea is that if a request is made to this entry
173: * user can be redirected to the corresponding request url.
174: */
175: private int _max_invalidated_entry_url_list_size = 0;
176:
177: /**
178: * Every ResponseBufferEntry is given a number by the ResponseBufferGroup, this is the
179: * number used by others to access the ResponseBufferEntry object.
180: */
181: private int _counter = 0;
182:
183: /**
184: * To mark whether or not the buffers maintained by this group are still valid.
185: */
186: private boolean _is_valid = true;
187: }
|