KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > ubik > rmi > server > transport > socket > SocketClientConnectionPool


1 package org.sapia.ubik.rmi.server.transport.socket;
2
3 import java.rmi.RemoteException JavaDoc;
4 import java.rmi.server.RMIClientSocketFactory JavaDoc;
5 import java.util.ArrayList JavaDoc;
6 import java.util.Hashtable JavaDoc;
7 import java.util.Iterator JavaDoc;
8 import java.util.List JavaDoc;
9 import java.util.Map JavaDoc;
10
11 import org.sapia.taskman.PeriodicTaskDescriptor;
12 import org.sapia.taskman.Task;
13 import org.sapia.taskman.TaskContext;
14 import org.sapia.ubik.net.Connection;
15 import org.sapia.ubik.net.ConnectionPool;
16 import org.sapia.ubik.net.ServerAddress;
17 import org.sapia.ubik.net.TCPAddress;
18 import org.sapia.ubik.rmi.server.Hub;
19 import org.sapia.ubik.rmi.server.transport.Connections;
20 import org.sapia.ubik.rmi.server.transport.RmiConnection;
21
22
23 /**
24  * Implements a pool of client-side <code>SocketConnection</code> instances.
25  * It multiplexes the pooled connections among multiple callers - thereby
26  * sparing the creation of multiple connections, and releiving the server
27  * of intensive threading - typically, one connection corresponds to one server
28  * thread.
29  *
30  * @author Yanick Duchesne
31  * <dl>
32  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
33  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
34  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
35  * </dl>
36  */

37 public class SocketClientConnectionPool implements Connections {
38   private static Map JavaDoc _pools = new Hashtable JavaDoc();
39   private static boolean _started;
40   ConnectionPool _pool;
41
42   /**
43    * Constructor for RMIClientConnectionPool.
44    * @param host the host of the server to connect to.
45    * @param port the port of the server.
46    */

47   SocketClientConnectionPool(String JavaDoc host, int port,
48     RMIClientSocketFactory JavaDoc socketFactory) {
49     _pool = new ConnectionPool(host, port, new SocketRmiConnectionFactory(),
50         socketFactory);
51   }
52
53   /**
54    * @see org.sapia.ubik.rmi.server.transport.Connections#acquire()
55    */

56   public RmiConnection acquire() throws RemoteException JavaDoc {
57     try {
58       return (RmiConnection) _pool.acquire();
59     } catch (java.io.IOException JavaDoc e) {
60       throw new RemoteException JavaDoc("Could not acquire a connection", e);
61     }
62   }
63
64   /**
65    * @see org.sapia.ubik.rmi.server.transport.Connections#release(Connection)
66    */

67   public void release(Connection conn) {
68     _pool.release(conn);
69   }
70
71   /**
72    * @see org.sapia.ubik.rmi.server.transport.Connections#clear()
73    */

74   public void clear() {
75     _pool.shrinkTo(0);
76   }
77
78   /**
79    * @see org.sapia.ubik.rmi.server.transport.Connections#getTransportType()
80    */

81   public String JavaDoc getTransportType() {
82     return SocketTransportProvider.TRANSPORT_TYPE;
83   }
84
85   ConnectionPool internalPool() {
86     return _pool;
87   }
88
89   static synchronized void shutdown() {
90     SocketClientConnectionPool pool;
91     Iterator JavaDoc pools;
92
93     if (_started) {
94       synchronized (_pools) {
95         pools = _pools.values().iterator();
96
97         while (pools.hasNext()) {
98           pool = (SocketClientConnectionPool) pools.next();
99           pool.internalPool().shrinkTo(0);
100         }
101       }
102     }
103   }
104
105   static synchronized SocketClientConnectionPool getInstance(
106     ServerAddress address, RMIClientSocketFactory JavaDoc factory) {
107     if (!_started) {
108       _started = true;
109
110       List JavaDoc desc = new ArrayList JavaDoc();
111       desc.add(new PeriodicTaskDescriptor(
112           SocketClientConnectionPool.class.getName(), PoolCleaner.INTERVAL,
113           new PoolCleaner(_pools)));
114       Hub.taskMan.addTaskDescriptors(desc);
115     }
116
117     SocketClientConnectionPool pool = (SocketClientConnectionPool) _pools.get(address);
118
119     if (pool == null) {
120       pool = new SocketClientConnectionPool(((TCPAddress) address).getHost(),
121           ((TCPAddress) address).getPort(), factory);
122       _pools.put(address, pool);
123     }
124
125     return pool;
126   }
127
128   static synchronized void invalidate(TCPAddress id) {
129     _pools.remove(id);
130   }
131
132   /*//////////////////////////////////////////////////
133                     INNER CLASSES
134   //////////////////////////////////////////////////*/

135   static final class PoolCleaner implements Task {
136     static final long INTERVAL = 5000;
137     Map JavaDoc _pools;
138
139     PoolCleaner(Map JavaDoc pools) {
140       _pools = pools;
141     }
142
143     /**
144      * @see org.sapia.taskman.Task#exec(TaskContext)
145      */

146     public void exec(TaskContext context) {
147       SocketClientConnectionPool[] pools;
148
149       pools = (SocketClientConnectionPool[]) _pools.values().toArray(new SocketClientConnectionPool[_pools.size()]);
150
151       for (int i = 0; i < pools.length; i++) {
152         if ((System.currentTimeMillis() -
153               pools[i].internalPool().getLastUsageTime()) > INTERVAL) {
154           context.getTaskOutput().debug("Shrinking socket client connection pool...");
155           pools[i].internalPool().shrinkTo(0);
156         }
157       }
158     }
159   }
160 }
161
Popular Tags