1 6 package org.logicalcobwebs.proxool; 7 8 import org.logicalcobwebs.logging.Log; 9 import org.logicalcobwebs.logging.LogFactory; 10 11 import javax.sql.DataSource ; 12 import javax.naming.NamingException ; 13 import javax.naming.Reference ; 14 import javax.naming.Context ; 15 import javax.naming.Name ; 16 import javax.naming.StringRefAddr ; 17 import javax.naming.BinaryRefAddr ; 18 import javax.naming.InitialContext ; 19 import java.sql.Connection ; 20 import java.sql.SQLException ; 21 import java.io.PrintWriter ; 22 import java.io.IOException ; 23 import java.io.Serializable ; 24 import java.io.ByteArrayOutputStream ; 25 import java.io.ObjectOutputStream ; 26 import java.util.Hashtable ; 27 import java.util.Properties ; 28 29 36 public class BasicDataSource implements DataSource { 37 38 private static final Log LOG = LogFactory.getLog(BasicDataSource.class); 39 40 private Properties jndiEnvironment; 41 42 44 private Context context; 45 46 private String contextFactory; 47 48 private String providerUrl; 49 50 private String securityAuthentication; 51 52 private String securityPrincipal; 53 54 private String securityCredentials; 55 56 58 private String alias; 59 60 private String url; 61 62 private String user; 63 64 private String password; 65 66 private String driver; 67 68 private int maximumConnectionLifetime;; 69 70 private int prototypeCount; 71 72 private int minimumConnectionCount; 73 74 private int maximumConnectionCount; 75 76 private int houseKeepingSleepTime; 77 78 private int simultaneousBuildThrottle; 79 80 private int recentlyStartedThreshold; 81 82 private int overloadWithoutRefusalLifetime; 83 84 private int maximumActiveTime; 85 86 private boolean verbose; 87 88 private boolean trace; 89 90 private String statistics; 91 92 private String statisticsLogLevel; 93 94 98 private String fatalSqlExceptionsAsString; 99 100 private String houseKeepingTestSql; 101 102 public BasicDataSource() { 103 reset(); 104 } 105 106 109 public Connection getConnection() throws SQLException { 110 111 ConnectionPool cp = null; 112 try { 113 if (!ConnectionPoolManager.getInstance().isPoolExists(alias)) { 114 registerPool(); 115 } 116 cp = ConnectionPoolManager.getInstance().getConnectionPool(alias); 117 return cp.getConnection(); 118 119 } catch (ProxoolException e) { 120 LOG.error("Problem getting connection", e); 121 throw new SQLException (e.toString()); 122 } catch (NamingException e) { 123 LOG.error("JNDI Problem whilst getting connection", e); 124 throw new SQLException (e.toString()); 125 } 126 } 127 128 133 private synchronized void registerPool() throws ProxoolException, NamingException { 134 if (!ConnectionPoolManager.getInstance().isPoolExists(alias)) { 135 ConnectionPoolDefinition cpd = new ConnectionPoolDefinition(); 136 cpd.setAlias(getAlias()); 137 cpd.setDriver(getDriver()); 138 cpd.setFatalSqlExceptionsAsString(getFatalSqlExceptionsAsString()); 139 cpd.setHouseKeepingSleepTime(getHouseKeepingSleepTime()); 140 cpd.setHouseKeepingTestSql(getHouseKeepingTestSql()); 141 cpd.setMaximumActiveTime(getMaximumActiveTime()); 142 cpd.setMaximumConnectionCount(getMaximumConnectionCount()); 143 cpd.setMaximumConnectionLifetime(getMaximumConnectionLifetime()); 144 cpd.setMinimumConnectionCount(getMinimumConnectionCount()); 145 cpd.setOverloadWithoutRefusalLifetime(getOverloadWithoutRefusalLifetime()); 146 cpd.setPassword(getPassword()); 147 cpd.setPrototypeCount(getPrototypeCount()); 148 cpd.setRecentlyStartedThreshold(getRecentlyStartedThreshold()); 149 cpd.setSimultaneousBuildThrottle(getSimultaneousBuildThrottle()); 150 cpd.setStatistics(getStatistics()); 151 cpd.setStatisticsLogLevel(getStatisticsLogLevel()); 152 cpd.setTrace(isTrace()); 153 cpd.setUrl(getUrl()); 154 cpd.setUser(getUser()); 155 cpd.setVerbose(isVerbose()); 156 ProxoolFacade.registerConnectionPool(cpd); 157 158 } 159 160 Hashtable env = new Hashtable (); 161 env.put(Context.INITIAL_CONTEXT_FACTORY, getContextFactory()); 162 env.put(Context.PROVIDER_URL, getProviderUrl()); 163 env.put(Context.SECURITY_AUTHENTICATION, getSecurityAuthentication()); 164 env.put(Context.SECURITY_PRINCIPAL, getSecurityPrincipal()); 165 env.put(Context.SECURITY_CREDENTIALS, getSecurityCredentials()); 166 context = new InitialContext (env); 167 context.bind("java:/comp/env/jdbc/proxool." + getAlias(), this); 168 169 } 170 171 public String getContextFactory() { 172 return contextFactory; 173 } 174 175 public void setContextFactory(String contextFactory) { 176 this.contextFactory = contextFactory; 177 } 178 179 public String getProviderUrl() { 180 return providerUrl; 181 } 182 183 public void setProviderUrl(String providerUrl) { 184 this.providerUrl = providerUrl; 185 } 186 187 public String getSecurityAuthentication() { 188 return securityAuthentication; 189 } 190 191 public void setSecurityAuthentication(String securityAuthentication) { 192 this.securityAuthentication = securityAuthentication; 193 } 194 195 public String getSecurityPrincipal() { 196 return securityPrincipal; 197 } 198 199 public void setSecurityPrincipal(String securityPrincipal) { 200 this.securityPrincipal = securityPrincipal; 201 } 202 203 public String getSecurityCredentials() { 204 return securityCredentials; 205 } 206 207 public void setSecurityCredentials(String securityCredentials) { 208 this.securityCredentials = securityCredentials; 209 } 210 211 215 public Connection getConnection(String username, String password) 216 throws SQLException { 217 throw new UnsupportedOperationException ("You should configure the username and password " 218 + "within the proxool configuration and just call getConnection() instead."); 219 } 220 221 225 public int getLoginTimeout() { 226 throw new UnsupportedOperationException ("login timeout is not supported"); 227 } 228 229 233 public void setLoginTimeout(int loginTimeout) { 234 throw new UnsupportedOperationException ("login timeout is not supported"); 235 } 236 237 241 public PrintWriter getLogWriter() { 242 throw new UnsupportedOperationException ("Proxool uses Jakarta's Commons' Logging API"); 243 } 244 245 249 public void setLogWriter(PrintWriter logWriter) { 250 throw new UnsupportedOperationException ("Proxool uses Jakarta's Commons' Logging API"); 251 } 252 253 256 public String getAlias() { 257 return alias; 258 } 259 260 263 public void setAlias(String alias) { 264 this.alias = alias; 265 } 266 267 270 public String getUrl() { 271 return url; 272 } 273 274 277 public void setUrl(String url) { 278 this.url = url; 279 } 280 281 284 public String getDriver() { 285 return driver; 286 } 287 288 291 public void setDriver(String driver) { 292 this.driver = driver; 293 } 294 295 298 public int getMaximumConnectionLifetime() { 299 return maximumConnectionLifetime; 300 } 301 302 305 public void setMaximumConnectionLifetime(int maximumConnectionLifetime) { 306 this.maximumConnectionLifetime = maximumConnectionLifetime; 307 } 308 309 312 public int getPrototypeCount() { 313 return prototypeCount; 314 } 315 316 319 public void setPrototypeCount(int prototypeCount) { 320 this.prototypeCount = prototypeCount; 321 } 322 323 326 public int getMinimumConnectionCount() { 327 return minimumConnectionCount; 328 } 329 330 333 public void setMinimumConnectionCount(int minimumConnectionCount) { 334 this.minimumConnectionCount = minimumConnectionCount; 335 } 336 337 340 public int getMaximumConnectionCount() { 341 return maximumConnectionCount; 342 } 343 344 347 public void setMaximumConnectionCount(int maximumConnectionCount) { 348 this.maximumConnectionCount = maximumConnectionCount; 349 } 350 351 354 public int getHouseKeepingSleepTime() { 355 return houseKeepingSleepTime; 356 } 357 358 361 public void setHouseKeepingSleepTime(int houseKeepingSleepTime) { 362 this.houseKeepingSleepTime = houseKeepingSleepTime; 363 } 364 365 368 public int getSimultaneousBuildThrottle() { 369 return simultaneousBuildThrottle; 370 } 371 372 375 public void setSimultaneousBuildThrottle(int simultaneousBuildThrottle) { 376 this.simultaneousBuildThrottle = simultaneousBuildThrottle; 377 } 378 379 382 public int getRecentlyStartedThreshold() { 383 return recentlyStartedThreshold; 384 } 385 386 389 public void setRecentlyStartedThreshold(int recentlyStartedThreshold) { 390 this.recentlyStartedThreshold = recentlyStartedThreshold; 391 } 392 393 396 public int getOverloadWithoutRefusalLifetime() { 397 return overloadWithoutRefusalLifetime; 398 } 399 400 403 public void setOverloadWithoutRefusalLifetime(int overloadWithoutRefusalLifetime) { 404 this.overloadWithoutRefusalLifetime = overloadWithoutRefusalLifetime; 405 } 406 407 410 public int getMaximumActiveTime() { 411 return maximumActiveTime; 412 } 413 414 417 public void setMaximumActiveTime(int maximumActiveTime) { 418 this.maximumActiveTime = maximumActiveTime; 419 } 420 421 424 public boolean isVerbose() { 425 return verbose; 426 } 427 428 431 public void setVerbose(boolean verbose) { 432 this.verbose = verbose; 433 } 434 435 438 public boolean isTrace() { 439 return trace; 440 } 441 442 445 public void setTrace(boolean trace) { 446 this.trace = trace; 447 } 448 449 452 public String getStatistics() { 453 return statistics; 454 } 455 456 459 public void setStatistics(String statistics) { 460 this.statistics = statistics; 461 } 462 463 466 public String getStatisticsLogLevel() { 467 return statisticsLogLevel; 468 } 469 470 473 public void setStatisticsLogLevel(String statisticsLogLevel) { 474 this.statisticsLogLevel = statisticsLogLevel; 475 } 476 477 480 public String getFatalSqlExceptionsAsString() { 481 return fatalSqlExceptionsAsString; 482 } 483 484 487 public void setFatalSqlExceptionsAsString(String fatalSqlExceptionsAsString) { 488 this.fatalSqlExceptionsAsString = fatalSqlExceptionsAsString; 489 } 490 491 494 public String getHouseKeepingTestSql() { 495 return houseKeepingTestSql; 496 } 497 498 501 public void setHouseKeepingTestSql(String houseKeepingTestSql) { 502 this.houseKeepingTestSql = houseKeepingTestSql; 503 } 504 505 508 public String getUser() { 509 return user; 510 } 511 512 515 public void setUser(String user) { 516 this.user = user; 517 } 518 519 522 public String getPassword() { 523 return password; 524 } 525 526 529 public void setPassword(String password) { 530 this.password = password; 531 } 532 533 536 private void reset() { 537 url = null; 538 driver = null; 539 maximumConnectionLifetime = ConnectionPoolDefinitionIF.DEFAULT_MAXIMUM_CONNECTION_LIFETIME; 540 prototypeCount = ConnectionPoolDefinitionIF.DEFAULT_PROTOTYPE_COUNT; 541 minimumConnectionCount = ConnectionPoolDefinitionIF.DEFAULT_MINIMUM_CONNECTION_COUNT; 542 maximumConnectionCount = ConnectionPoolDefinitionIF.DEFAULT_MAXIMUM_CONNECTION_COUNT; 543 houseKeepingSleepTime = ConnectionPoolDefinitionIF.DEFAULT_HOUSE_KEEPING_SLEEP_TIME; 544 houseKeepingTestSql = null; 545 simultaneousBuildThrottle = ConnectionPoolDefinitionIF.DEFAULT_SIMULTANEOUS_BUILD_THROTTLE; 546 ; 547 recentlyStartedThreshold = ConnectionPoolDefinitionIF.DEFAULT_RECENTLY_STARTED_THRESHOLD; 548 overloadWithoutRefusalLifetime = ConnectionPoolDefinitionIF.DEFAULT_OVERLOAD_WITHOUT_REFUSAL_THRESHOLD; 549 maximumActiveTime = ConnectionPoolDefinitionIF.DEFAULT_MAXIMUM_ACTIVE_TIME; 550 verbose = false; 551 trace = false; 552 statistics = null; 553 statisticsLogLevel = null; 554 } 555 556 560 public Reference getReference() 561 throws NamingException { 562 String factory = getClass().getName(); 564 Reference ref = new Reference (getClass().getName(), factory, null); 565 566 ref.add(new StringRefAddr ("url", getUrl())); 567 568 byte[] ser = null; 569 if (jndiEnvironment != null) { 571 try { 572 ser = serialize(jndiEnvironment); 573 ref.add(new BinaryRefAddr ("jndiEnvironment", ser)); 574 } catch (IOException ioe) { 575 throw new NamingException ("An IOException prevented " 576 + "serializing the jndiEnvironment properties."); 577 } 578 } 579 580 return null; 582 } 583 584 587 public Object getObjectInstance(Object refObj, Name name, 588 Context context, Hashtable env) 589 throws Exception { 590 return null; 592 } 593 594 601 public String getJndiEnvironment(String key) { 602 String value = null; 603 if (jndiEnvironment != null) { 604 value = jndiEnvironment.getProperty(key); 605 } 606 return value; 607 } 608 609 617 public void setJndiEnvironment(String key, String value) { 618 if (jndiEnvironment == null) { 619 jndiEnvironment = new Properties (); 620 } 621 jndiEnvironment.setProperty(key, value); 622 } 623 624 631 private static byte[] serialize(Serializable obj) 632 throws IOException { 633 byte[] byteArray = null; 634 ByteArrayOutputStream baos = null; 635 ObjectOutputStream out = null; 636 try { 637 baos = new ByteArrayOutputStream (); 639 out = new ObjectOutputStream (baos); 640 641 out.writeObject(obj); 642 byteArray = baos.toByteArray(); 643 } finally { 644 if (out != null) { 645 out.close(); 646 } 647 } 648 return byteArray; 649 } 650 651 } 652 653 | Popular Tags |