| 1 | /* WRS 2.0 |
|---|
| 2 | * Copyright (C) 2009 Anton Patrushev |
|---|
| 3 | * |
|---|
| 4 | * This program is free software; you can redistribute it and/or modify |
|---|
| 5 | * it under the terms of the GNU General Public License as published by |
|---|
| 6 | * the Free Software Foundation; either version 3 of the License, or |
|---|
| 7 | * (at your option) any later version. |
|---|
| 8 | * |
|---|
| 9 | * This program is distributed in the hope that it will be useful, |
|---|
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 12 | * GNU General Public License for more details. |
|---|
| 13 | * |
|---|
| 14 | * You should have received a copy of the GNU General Public License |
|---|
| 15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
|---|
| 16 | */ |
|---|
| 17 | |
|---|
| 18 | package util; |
|---|
| 19 | |
|---|
| 20 | import java.util.Enumeration; |
|---|
| 21 | import java.util.Hashtable; |
|---|
| 22 | |
|---|
| 23 | public abstract class ObjectPool<T> |
|---|
| 24 | { |
|---|
| 25 | protected String url; |
|---|
| 26 | protected String user; |
|---|
| 27 | protected String password; |
|---|
| 28 | |
|---|
| 29 | private long expirationTime; |
|---|
| 30 | private Hashtable<T, Long> locked, unlocked; |
|---|
| 31 | private int capacity; |
|---|
| 32 | |
|---|
| 33 | public ObjectPool(String url, String user, String password) |
|---|
| 34 | { |
|---|
| 35 | this.url = url; |
|---|
| 36 | this.user = user; |
|---|
| 37 | this.password = password; |
|---|
| 38 | |
|---|
| 39 | expirationTime = 60000; // 1 minute |
|---|
| 40 | capacity = 10; |
|---|
| 41 | locked = new Hashtable<T, Long>(); |
|---|
| 42 | unlocked = new Hashtable<T, Long>(); |
|---|
| 43 | |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | protected abstract T create(); |
|---|
| 47 | |
|---|
| 48 | public abstract boolean validate(T o); |
|---|
| 49 | |
|---|
| 50 | public abstract void expire(T o); |
|---|
| 51 | |
|---|
| 52 | public synchronized T checkOut() |
|---|
| 53 | { |
|---|
| 54 | long now = System.currentTimeMillis(); |
|---|
| 55 | T t; |
|---|
| 56 | |
|---|
| 57 | if (locked.size() + unlocked.size() < capacity) |
|---|
| 58 | { |
|---|
| 59 | if (unlocked.size() > 0) |
|---|
| 60 | { |
|---|
| 61 | Enumeration<T> e = unlocked.keys(); |
|---|
| 62 | |
|---|
| 63 | while (e.hasMoreElements()) |
|---|
| 64 | { |
|---|
| 65 | t = e.nextElement(); |
|---|
| 66 | |
|---|
| 67 | if ((now - unlocked.get(t)) > expirationTime) |
|---|
| 68 | { |
|---|
| 69 | // object has expired |
|---|
| 70 | unlocked.remove(t); |
|---|
| 71 | expire(t); |
|---|
| 72 | t = null; |
|---|
| 73 | } |
|---|
| 74 | else |
|---|
| 75 | { |
|---|
| 76 | if (validate(t)) |
|---|
| 77 | { |
|---|
| 78 | unlocked.remove(t); |
|---|
| 79 | locked.put(t, now); |
|---|
| 80 | return (t); |
|---|
| 81 | } |
|---|
| 82 | else |
|---|
| 83 | { |
|---|
| 84 | // object failed validation |
|---|
| 85 | unlocked.remove(t); |
|---|
| 86 | expire(t); |
|---|
| 87 | t = null; |
|---|
| 88 | } |
|---|
| 89 | } |
|---|
| 90 | } |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | // no objects available, create a new one |
|---|
| 94 | t = create(); |
|---|
| 95 | locked.put(t, now); |
|---|
| 96 | return (t); |
|---|
| 97 | |
|---|
| 98 | } |
|---|
| 99 | else |
|---|
| 100 | { |
|---|
| 101 | //TODO Process error |
|---|
| 102 | return null; |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | public synchronized void checkIn(T t) |
|---|
| 108 | { |
|---|
| 109 | locked.remove(t); |
|---|
| 110 | unlocked.put(t, System.currentTimeMillis()); |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | } |
|---|