001: /* JFox, the OpenSource J2EE Application Server
002: *
003: * Copyright (C) 2002 huihoo.org
004: * Distributable under GNU LGPL license
005: * See the GNU Lesser General Public License for more details.
006: */
007:
008: package org.huihoo.jfox.pool;
009:
010: import java.util.Iterator;
011: import java.util.List;
012: import java.util.ArrayList;
013: import java.lang.reflect.Proxy;
014:
015: /**
016: *
017: * @author <a href="mailto:young_yy@hotmail.com">Young Yang</a>
018: */
019:
020: public class SimpleObjectPool extends AbstractObjectPool implements
021: SimpleObjectPoolMBean {
022: //已从池中取出的对象数目
023: private volatile int numWorking = 0;
024: private List pool = new ArrayList();
025:
026: /** the number of "sleeping" instances in the pool. */
027: private int maxRest = 10;
028:
029: private int initNum = 5;
030:
031: public SimpleObjectPool(ObjectFactory factory) {
032: this (factory, 5, 10);
033: }
034:
035: public SimpleObjectPool(ObjectFactory factory, int initNum,
036: int maxRest) {
037: super (factory);
038: this .factory = factory;
039: this .initNum = initNum;
040: this .maxRest = maxRest;
041: }
042:
043: public SimpleObjectPool(String objectFactoryClassName,
044: String poolableClassName) {
045: super (objectFactoryClassName, poolableClassName);
046: }
047:
048: public SimpleObjectPool(String objectFactoryClassName,
049: String poolableClassName, int initNum, int maxRest) {
050: super (objectFactoryClassName, poolableClassName);
051: this .initNum = initNum;
052: this .maxRest = maxRest;
053: }
054:
055: /**
056: * retrieve Object from object pool
057: * @return
058: */
059: public PoolableObject retrieveObject() throws Exception {
060: PoolableObject obj = null;
061: try {
062: synchronized (pool) {
063: obj = (PoolableObject) pool.remove(0);
064: }
065: } catch (Exception e) {
066: obj = factory.makeObject();
067: }
068: if (obj != null) {
069: obj.activate();
070: }
071: numWorking++;
072: return obj;
073: }
074:
075: /**
076: * restore the retrived object to object pool
077: * @return true if object passivate && push success, false if failed
078: */
079: public boolean restoreObject(PoolableObject obj) {
080: numWorking--;
081:
082: PoolableObject realObject = obj;
083:
084: if (Proxy.isProxyClass(obj.getClass())) {
085: realObject = ((PoolableObjectInvocationHandler) Proxy
086: .getInvocationHandler(obj)).getPoolableObject();
087: }
088:
089: if (factory.validateObject(realObject)) {
090: try {
091: obj.passivate();
092: synchronized (pool) {
093: if (pool.size() < maxRest) {
094: pool.add(realObject);
095: return true;
096: }
097: }
098: } catch (Exception e) {
099: // do nothing , just jump to below
100: }
101: }
102:
103: // failed
104: try {
105: factory.destroyObject(realObject);
106: } catch (Exception e) {
107: // ignored
108: }
109: return false;
110: }
111:
112: public boolean removeObject(PoolableObject obj) {
113: return pool.remove(obj);
114: }
115:
116: // clear the pool
117: public void clear() {
118: Iterator iter = pool.iterator();
119: while (iter.hasNext()) {
120: try {
121: factory.destroyObject((PoolableObject) iter.next());
122: } catch (Exception e) {
123: // ignore error, keep destroying the rest
124: }
125: }
126: synchronized (pool) {
127: pool.clear();
128: }
129: }
130:
131: public int getWorking() {
132: return numWorking;
133: }
134:
135: public int getRest() {
136: return pool.size();
137: }
138:
139: protected void doInit() throws Exception {
140: int i = initNum;
141: while (i-- > 0) {
142: try {
143: pool.add(factory.makeObject());
144: } catch (Exception e) {
145: throw new RuntimeException(e.getMessage());
146: }
147: }
148:
149: }
150:
151: // just do clear
152: protected void doDestroy() throws Exception {
153: this .clear();
154: }
155:
156: public int getInitNum() {
157: return initNum;
158: }
159:
160: public int getMaxRest() {
161: return maxRest;
162: }
163:
164: public static void main(String[] args) {
165:
166: }
167: }
|