/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.apache.guacamole.auth.jdbc;

import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.google.inject.name.Named;
import java.sql.Connection;
import java.sql.SQLException;
import org.apache.guacamole.GuacamoleException;
import org.apache.ibatis.datasource.pooled.PooledDataSource;
import org.apache.ibatis.datasource.unpooled.UnpooledDataSource;

/**
 * Pooled DataSource implementation which dynamically retrieves the database
 * username and password from the Guacamole server environment each time a
 * new database connection is created.
 */
@Singleton
public class DynamicallyAuthenticatedDataSource extends PooledDataSource {

    /**
     * Creates a new DynamicallyAuthenticatedDataSource which dynamically
     * retrieves database credentials from the given JDBCEnvironment each time
     * a new database connection is needed.
     *
     * @param environment
     *     The JDBCEnvironment that should be used to retrieve database
     *     credentials.
     *
     * @param driverClassLoader
     * @param driver
     * @param url
     */
    @Inject
    public DynamicallyAuthenticatedDataSource(JDBCEnvironment environment,
            @Named(value="JDBC.driverClassLoader") ClassLoader driverClassLoader,
            @Named(value="JDBC.driver") String driver,
            @Named(value="JDBC.url") String url) {

        // Wrap unpooled DataSource, overriding the connection process such
        // that credentials are dynamically retrieved from the JDBCEnvironment
        super(new UnpooledDataSource(driverClassLoader, driver, url, null, null) {

            @Override
            public Connection getConnection() throws SQLException {
                try {
                    return super.getConnection(environment.getUsername(), environment.getPassword());
                }
                catch (GuacamoleException e) {
                    throw new SQLException("Retrieval of database credentials failed.", e);
                }
            }

        });

    }

}

@@ -999,32 +999,61 @@
 l.SQLException;%0A
+import java.util.Properties;%0A
 import org.apach
@@ -2856,10 +2856,2166 @@
 %0A    %7D%0A%0A
+    @Override%0A    @Inject(optional=true)%0A    public void setPoolPingConnectionsNotUsedFor(%0A            @Named(%22mybatis.pooled.pingConnectionsNotUsedFor%22) int milliseconds) %7B%0A        super.setPoolPingConnectionsNotUsedFor(milliseconds);%0A    %7D%0A%0A    @Override%0A    @Inject(optional=true)%0A    public void setPoolPingEnabled(@Named(%22mybatis.pooled.pingEnabled%22) boolean poolPingEnabled) %7B%0A        super.setPoolPingEnabled(poolPingEnabled);%0A    %7D%0A%0A    @Override%0A    @Inject(optional=true)%0A    public void setPoolPingQuery(@Named(%22mybatis.pooled.pingQuery%22) String poolPingQuery) %7B%0A        super.setPoolPingQuery(poolPingQuery);%0A    %7D%0A%0A    @Override%0A    @Inject(optional=true)%0A    public void setPoolTimeToWait(@Named(%22mybatis.pooled.timeToWait%22) int poolTimeToWait) %7B%0A        super.setPoolTimeToWait(poolTimeToWait);%0A    %7D%0A%0A    @Override%0A    @Inject(optional=true)%0A    public void setPoolMaximumCheckoutTime(%0A            @Named(%22mybatis.pooled.maximumCheckoutTime%22) int poolMaximumCheckoutTime) %7B%0A        super.setPoolMaximumCheckoutTime(poolMaximumCheckoutTime);%0A    %7D%0A%0A    @Override%0A    @Inject(optional=true)%0A    public void setPoolMaximumIdleConnections(%0A            @Named(%22mybatis.pooled.maximumIdleConnections%22) int poolMaximumIdleConnections) %7B%0A        super.setPoolMaximumIdleConnections(poolMaximumIdleConnections);%0A    %7D%0A%0A    @Override%0A    @Inject(optional=true)%0A    public void setPoolMaximumActiveConnections(%0A            @Named(%22mybatis.pooled.maximumActiveConnections%22) int poolMaximumActiveConnections) %7B%0A        super.setPoolMaximumActiveConnections(poolMaximumActiveConnections);%0A    %7D%0A%0A    @Override%0A    @Inject(optional=true)%0A    public void setDriverProperties(@Named(%22JDBC.driverProperties%22) Properties driverProps) %7B%0A        super.setDriverProperties(driverProps);%0A    %7D%0A%0A    @Override%0A    @Inject(optional=true)%0A    public void setDefaultAutoCommit(@Named(%22JDBC.autoCommit%22) boolean defaultAutoCommit) %7B%0A        super.setDefaultAutoCommit(defaultAutoCommit);%0A    %7D%0A%0A    @Override%0A    @Inject(optional=true)%0A    public void setLoginTimeout(@Named(%22JDBC.loginTimeout%22) int loginTimeout) %7B%0A        super.setLoginTimeout(loginTimeout);%0A    %7D%0A%0A
 %7D%0A

Add support for all MyBatis JDBC properties to DynamicallyAuthenticatedDataSource.