FileDocCategorySizeDatePackage
BoundedPool.javaAPI DocGlassfish v2 API7698Fri May 04 22:24:42 BST 2007com.sun.enterprise.admin.wsmgmt.pool.impl

BoundedPool

public class BoundedPool extends Object implements com.sun.enterprise.admin.wsmgmt.pool.spi.Pool
Pool used to keep track of SOAP messages. This implementation of the pool keeps a fixed amount of entries in the pool. If pool size exceeds the max allowed entry, the oldest entry is removed from the pool.

Fields Summary
private List
_keyList
private Map
_map
private String
_name
private int
_maxSizeAllowed
Constructors Summary
public BoundedPool(String name)
Constructor.

param
name name of the pool

        _name    = name;
        _keyList = Collections.synchronizedList(new ArrayList());
        _map     = new HashMap();
    
public BoundedPool(String name, int size)
Constructor.

param
name name of the pool
param
size max size of the pool


        this(name);
        _maxSizeAllowed  = size;
    
Methods Summary
public voidclear()
Removes all mappings from this pool.

        synchronized (this) {
            _map.clear();
            _keyList.clear();
        }
    
public booleancontainsKey(java.lang.Object key)
Returns true if this pool contains mapping for the specified key.

param
key the presence of the key to be tested
return
true if this pool contains mapping for the specified key

        return _map.containsKey(key);
    
public booleancontainsValue(java.lang.Object val)
Returns true if this pool contains mapping for the specified value.

param
val the presence of the value to be tested
return
true if this pool contains mapping for the specified value

        return _map.containsValue(val);
    
public java.lang.Objectget(java.lang.Object key)
Returns the object for the given key.

param
key key used to put objects in the pool
return
object for the given key or null

        return _map.get(key);
    
public intgetMaxSize()
Returns the maximum number of mappings allowed in this pool.

return
max number of mappings allowed

        return _maxSizeAllowed;
    
public java.lang.StringgetName()
Returns the name of the pool.

return
name of the pool

        return _name;
    
public java.lang.Objectput(java.lang.Object key, java.lang.Object val)
Adds the object to the pool. If pool size is greater than the max allowed, the oldest entry is removed from the pool.

param
key key used to put this object
param
val actual object to be added to the pool
return
previous value for this key or null


        if ((key == null) && (val == null)) {
            throw new IllegalArgumentException();
        }

        Object oldVal = null;

        synchronized (this) {
            oldVal = _map.put(key, val);
            _keyList.add(key);

            // remove the extra entry from the pool
            if (_keyList.size() > _maxSizeAllowed) {
                Object rkey = _keyList.remove(0);
                assert (rkey != null);

                Object rval = _map.remove(rkey);
                assert (rval != null);
            }
        }

        return oldVal;
    
public java.lang.Objectremove(java.lang.Object key)
Removes this keyed entry from the pool.

param
key key of the entry that is targeted to be removed
return
removed entry or null if key not found

        Object val = null;

        synchronized (this) {
            _keyList.remove(key);
            val = _map.remove(key);
        }

        return val;
    
public voidresize(int size)
Resets the max size of the pool. If new size is less than current size, all extra entries in the pool are removed.

param
size new max size of the pool


        if (size < 1) {
            return;
        }

        if (size == _maxSizeAllowed) {
            // do nothing
        } else if (size > _maxSizeAllowed) {
            synchronized (this) {
                _maxSizeAllowed = size;
            }
        } else {
            // remove the extra entries from the pool
            synchronized (this) {
                int currentSize = _map.size();
                int diff = currentSize - size;

                // remove extra entries if current size is bigger than diff
                for (int i=0; i<diff; i++) {
                    Object key = _keyList.get(i);
                    assert (key != null);

                    if (key != null) {
                        Object val = _map.remove(key);
                        assert (val != null);
                    }
                }
                for(int i=0; i <diff; i++) {
                    _keyList.remove(0);
                }
                _maxSizeAllowed = size;
            }
        }
    
public intsize()
Returns the number of mappings in this pool.

return
current number of mappings in this pool

        return _map.size();
    
public java.util.Collectionvalues()
Returns a collection view of the values contained in this pool.

return
a collection view of the values contained in this pool

        return _map.values();