KickJava   Java API By Example, From Geeks To Geeks.

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


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

4
5
6
7 package org.smartlib.pool.core;
8
9 import org.apache.log4j.Logger;
10
11 import java.io.*;
12 import java.sql.*;
13
14 /**
15  * <tt>SmartPoolFactory</tt> provides a Singleton interface to the
16  * implementation of the PoolManager Interface.
17  * An object of <tt>SmartPoolFactory</tt> should be loaded with the
18  * configuration file prior to calling any static methods on the class.
19  *
20  * Once a object is loaded it maintains a single instance of PoolManager
21  * across all objects and static methods can be directly invoked on the class
22  * without creating any other instance of <tt>SmartPoolFactory<tt>.
23  *
24  * // Creating an instance .
25  * <pre>
26  * SmartPoolFactory = new SmartPoolFactory();
27  * </pre><p>
28  *
29  *
30  * // using <tt>SmartPoolFactory</tt> once it is initialised.
31  * <pre>
32  * Connection conn = SmartPoolFactory.getConnection();
33  * </pre><p>
34  *
35  * It is <b>advisable</b> to load the <tt>SmartPoolFactory</tt> on your
36  * application boot up (start-up-servlet in a web application), so that
37  * it is available for other components from the word go.
38  *
39  * @author Sachin Shekar Shetty
40  * @version 1.0, 02/08/01
41  *
42  */

