001: /**
002: * $Id: SimpleObjectPool.java,v 1.7 2005/12/02 13:53:26 as133206 Exp $
003: * Copyright 2002 Sun Microsystems, Inc. All
004: * rights reserved. Use of this product is subject
005: * to license terms. Federal Acquisitions:
006: * Commercial Software -- Government Users
007: * Subject to Standard License Terms and
008: * Conditions.
009: *
010: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
011: * are trademarks or registered trademarks of Sun Microsystems,
012: * Inc. in the United States and other countries.
013: */package com.sun.common.pool;
014:
015: import com.sun.portal.desktop.monitoring.PoolStatistic;
016:
017: import java.util.List;
018: import java.util.ArrayList;
019:
020: public class SimpleObjectPool implements Pool {
021: private ObjectManager _objectManager;
022: private List _objectPool;
023: private int _minSize;
024: private int _maxSize;
025: private boolean _overflow;
026: private int _leased;
027: private int _mid;
028: private boolean _destroyed;
029:
030: public SimpleObjectPool(ObjectManager objectManager, int minSize,
031: int maxSize, boolean overflow) {
032: _objectManager = objectManager;
033: _objectPool = new ArrayList(maxSize);
034: _minSize = minSize;
035: _maxSize = maxSize;
036: _mid = (_minSize + _maxSize) / 2 + 1;
037: _overflow = overflow;
038: _leased = 0;
039: _destroyed = false;
040: for (int i = 0; i < _minSize; i++) {
041: _objectPool.add(_objectManager.createObject(null));
042: }
043: }
044:
045: public synchronized Object obtainObject(Object param) {
046: if (_destroyed) {
047: throw new IllegalStateException();
048: }
049: Object o = null;
050: int size = _objectPool.size();
051: if (size > 0) {
052: o = _objectPool.remove(size - 1); // takes the last one to avoid shifting in the arraylist
053: _leased++;
054: } else {
055: if (_leased < _maxSize || _overflow) {
056: o = _objectManager.createObject(param);
057: _leased++;
058: }
059: }
060: if (poolStatistic != null) {
061: poolStatistic.setCurrent(_leased);
062: }
063: return o;
064: }
065:
066: public synchronized void releaseObject(Object o) {
067: _leased--;
068: if (_destroyed) {
069: _objectManager.destroyObject(o);
070: } else {
071: _objectPool.add(o);
072: int size = _objectPool.size();
073: int extra = size - _maxSize;
074: if (extra > 0 && _leased < _mid) {
075: for (int i = 0; i < extra; i++) {
076: o = _objectPool.remove(--size);
077: _objectManager.destroyObject(o);
078: }
079: }
080: }
081: if (poolStatistic != null) {
082: poolStatistic.setCurrent(_leased);
083: }
084: }
085:
086: public int getLeased() {
087: return _leased;
088: }
089:
090: public int getMinSize() {
091: return _minSize;
092: }
093:
094: public int getMaxSize() {
095: return _maxSize;
096: }
097:
098: public long size() {
099: return _objectPool.size();
100: }
101:
102: public void setMaxSize(int maxSize) {
103: if (_destroyed) {
104: throw new IllegalStateException();
105: }
106: _maxSize = maxSize;
107: }
108:
109: public synchronized void destroy() {
110: if (_destroyed) {
111: throw new IllegalStateException();
112: }
113: _destroyed = true;
114: for (int i = 0; i < _objectPool.size(); i++) {
115: Object o = _objectPool.get(i);
116: _objectManager.destroyObject(o);
117: }
118: }
119:
120: public final boolean doesReuseObjects() {
121: return true;
122: }
123:
124: protected void finalize() {
125: if (!_destroyed) {
126: destroy();
127: }
128: }
129:
130: public void setPoolStatistic(PoolStatistic poolStatistic) {
131: this .poolStatistic = poolStatistic;
132: }
133:
134: private PoolStatistic poolStatistic;
135: }
|