KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > ubik > net > ConnectionPool


1 package org.sapia.ubik.net;
2
3 import java.io.*;
4
5 import java.rmi.server.RMIClientSocketFactory JavaDoc;
6
7 import java.util.*;
8
9
10 /**
11  * Implements a pool of <code>Connection</code> instances.
12  *
13  * @author Yanick Duchesne
14  * 2002-08-09
15  */

16 public class ConnectionPool {
17   public static final long DEFAULT_ACQUIRE_TIME_OUT = 2000;
18   private Vector _conns = new Vector(50);
19   protected ConnectionFactory _fac;
20   protected String JavaDoc _host;
21   protected int _port;
22   protected int _maxSize;
23   protected int _currentCount;
24   protected long _lastUsageTime = System.currentTimeMillis();
25   protected RMIClientSocketFactory JavaDoc _socketFactory;
26
27   public ConnectionPool(String JavaDoc host, int port, ConnectionFactory fac,
28     RMIClientSocketFactory JavaDoc socketFactory) {
29     _fac = fac;
30     _host = host;
31     _port = port;
32     _socketFactory = socketFactory;
33   }
34
35   public ConnectionPool(String JavaDoc host, int port, ConnectionFactory fac,
36     RMIClientSocketFactory JavaDoc socketFactory, int maxSize) {
37     this(host, port, fac, socketFactory);
38     _maxSize = maxSize;
39   }
40
41   public ConnectionPool(String JavaDoc host, int port,
42     RMIClientSocketFactory JavaDoc socketFactory) {
43     this(host, port, new SocketConnectionFactory(), socketFactory);
44   }
45
46   public ConnectionPool(String JavaDoc host, int port, int maxSize,
47     RMIClientSocketFactory JavaDoc socketFactory) {
48     this(host, port, new SocketConnectionFactory(), socketFactory, maxSize);
49   }
50
51   public synchronized Connection acquire() throws IOException {
52     return acquire(DEFAULT_ACQUIRE_TIME_OUT);
53   }
54
55   public synchronized Connection acquire(long timeout)
56     throws IOException {
57     Connection conn;
58
59     if (_conns.size() == 0) {
60       if (_maxSize <= 0) {
61         conn = newConnection();
62       } else {
63         if (_conns.size() == 0) {
64           try {
65             if (timeout > 0) {
66               wait(timeout);
67             }
68           } catch (InterruptedException JavaDoc e) {
69             throw new IOException("thread interrupted");
70           }
71
72           if (_currentCount >= _maxSize) {
73             throw new IOException("maximum connection pool size reached");
74           } else {
75             conn = newConnection();
76           }
77         } else {
78           conn = (Connection) _conns.remove(0);
79         }
80       }
81     } else {
82       conn = (Connection) _conns.remove(0);
83     }
84
85     return conn;
86   }
87
88   public synchronized void release(Connection conn) {
89     _lastUsageTime = System.currentTimeMillis();
90     _conns.add(conn);
91     notify();
92   }
93
94   public synchronized int getCount() {
95     return _currentCount;
96   }
97
98   /**
99    * Returns the time an object was last acquired from this
100    * pool.
101    */

102   public long getLastUsageTime() {
103     return _lastUsageTime;
104   }
105
106   /**
107    * Shrinks the pool to the specified size, or until the pool is
108    * empty.
109    *
110    * @param size the size to which to shrink the pool.
111    */

112   public synchronized void shrinkTo(int size) {
113     while ((_conns.size() > size) && (_conns.size() > 0)) {
114       _currentCount--;
115
116       if (_currentCount < 0) {
117         _currentCount = 0;
118       }
119
120       Connection conn = (Connection) _conns.remove(0);
121       conn.close();
122     }
123   }
124
125   protected Connection newConnection() throws IOException {
126     _currentCount++;
127
128     return _fac.newConnection(_host, _port);
129   }
130 }
131
Popular Tags