public Connection getConnection()

in wrapper/src/main/java/software/amazon/jdbc/ds/AwsWrapperDataSource.java [99:245]


  public Connection getConnection(final String username, final String password) throws SQLException {
    this.user = username;
    this.password = password;

    final Properties props = PropertyUtils.copyProperties(this.targetDataSourceProperties);

    final String profileName = PropertyDefinition.PROFILE_NAME.getString(props);
    ConfigurationProfile configurationProfile = null;
    if (!StringUtils.isNullOrEmpty(profileName)) {
      configurationProfile = DriverConfigurationProfiles.getProfileConfiguration(profileName);
      if (configurationProfile != null) {
        PropertyUtils.addProperties(props, configurationProfile.getProperties());
        if (configurationProfile.getAwsCredentialsProviderHandler() != null) {
          AwsCredentialsManager.setCustomHandler(configurationProfile.getAwsCredentialsProviderHandler());
        }
      } else {
        throw new SQLException(
            Messages.get(
                "AwsWrapperDataSource.configurationProfileNotFound",
                new Object[] {profileName}));
      }
    }

    String finalUrl;

    final TelemetryFactory telemetryFactory = new DefaultTelemetryFactory(props);
    final TelemetryContext context = telemetryFactory.openTelemetryContext(
        "software.amazon.jdbc.ds.AwsWrapperDataSource.getConnection",
        TelemetryTraceLevel.TOP_LEVEL);

    try {
      // Identify the URL for connection.
      if (!StringUtils.isNullOrEmpty(this.jdbcUrl)) {
        finalUrl = this.jdbcUrl.replaceFirst(PROTOCOL_PREFIX, "jdbc:");

        parsePropertiesFromUrl(this.jdbcUrl, props);
        setDatabasePropertyFromUrl(props);

        // Override credentials with the ones provided through the data source property.
        setCredentialProperties(props);

        // Override database with the one provided through the data source property.
        if (!StringUtils.isNullOrEmpty(this.database)) {
          PropertyDefinition.DATABASE.set(props, this.database);
        }

      } else {
        final String serverName = !StringUtils.isNullOrEmpty(this.serverName)
            ? this.serverName
            : props.getProperty(SERVER_NAME);
        final String serverPort = !StringUtils.isNullOrEmpty(this.serverPort)
            ? this.serverPort
            : props.getProperty(SERVER_PORT);
        final String databaseName = !StringUtils.isNullOrEmpty(this.database)
            ? this.database
            : PropertyDefinition.DATABASE.getString(props);

        if (StringUtils.isNullOrEmpty(serverName)) {
          throw new SQLException(Messages.get("AwsWrapperDataSource.missingTarget"));
        }
        if (StringUtils.isNullOrEmpty(this.jdbcProtocol)) {
          throw new SQLException(Messages.get("AwsWrapperDataSource.missingJdbcProtocol"));
        }

        int port = HostSpec.NO_PORT;
        if (!StringUtils.isNullOrEmpty(serverPort)) {
          port = Integer.parseInt(serverPort);
        }

        finalUrl = buildUrl(this.jdbcProtocol, serverName, port, databaseName);

        // Override credentials with the ones provided through the data source property.
        setCredentialProperties(props);

        // Override database with the one provided through the data source property.
        if (!StringUtils.isNullOrEmpty(databaseName)) {
          PropertyDefinition.DATABASE.set(props, databaseName);
        }
      }

      TargetDriverDialect targetDriverDialect = configurationProfile == null
          ? null
          : configurationProfile.getTargetDriverDialect();

      ConnectionProvider effectiveConnectionProvider = null;
      if (configurationProfile != null) {
        effectiveConnectionProvider = configurationProfile.getConnectionProvider();
      }

      // Identify what connection provider to use.
      if (!StringUtils.isNullOrEmpty(this.targetDataSourceClassName)) {

        final DataSource targetDataSource = createTargetDataSource();

        try {
          targetDataSource.setLoginTimeout(loginTimeout);
        } catch (Exception ex) {
          LOGGER.finest(
              () ->
                  Messages.get(
                      "DataSource.failedToSetProperty",
                      new Object[] {"loginTimeout", targetDataSource.getClass(), ex.getCause().getMessage()}));
        }

        if (targetDriverDialect == null) {
          final TargetDriverDialectManager targetDriverDialectManager = new TargetDriverDialectManager();
          targetDriverDialect = targetDriverDialectManager.getDialect(this.targetDataSourceClassName, props);
        }

        ConnectionProvider defaultConnectionProvider = new DataSourceConnectionProvider(targetDataSource);

        return createConnectionWrapper(
            props,
            finalUrl,
            defaultConnectionProvider,
            effectiveConnectionProvider,
            targetDriverDialect,
            configurationProfile,
            telemetryFactory);
      } else {
        TargetDriverHelper helper = new TargetDriverHelper();
        final java.sql.Driver targetDriver = helper.getTargetDriver(finalUrl, props);

        if (targetDriverDialect == null) {
          final TargetDriverDialectManager targetDriverDialectManager = new TargetDriverDialectManager();
          targetDriverDialect = targetDriverDialectManager.getDialect(targetDriver, props);
        }

        ConnectionProvider defaultConnectionProvider = new DriverConnectionProvider(targetDriver);

        return createConnectionWrapper(
            props,
            finalUrl,
            defaultConnectionProvider,
            effectiveConnectionProvider,
            targetDriverDialect,
            configurationProfile,
            telemetryFactory);
      }
    } catch (Exception ex) {
      context.setException(ex);
      context.setSuccess(false);
      throw ex;
    } finally {
      context.closeContext();
    }
  }