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.SQLException;
021
022import javax.sql.DataSource;
023
024/**
025 * A {@link DataSource}-based implementation of {@link ConnectionFactory}.
026 *
027 * @since 2.0
028 */
029public class DataSourceConnectionFactory implements ConnectionFactory {
030
031    private final DataSource dataSource;
032
033    private final String userName;
034
035    private final char[] userPassword;
036
037    /**
038     * Constructs an instance for the given DataSource.
039     *
040     * @param dataSource
041     *            The DataSource for this factory.
042     */
043    public DataSourceConnectionFactory(final DataSource dataSource) {
044        this(dataSource, null, (char[]) null);
045    }
046
047    /**
048     * Constructs an instance for the given DataSource.
049     *
050     * @param dataSource
051     *            The DataSource for this factory.
052     * @param userName
053     *            The user name.
054     * @param userPassword
055     *            The user password.
056     * @since 2.4.0
057     */
058    public DataSourceConnectionFactory(final DataSource dataSource, final String userName, final char[] userPassword) {
059        this.dataSource = dataSource;
060        this.userName = userName;
061        this.userPassword = Utils.clone(userPassword);
062    }
063
064    /**
065     * Constructs an instance for the given DataSource.
066     *
067     * @param dataSource
068     *            The DataSource for this factory.
069     * @param userName
070     *            The user name.
071     * @param password
072     *            The user password.
073     */
074    public DataSourceConnectionFactory(final DataSource dataSource, final String userName, final String password) {
075        this.dataSource = dataSource;
076        this.userName = userName;
077        this.userPassword = Utils.toCharArray(password);
078    }
079
080    @Override
081    public Connection createConnection() throws SQLException {
082        if (null == userName && null == userPassword) {
083            return dataSource.getConnection();
084        }
085        return dataSource.getConnection(userName, Utils.toString(userPassword));
086    }
087
088    /**
089     * @return The data source.
090     * @since 2.6.0
091     */
092    public DataSource getDataSource() {
093        return dataSource;
094    }
095
096    /**
097     * @return The user name.
098     * @since 2.6.0
099     */
100    public String getUserName() {
101        return userName;
102    }
103
104    /**
105     * @return The user password.
106     * @since 2.6.0
107     */
108    public char[] getUserPassword() {
109        return Utils.clone(userPassword);
110    }
111}