01: /**
02: * Objective Database Abstraction Layer (ODAL)
03: * Copyright (c) 2004, The ODAL Development Group
04: * All rights reserved.
05: * For definition of the ODAL Development Group please refer to LICENCE.txt file
06: *
07: * Distributable under LGPL license.
08: * See terms of license at gnu.org.
09: */package com.completex.objective.components.pool.impl;
10:
11: import com.completex.objective.components.pool.Pool;
12: import com.completex.objective.components.pool.ResourceFactory;
13:
14: import java.util.Iterator;
15: import java.util.LinkedList;
16:
17: /**
18: * @author Gennady Krizhevsky
19: */
20: public class PrimitivePool implements Pool {
21:
22: private ResourceFactory resourceFactory;
23: private int numberOfBuzy;
24: private LinkedList idleResources = new LinkedList();
25: private final Object resourceLock = new Object();
26:
27: public PrimitivePool(ResourceFactory resourceFactory) {
28: this .resourceFactory = resourceFactory;
29: }
30:
31: public void start() {
32: }
33:
34: public Object acquireResource() {
35: synchronized (resourceLock) {
36: if (idleResources.size() != 0) {
37: return idleResources.removeFirst();
38: }
39: }
40: numberOfBuzy++;
41: return resourceFactory.createResource();
42: }
43:
44: public void releaseResource(Object resource) {
45: synchronized (resourceLock) {
46: idleResources.addLast(resource);
47: }
48: }
49:
50: public void releaseBadResource(Object resource) {
51: }
52:
53: public void shutdown() {
54: synchronized (resourceLock) {
55: for (Iterator iterator = idleResources.iterator(); iterator
56: .hasNext();) {
57: resourceFactory.destroyResource(iterator.next());
58: }
59: idleResources = new LinkedList();
60: }
61: }
62:
63: public int getMaxSize() {
64: return UNLIMITTED;
65: }
66:
67: public String toString() {
68: return super .toString() + " : Number Of Buzy : " + numberOfBuzy
69: + "; number of idle: " + idleResources.size();
70: }
71: }
|