io.r2dbc.spi.ConnectionFactoryOptions Java Examples

The following examples show how to use io.r2dbc.spi.ConnectionFactoryOptions. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CloudConfig.java    From cf-butler with Apache License 2.0 8 votes vote down vote up
@Bean
@ConditionalOnProperty(VCAP_SERVICE_VARIABLE)
ConnectionFactory connectionFactory() {
    R2dbcProperties properties = r2dbcProperties();
    ConnectionFactoryOptions.Builder builder = ConnectionFactoryOptions
            .parse(properties.getUrl()).mutate();
    String username = properties.getUsername();
    if (StringUtils.hasText(username)) {
        builder.option(ConnectionFactoryOptions.USER, username);
    }
    String password = properties.getPassword();
    if (StringUtils.hasText(password)) {
        builder.option(ConnectionFactoryOptions.PASSWORD, password);
    }
    String databaseName = properties.getName();
    if (StringUtils.hasText(databaseName)) {
        builder.option(ConnectionFactoryOptions.DATABASE, databaseName);
    }
    if (properties.getProperties() != null) {
        properties.getProperties()
                .forEach((key, value) -> builder
                        .option(Option.valueOf(key), value));
    }
    return ConnectionFactories.get(builder.build());
}
 
Example #2
Source File: MySqlConnectionFactoryProviderTest.java    From r2dbc-mysql with Apache License 2.0 7 votes vote down vote up
@Test
void invalidProgrammatic() {
    assertThat(assertThrows(IllegalArgumentException.class, () -> ConnectionFactories.get(ConnectionFactoryOptions.builder()
        .option(DRIVER, "mysql")
        .option(Option.valueOf("unixSocket"), "/path/to/mysql.sock")
        .option(USER, "root")
        .option(SSL, true)
        .build()))
        .getMessage())
        .contains("sslMode");

    for (SslMode mode : SslMode.values()) {
        if (mode.startSsl()) {
            assertThat(assertThrows(IllegalArgumentException.class, () -> ConnectionFactories.get(ConnectionFactoryOptions.builder()
                .option(DRIVER, "mysql")
                .option(Option.valueOf("unixSocket"), "/path/to/mysql.sock")
                .option(USER, "root")
                .option(Option.valueOf("sslMode"), mode.name().toLowerCase())
                .build()))
                .getMessage())
                .contains("sslMode");
        }
    }
}
 
Example #3
Source File: DatasourceConfig.java    From tutorials with MIT License 6 votes vote down vote up
@Bean
public ConnectionFactory connectionFactory(R2DBCConfigurationProperties properties) {
    
    ConnectionFactoryOptions baseOptions = ConnectionFactoryOptions.parse(properties.getUrl());
    Builder ob = ConnectionFactoryOptions.builder().from(baseOptions);
    if ( !StringUtil.isNullOrEmpty(properties.getUser())) {
        ob = ob.option(USER, properties.getUser());
    }

    if ( !StringUtil.isNullOrEmpty(properties.getPassword())) {
        ob = ob.option(PASSWORD, properties.getPassword());
    }
    
    ConnectionFactory cf = ConnectionFactories.get(ob.build());
    return cf;
}
 
Example #4
Source File: Hidden.java    From testcontainers-java with MIT License 6 votes vote down vote up
private ConnectionFactoryOptions removeProxying(ConnectionFactoryOptions options) {
    // To delegate to the next factory provider, inspect the PROTOCOL and convert it to the next DRIVER and PROTOCOL values.
    //
    // example:
    //   | Property | Input           | Output       |
    //   |----------|-----------------|--------------|
    //   | DRIVER   | tc              | postgres     |
    //   | PROTOCOL | postgres        | <empty>      |

    String protocol = options.getRequiredValue(ConnectionFactoryOptions.PROTOCOL);
    if (protocol.trim().length() == 0) {
        throw new IllegalArgumentException("Invalid protocol: " + protocol);
    }
    String[] protocols = protocol.split(":", 2);
    String driverDelegate = protocols[0];

    // when protocol does NOT contain COLON, the length becomes 1
    String protocolDelegate = protocols.length == 2 ? protocols[1] : "";

    return ConnectionFactoryOptions.builder()
        .from(options)
        .option(ConnectionFactoryOptions.DRIVER, driverDelegate)
        .option(ConnectionFactoryOptions.PROTOCOL, protocolDelegate)
        .build();
}
 
