001: package org.geotools.data.jdbc.datasource;
002:
003: import java.io.IOException;
004: import java.sql.Connection;
005: import java.sql.SQLException;
006: import java.util.Map;
007:
008: import javax.sql.DataSource;
009:
010: import org.apache.commons.dbcp.BasicDataSource;
011: import org.geotools.data.DataSourceException;
012: import org.geotools.data.DataStoreFactorySpi.Param;
013:
014: /**
015: * A datasource factory using DBCP connection pool
016: *
017: * @author Andrea Aime - TOPP
018: *
019: */
020: public class DBCPDataSourceFactory extends AbstractDataSourceFactorySpi {
021:
022: public static final Param DSTYPE = new Param("dstype",
023: String.class, "Must be DBCP", false);
024:
025: public static final Param USERNAME = new Param("username",
026: String.class, "User name to login as", false);
027:
028: public static final Param PASSWORD = new Param("password",
029: String.class, "Password used to login", false);
030:
031: public static final Param JDBC_URL = new Param(
032: "jdbcUrl",
033: String.class,
034: "The JDBC url (check the JDCB driver docs to find out its format)",
035: true);
036:
037: public static final Param DRIVERCLASS = new Param(
038: "driverClassName",
039: String.class,
040: "The JDBC driver class name (check the JDCB driver docs to find out its name)",
041: true);
042:
043: public static final Param MAXACTIVE = new Param("maxActive",
044: Integer.class,
045: "The maximum number of active connections in the pool",
046: true);
047:
048: public static final Param MAXIDLE = new Param("maxIdle",
049: Integer.class,
050: "The maximum number of idle connections in the pool", true);
051:
052: private static final Param[] PARAMS = new Param[] { DSTYPE,
053: DRIVERCLASS, JDBC_URL, USERNAME, PASSWORD, MAXACTIVE,
054: MAXIDLE };
055:
056: public DataSource createDataSource(Map params) throws IOException {
057: return createNewDataSource(params);
058: }
059:
060: public boolean canProcess(Map params) {
061: return super .canProcess(params)
062: && "DBCP".equals(params.get("dstype"));
063: }
064:
065: public DataSource createNewDataSource(Map params)
066: throws IOException {
067: BasicDataSource dataSource = new BasicDataSource();
068: dataSource.setDriverClassName((String) DRIVERCLASS
069: .lookUp(params));
070: dataSource.setUrl((String) JDBC_URL.lookUp(params));
071: dataSource.setUsername((String) USERNAME.lookUp(params));
072: dataSource.setPassword((String) PASSWORD.lookUp(params));
073: dataSource.setAccessToUnderlyingConnectionAllowed(true);
074: dataSource.setMaxActive(((Integer) MAXACTIVE.lookUp(params))
075: .intValue());
076: dataSource.setMaxIdle(((Integer) MAXIDLE.lookUp(params))
077: .intValue());
078:
079: // check the data source is properly setup by trying to gather a connection out of it
080: Connection conn = null;
081: try {
082: conn = dataSource.getConnection();
083: } catch (SQLException e) {
084: throw new DataSourceException(
085: "Connection pool improperly set up: "
086: + e.getMessage(), e);
087: } finally {
088: // close the connection at once
089: if (conn != null)
090: try {
091: conn.close();
092: } catch (SQLException e) {
093: }
094: }
095:
096: return dataSource;
097: }
098:
099: public String getDescription() {
100: return "A BDCP connection pool.";
101: }
102:
103: public Param[] getParametersInfo() {
104: return PARAMS;
105: }
106:
107: public boolean isAvailable() {
108: try {
109: new BasicDataSource();
110: } catch (Exception e) {
111: return false;
112: }
113: return true;
114: }
115:
116: }
|