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    private final Driver driver;
033    private final Properties properties;
034
035    /**
036     * Constructs a connection factory for a given Driver.
037     *
038     * @param driver
039     *            The Driver.
040     * @param connectString
041     *            The connection string.
042     * @param properties
043     *            The connection properties.
044     */
045    public DriverConnectionFactory(final Driver driver, final String connectString, final Properties properties) {
046        this.driver = driver;
047        this.connectionString = connectString;
048        this.properties = properties;
049    }
050
051    @Override
052    public Connection createConnection() throws SQLException {
053        return driver.connect(connectionString, properties);
054    }
055
056    @Override
057    public String toString() {
058        return this.getClass().getName() + " [" + String.valueOf(driver) + ";" + String.valueOf(connectionString) + ";"
059                + String.valueOf(properties) + "]";
060    }
061}