001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.dbcp2;
018
019import java.sql.Connection;
020import java.sql.Driver;
021import java.sql.SQLException;
022import java.util.Properties;
023
024/**
025 * A {@link Driver}-based implementation of {@link ConnectionFactory}.
026 *
027 * @since 2.0
028 */
029public class DriverConnectionFactory implements ConnectionFactory {
030
031    private final String connectionString;
032
033    private final Driver driver;
034
035    private final Properties properties;
036
037    /**
038     * Constructs a connection factory for a given Driver.
039     *
040     * @param driver The Driver.
041     * @param connectString The connection string.
042     * @param properties The connection properties.
043     */
044    public DriverConnectionFactory(final Driver driver, final String connectString, final Properties properties) {
045        this.driver = driver;
046        this.connectionString = connectString;
047        this.properties = properties;
048    }
049
050    @Override
051    public Connection createConnection() throws SQLException {
052        return driver.connect(connectionString, properties);
053    }
054
055    /**
056     * Gets the connection String.
057     *
058     * @return The connection String.
059     * @since 2.6.0
060     */
061    public String getConnectionString() {
062        return connectionString;
063    }
064
065    /**
066     * Gets the JDBC Driver.
067     *
068     * @return The JDBC Driver.
069     * @since 2.6.0
070     */
071    public Driver getDriver() {
072        return driver;
073    }
074
075    /**
076     * Gets the properties.
077     *
078     * @return The properties.
079     * @since 2.6.0
080     */
081    public Properties getProperties() {
082        return properties;
083    }
084
085    @Override
086    public String toString() {
087        return this.getClass().getName() + " [" + driver + ";" + connectionString + ";"
088            + Utils.cloneWithoutCredentials(properties) + "]";
089    }
090}