KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfox > pool > SimpleIndexableObjectPool


1 /* JFox, the OpenSource J2EE Application Server
2  *
3  * Distributable under GNU LGPL license by gun.org
4  * more details please visit http://www.huihoo.org/jfox
5  */

6
7 package org.jfox.pool;
8
9 import java.util.HashMap JavaDoc;
10 import java.util.Map JavaDoc;
11
12 /**
13  * @author <a HREF="mailto:young_yy@hotmail.com">Young Yang</a>
14  */

15
16 public class SimpleIndexableObjectPool extends AbstractIndexableObjectPool {
17
18     private Map JavaDoc indexedObjects = null;
19
20     public SimpleIndexableObjectPool(ObjectFactory factory) {
21         super(factory);
22     }
23
24     public SimpleIndexableObjectPool(String JavaDoc objectFactoryClassName, String JavaDoc poolableClassName) {
25         super(objectFactoryClassName, poolableClassName);
26     }
27
28     protected void doInit() throws Exception JavaDoc {
29         super.doInit();
30         indexedObjects = new HashMap JavaDoc();
31     }
32
33     protected void doDestroy() throws Exception JavaDoc {
34         super.doDestroy();
35         indexedObjects = null;
36     }
37
38     public void clear() {
39         super.clear();
40         indexedObjects.clear();
41     }
42
43     public PoolableObject retrieveObject(Object JavaDoc key) throws Exception JavaDoc {
44         if(indexedObjects.containsKey(key)) {
45             return (PoolableObject) indexedObjects.remove(key);
46         }
47         else {
48             return pool.retrieveObject();
49         }
50
51     }
52
53     public boolean restoreObject(Object JavaDoc key, PoolableObject obj) {
54         if(!factory.validateObject(obj)) {
55             logger.warn("PoolableObject " + obj + " is not a valid pool object in this ObjectPool");
56             return false;
57         }
58
59         if(indexedObjects.containsKey(key)) {
60             logger.warn("Key " + key + " already exists, will overwrite!");
61         }
62         indexedObjects.put(key, obj);
63         return true;
64
65     }
66
67     public boolean removeObject(Object JavaDoc key) {
68         if(!indexedObjects.containsKey(key)) {
69             logger.warn("Key " + key + " is not exists!");
70             return false;
71         }
72         else {
73             indexedObjects.remove(key);
74             return true;
75         }
76     }
77
78     public static void main(String JavaDoc[] args) {
79
80     }
81 }
82
Popular Tags