Example #5
Source File: MySqlConnectionFactoryProviderTest.java    From r2dbc-mysql with Apache License 2.0 6 votes vote down vote up
@Test
void invalidServerPreparing() {
    assertThrows(IllegalArgumentException.class, () -> ConnectionFactories.get(ConnectionFactoryOptions.builder()
        .option(DRIVER, "mysql")
        .option(HOST, "127.0.0.1")
        .option(USER, "root")
        .option(USE_SERVER_PREPARE_STATEMENT, NotPredicate.class.getTypeName())
        .build()));

    assertThrows(IllegalArgumentException.class, () -> ConnectionFactories.get(ConnectionFactoryOptions.builder()
        .option(DRIVER, "mysql")
        .option(HOST, "127.0.0.1")
        .option(USER, "root")
        .option(USE_SERVER_PREPARE_STATEMENT, NotPredicate.class.getPackage() + "NonePredicate")
        .build()));
}
 
Example #6
Source File: SpannerConnectionConfiguration.java    From cloud-spanner-r2dbc with Apache License 2.0 6 votes vote down vote up
/**
 * Constructor which initializes the configuration from an Cloud Spanner R2DBC url.
 */
private SpannerConnectionConfiguration(String url, GoogleCredentials credentials) {
  String databaseString =
      ConnectionFactoryOptions.parse(url).getValue(ConnectionFactoryOptions.DATABASE);

  if (!databaseString.matches(DB_NAME_VALIDATE_PATTERN)) {
    throw new IllegalArgumentException(
        String.format(
            "Malformed Cloud Spanner Database String: %s. The url must have the format: %s",
            databaseString,
            FULLY_QUALIFIED_DB_NAME_PATTERN));
  }

  this.fullyQualifiedDbName = databaseString;
  this.credentials = credentials;
}
 
Example #7
Source File: MySqlConnectionFactoryProviderTest.java    From r2dbc-mysql with Apache License 2.0 6 votes vote down vote up
@Test
void invalidProgrammatic() {
    assertThat(assertThrows(IllegalArgumentException.class, () -> ConnectionFactories.get(ConnectionFactoryOptions.builder()
        .option(DRIVER, "mysql")
        .option(Option.valueOf("unixSocket"), "/path/to/mysql.sock")
        .option(USER, "root")
        .option(SSL, true)
        .build()))
        .getMessage())
        .contains("sslMode");

    for (SslMode mode : SslMode.values()) {
        if (mode.startSsl()) {
            assertThat(assertThrows(IllegalArgumentException.class, () -> ConnectionFactories.get(ConnectionFactoryOptions.builder()
                .option(DRIVER, "mysql")
                .option(Option.valueOf("unixSocket"), "/path/to/mysql.sock")
                .option(USER, "root")
                .option(Option.valueOf("sslMode"), mode.name().toLowerCase())
                .build()))
                .getMessage())
                .contains("sslMode");
        }
    }
}
 
Example #8
Source File: MySqlConnectionFactoryProviderTest.java    From r2dbc-mysql with Apache License 2.0 6 votes vote down vote up
@Test
void invalidServerPreparing() {
    assertThrows(IllegalArgumentException.class, () -> ConnectionFactories.get(ConnectionFactoryOptions.builder()
        .option(DRIVER, "mysql")
        .option(HOST, "127.0.0.1")
        .option(USER, "root")
        .option(USE_SERVER_PREPARE_STATEMENT, NotPredicate.class.getTypeName())
        .build()));

    assertThrows(IllegalArgumentException.class, () -> ConnectionFactories.get(ConnectionFactoryOptions.builder()
        .option(DRIVER, "mysql")
        .option(HOST, "127.0.0.1")
        .option(USER, "root")
        .option(USE_SERVER_PREPARE_STATEMENT, NotPredicate.class.getPackage() + "NonePredicate")
        .build()));
}
 
Example #9
Source File: PostgreSQLR2DBCDatabaseContainer.java    From testcontainers-java with MIT License 5 votes vote down vote up
public static ConnectionFactoryOptions getOptions(PostgreSQLContainer<?> container) {
    ConnectionFactoryOptions options = ConnectionFactoryOptions.builder()
        .option(ConnectionFactoryOptions.DRIVER, PostgreSQLR2DBCDatabaseContainerProvider.DRIVER)
        .build();

    return new PostgreSQLR2DBCDatabaseContainer(container).configure(options);
}
 
Example #10
Source File: TestcontainersR2DBCConnectionFactory.java    From testcontainers-java with MIT License 5 votes vote down vote up
TestcontainersR2DBCConnectionFactory(ConnectionFactoryOptions options) {
    this.options = options;

    containerProvider = StreamSupport.stream(ServiceLoader.load(R2DBCDatabaseContainerProvider.class).spliterator(), false)
        .filter(it -> it.supports(options))
        .findAny()
        .orElseThrow(() -> new IllegalArgumentException("Missing provider for " + options));
}
 
