/*
 * Licensed 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 io.prestosql.plugin.oracle;

import io.prestosql.plugin.jdbc.ConnectionFactory;
import io.prestosql.plugin.jdbc.JdbcIdentity;
import io.prestosql.plugin.jdbc.credential.CredentialProvider;
import oracle.jdbc.pool.OracleDataSource;
import oracle.ucp.jdbc.PoolDataSource;
import oracle.ucp.jdbc.PoolDataSourceFactory;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Optional;
import java.util.Properties;

import static com.google.common.base.Preconditions.checkArgument;

public class OraclePoolConnectorFactory
        implements ConnectionFactory
{
    private final PoolDataSource dataSource;
    private final CredentialProvider credentialProvider;

    public OraclePoolConnectorFactory(
            String connectionUrl,
            Properties connectionProperties,
            CredentialProvider credentialProvider,
            int connectionPoolMinSize,
            int connectionPoolMaxSize)
            throws SQLException
    {
        this.credentialProvider = credentialProvider;
        this.dataSource = PoolDataSourceFactory.getPoolDataSource();

        //Setting connection properties of the data source
        this.dataSource.setConnectionFactoryClassName(OracleDataSource.class.getName());
        this.dataSource.setURL(connectionUrl);

        //Setting pool properties
        this.dataSource.setInitialPoolSize(connectionPoolMinSize);
        this.dataSource.setMinPoolSize(connectionPoolMinSize);
        this.dataSource.setMaxPoolSize(connectionPoolMaxSize);
        this.dataSource.setValidateConnectionOnBorrow(true);
        this.dataSource.setConnectionProperties(connectionProperties);
    }

    @Override
    public Connection openConnection(JdbcIdentity identity)
            throws SQLException
    {
        Optional<String> user = credentialProvider.getConnectionUser(Optional.of(identity));
        Optional<String> password = credentialProvider.getConnectionPassword(Optional.of(identity));

        checkArgument(user.isPresent(), "Credentials returned null user");
        checkArgument(password.isPresent(), "Credentials returned null password");

        return dataSource.getConnection(user.get(), password.get());
    }
}

@@ -999,75 +999,8 @@
 s;%0A%0A
-import static com.google.common.base.Preconditions.checkArgument;%0A%0A
 publ
@@ -1122,65 +1122,8 @@
 rce;
-%0A    private final CredentialProvider credentialProvider;
 %0A%0A  
@@ -1409,62 +1409,8 @@
   %7B%0A
-        this.credentialProvider = credentialProvider;%0A
     
@@ -2038,154 +2038,351 @@
     
-%7D%0A%0A
     
-@Override%0A    public Connection openConnection(JdbcIdentity identity)%0A            throws SQLException%0A    %7B%0A        Optional%3CString%3E user =
+credentialProvider.getConnectionUser(Optional.empty())%0A                .ifPresent(user -%3E %7B%0A                    try %7B%0A                        dataSource.setUser(user);%0A                    %7D%0A                    catch (SQLException e) %7B%0A                        throw new RuntimeException(e);%0A                    %7D%0A                %7D);%0A       
  cre
@@ -2402,36 +2402,40 @@
 er.getConnection
-User
+Password
 (Optional.of(ide
@@ -2432,31 +2432,29 @@
 nal.
-of(identi
+emp
 ty
+(
 ))
-;
 %0A
+    
         
 Opti
@@ -2453,260 +2453,403 @@
     
-Optional%3CString%3E password = credentialProvider.getConnectionPassword(Optional.of(identity));%0A%0A        checkArgument(user.isPresent(), %22Credentials returned null user%22
+    .ifPresent(password -%3E %7B%0A                    try %7B%0A                        dataSource.setPassword(password);%0A                    %7D%0A                    catch (SQLException e) %7B%0A                        throw new RuntimeException(e);%0A                    %7D%0A                %7D
 );%0A    
+%7D%0A%0A
     
-checkArgument(password.isPresent(), %22Credentials returned null password%22);%0A
+@Override%0A    public Connection openConnection(JdbcIdentity identity)%0A            throws SQLException%0A    %7B
 %0A   
@@ -2889,34 +2889,8 @@
 ion(
-user.get(), password.get()
 );%0A 

Set connection credentials in Oracle pooling DataSource