1 22 23 package org.cofax.connectionpool; 24 25 import java.sql.*; 26 import java.io.*; 27 import java.util.*; 28 29 public class PoolManager { 30 31 static private PoolManager instance; 32 33 static private int clients; 34 35 private LogWriter logWriter; 36 37 private PrintWriter pw; 38 39 private Vector drivers = new Vector(); 40 41 private Hashtable pools = new Hashtable(); 42 43 private PoolManager() { 44 init(); 45 } 46 47 private PoolManager(String configFile) { 48 init(configFile); 49 } 50 51 private PoolManager(Properties dbProps) { 52 init(dbProps); 53 } 54 55 static synchronized public PoolManager getInstance() { 56 if (instance == null) { 57 instance = new PoolManager(); 58 } 59 clients++; 60 return instance; 61 } 62 63 static synchronized public PoolManager getInstance(String configFile) { 64 if (instance == null) { 65 instance = new PoolManager(configFile); 66 } 67 clients++; 68 return instance; 69 } 70 71 static synchronized public PoolManager getInstance(Properties dbProps) { 72 if (instance == null) { 73 instance = new PoolManager(dbProps); 74 } 75 clients++; 76 return instance; 77 } 78 79 private void init(Properties dbProps) { 80 pw = new PrintWriter(System.err, true); 82 logWriter = new LogWriter("PoolManager", LogWriter.INFO, pw); 83 84 String logFile = dbProps.getProperty("logfile"); 85 if (logFile != null) { 86 try { 87 pw = new PrintWriter(new FileWriter(logFile, true), true); 88 logWriter.setPrintWriter(pw); 89 } catch (IOException e) { 90 logWriter.log("Can't open the log file: " + logFile + ". Using System.err instead", LogWriter.ERROR); 91 } 92 } 93 loadDrivers(dbProps); 94 createPools(dbProps); 95 } 96 97 private void init(String configFile) { 98 pw = new PrintWriter(System.err, true); 100 logWriter = new LogWriter("PoolManager", LogWriter.INFO, pw); 101 InputStream is = null; 102 try { 103 is = new FileInputStream(configFile); 104 } catch (Exception e) { 105 logWriter.log("Cannot find the properties file. " + configFile, LogWriter.ERROR); 106 return; 107 } 108 Properties dbProps = new Properties(); 109 try { 110 dbProps.load(is); 111 } catch (Exception e) { 112 logWriter.log("Can't read the properties file. " + configFile, LogWriter.ERROR); 113 return; 114 } 115 String logFile = dbProps.getProperty("logfile"); 116 if (logFile != null) { 117 try { 118 pw = new PrintWriter(new FileWriter(logFile, true), true); 119 logWriter.setPrintWriter(pw); 120 } catch (IOException e) { 121 logWriter.log("Can't open the log file: " + logFile + ". Using System.err instead", LogWriter.ERROR); 122 } 123 } 124 loadDrivers(dbProps); 125 createPools(dbProps); 126 } 127 128 private void init() { 129 pw = new PrintWriter(System.err, true); 131 logWriter = new LogWriter("PoolManager", LogWriter.INFO, pw); 132 InputStream is = getClass().getResourceAsStream("/db.properties"); 133 Properties dbProps = new Properties(); 134 try { 135 dbProps.load(is); 136 } catch (Exception e) { 137 logWriter.log("Can't read the properties file. " + "Make sure db.properties is in the CLASSPATH", LogWriter.ERROR); 138 return; 139 } 140 String logFile = dbProps.getProperty("logfile"); 141 if (logFile != null) { 142 try { 143 pw = new PrintWriter(new FileWriter(logFile, true), true); 144 logWriter.setPrintWriter(pw); 145 } catch (IOException e) { 146 logWriter.log("Can't open the log file: " + logFile + ". Using System.err instead", LogWriter.ERROR); 147 } 148 } 149 loadDrivers(dbProps); 150 createPools(dbProps); 151 } 152 153 private void loadDrivers(Properties props) { 154 String driverClasses = props.getProperty("drivers"); 155 StringTokenizer st = new StringTokenizer(driverClasses); 156 while (st.hasMoreElements()) { 157 String driverClassName = st.nextToken().trim(); 158 try { 159 Driver driver = (Driver) Class.forName(driverClassName).newInstance(); 160 DriverManager.registerDriver(driver); 161 drivers.addElement(driver); 162 logWriter.log("Registered JDBC driver " + driverClassName, LogWriter.INFO); 163 } catch (Exception e) { 164 logWriter.log(e, "Can't register JDBC driver: " + driverClassName, LogWriter.ERROR); 165 } 166 } 167 } 168 169 private void createPools(Properties props) { 170 Enumeration propNames = props.propertyNames(); 171 while (propNames.hasMoreElements()) { 172 String name = (String ) propNames.nextElement(); 173 if (name.endsWith(".url")) { 174 String poolName = name.substring(0, name.lastIndexOf(".")); 175 String url = props.getProperty(poolName + ".url"); 176 if (url == null) { 177 logWriter.log("No URL specified for " + poolName, LogWriter.ERROR); 178 continue; 179 } 180 181 String user = props.getProperty(poolName + ".user"); 182 String password = props.getProperty(poolName + ".password"); 183 184 String maxConns = props.getProperty(poolName + ".maxconns", "0"); 185 int max; 186 try { 187 max = Integer.valueOf(maxConns).intValue(); 188 } catch (NumberFormatException e) { 189 logWriter.log("Invalid maxconns value " + maxConns + " for " + poolName, LogWriter.ERROR); 190 max = 0; 191 } 192 193 String initConns = props.getProperty(poolName + ".initconns", "0"); 194 int init; 195 try { 196 init = Integer.valueOf(initConns).intValue(); 197 } catch (NumberFormatException e) { 198 logWriter.log("Invalid initconns value " + initConns + " for " + poolName, LogWriter.ERROR); 199 init = 0; 200 } 201 202 String loginTimeOut = props.getProperty(poolName + ".logintimeout", "5"); 203 int timeOut; 204 try { 205 timeOut = Integer.valueOf(loginTimeOut).intValue(); 206 } catch (NumberFormatException e) { 207 logWriter.log("Invalid logintimeout value " + loginTimeOut + " for " + poolName, LogWriter.ERROR); 208 timeOut = 5; 209 } 210 211 String logLevelProp = props.getProperty(poolName + ".loglevel", String.valueOf(LogWriter.ERROR)); 212 int logLevel = LogWriter.INFO; 213 if (logLevelProp.equalsIgnoreCase("none")) { 214 logLevel = LogWriter.NONE; 215 } else if (logLevelProp.equalsIgnoreCase("error")) { 216 logLevel = LogWriter.ERROR; 217 } else if (logLevelProp.equalsIgnoreCase("debug")) { 218 logLevel = LogWriter.DEBUG; 219 } 220 221 String usageLimitTmp = props.getProperty(poolName + ".connusagelimit", "50"); 222 int connUsageLimit; 223 try { 224 connUsageLimit = Integer.valueOf(usageLimitTmp).intValue(); 225 } catch (NumberFormatException e) { 226 logWriter.log("Invalid usage limit value " + usageLimitTmp + " for " + poolName, LogWriter.ERROR); 227 connUsageLimit = 100; 228 } 229 230 String testQuery = props.getProperty(poolName + ".testquery"); 231 if (testQuery == null) { 232 logWriter.log("No test query specified for " + poolName, LogWriter.ERROR); 233 } 234 235 ConnectionPool pool = new ConnectionPool(poolName, url, user, password, max, init, timeOut, pw, logLevel, testQuery, connUsageLimit); 236 pools.put(poolName, pool); 237 } 238 } 239 } 240 241 public ConnectionWrapper getConnection(String name) { 242 ConnectionWrapper conn = null; 243 ConnectionPool pool = (ConnectionPool) pools.get(name); 244 if (pool != null) { 245 try { 246 conn = pool.getConnection(); 247 } catch (SQLException e) { 248 } 253 } 254 return conn; 255 } 256 257 public void freeConnection(String name, ConnectionWrapper con) { 258 ConnectionPool pool = (ConnectionPool) pools.get(name); 259 if (pool != null) { 260 pool.freeConnection(con); 261 } 262 } 263 264 public synchronized void release() { 265 if (--clients != 0) { 267 return; 268 } 269 270 Enumeration allPools = pools.elements(); 271 while (allPools.hasMoreElements()) { 272 ConnectionPool pool = (ConnectionPool) allPools.nextElement(); 273 pool.release(); 274 } 275 276 Enumeration allDrivers = drivers.elements(); 277 while (allDrivers.hasMoreElements()) { 278 Driver driver = (Driver) allDrivers.nextElement(); 279 try { 280 DriverManager.deregisterDriver(driver); 281 logWriter.log("Deregistered JDBC driver " + driver.getClass().getName(), LogWriter.INFO); 282 } catch (SQLException e) { 283 logWriter.log(e, "Couldn't deregister JDBC driver: " + driver.getClass().getName(), LogWriter.ERROR); 284 } 285 } 286 } 287 288 public String getConnectionStats(String name) { 289 290 ConnectionPool pool = (ConnectionPool) pools.get(name); 291 if (pool != null) 292 return (pool.getConnectionStats()); 293 return (""); 294 295 } 296 297 public String getURL() { 298 ConnectionPool pool = (ConnectionPool) pools.get("cofax"); 299 if (pool != null) 300 return pool.getURL(); 301 return (""); 302 } 303 304 public String getUser() { 305 ConnectionPool pool = (ConnectionPool) pools.get("cofax"); 306 if (pool != null) 307 return pool.getUser(); 308 return (""); 309 } 310 311 public String getPassword() { 312 ConnectionPool pool = (ConnectionPool) pools.get("cofax"); 313 if (pool != null) 314 return pool.getPassword(); 315 return (""); 316 } 317 318 public int getMaxConns() { 319 ConnectionPool pool = (ConnectionPool) pools.get("cofax"); 320 if (pool != null) 321 return pool.getMaxConns(); 322 return (0); 323 } 324 325 public int getInitConns() { 326 ConnectionPool pool = (ConnectionPool) pools.get("cofax"); 327 if (pool != null) 328 return pool.getInitConns(); 329 return (0); 330 } 331 332 public int getTimeOut() { 333 ConnectionPool pool = (ConnectionPool) pools.get("cofax"); 334 if (pool != null) 335 return pool.getTimeOut(); 336 return (0); 337 } 338 339 public int getConUsageLimit() { 340 ConnectionPool pool = (ConnectionPool) pools.get("cofax"); 341 if (pool != null) 342 return pool.getConUsageLimit(); 343 return (0); 344 } 345 346 public long getKillTime() { 347 ConnectionPool pool = (ConnectionPool) pools.get("cofax"); 348 if (pool != null) 349 return pool.getKillTime(); 350 return (0); 351 } 352 353 public void setURL(String in) { 354 ConnectionPool pool = (ConnectionPool) pools.get("cofax"); 355 if (pool != null) 356 pool.setURL(in); 357 } 358 359 public void setUser(String in) { 360 ConnectionPool pool = (ConnectionPool) pools.get("cofax"); 361 if (pool != null) 362 pool.setUser(in); 363 } 364 365 public void setPassword(String in) { 366 ConnectionPool pool = (ConnectionPool) pools.get("cofax"); 367 if (pool != null) 368 pool.setPassword(in); 369 } 370 371 public void setMaxConns(int in) { 372 ConnectionPool pool = (ConnectionPool) pools.get("cofax"); 373 if (pool != null) 374 pool.setMaxConns(in); 375 } 376 377 public void setInitConns(int in) { 378 ConnectionPool pool = (ConnectionPool) pools.get("cofax"); 379 if (pool != null) 380 pool.setInitConns(in); 381 } 382 383 public void setTimeOut(int in) { 384 ConnectionPool pool = (ConnectionPool) pools.get("cofax"); 385 if (pool != null) 386 pool.setTimeOut(in); 387 } 388 389 public void setConUsageLimit(int in) { 390 ConnectionPool pool = (ConnectionPool) pools.get("cofax"); 391 if (pool != null) 392 pool.setConUsageLimit(in); 393 } 394 395 public void setKillTime(long in) { 396 ConnectionPool pool = (ConnectionPool) pools.get("cofax"); 397 if (pool != null) 398 pool.setKillTime(in); 399 } 400 401 } 402 | Popular Tags |