Example #11
Source File: R2DBCDatabaseContainerProvider.java    From testcontainers-java with MIT License 5 votes vote down vote up
@Nullable
default ConnectionFactoryMetadata getMetadata(ConnectionFactoryOptions options) {
    ConnectionFactoryOptions.Builder builder = options.mutate();
    if (!options.hasOption(ConnectionFactoryOptions.HOST)) {
        builder.option(ConnectionFactoryOptions.HOST, "localhost");
    }
    if (!options.hasOption(ConnectionFactoryOptions.PORT)) {
        builder.option(ConnectionFactoryOptions.PORT, 65535);
    }

    ConnectionFactory connectionFactory = ConnectionFactories.find(builder.build());
    return connectionFactory != null ? connectionFactory.getMetadata() : null;
}
 
Example #12
Source File: Hidden.java    From testcontainers-java with MIT License 5 votes vote down vote up
@Override
public ConnectionFactory create(ConnectionFactoryOptions options) {
    options = sanitize(options);
    options = removeProxying(options);

    return new TestcontainersR2DBCConnectionFactory(options);
}
 
Example #13
Source File: Hidden.java    From testcontainers-java with MIT License 5 votes vote down vote up
private ConnectionFactoryOptions sanitize(ConnectionFactoryOptions options) {
    ConnectionFactoryOptions.Builder builder = options.mutate();

    Object reusable = options.getValue(R2DBCDatabaseContainerProvider.REUSABLE_OPTION);
    if (reusable instanceof String) {
        builder.option(R2DBCDatabaseContainerProvider.REUSABLE_OPTION, Boolean.valueOf((String) reusable));
    }
    return builder.build();
}
 
Example #14
Source File: R2dbcConfig.java    From FrameworkBenchmarks with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Bean
public ConnectionFactory connectionFactory() {
    return ConnectionFactories.get(ConnectionFactoryOptions.builder()
            .option(DRIVER,"pool")
            .option(PROTOCOL,"postgresql")
            .option(HOST, host)
            .option(PORT, port)
            .option(USER, username)
            .option(PASSWORD, password)
            .option(DATABASE, name)
            .build());
}
 
Example #15
Source File: PostgreSQLR2DBCDatabaseContainerProvider.java    From testcontainers-java with MIT License 5 votes vote down vote up
@Override
public R2DBCDatabaseContainer createContainer(ConnectionFactoryOptions options) {
    String image = PostgreSQLContainer.IMAGE + ":" + options.getRequiredValue(IMAGE_TAG_OPTION);
    PostgreSQLContainer<?> container = new PostgreSQLContainer<>(image)
        .withDatabaseName(options.getRequiredValue(ConnectionFactoryOptions.DATABASE));

    if (Boolean.TRUE.equals(options.getValue(REUSABLE_OPTION))) {
        container.withReuse(true);
    }
    return new PostgreSQLR2DBCDatabaseContainer(container);
}
 
Example #16
Source File: MariaDBR2DBCDatabaseContainerProvider.java    From testcontainers-java with MIT License 5 votes vote down vote up
@Nullable
@Override
public ConnectionFactoryMetadata getMetadata(ConnectionFactoryOptions options) {
    ConnectionFactoryOptions.Builder builder = options.mutate();
    if (!options.hasOption(ConnectionFactoryOptions.USER)) {
        builder.option(ConnectionFactoryOptions.USER, MariaDBContainer.DEFAULT_USER);
    }
    if (!options.hasOption(ConnectionFactoryOptions.PASSWORD)) {
        builder.option(ConnectionFactoryOptions.PASSWORD, MariaDBContainer.DEFAULT_PASSWORD);
    }
    return R2DBCDatabaseContainerProvider.super.getMetadata(builder.build());
}
 
Example #17
Source File: PostgreSQLR2DBCDatabaseContainerProvider.java    From testcontainers-java with MIT License 5 votes vote down vote up
@Nullable
@Override
public ConnectionFactoryMetadata getMetadata(ConnectionFactoryOptions options) {
    ConnectionFactoryOptions.Builder builder = options.mutate();
    if (!options.hasOption(ConnectionFactoryOptions.USER)) {
        builder.option(ConnectionFactoryOptions.USER, PostgreSQLContainer.DEFAULT_USER);
    }
    if (!options.hasOption(ConnectionFactoryOptions.PASSWORD)) {
        builder.option(ConnectionFactoryOptions.PASSWORD, PostgreSQLContainer.DEFAULT_PASSWORD);
    }
    return R2DBCDatabaseContainerProvider.super.getMetadata(builder.build());
}
 
