1 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 43 44 45 public class SmartPoolFactory { 46 47 48 private Logger logger = Logger.getLogger(SmartPoolFactory.class); 49 50 private static PoolManager pm = null; 52 53 private static boolean shutDown = false; 54 55 private void startPool(File file) throws ConnectionPoolException { 56 57 if (pm == null) 59 pm = new PoolManagerImpl(file); 60 61 } 62 63 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 ("file cannot be null"); 77 startPool(file); 78 79 } 80 81 88 public SmartPoolFactory() throws ConnectionPoolException { 89 90 String 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 ("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 115 public SmartPoolFactory(String 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 ("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 149 150 public static Connection getConnection() throws ConnectionPoolException { 151 152 checkAndThrow(); 153 return pm.getConnection(); 154 155 } 156 157 173 174 public static Connection getConnection(String poolName) 175 throws ConnectionPoolException { 176 177 checkAndThrow(); 178 return pm.getConnection(poolName); 179 180 } 181 182 200 201 public static Connection getConnection(String poolName , String owner) 202 throws ConnectionPoolException { 203 204 checkAndThrow(); 205 return pm.getConnection(poolName,owner); 206 207 } 208 209 219 public static void addConnectionLeakListener(String poolName 220 , ConnectionLeakListener cle) throws ConnectionPoolException { 221 222 checkAndThrow(); 223 pm.addConnectionLeakListener(poolName , cle ); 224 225 } 226 227 236 237 public static void removeConnectionLeakListener(String poolName 238 , ConnectionLeakListener cle) throws ConnectionPoolException { 239 240 checkAndThrow(); 241 pm.removeConnectionLeakListener(poolName,cle); 242 243 } 244 245 254 255 public static MultiPoolMonitor getPoolMonitor(String poolName) 256 throws ConnectionPoolException { 257 258 checkAndThrow(); 259 return pm.getMultiPoolMonitor(poolName); 260 } 261 262 266 public static void shutDown() { 267 shutDown = true; 268 pm.shutDown(); 269 } 270 271 272 public static void main (String arg[]) throws Exception { 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 |