00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 package org.openmobileis.common.util;
00031
00032 import org.openmobileis.common.util.collection.Array;
00033
00054 public abstract class ObjectPool {
00055 protected java.util.Stack pool;
00056 protected Array arrayPool;
00057 protected Class objectClass;
00058 protected int minSize, incSize;
00059
00072 protected ObjectPool(String objectClass, int minSize, int incSize) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
00073 this.pool = new java.util.Stack();
00074 this.objectClass = Class.forName(objectClass);
00075 if (minSize == 0) {
00076 this.minSize = 10;
00077 } else {
00078 this.minSize = minSize;
00079 }
00080 this.incSize = incSize;
00081 this.arrayPool = new Array(minSize);
00082 createInPool(minSize);
00083 }
00084
00085 private final void createInPool(int size) throws IllegalAccessException, InstantiationException {
00086 for (int i=0; i<size; i++) {
00087 Object obj = createNewInstance(objectClass);
00088 this.pool.push(obj);
00089 this.arrayPool.add(obj);
00090 }
00091 }
00092
00097 protected Object createNewInstance(Class objectClass) throws IllegalAccessException, InstantiationException {
00098 return objectClass.newInstance();
00099 }
00100
00111 protected final synchronized Object getObject() {
00112 try {
00113 return this.pool.pop();
00114 } catch(java.util.EmptyStackException ex) {
00115 try {
00116 createInPool(this.incSize);
00117 return pool.pop();
00118 } catch (Exception e) {
00119
00120 }
00121 }
00122 return null;
00123 }
00124
00136 public synchronized void disposeObject(Object object) {
00137 if (object != null) {
00138 this.pool.push( object );
00139 }
00140 }
00141
00142 }