    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 *      https://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.DriverManager;
    021import java.sql.SQLException;
    022import java.util.Properties;
    023
    024/**
    025 * A {@link DriverManager}-based implementation of {@link ConnectionFactory}.
    026 *
    027 * @since 2.0
    028 */
    029public class DriverManagerConnectionFactory implements ConnectionFactory {
    030
    031    static {
    032        // Related to DBCP-272
    033        // Driver manager does not sync loading of drivers that use the service
    034        // provider interface. This will cause issues is multi-threaded
    035        // environments. This hack makes sure the drivers are loaded before
    036        // DBCP tries to use them.
    037        DriverManager.getDrivers(); // NOPMD
    038    }
    039
    040    private final String connectionUri;
    041
    042    private final String userName;
    043
    044    private final char[] userPassword;
    045
    046    private final Properties properties;
    047
    048    /**
    049     * Constructor for DriverManagerConnectionFactory.
    050     *
    051     * @param connectionUri
    052     *            a database connection string of the form {@code  jdbc:<em>subprotocol</em>:<em>subname</em>}
    053     * @since 2.2
    054     */
    055    public DriverManagerConnectionFactory(final String connectionUri) {
    056        this.connectionUri = connectionUri;
    057        this.properties = new Properties();
    058        this.userName = null;
    059        this.userPassword = null;
    060    }
    061
    062    /**
    063     * Constructor for DriverManagerConnectionFactory.
    064     *
    065     * @param connectionUri
    066     *            a database connection string of the form {@code  jdbc:<em>subprotocol</em>:<em>subname</em>}
    067     * @param properties
    068     *            a list of arbitrary string tag/value pairs as connection arguments; normally at least a "user" and
    069     *            "password" property should be included.
    070     */
    071    public DriverManagerConnectionFactory(final String connectionUri, final Properties properties) {
    072        this.connectionUri = connectionUri;
    073        this.properties = properties;
    074        this.userName = null;
    075        this.userPassword = null;
    076    }
    077
    078    /**
    079     * Constructor for DriverManagerConnectionFactory.
    080     *
    081     * @param connectionUri
    082     *            a database connection string of the form {@code jdbc:<em>subprotocol</em>:<em>subname</em>}
    083     * @param userName
    084     *            the database user
    085     * @param userPassword
    086     *            the user's password
    087     */
    088    public DriverManagerConnectionFactory(final String connectionUri, final String userName,
    089            final char[] userPassword) {
    090        this.connectionUri = connectionUri;
    091        this.userName = userName;
    092        this.userPassword = Utils.clone(userPassword);
    093        this.properties = null;
    094    }
    095
    096    /**
    097     * Constructor for DriverManagerConnectionFactory.
    098     *
    099     * @param connectionUri
    100     *            a database connection string of the form {@code jdbc:<em>subprotocol</em>:<em>subname</em>}
    101     * @param userName
    102     *            the database user
    103     * @param userPassword
    104     *            the user's password
    105     */
    106    public DriverManagerConnectionFactory(final String connectionUri, final String userName,
    107            final String userPassword) {
    108        this.connectionUri = connectionUri;
    109        this.userName = userName;
    110        this.userPassword =  Utils.toCharArray(userPassword);
    111        this.properties = null;
    112    }
    113
    114    @Override
    115    public Connection createConnection() throws SQLException {
    116        if (null == properties) {
    117            if (userName == null && userPassword == null) {
    118                return DriverManager.getConnection(connectionUri);
    119            }
    120            return DriverManager.getConnection(connectionUri, userName, Utils.toString(userPassword));
    121        }
    122        return DriverManager.getConnection(connectionUri, properties);
    123    }
    124
    125    /**
    126     * Gets the connection URI.
    127     *
    128     * @return The connection URI.
    129     * @since 2.6.0
    130     */
    131    public String getConnectionUri() {
    132        return connectionUri;
    133    }
    134
    135    /**
    136     * Gets the Properties, may be null.
    137     *
    138     * @return The Properties, may be null.
    139     * @since 2.6.0
    140     */
    141    public Properties getProperties() {
    142        return properties;
    143    }
    144
    145    /**
    146     * Gets the user name, may be null.
    147     *
    148     * @return The user name, may be null.
    149     * @since 2.6.0
    150     */
    151    public String getUserName() {
    152        return userName;
    153    }
    154}