KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > smartlib > pool > core > PoolManagerImpl


1 /*
2  * @(#) PoolManagerImpl 1.0 02/08/01
3  */

4
5 package org.smartlib.pool.core;
6
7
8 import org.xml.sax.SAXNotRecognizedException JavaDoc;
9 import org.xml.sax.SAXException JavaDoc;
10 import org.apache.log4j.Logger;
11
12 import java.io.*;
13 import java.util.*;
14 import java.sql.*;
15
16 /**
17  * This class implements the PoolManager interface.The basic responsibility of
18  * this class is to load the configuration file , and initialise the pools and
19  * priovide a single point access to the pools.
20  *
21  * @author Sachin Shekar Shetty
22  * @version 1.0, 02/08/01
23  *
24  */

25
26 public class PoolManagerImpl implements PoolManager {
27
28     private Map poolMap = new HashMap();
29     private String JavaDoc defaultPool;
30     private boolean shutDown = false;
31     private static Logger logger = Logger.getLogger(PoolManagerImpl.class);
32
33     private void loadConfig(File file ) throws ConnectionPoolException {
34
35
36         PoolConfig[] config = null;
37         try {
38             config = new ConfigFileProcessor().getPoolConfig(file.getAbsolutePath());
39         } catch(Exception JavaDoc e) {
40             throw new ConnectionPoolException("Error occurred during loading config file: "
41                     + file.getAbsolutePath(), e);
42         }
43
44         for (int i=0; i<config.length; i++) {
45             PoolConfig pc = config[i];
46             String JavaDoc poolName = pc.getMultiPoolName();
47             if (logger.isDebugEnabled()) {
48                 logger.debug("" + pc);
49             }
50             if (pc.isDefaultPool()) {
51                 if (defaultPool != null)
52                     throw new ConnectionPoolException("More than one Connection Pools cannot have default set to 'true'");
53                 defaultPool = poolName;
54             }
55             poolMap.put(poolName , new MultiPoolImpl(pc));
56         }
57
58     }
59     
60     /**
61      * This constructor intialises the SmartPoolClass , reads the
62      * configuration from <code>fileName</code> and loads the PoolManger.
63      *
64      * @param file The absolute configuration file path.
65      * @exception ConnectionPoolException if there is any problem
66      * initialising the pools
67      */

68     public PoolManagerImpl(String JavaDoc fileName) throws ConnectionPoolException {
69
70         if (fileName == null || fileName.trim().equals(""))
71         throw new IllegalArgumentException JavaDoc ("File Name cannot be null/empty");
72         File f1 = new File(fileName);
73         loadConfig(f1);
74
75     }
76
77     /**
78      * This constructor intialises the SmartPoolClass , reads the
79      * configuration from <code>file</code> and loads the PoolManger.
80      *
81      * @param file The configuration file.
82      * @exception ConnectionPoolException if there is any problem
83      * initialising the pools
84      */

85     public PoolManagerImpl(File file) throws ConnectionPoolException {
86
87         loadConfig(file);
88
89     }
90
91     /**
92      * This method returns a Connection from the default connection pool.
93      * The owner of this pool is marked as N/A indicating unknown.
94      *
95      * <b>Note: This method blocks if the pool size has reached it's
96      * maximum size and no free connections are available
97      * until a free connection is available.</b> The time period for which this
98      * method blocks depends on the connection-wait-time-out specified in
99      * the configuration file.
100      *
101      *
102      * @return Connection from the default pool
103      * @exception ConnectionPoolException if there is any problem
104      * getting connection.
105      */

106     
107     public Connection getConnection() throws ConnectionPoolException {
108
109         if ( defaultPool == null )
110             throw new ConnectionPoolException("No default pool specified");
111         return (getConnection(defaultPool));
112
113     }
114
115     /**
116      * This method returns a Connection from the pool <code>poolName</code>.
117      * The owner of this pool is marked as N/A indicating unknown.
118      *
119      * <b>Note: This method blocks if the pool size has reached it's
120      * maximum size and no free connections are available
121      * until a free connection is available.</b> The time period for which this
122      * method blocks depends on the connection-wait-time-out specified in
123      * the configuration file.
124      *
125      *
126      * @param poolName Name of the pool.
127      * @return Connection from the pool
128      * @exception ConnectionPoolException if there is any problem
129      * getting connection.
130      */

131     public Connection getConnection(String JavaDoc poolName)
132             throws ConnectionPoolException {
133
134         MultiPool p = (MultiPool)poolMap.get(poolName);
135         if (p==null)
136             throw new ConnectionPoolException("No such pool:" + poolName);
137         return p.getConnection();
138
139     }
140
141     /**
142      * This method returns a Connection from the pool <code>poolName</code>.
143      * The owner of this connection is identified by <code>owner</code> .
144      *
145      * <b>Note: This method blocks if the pool size has reached it's
146      * maximum size and no free connections are available
147      * until a free connection is available</b>. The time period for which this
148      * method blocks depends on the connection-wait-time-out specified in
149      * the configuration file.
150      *
151      * @param poolName Name of the pool.
152      * @param owner String identifying the owner.
153      * @return Connection from the pool
154      *
155      * @exception ConnectionPoolException if there is any problem
156      * getting connection.
157      */

158     public Connection getConnection(String JavaDoc poolName , String JavaDoc owner)
159         throws ConnectionPoolException {
160
161         MultiPool p = (MultiPool)poolMap.get(poolName);
162         if (p==null)
163             throw new ConnectionPoolException("No such pool:" + poolName);
164         return p.getConnection(owner);
165
166     }
167
168     /**
169      * This method adds a connection leak listener.The methods of
170      * <code>cle</code> will be called when a leak is detected as per the
171      * pool configuration.
172      *
173      * @param poolName Name of the pool.
174      * @param cle Class implementing ConnectionLeakListener interface.
175      * @exception ConnectionPoolException if there is any problem
176      * adding ConnectionLeakListener.
177      */

178     public void addConnectionLeakListener(String JavaDoc poolName
179             , ConnectionLeakListener cle) throws ConnectionPoolException {
180
181             MultiPool p = (MultiPool)poolMap.get(poolName);
182             if (p==null)
183                 throw new ConnectionPoolException("No such pool:" + poolName);
184             p.addConnectionLeakListener(cle);
185
186     }
187
188     /**
189      * This method removes a connection leak listener.<code>cle</code> will
190      * not get any further notifications.
191      *
192      * @param poolName Name of the pool.
193      * @param cle Class implementing ConnectionLeakListener interface.
194      * @exception ConnectionPoolException if there is any problem
195      * removing ConnectionLeakListener.
196      */

197     public void removeConnectionLeakListener(String JavaDoc poolName
198             , ConnectionLeakListener cle) throws ConnectionPoolException {
199
200             MultiPool p = (MultiPool)poolMap.get(poolName);
201             if (p==null)
202                 throw new ConnectionPoolException("No such pool:" + poolName);
203             p.removeConnectionLeakListener(cle);
204
205     }
206
207     /**
208      * This method returns the instance of PoolMonitor for the pool
209      * <code>poolName</code>
210      *
211      * @param poolName Name of the pool.
212      * @return PoolMonitor interface to monitor the state of the pool
213      * @exception ConnectionPoolException
214      *
215      */

216     public MultiPoolMonitor getMultiPoolMonitor(String JavaDoc poolName)
217                 throws ConnectionPoolException {
218
219         MultiPoolMonitor p = (MultiPoolMonitor)poolMap.get(poolName);
220         if (p==null)
221             throw new ConnectionPoolException("No such pool:" + poolName);
222         return p;
223
224     }
225
226     /**
227      * This method shuts down the pool, that is closes all connections to the database,
228      * the pool can no longer be used unless reinitialised using a new SmartPoolFactory instance.
229      */

230     public void shutDown() {
231         shutDown = true;
232         Set set = poolMap.entrySet();
233         for (Iterator it= set.iterator(); it.hasNext(); ) {
234             Map.Entry entry = (Map.Entry)it.next();
235             if (logger.isDebugEnabled()) {
236                 logger.debug("Shutting down: " + entry.getKey());
237             }
238             ((MultiPool)entry.getValue()).shutDown();
239         }
240
241     }
242
243     // Main method , once upon a time this used to be the only method in
244
// my java programs. That is how you do procedural programming in java.
245
public static void main (String JavaDoc args[]) throws Exception JavaDoc {
246
247         int k = 0;
248         Thread JavaDoc arr[] = new Thread JavaDoc[40] ;
249         try {
250             PoolManagerImpl p1 = new PoolManagerImpl("c:\\windows\\desktop\\org.smartlib.pool.test.xml");
251             System.exit(0);
252             for (int i = 0 ; i < 20 ; i++ ) {
253
254                 Thread JavaDoc r = new Thread JavaDoc(new ThreadRunner(p1 , "Sachin" , "" + i
255                         , i*800 ));
256                 arr[k] = r;
257                 k++;
258                 r.start();
259                 r = new Thread JavaDoc(new ThreadRunner(p1 , "Shetty" , "" + i
260                         , i*800 ));
261                 r.start();
262                 arr[k] = r;
263                 k++;
264             }
265
266         }
267         catch (Exception JavaDoc e ) {
268             if (logger.isDebugEnabled()) {
269                 logger.debug(e,e);
270             }
271         }
272         for ( int z =0 ; z<k ; z++ ) {
273             System.out.println("Wating ------> for " + z );
274             arr[z].join();
275         }
276         System.out.println("Child threads Finished -->Exiting");
277     }
278
279
280     public static class ThreadRunner implements Runnable JavaDoc {
281
282         PoolManager p1;
283         String JavaDoc poolName;
284         String JavaDoc owner;
285         int sleepTime ;
286
287         public ThreadRunner(PoolManager p1 , String JavaDoc poolName , String JavaDoc owner , int sleepTime) {
288
289             this.p1 = p1;
290             this.poolName = poolName;
291             this.owner = owner;
292             this.sleepTime= sleepTime;
293
294         }
295
296         public void run() {
297
298             try {
299                 for (int i = 0 ; i <= 10 ; i++) {
300                     System.out.println("Thread " + owner + " running");
301                     Connection conn = p1.getConnection(poolName);
302                     try {
303                         Thread.sleep(sleepTime);
304                     }
305                     catch (InterruptedException JavaDoc ie) {}
306                     conn.close();
307                 }
308             }
309             catch (Exception JavaDoc e){
310                 e.printStackTrace();
311             }
312
313
314         }
315
316     }
317
318 }
319
Popular Tags