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
041     *            The Driver.
042     * @param connectString
043     *            The connection string.
044     * @param properties
045     *            The connection properties.
046     */
047    public DriverConnectionFactory(final Driver driver, final String connectString, final Properties properties) {
048        this.driver = driver;
049        this.connectionString = connectString;
050        this.properties = properties;
051    }
052
053    @Override
054    public Connection createConnection() throws SQLException {
055        return driver.connect(connectionString, properties);
056    }
057
058    /**
059     * @return The connection String.
060     * @since 2.6.0
061     */
062    public String getConnectionString() {
063        return connectionString;
064    }
065
066    /**
067     * @return The Driver.
068     * @since 2.6.0
069     */
070    public Driver getDriver() {
071        return driver;
072    }
073
074    /**
075     * @return The Properties.
076     * @since 2.6.0
077     */
078    public Properties getProperties() {
079        return properties;
080    }
081
082    @Override
083    public String toString() {
084        return this.getClass().getName() + " [" + String.valueOf(driver) + ";" + String.valueOf(connectionString) + ";"
085                + String.valueOf(properties) + "]";
086    }
087}