Source code: org/alicebot/server/sql/pool/ObjectPool.java
1 // Decompiled by Jad v1.5.8c. Copyright 2001 Pavel Kouznetsov.
2 // Jad home page: http://www.geocities.com/kpdus/jad.html
3 // Decompiler options: packimports(3)
4
5 package org.alicebot.server.sql.pool;
6
7 import java.util.Enumeration;
8 import java.util.Hashtable;
9
10 // Referenced classes of package org.alicebot.server.sql.pool:
11 // CleanUpThread
12
13 public abstract class ObjectPool
14 {
15
16 protected ObjectPool()
17 {
18 expirationTime = 0x0L;
19 locked = new Hashtable();
20 unlocked = new Hashtable();
21 lastCheckOut = System.currentTimeMillis();
22 cleaner = new CleanUpThread(this, expirationTime);
23 cleaner.setDaemon(true);
24 cleaner.start();
25 }
26
27 protected void checkIn(Object obj)
28 {
29 if(obj != null)
30 {
31 locked.remove(obj);
32 unlocked.put(obj, new Long(System.currentTimeMillis()));
33 }
34 }
35
36 protected Object checkOut()
37 {
38 long l = System.currentTimeMillis();
39 lastCheckOut = l;
40 if(unlocked.size() > 0)
41 {
42 for(Enumeration enumeration = unlocked.keys(); enumeration.hasMoreElements();)
43 {
44 Object obj = enumeration.nextElement();
45 if(validate(obj))
46 {
47 unlocked.remove(obj);
48 locked.put(obj, new Long(l));
49 return obj;
50 }
51 unlocked.remove(obj);
52 expire(obj);
53 obj = null;
54 }
55
56 }
57 Object obj1 = create();
58 locked.put(obj1, new Long(l));
59 return obj1;
60 }
61
62 protected void cleanUp()
63 {
64 long l = System.currentTimeMillis();
65 for(Enumeration enumeration = unlocked.keys(); enumeration.hasMoreElements();)
66 {
67 Object obj = enumeration.nextElement();
68 if(l - ((Long)unlocked.get(obj)).longValue() > expirationTime)
69 {
70 unlocked.remove(obj);
71 expire(obj);
72 obj = null;
73 }
74 }
75
76 System.gc();
77 }
78
79 protected abstract Object create();
80
81 protected abstract void expire(Object obj);
82
83 protected abstract boolean validate(Object obj);
84
85 private long expirationTime;
86 private long lastCheckOut;
87 private Hashtable locked;
88 private Hashtable unlocked;
89 private CleanUpThread cleaner;
90 }