KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > perseus > pool > lib > LArrayPool


1 /**
2  * Copyright (C) 2001-2002
3  * - France Telecom R&D
4  * - Laboratoire Logiciels, Systemes, Reseaux - UMR 5526, CNRS-INPG-UJF
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * Release: 1.0
21  *
22  * Authors:
23  *
24  */

25
26 package org.objectweb.perseus.pool.lib;
27
28 import org.objectweb.fractal.api.control.BindingController;
29 import org.objectweb.perseus.pool.api.Pool;
30 import org.objectweb.perseus.pool.api.PoolAttributes;
31 import org.objectweb.perseus.pool.api.PoolMatchFactory;
32 import org.objectweb.perseus.pool.api.PoolException;
33
34 import java.util.Collection JavaDoc;
35 import java.util.Collections JavaDoc;
36 import java.util.Iterator JavaDoc;
37
38 /**
39  * The class <b>LArrayPool</b> implements a Pool as an array of PoolResource,
40  * managing free/active resources through lists of cells encapsulating the
41  * resources.
42  *
43  * @author P.Dechamboux
44  */

45 public class LArrayPool implements Pool, PoolAttributes, BindingController {
46
47     public final static String JavaDoc POOL_MATCH_FACTORY_BINDING = "pool-match-factory";
48
49     private PRCell arrayPool[] = null;
50     private int minSize = 0;
51     private int maxSize = 0;
52   private int newMinSize = 0;
53   private int newMaxSize = 0;
54     private long timeout = 0;
55     private int curSize = 0;
56     private int freeSize = 0;
57     private PoolMatchFactory matchFactory = null;
58     private PRCell freeList = null;
59     private PRCell activeList = null;
60
61     // IMPLEMENTATION OF METHODS FROM THE UserBindingController INTERFACE //
62
//--------------------------------------------------------------------//
63

64     public String JavaDoc[] listFc() {
65         return new String JavaDoc[] { POOL_MATCH_FACTORY_BINDING };
66     }
67
68     public Object JavaDoc lookupFc(String JavaDoc clientItfName) {
69         if (POOL_MATCH_FACTORY_BINDING.equals(clientItfName))
70             return matchFactory;
71         else
72             return null;
73     }
74
75     public void bindFc(String JavaDoc clientItfName, Object JavaDoc serverItf) {
76         if (POOL_MATCH_FACTORY_BINDING.equals(clientItfName))
77             matchFactory = (PoolMatchFactory) serverItf;
78     }
79
80     public void unbindFc(String JavaDoc clientItfName) {
81         if (POOL_MATCH_FACTORY_BINDING.equals(clientItfName))
82             matchFactory = null;
83     }
84
85
86     // IMPLEMENTATION OF METHODS FROM THE PoolAttributes INTERFACE //
87
//-------------------------------------------------------------//
88

89     public long getTimeout() {
90         return timeout;
91     }
92
93     public int getMinSize() {
94         return newMinSize;
95     }
96
97     public int getMaxSize() {
98         return newMaxSize;
99     }
100
101     public synchronized void setTimeout(long crto) {
102     }
103
104     public void setMinSize(int minsize) {
105     newMinSize = minsize;
106   }
107
108   public void setMaxSize(int maxsize) {
109     newMaxSize = maxsize;
110   }
111
112     public long getTTL() {
113         return -1;
114     }
115
116     public void setTTL(long ttl) {
117     }
118
119     public long getInactiveTTL() {
120         return -1;
121     }
122
123     public void setInactiveTTL(long ttl) {
124     }
125
126   private void updateSize () throws PoolException {
127     if (maxSize != newMaxSize) {
128       changeMaxSize(newMaxSize);
129       maxSize = newMaxSize;
130     }
131     if (minSize != newMinSize) {
132       changeMinSize(newMinSize);
133       minSize = newMinSize;
134     }
135   }
136
137   private void changeMaxSize(int maxsize) throws PoolException {
138         if ((maxsize < 0) || (maxsize < newMinSize) || (maxsize == maxSize))
139             return;
140         PRCell nap[];
141         if (maxsize > maxSize) {
142             nap = new PRCell[maxsize];
143             int i;
144             for (i = 0; i < maxSize; i++)
145                 nap[i] = arrayPool[i];
146             for (; i < maxsize; i++)
147                 nap[i] = null;
148             maxSize = maxsize;
149         } else {
150             int cursize = curSize;
151             if (maxsize < curSize) {
152                 // Removes as many free cells as possible
153
int nbRemove = maxSize - maxsize;
154                 PRCell remcell;
155                 while ((freeList != null) && (nbRemove > 0)) {
156                     remcell = freeList;
157                     freeList = freeList.remove(freeList);
158                     matchFactory.destroyResource(remcell.getResource());
159                     arrayPool[remcell.getIndex()] = null;
160                     nbRemove--;
161                     cursize--;
162                 }
163                 // Adds non-removed cells to the new maxsize
164
nap = new PRCell[cursize];
165                 maxSize = cursize;
166             } else {
167                 nap = new PRCell[maxsize];
168                 maxSize = maxsize;
169             }
170             // Copies to new Pool array
171
for (int i = 0, j = 0; i < curSize; i++) {
172                 if (arrayPool[i] != null)
173                     nap[j++] = arrayPool[i];
174             }
175             curSize = cursize;
176         }
177         arrayPool = nap;
178     }
179
180   private void changeMinSize(int minsize) throws PoolException {
181         if ((minsize < 0) || (minsize > maxSize) || (minsize == minSize))
182             return;
183         if (minsize < minSize) {
184             minSize = minsize;
185             return;
186         }
187         PRCell newprc;
188         while (minsize > curSize) {
189             newprc = new PRCell(matchFactory.createResource(null), curSize);
190             arrayPool[curSize++] = newprc;
191             freeList = newprc.insert(freeList);
192             freeSize++;
193         }
194         minSize = minsize;
195     }
196
197     // IMPLEMENTATION OF METHODS FROM THE Pool INTERFACE //
198
//---------------------------------------------------//
199

200     public Object JavaDoc getResource(Object JavaDoc hints)
201         throws PoolException {
202         return getResource(hints, null);
203     }
204     
205     public synchronized Object JavaDoc getResource(Object JavaDoc hints,
206             Object JavaDoc user) throws PoolException {
207         updateSize();
208         PRCell res = null;
209         if (freeList != null) {
210             Iterator JavaDoc it = freeList.iterator();
211             while (it.hasNext()) {
212                 res = (PRCell) it.next();
213                 if (matchFactory.matchResource(res.getResource(), hints)) {
214                     freeList = res.remove(freeList);
215                     activeList = res.insert(activeList);
216                     freeSize--;
217                     break;
218                 }
219             }
220         }
221         if (res == null) {
222             if (curSize < maxSize) {
223                 res = new PRCell(matchFactory.createResource(hints), curSize);
224                 arrayPool[curSize++] = res;
225                 activeList = res.insert(activeList);
226             } else {
227                 throw new PoolException("No more resource available in pool!!");
228             }
229         }
230 // printLists();
231
return res.getResource();
232     }
233
234     public synchronized int getSize() {
235         return curSize;
236     }
237
238     public synchronized int getFreeResourceNumber() {
239         return freeSize;
240     }
241     public synchronized int getUsedResourceNumber() {
242         return curSize - freeSize;
243     }
244     public synchronized Collection JavaDoc getUsers() {
245         return Collections.EMPTY_LIST;
246     }
247     
248
249     public synchronized void releaseResource(Object JavaDoc resource)
250         throws PoolException {
251         updateSize();
252         if (activeList == null)
253             throw new PoolException("Releases inactive resource - 1!!");
254         PRCell prc = activeList.search(resource);
255         if (prc == null)
256             throw new PoolException("Releases inactive resource - 2!!");
257         activeList = prc.remove(activeList);
258         freeList = prc.insert(freeList);
259         freeSize++;
260 // printLists();
261
}
262
263
264     private void printLists() {
265         if (maxSize != 200)
266             return;
267         System.err.println("minSize=" + minSize + ", maxSize=" + maxSize
268             + ", curSize=" + curSize + ", freeSize=" + freeSize);
269         System.err.print("activeList:");
270         if (activeList == null)
271             System.err.print(" null");
272         else {
273             PRCell cur = activeList;
274             do {
275                 System.err.print(" " + cur);
276                 cur = cur.getNext();
277             } while (cur != activeList);
278         }
279         System.err.println(".");
280         System.err.print("freeList:");
281         if (freeList == null)
282             System.err.print(" null");
283         else {
284             PRCell cur = freeList;
285             do {
286                 System.err.print(" " + cur);
287                 cur = cur.getNext();
288             } while (cur != freeList);
289         }
290         System.err.println(".");
291     }
292
293 }
294
295 class PRCell {
296     private PRCell next;
297     private PRCell prev;
298     private Object JavaDoc poolResource;
299     private int index;
300
301     PRCell(Object JavaDoc pr, int ind) {
302         next = prev = null;
303         poolResource = pr;
304         index = ind;
305     }
306
307     int getIndex() {
308         return index;
309     }
310
311     PRCell getNext() {
312         return next;
313     }
314
315     Object JavaDoc getResource() {
316         return poolResource;
317     }
318
319     void destroy() throws PoolException {
320     }
321
322     PRCell insert(PRCell prclist) {
323         if (prclist == null) {
324             next = prev = this;
325         } else {
326             PRCell prev2 = prclist.prev;
327             prclist.prev.next = this;
328             prclist.prev = this;
329             next = prclist;
330             prev = prev2;
331         }
332         return this;
333     }
334
335     Iterator JavaDoc iterator() {
336         return new PRCIter(this);
337     }
338
339     PRCell remove(PRCell prclist) throws PoolException {
340         if (prclist == this) {
341             if (prev == this) {
342                 next = prev = null;
343                 return null;
344             }
345             PRCell next2 = prclist.next;
346             prclist.prev.next = next2;
347             next2.prev = prclist.prev;
348             next = prev = null;
349             return next2;
350         }
351         PRCell rem = prclist.next;
352         while ((rem != this) && (rem != prclist))
353             rem = rem.next;
354         if (rem == prclist)
355             throw new PoolException("LArrayPool.PRCell.remove: cell not on this list!!");
356         rem.next.prev = rem.prev;
357         rem.prev.next = rem.next;
358         next = prev = null;
359         return prclist;
360     }
361
362     PRCell search(Object JavaDoc pr) {
363         PRCell end = this;
364         PRCell cur = this;
365         do {
366             if (cur.poolResource == pr)
367                 return cur;
368             cur = cur.next;
369         } while (cur != end);
370         return null;
371     }
372
373     void setIndex(int ind) {
374         index = ind;
375     }
376
377     class PRCIter implements Iterator JavaDoc {
378         PRCell cur, end;
379
380         PRCIter(PRCell list) {
381             if (list != null)
382                 end = list.prev;
383             cur = list;
384         }
385
386         public boolean hasNext() {
387             return cur != null;
388         }
389
390         public Object JavaDoc next() {
391             if (cur == null)
392                 return null;
393             if (cur == end) {
394                 cur = null;
395                 return end;
396             }
397             cur = cur.next;
398             return cur.prev;
399         }
400
401         public void remove() {
402         }
403     }
404 }
Popular Tags