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