1 4 5 package org.smartlib.pool.core; 6 7 8 import org.xml.sax.SAXNotRecognizedException ; 9 import org.xml.sax.SAXException ; 10 import org.apache.log4j.Logger; 11 12 import java.io.*; 13 import java.util.*; 14 import java.sql.*; 15 16 25 26 public class PoolManagerImpl implements PoolManager { 27 28 private Map poolMap = new HashMap(); 29 private String 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 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 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 68 public PoolManagerImpl(String fileName) throws ConnectionPoolException { 69 70 if (fileName == null || fileName.trim().equals("")) 71 throw new IllegalArgumentException ("File Name cannot be null/empty"); 72 File f1 = new File(fileName); 73 loadConfig(f1); 74 75 } 76 77 85 public PoolManagerImpl(File file) throws ConnectionPoolException { 86 87 loadConfig(file); 88 89 } 90 91 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 131 public Connection getConnection(String 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 158 public Connection getConnection(String poolName , String 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 178 public void addConnectionLeakListener(String 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 197 public void removeConnectionLeakListener(String 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 216 public MultiPoolMonitor getMultiPoolMonitor(String 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 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 public static void main (String args[]) throws Exception { 246 247 int k = 0; 248 Thread arr[] = new Thread [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 r = new Thread (new ThreadRunner(p1 , "Sachin" , "" + i 255 , i*800 )); 256 arr[k] = r; 257 k++; 258 r.start(); 259 r = new Thread (new ThreadRunner(p1 , "Shetty" , "" + i 260 , i*800 )); 261 r.start(); 262 arr[k] = r; 263 k++; 264 } 265 266 } 267 catch (Exception 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 { 281 282 PoolManager p1; 283 String poolName; 284 String owner; 285 int sleepTime ; 286 287 public ThreadRunner(PoolManager p1 , String poolName , String 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 ie) {} 306 conn.close(); 307 } 308 } 309 catch (Exception e){ 310 e.printStackTrace(); 311 } 312 313 314 } 315 316 } 317 318 } 319 | Popular Tags |