001 /*****************************************************************************
002 * Copyright (c) PicoContainer Organization. All rights reserved. *
003 * ------------------------------------------------------------------------- *
004 * The software in this package is published under the terms of the BSD *
005 * style license a copy of which has been included with this distribution in *
006 * the license.html file. *
007 * *
008 * Idea by Rachel Davies, Original code by Aslak Hellesoy and Paul Hammant *
009 *****************************************************************************/
010 package org.nanocontainer.persistence.jdbc;
011
012 import java.util.Properties;
013
014 import javax.sql.DataSource;
015
016 import org.apache.commons.dbcp.BasicDataSource;
017 import org.apache.commons.dbcp.BasicDataSourceFactory;
018 import org.nanocontainer.persistence.ExceptionHandler;
019 import org.picocontainer.Startable;
020
021 /**
022 * Commons-DBCP DataSource component implementation. It has failover support.
023 *
024 * @author Juze Peleteiro <juze -a-t- intelli -dot- biz>
025 */
026 public class DBCPDataSource extends AbstractDataSource implements Startable {
027
028 private BasicDataSource dataSource;
029
030 private final Properties properties;
031
032 /**
033 * @param driver The driver classname.
034 * @param connectionURL The connection url.
035 * @param username The connection username.
036 * @param password The connection password.
037 */
038 public DBCPDataSource(final String driver, final String connectionURL, final String username, final String password) {
039 properties = new Properties();
040 properties.put("driverClassName", driver);
041 properties.put("url", connectionURL);
042 properties.put("username", username);
043 properties.put("password", password);
044 }
045
046 /**
047 * @param driver The driver classname.
048 * @param connectionURL The connection url.
049 * @param username The connection username.
050 * @param password The connection password.
051 * @param jdbcExceptionHandler The ExceptionHandler component instance.
052 */
053 public DBCPDataSource(final String driver, final String connectionURL, final String username, final String password, final ExceptionHandler jdbcExceptionHandler) {
054 super(jdbcExceptionHandler);
055 properties = new Properties();
056 properties.put("driverClassName", driver);
057 properties.put("url", connectionURL);
058 properties.put("username", username);
059 properties.put("password", password);
060 }
061
062 /**
063 * @param properties DBCP properties. See at @{link http://jakarta.apache.org/commons/dbcp/configuration.html}
064 */
065 public DBCPDataSource(final Properties properties) {
066 this.properties = properties;
067 }
068
069 /**
070 * @param properties DBCP properties. See at @{link http://jakarta.apache.org/commons/dbcp/configuration.html}
071 * @param jdbcExceptionHandler The ExceptionHandler component instance.
072 */
073 public DBCPDataSource(final Properties properties, final ExceptionHandler jdbcExceptionHandler) {
074 super(jdbcExceptionHandler);
075 this.properties = properties;
076 }
077
078 /**
079 * @see org.nanocontainer.persistence.jdbc.AbstractDataSource#getDelegatedDataSource()
080 */
081 protected DataSource getDelegatedDataSource() throws Exception {
082 if (dataSource == null) {
083 dataSource = (BasicDataSource) BasicDataSourceFactory.createDataSource(properties);
084 }
085
086 return dataSource;
087 }
088
089 /**
090 * @see org.nanocontainer.persistence.jdbc.AbstractDataSource#invalidateDelegatedDataSource()
091 */
092 protected void invalidateDelegatedDataSource() throws Exception {
093 dataSource.close();
094 dataSource = null;
095 }
096
097 /**
098 * @see org.picocontainer.Startable#start()
099 */
100 public void start() {
101 // Do nothing
102 }
103
104 /**
105 * @see org.picocontainer.Startable#stop()
106 */
107 public void stop() {
108 try {
109 dataSource.close();
110 } catch (Exception e) {
111 // Do nothing?
112 }
113 dataSource = null;
114 }
115
116 }