Main Page | Packages | Class Hierarchy | Class List | Directories | File List | Class Members

ObjectPool.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2005 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: openmobileis@e-care.fr
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00020  * USA
00021  *
00022  *  Author : Philippe Delrieu
00023  *  
00024  *  Modifications :
00025  *  2004 Creation P.Delrieu
00026  * 
00027  */
00028 
00029 
00030 package org.openmobileis.common.util;
00031 
00052 public abstract class ObjectPool {
00053   protected java.util.Stack pool_;
00054   protected Class objectClass_;
00055   protected int minSize_, incSize_;
00056 
00069   protected ObjectPool(String objectClass, int minSize, int incSize) throws ClassNotFoundException, IllegalAccessException, InstantiationException  {
00070     pool_ = new java.util.Stack();
00071       objectClass_ = Class.forName(objectClass);
00072     if (minSize == 0) {
00073       minSize_ = 10;
00074     } else  {
00075       minSize_ = minSize;
00076     }
00077     incSize_ = incSize;
00078     createInPool(minSize);
00079   }
00080 
00081   private final void createInPool(int size) throws IllegalAccessException, InstantiationException {
00082     for (int i=0; i<size; i++)  {
00083       Object obj = createNewInstance(objectClass_);
00084       pool_.push(obj);
00085     }
00086   }
00087 
00092   protected Object createNewInstance(Class objectClass) throws IllegalAccessException, InstantiationException {
00093         return objectClass.newInstance();
00094   }
00095 
00106   protected final synchronized Object getObject() {
00107     try {
00108       return pool_.pop();
00109     } catch(java.util.EmptyStackException ex) {
00110       try {
00111         createInPool(minSize_);
00112                     return pool_.pop();
00113       } catch (Exception e) {
00114         // already catch in constructor call
00115       }
00116     }
00117     return null;
00118   }
00119 
00131   public synchronized void disposeObject(Object object) {
00132     if (object != null) {
00133       pool_.push( object );
00134     }
00135   }
00136 
00137 }

Generated on Thu Oct 6 10:06:33 2005 for OpenMobileIS by  doxygen 1.4.3