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     * @return The connection String.
057     * @since 2.6.0
058     */
059    public String getConnectionString() {
060        return connectionString;
061    }
062
063    /**
064     * @return The Driver.
065     * @since 2.6.0
066     */
067    public Driver getDriver() {
068        return driver;
069    }
070
071    /**
072     * @return The Properties.
073     * @since 2.6.0
074     */
075    public Properties getProperties() {
076        return properties;
077    }
078
079    @Override
080    public String toString() {
081        return this.getClass().getName() + " [" + driver + ";" + connectionString + ";"
082            + Utils.cloneWithoutCredentials(properties) + "]";
083    }
084}