1 6 package org.logicalcobwebs.proxool; 7 8 import org.apache.commons.logging.Log; 9 import org.apache.commons.logging.LogFactory; 10 11 import javax.naming.spi.ObjectFactory ; 12 import javax.naming.Name ; 13 import javax.naming.Context ; 14 import javax.naming.Reference ; 15 import javax.naming.RefAddr ; 16 import javax.naming.StringRefAddr ; 17 import javax.sql.DataSource ; 18 import java.sql.Connection ; 19 import java.sql.SQLException ; 20 import java.util.Hashtable ; 21 import java.util.Enumeration ; 22 import java.util.Properties ; 23 import java.util.StringTokenizer ; 24 import java.io.PrintWriter ; 25 26 40 public class ProxoolDataSource implements DataSource , ObjectFactory { 41 private static final Log LOG = LogFactory.getLog(ProxoolDataSource.class); 42 43 private int loginTimeout; 44 private PrintWriter logWriter; 45 46 private String alias; 47 private String driver; 48 private String fatalSqlExceptionWrapperClass; 49 private int houseKeepingSleepTime; 50 private String houseKeepingTestSql; 51 private int maximumActiveTime; 52 private int maximumConnectionCount; 53 private int maximumConnectionLifetime;; 54 private int minimumConnectionCount; 55 private int overloadWithoutRefusalLifetime; 56 private String password; 57 private int prototypeCount; 58 private int recentlyStartedThreshold; 59 private int simultaneousBuildThrottle; 60 private String statistics; 61 private String statisticsLogLevel; 62 private boolean trace; 63 private String driverUrl; 64 private String user; 65 private boolean verbose; 66 private boolean jmx; 67 private String jmxAgentId; 68 private boolean testBeforeUse; 69 private boolean testAfterUse; 70 private Properties delegateProperties = new Properties (); 71 72 76 private String fatalSqlExceptionsAsString; 77 78 public ProxoolDataSource() { 79 reset(); 80 } 81 82 public ProxoolDataSource (String alias) { 83 this.alias = alias; 84 } 85 86 89 public Connection getConnection() throws SQLException { 90 91 ConnectionPool cp = null; 92 try { 93 if (!ConnectionPoolManager.getInstance().isPoolExists(alias)) { 94 registerPool(); 95 } 96 cp = ConnectionPoolManager.getInstance().getConnectionPool(alias); 97 return cp.getConnection(); 98 } catch (ProxoolException e) { 99 LOG.error("Problem getting connection", e); 100 throw new SQLException (e.toString()); 101 } 102 } 103 104 109 private synchronized void registerPool() throws ProxoolException { 110 if (!ConnectionPoolManager.getInstance().isPoolExists(alias)) { 111 ConnectionPoolDefinition cpd = new ConnectionPoolDefinition(); 112 cpd.setAlias(getAlias()); 113 cpd.setDriver(getDriver()); 114 cpd.setFatalSqlExceptionsAsString(getFatalSqlExceptionsAsString()); 115 cpd.setFatalSqlExceptionWrapper(getFatalSqlExceptionWrapperClass()); 116 cpd.setHouseKeepingSleepTime(getHouseKeepingSleepTime()); 117 cpd.setHouseKeepingTestSql(getHouseKeepingTestSql()); 118 cpd.setMaximumActiveTime(getMaximumActiveTime()); 119 cpd.setMaximumConnectionCount(getMaximumConnectionCount()); 120 cpd.setMaximumConnectionLifetime(getMaximumConnectionLifetime()); 121 cpd.setMinimumConnectionCount(getMinimumConnectionCount()); 122 cpd.setOverloadWithoutRefusalLifetime(getOverloadWithoutRefusalLifetime()); 123 cpd.setPassword(getPassword()); 124 cpd.setPrototypeCount(getPrototypeCount()); 125 cpd.setRecentlyStartedThreshold(getRecentlyStartedThreshold()); 126 cpd.setSimultaneousBuildThrottle(getSimultaneousBuildThrottle()); 127 cpd.setStatistics(getStatistics()); 128 cpd.setStatisticsLogLevel(getStatisticsLogLevel()); 129 cpd.setTrace(isTrace()); 130 cpd.setUrl(getDriverUrl()); 131 cpd.setUser(getUser()); 132 cpd.setVerbose(isVerbose()); 133 cpd.setJmx(isJmx()); 134 cpd.setJmxAgentId(getJmxAgentId()); 135 cpd.setTestAfterUse(isTestAfterUse()); 136 cpd.setTestBeforeUse(isTestBeforeUse()); 137 cpd.setDelegateProperties(delegateProperties); 138 ProxoolFacade.registerConnectionPool(cpd); 139 } 140 } 141 142 143 public Object getObjectInstance(Object refObject, Name name, Context context, Hashtable hashtable) throws Exception { 144 if (!(refObject instanceof Reference )) { 146 return null; 147 } 148 Reference reference = (Reference ) refObject; 149 155 if (!ConnectionPoolManager.getInstance().isPoolExists(reference.get(ProxoolConstants.ALIAS_PROPERTY).toString())) { 157 populatePropertiesFromReference(reference); 158 } 159 return this; 160 } 161 162 165 public String getAlias() { 166 return alias; 167 } 168 169 172 public void setAlias(String alias) { 173 this.alias = alias; 174 } 175 176 179 public String getDriverUrl() { 180 return driverUrl; 181 } 182 183 186 public void setDriverUrl(String url) { 187 this.driverUrl = url; 188 } 189 190 193 public String getDriver() { 194 return driver; 195 } 196 197 200 public void setDriver(String driver) { 201 this.driver = driver; 202 } 203 204 207 public int getMaximumConnectionLifetime() { 208 return maximumConnectionLifetime; 209 } 210 211 214 public void setMaximumConnectionLifetime(int maximumConnectionLifetime) { 215 this.maximumConnectionLifetime = maximumConnectionLifetime; 216 } 217 218 221 public int getPrototypeCount() { 222 return prototypeCount; 223 } 224 225 228 public void setPrototypeCount(int prototypeCount) { 229 this.prototypeCount = prototypeCount; 230 } 231 232 235 public int getMinimumConnectionCount() { 236 return minimumConnectionCount; 237 } 238 239 242 public void setMinimumConnectionCount(int minimumConnectionCount) { 243 this.minimumConnectionCount = minimumConnectionCount; 244 } 245 246 249 public int getMaximumConnectionCount() { 250 return maximumConnectionCount; 251 } 252 253 256 public void setMaximumConnectionCount(int maximumConnectionCount) { 257 this.maximumConnectionCount = maximumConnectionCount; 258 } 259 260 263 public int getHouseKeepingSleepTime() { 264 return houseKeepingSleepTime; 265 } 266 267 270 public void setHouseKeepingSleepTime(int houseKeepingSleepTime) { 271 this.houseKeepingSleepTime = houseKeepingSleepTime; 272 } 273 274 277 public int getSimultaneousBuildThrottle() { 278 return simultaneousBuildThrottle; 279 } 280 281 284 public void setSimultaneousBuildThrottle(int simultaneousBuildThrottle) { 285 this.simultaneousBuildThrottle = simultaneousBuildThrottle; 286 } 287 288 291 public int getRecentlyStartedThreshold() { 292 return recentlyStartedThreshold; 293 } 294 295 298 public void setRecentlyStartedThreshold(int recentlyStartedThreshold) { 299 this.recentlyStartedThreshold = recentlyStartedThreshold; 300 } 301 302 305 public int getOverloadWithoutRefusalLifetime() { 306 return overloadWithoutRefusalLifetime; 307 } 308 309 312 public void setOverloadWithoutRefusalLifetime(int overloadWithoutRefusalLifetime) { 313 this.overloadWithoutRefusalLifetime = overloadWithoutRefusalLifetime; 314 } 315 316 319 public int getMaximumActiveTime() { 320 return maximumActiveTime; 321 } 322 323 326 public void setMaximumActiveTime(int maximumActiveTime) { 327 this.maximumActiveTime = maximumActiveTime; 328 } 329 330 333 public boolean isVerbose() { 334 return verbose; 335 } 336 337 340 public void setVerbose(boolean verbose) { 341 this.verbose = verbose; 342 } 343 344 347 public boolean isTrace() { 348 return trace; 349 } 350 351 354 public void setTrace(boolean trace) { 355 this.trace = trace; 356 } 357 358 361 public String getStatistics() { 362 return statistics; 363 } 364 365 368 public void setStatistics(String statistics) { 369 this.statistics = statistics; 370 } 371 372 375 public String getStatisticsLogLevel() { 376 return statisticsLogLevel; 377 } 378 379 382 public void setStatisticsLogLevel(String statisticsLogLevel) { 383 this.statisticsLogLevel = statisticsLogLevel; 384 } 385 386 389 public String getFatalSqlExceptionsAsString() { 390 return fatalSqlExceptionsAsString; 391 } 392 393 396 public void setFatalSqlExceptionsAsString(String fatalSqlExceptionsAsString) { 397 this.fatalSqlExceptionsAsString = fatalSqlExceptionsAsString; 398 } 399 400 403 public String getFatalSqlExceptionWrapperClass() { 404 return fatalSqlExceptionWrapperClass; 405 } 406 407 410 public void setFatalSqlExceptionWrapperClass(String fatalSqlExceptionWrapperClass) { 411 this.fatalSqlExceptionWrapperClass = fatalSqlExceptionWrapperClass; 412 } 413 414 417 public String getHouseKeepingTestSql() { 418 return houseKeepingTestSql; 419 } 420 421 424 public void setHouseKeepingTestSql(String houseKeepingTestSql) { 425 this.houseKeepingTestSql = houseKeepingTestSql; 426 } 427 428 431 public String getUser() { 432 return user; 433 } 434 435 438 public void setUser(String user) { 439 this.user = user; 440 } 441 442 445 public String getPassword() { 446 return password; 447 } 448 449 452 public void setPassword(String password) { 453 this.password = password; 454 } 455 456 459 public boolean isJmx() { 460 return jmx; 461 } 462 463 466 public void setJmx(boolean jmx) { 467 this.jmx = jmx; 468 } 469 470 473 public String getJmxAgentId() { 474 return jmxAgentId; 475 } 476 477 480 public void setJmxAgentId(String jmxAgentId) { 481 this.jmxAgentId = jmxAgentId; 482 } 483 484 487 public boolean isTestBeforeUse() { 488 return testBeforeUse; 489 } 490 491 494 public void setTestBeforeUse(boolean testBeforeUse) { 495 this.testBeforeUse = testBeforeUse; 496 } 497 498 501 public boolean isTestAfterUse() { 502 return testAfterUse; 503 } 504 505 508 public void setTestAfterUse(boolean testAfterUse) { 509 this.testAfterUse = testAfterUse; 510 } 511 512 518 public void setDelegateProperties(String properties) { 519 StringTokenizer stOuter = new StringTokenizer (properties, ","); 520 while (stOuter.hasMoreTokens()) { 521 StringTokenizer stInner = new StringTokenizer (stOuter.nextToken(), "="); 522 if (stInner.countTokens() != 2) { 523 throw new IllegalArgumentException ("Unexpected delegateProperties value: '" + properties + "'. Expected 'name=value'"); 524 } 525 delegateProperties.put(stInner.nextToken().trim(), stInner.nextToken().trim()); 526 } 527 } 528 529 private void populatePropertiesFromReference(Reference reference) { 530 RefAddr property = reference.get(ProxoolConstants.ALIAS_PROPERTY); 531 if (property != null) { 532 setAlias(property.getContent().toString()); 533 } 534 property = reference.get(ProxoolConstants.DRIVER_CLASS_PROPERTY); 535 if (property != null) { 536 setDriver(property.getContent().toString()); 537 } 538 property = reference.get(ProxoolConstants.FATAL_SQL_EXCEPTION_WRAPPER_CLASS_PROPERTY); 539 if (property != null) { 540 setFatalSqlExceptionWrapperClass(property.getContent().toString()); 541 } 542 property = reference.get(ProxoolConstants.HOUSE_KEEPING_SLEEP_TIME_PROPERTY); 543 if (property != null) { 544 setHouseKeepingSleepTime(Integer.valueOf(property.getContent().toString()).intValue()); 545 } 546 property = reference.get(ProxoolConstants.HOUSE_KEEPING_TEST_SQL_PROPERTY); 547 if (property != null) { 548 setHouseKeepingTestSql(property.getContent().toString()); 549 } 550 property = reference.get(ProxoolConstants.MAXIMUM_CONNECTION_COUNT_PROPERTY); 551 if (property != null) { 552 setMaximumConnectionCount(Integer.valueOf(property.getContent().toString()).intValue()); 553 } 554 property = reference.get(ProxoolConstants.MAXIMUM_CONNECTION_LIFETIME_PROPERTY); 555 if (property != null) { 556 setMaximumConnectionLifetime(Integer.valueOf(property.getContent().toString()).intValue()); 557 } 558 property = reference.get(ProxoolConstants.MAXIMUM_ACTIVE_TIME_PROPERTY); 559 if (property != null) { 560 setMaximumActiveTime(Integer.valueOf(property.getContent().toString()).intValue()); 561 } 562 property = reference.get(ProxoolConstants.MINIMUM_CONNECTION_COUNT_PROPERTY); 563 if (property != null) { 564 setMinimumConnectionCount(Integer.valueOf(property.getContent().toString()).intValue()); 565 } 566 property = reference.get(ProxoolConstants.OVERLOAD_WITHOUT_REFUSAL_LIFETIME_PROPERTY); 567 if (property != null) { 568 setOverloadWithoutRefusalLifetime(Integer.valueOf(property.getContent().toString()).intValue()); 569 } 570 property = reference.get(ProxoolConstants.PASSWORD_PROPERTY); 571 if (property != null) { 572 setPassword(property.getContent().toString()); 573 } 574 property = reference.get(ProxoolConstants.PROTOTYPE_COUNT_PROPERTY); 575 if (property != null) { 576 setPrototypeCount(Integer.valueOf(property.getContent().toString()).intValue()); 577 } 578 property = reference.get(ProxoolConstants.RECENTLY_STARTED_THRESHOLD_PROPERTY); 579 if (property != null) { 580 setRecentlyStartedThreshold(Integer.valueOf(property.getContent().toString()).intValue()); 581 } 582 property = reference.get(ProxoolConstants.SIMULTANEOUS_BUILD_THROTTLE_PROPERTY); 583 if (property != null) { 584 setSimultaneousBuildThrottle(Integer.valueOf(property.getContent().toString()).intValue()); 585 } 586 property = reference.get(ProxoolConstants.STATISTICS_PROPERTY); 587 if (property != null) { 588 setStatistics(property.getContent().toString()); 589 } 590 property = reference.get(ProxoolConstants.STATISTICS_LOG_LEVEL_PROPERTY); 591 if (property != null) { 592 setStatisticsLogLevel(property.getContent().toString()); 593 } 594 property = reference.get(ProxoolConstants.TRACE_PROPERTY); 595 if (property != null) { 596 setTrace("true".equalsIgnoreCase(property.getContent().toString())); 597 } 598 property = reference.get(ProxoolConstants.DRIVER_URL_PROPERTY); 599 if (property != null) { 600 setDriverUrl(property.getContent().toString()); 601 } 602 property = reference.get(ProxoolConstants.USER_PROPERTY); 603 if (property != null) { 604 setUser(property.getContent().toString()); 605 } 606 property = reference.get(ProxoolConstants.VERBOSE_PROPERTY); 607 if (property != null) { 608 setVerbose("true".equalsIgnoreCase(property.getContent().toString())); 609 } 610 property = reference.get(ProxoolConstants.JMX_PROPERTY); 611 if (property != null) { 612 setJmx("true".equalsIgnoreCase(property.getContent().toString())); 613 } 614 property = reference.get(ProxoolConstants.JMX_AGENT_PROPERTY); 615 if (property != null) { 616 setJmxAgentId(property.getContent().toString()); 617 } 618 property = reference.get(ProxoolConstants.TEST_BEFORE_USE_PROPERTY); 619 if (property != null) { 620 setTestBeforeUse("true".equalsIgnoreCase(property.getContent().toString())); 621 } 622 property = reference.get(ProxoolConstants.TEST_AFTER_USE_PROPERTY); 623 if (property != null) { 624 setTestAfterUse("true".equalsIgnoreCase(property.getContent().toString())); 625 } 626 Enumeration e = reference.getAll(); 628 while (e.hasMoreElements()) { 629 StringRefAddr stringRefAddr = (StringRefAddr ) e.nextElement(); 630 String name = stringRefAddr.getType(); 631 String content = stringRefAddr.getContent().toString(); 632 if (name.indexOf(ProxoolConstants.PROPERTY_PREFIX) != 0) { 633 delegateProperties.put(name, content); 634 } 635 } 636 } 637 638 641 private void reset() { 642 driverUrl = null; 643 driver = null; 644 maximumConnectionLifetime = ConnectionPoolDefinitionIF.DEFAULT_MAXIMUM_CONNECTION_LIFETIME; 645 prototypeCount = ConnectionPoolDefinitionIF.DEFAULT_PROTOTYPE_COUNT; 646 minimumConnectionCount = ConnectionPoolDefinitionIF.DEFAULT_MINIMUM_CONNECTION_COUNT; 647 maximumConnectionCount = ConnectionPoolDefinitionIF.DEFAULT_MAXIMUM_CONNECTION_COUNT; 648 houseKeepingSleepTime = ConnectionPoolDefinitionIF.DEFAULT_HOUSE_KEEPING_SLEEP_TIME; 649 houseKeepingTestSql = null; 650 simultaneousBuildThrottle = ConnectionPoolDefinitionIF.DEFAULT_SIMULTANEOUS_BUILD_THROTTLE; 651 recentlyStartedThreshold = ConnectionPoolDefinitionIF.DEFAULT_RECENTLY_STARTED_THRESHOLD; 652 overloadWithoutRefusalLifetime = ConnectionPoolDefinitionIF.DEFAULT_OVERLOAD_WITHOUT_REFUSAL_THRESHOLD; 653 maximumActiveTime = ConnectionPoolDefinitionIF.DEFAULT_MAXIMUM_ACTIVE_TIME; 654 verbose = false; 655 trace = false; 656 statistics = null; 657 statisticsLogLevel = null; 658 delegateProperties.clear(); 659 } 660 661 public PrintWriter getLogWriter() throws SQLException { 662 return this.logWriter; 663 } 664 665 public int getLoginTimeout() throws SQLException { 666 return this.loginTimeout; 667 } 668 669 public void setLogWriter(PrintWriter logWriter) throws SQLException { 670 this.logWriter = logWriter; 671 } 672 673 public void setLoginTimeout(int loginTimeout) throws SQLException { 674 this.loginTimeout = loginTimeout; 675 } 676 677 public Connection getConnection(String s, String s1) throws SQLException { 678 throw new UnsupportedOperationException ("You should configure the username and password " 679 + "within the proxool configuration and just call getConnection() instead."); 680 } 681 } 682 683 | Popular Tags |