Example #18
Source File: PostgreSQLR2DBCDatabaseContainer.java    From testcontainers-java with MIT License 5 votes vote down vote up
@Override
public ConnectionFactoryOptions configure(ConnectionFactoryOptions options) {
    return options.mutate()
        .option(ConnectionFactoryOptions.HOST, container.getHost())
        .option(ConnectionFactoryOptions.PORT, container.getMappedPort(PostgreSQLContainer.POSTGRESQL_PORT))
        .option(ConnectionFactoryOptions.DATABASE, container.getDatabaseName())
        .option(ConnectionFactoryOptions.USER, container.getUsername())
        .option(ConnectionFactoryOptions.PASSWORD, container.getPassword())
        .build();
}
 
Example #19
Source File: PostgreSQLR2DBCDatabaseContainerTest.java    From testcontainers-java with MIT License 5 votes vote down vote up
@Override
protected ConnectionFactoryOptions getOptions(PostgreSQLContainer<?> container) {
    // get_options {
    ConnectionFactoryOptions options = PostgreSQLR2DBCDatabaseContainer.getOptions(
        container
    );
    // }

    return options;
}
 
Example #20
Source File: MySQLR2DBCDatabaseContainer.java    From testcontainers-java with MIT License 5 votes vote down vote up
public static ConnectionFactoryOptions getOptions(MySQLContainer<?> container) {
    ConnectionFactoryOptions options = ConnectionFactoryOptions.builder()
        .option(ConnectionFactoryOptions.DRIVER, MySQLR2DBCDatabaseContainerProvider.DRIVER)
        .build();

    return new MySQLR2DBCDatabaseContainer(container).configure(options);
}
 
Example #21
Source File: MySQLR2DBCDatabaseContainer.java    From testcontainers-java with MIT License 5 votes vote down vote up
@Override
public ConnectionFactoryOptions configure(ConnectionFactoryOptions options) {
    return options.mutate()
        .option(ConnectionFactoryOptions.HOST, container.getHost())
        .option(ConnectionFactoryOptions.PORT, container.getMappedPort(MySQLContainer.MYSQL_PORT))
        .option(ConnectionFactoryOptions.DATABASE, container.getDatabaseName())
        .option(ConnectionFactoryOptions.USER, container.getUsername())
        .option(ConnectionFactoryOptions.PASSWORD, container.getPassword())
        .build();
}
 
Example #22
Source File: MySQLR2DBCDatabaseContainerProvider.java    From testcontainers-java with MIT License 5 votes vote down vote up
@Override
public R2DBCDatabaseContainer createContainer(ConnectionFactoryOptions options) {
    String image = MySQLContainer.IMAGE + ":" + options.getRequiredValue(IMAGE_TAG_OPTION);
    MySQLContainer<?> container = new MySQLContainer<>(image)
        .withDatabaseName(options.getRequiredValue(ConnectionFactoryOptions.DATABASE));

    if (Boolean.TRUE.equals(options.getValue(REUSABLE_OPTION))) {
        container.withReuse(true);
    }
    return new MySQLR2DBCDatabaseContainer(container);
}
 
Example #23
Source File: MySQLR2DBCDatabaseContainerProvider.java    From testcontainers-java with MIT License 5 votes vote down vote up
@Nullable
@Override
public ConnectionFactoryMetadata getMetadata(ConnectionFactoryOptions options) {
    ConnectionFactoryOptions.Builder builder = options.mutate();
    if (!options.hasOption(ConnectionFactoryOptions.USER)) {
        builder.option(ConnectionFactoryOptions.USER, MySQLContainer.DEFAULT_USER);
    }
    if (!options.hasOption(ConnectionFactoryOptions.PASSWORD)) {
        builder.option(ConnectionFactoryOptions.PASSWORD, MySQLContainer.DEFAULT_PASSWORD);
    }
    return R2DBCDatabaseContainerProvider.super.getMetadata(builder.build());
}
 
Example #24
Source File: MariaDBR2DBCDatabaseContainer.java    From testcontainers-java with MIT License 5 votes vote down vote up
public static ConnectionFactoryOptions getOptions(MariaDBContainer<?> container) {
    ConnectionFactoryOptions options = ConnectionFactoryOptions.builder()
        .option(ConnectionFactoryOptions.DRIVER, MariaDBR2DBCDatabaseContainerProvider.DRIVER)
        .build();

    return new MariaDBR2DBCDatabaseContainer(container).configure(options);
}
 