43
44
45 public class SmartPoolFactory {
46
47
48     private Logger logger = Logger.getLogger(SmartPoolFactory.class);
49
50     //static for Singleton
51
private static PoolManager pm = null;
52
53     private static boolean shutDown = false;
54
55     private void startPool(File file) throws ConnectionPoolException {
56
57         // maintaining Singleton
58
if (pm == null)
59             pm = new PoolManagerImpl(file);
60
61     }
62
63     /**
64      * This constructor intialises the SmartPoolClass , reads the
65      * configuration from <code>file</code> and loads the PoolManger.
66      *
67      * @param file The configuration file.
68      * @exception ConnectionPoolException if there is any problem
69      * initialising the pools
70      */

71     public SmartPoolFactory(File file) throws ConnectionPoolException {
72     
73         if (pm != null)
74             throw new ConnectionPoolException("This Class follows a Singleton Pattern. Instance has already been created and initialized.");
75         if (file == null )
76             throw new IllegalArgumentException JavaDoc("file cannot be null");
77         startPool(file);
78         
79     }
80
81     /**
82      * This constructor intialises the SmartPoolClass , reads the
83      * configuration from system property and loads the PoolManger.
84      *
85      * @exception ConnectionPoolException if there is any problem
86      * initialising the pools
87      */

88     public SmartPoolFactory() throws ConnectionPoolException {
89
90         String JavaDoc fileName = System.getProperty(PoolConstants.CONFIG_FILE_SYSTEM_PROPERTY);
91         if (logger.isDebugEnabled()) {
92             logger.debug("Config File System Property: " + fileName);
93         }
94
95         if (pm != null && !shutDown)
96             throw new ConnectionPoolException("This Class follows a Singleton Pattern. Instance has already been created and is active.");
97         if (fileName == null && fileName.trim().length() != 0)
98             throw new IllegalArgumentException JavaDoc("System Property:" + PoolConstants.CONFIG_FILE_SYSTEM_PROPERTY
99                     + " cannnot be null for this constructor invocation, set it to location of the smart pool config file.");
100
101
102         startPool(new File(fileName));
103
104     }
105
106
107     /**
108      * This constructor intialises the SmartPoolClass , reads the
109      * configuration from <code>fileName</code> and loads the PoolManger.
110      *
111      * @param file The absolute configuration file path.
112      * @exception ConnectionPoolException if there is any problem
113      * initialising the pools
114      */

115     public SmartPoolFactory(String JavaDoc fileName) throws ConnectionPoolException {
116     
117         if (pm != null)
118             throw new ConnectionPoolException("This Class follows a Singleton Pattern. Instance has already been created and initialized.");
119         if (fileName == null || fileName.trim().equals(""))
120             throw new IllegalArgumentException JavaDoc("filename cannot be null/empty");
121         File file = new File(fileName);
122         startPool(file);
123
124     }
125
126     private static void checkAndThrow() throws ConnectionPoolException {
127         
128         if (pm == null )
129             throw new ConnectionPoolException("PoolManager is not initialised ,"
130                 + "Please create an object first with the configuration");
131
132     }
133
134     /**
135      * This method returns a Connection from the default connection pool.
136      * The owner of this pool is marked as N/A indicating unknown.
137      *
138      * <b>Note: This method blocks if the pool size has reached it's
139      * maximum size and no free connections are available
140      * until a free connection is available.</b> The time period for which this
141      * method blocks depends on the connection-wait-time-out specified in
142      * the configuration file.
143      *
144      *
145      * @return Connection from the default pool
146      * @exception ConnectionPoolException if there is any problem
147      * getting connection.
148      */

149     
150     public static Connection getConnection() throws ConnectionPoolException {
151
152         checkAndThrow();
153         return pm.getConnection();
154         
155     }
156
157     /**
158      * This method returns a Connection from the pool <code>poolName</code>.
159      * The owner of this pool is marked as N/A indicating unknown.
160      *
161      * <b>Note: This method blocks if the pool size has reached it's
162      * maximum size and no free connections are available
163      * until a free connection is available.</b> The time period for which this
164      * method blocks depends on the connection-wait-time-out specified in
165      * the configuration file.
166      *
167      *
168      * @param poolName Name of the pool.
169      * @return Connection from the pool
170      * @exception ConnectionPoolException if there is any problem
171      * getting connection.
172      */

173
174     public static Connection getConnection(String JavaDoc poolName)
175         throws ConnectionPoolException {
176
177         checkAndThrow();
178         return pm.getConnection(poolName);
179
180     }
181
182     /**
183      * This method returns a Connection from the pool <code>poolName</code>.
184      * The owner of this connection is identified by <code>owner</code> .
185      *
186      * <b>Note: This method blocks if the pool size has reached it's
187      * maximum size and no free connections are available
188      * until a free connection is available.</b> The time period for which this
189      * method blocks depends on the connection-wait-time-out specified in
190      * the configuration file.
191      *
192      *
193      * @param poolName Name of the pool.
194      * @param owner String identifying the owner.
195      * @return Connection from the pool
196      *
197      * @exception ConnectionPoolException if there is any problem
198      * getting connection.
199      */

200
201     public static Connection getConnection(String JavaDoc poolName , String JavaDoc owner)
202         throws ConnectionPoolException {
203
204         checkAndThrow();
205         return pm.getConnection(poolName,owner);
206
207     }
208
209     /**
210      * This method adds a connection leak listener.The methods of
211      * <code>cle</code> will be called when a leak is detected as per the
212      * pool configuration.
213      *
214      * @param poolName Name of the pool.
215      * @param cle Class implementing ConnectionLeakListener interface.
216      * @exception ConnectionPoolException If there is any problem
217      * adding ConnectionLeakListener.
218      */

219     public static void addConnectionLeakListener(String JavaDoc poolName
220             , ConnectionLeakListener cle) throws ConnectionPoolException {
221
222         checkAndThrow();
223         pm.addConnectionLeakListener(poolName , cle );
224
225     }
226
227     /**
228      * This method removes a connection leak listener.<code>cle</code> will
229      * not get any further notifications.
230      *
231      * @param poolName Name of the pool.
232      * @param cle Class implementing ConnectionLeakListener interface.
233      * @exception ConnectionPoolException if there is any problem
234      * removing ConnectionLeakListener.
235      */

236
237     public static void removeConnectionLeakListener(String JavaDoc poolName
238             , ConnectionLeakListener cle) throws ConnectionPoolException {
239
240         checkAndThrow();
241         pm.removeConnectionLeakListener(poolName,cle);
242         
243     }
244
245     /**
246      * This method returns the instance of PoolMonitor for the pool
247      * <code>poolName</code>
248      *
249      * @param poolName Name of the pool.
250      * @return Instance of PoolMonitor to monitor the state of the pool.
251      * @exception ConnectionPoolException
252      *
253      */

254
255     public static MultiPoolMonitor getPoolMonitor(String JavaDoc poolName)
256             throws ConnectionPoolException {
257
258         checkAndThrow();
259         return pm.getMultiPoolMonitor(poolName);
260     }
261
262     /**
263      * This method shuts down the pool, that is closes all connections to the database,
264      * the pool can no longer be used unless reinitialised using a new SmartPoolFactory instance.
265      */

266     public static void shutDown() {
267         shutDown = true;
268         pm.shutDown();
269     }
270
271
272     public static void main (String JavaDoc arg[]) throws Exception JavaDoc {
273
274         SmartPoolFactory smp= new SmartPoolFactory("c:\\windows\\desktop\\org.smartlib.pool.test.xml");
275         Connection conn = smp.getConnection();
276             
277         
278     }
279
280 }
281
282
Popular Tags