Example #25
Source File: MariaDBR2DBCDatabaseContainer.java    From testcontainers-java with MIT License 5 votes vote down vote up
@Override
public ConnectionFactoryOptions configure(ConnectionFactoryOptions options) {
    return options.mutate()
        .option(ConnectionFactoryOptions.HOST, container.getHost())
        .option(ConnectionFactoryOptions.PORT, container.getMappedPort(MariaDBContainer.MARIADB_PORT))
        .option(ConnectionFactoryOptions.DATABASE, container.getDatabaseName())
        .option(ConnectionFactoryOptions.USER, container.getUsername())
        .option(ConnectionFactoryOptions.PASSWORD, container.getPassword())
        .build();
}
 
Example #26
Source File: MariaDBR2DBCDatabaseContainerProvider.java    From testcontainers-java with MIT License 5 votes vote down vote up
@Override
public R2DBCDatabaseContainer createContainer(ConnectionFactoryOptions options) {
    String image = MariaDBContainer.IMAGE + ":" + options.getRequiredValue(IMAGE_TAG_OPTION);
    MariaDBContainer<?> container = new MariaDBContainer<>(image)
        .withDatabaseName(options.getRequiredValue(ConnectionFactoryOptions.DATABASE));

    if (Boolean.TRUE.equals(options.getValue(REUSABLE_OPTION))) {
        container.withReuse(true);
    }
    return new MariaDBR2DBCDatabaseContainer(container);
}
 
Example #27
Source File: MSSQLR2DBCDatabaseContainer.java    From testcontainers-java with MIT License 5 votes vote down vote up
@Override
public ConnectionFactoryOptions configure(ConnectionFactoryOptions options) {
    return options.mutate()
        .option(ConnectionFactoryOptions.HOST, container.getHost())
        .option(ConnectionFactoryOptions.PORT, container.getMappedPort(MSSQLServerContainer.MS_SQL_SERVER_PORT))
        // TODO enable if/when MSSQLServerContainer adds support for customizing the DB name
        // .option(ConnectionFactoryOptions.DATABASE, container.getDatabasseName())
        .option(ConnectionFactoryOptions.USER, container.getUsername())
        .option(ConnectionFactoryOptions.PASSWORD, container.getPassword())
        .build();
}
 
Example #28
Source File: BookExampleApp.java    From cloud-spanner-r2dbc with Apache License 2.0 5 votes vote down vote up
/**
 * Constructor.
 *
 * @param sampleInstance the sample instance to use.
 * @param sampleDatabase the sample database to use.
 * @param sampleProjectId the sample project to use.
 */
public BookExampleApp(String sampleInstance, String sampleDatabase,
    String sampleProjectId) {
  this.connectionFactory = ConnectionFactories.get(ConnectionFactoryOptions.builder()
      .option(Option.valueOf("project"), sampleProjectId)
      .option(DRIVER, DRIVER_NAME)
      .option(INSTANCE, sampleInstance)
      .option(DATABASE, sampleDatabase)
      .build());
}
 
Example #29
Source File: MSSQLR2DBCDatabaseContainer.java    From testcontainers-java with MIT License 5 votes vote down vote up
public static ConnectionFactoryOptions getOptions(MSSQLServerContainer<?> container) {
    ConnectionFactoryOptions options = ConnectionFactoryOptions.builder()
        .option(ConnectionFactoryOptions.DRIVER, MSSQLR2DBCDatabaseContainerProvider.DRIVER)
        .build();

    return new MSSQLR2DBCDatabaseContainer(container).configure(options);
}
 
Example #30
Source File: MSSQLR2DBCDatabaseContainerProvider.java    From testcontainers-java with MIT License 5 votes vote down vote up
@Nullable
@Override
public ConnectionFactoryMetadata getMetadata(ConnectionFactoryOptions options) {
    ConnectionFactoryOptions.Builder builder = options.mutate();
    if (!options.hasOption(ConnectionFactoryOptions.USER)) {
        builder.option(ConnectionFactoryOptions.USER, MSSQLServerContainer.DEFAULT_USER);
    }
    if (!options.hasOption(ConnectionFactoryOptions.PASSWORD)) {
        builder.option(ConnectionFactoryOptions.PASSWORD, MSSQLServerContainer.DEFAULT_PASSWORD);
    }
    return R2DBCDatabaseContainerProvider.super.getMetadata(builder.build());
}