Skip to content

Commit

Permalink
Remove Greg MariadDB code
Browse files Browse the repository at this point in the history
* it's now handled directly in MySqlJdbcUrlCreator
  • Loading branch information
anthonydahanne committed Aug 12, 2024
1 parent 5b9bb69 commit dd0fd67
Showing 1 changed file with 18 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,6 @@ public class CfDataSourceEnvironmentPostProcessor implements CfServiceEnablingEn

private static int invocationCount;

/**
* MySQL connection protocol constant.
*/
private static final String MYSQL_PROTOCOL = "mysql";

/**
* MariaDB connection protocol constant.
*/
private static final String MARIADB_PROTOCOL = "mariadb";

// After ConfigFileApplicationListener so values from files can be used here
private int order = ConfigDataEnvironmentPostProcessor.ORDER + 1;

Expand All @@ -85,7 +75,7 @@ public void postProcessEnvironment(ConfigurableEnvironment environment,

List<CfJdbcService> jdbcServices = cfJdbcEnv.findJdbcServices().stream()
.filter(service -> this.isEnabled(service, environment))
.collect(Collectors.toList());
.toList();

if (jdbcServices.size() > 1) {
if (invocationCount == 1) {
Expand All @@ -108,41 +98,35 @@ public void postProcessEnvironment(ConfigurableEnvironment environment,
if (driverClassName != null) {
properties.put("spring.datasource.driver-class-name", driverClassName);
}

/* R2DBC processing
* Split query param options and URL into two string
* and move options to spring.r2dbc.properties.<option>
*/

String[] splitJDBCUrl = cfJdbcService.getJdbcUrl().split("\\?");

String r2dbcUrl = splitJDBCUrl[0].replaceFirst("jdbc:", "r2dbc:");

// Case for MySQL where Maria DB protocol may be required
if (r2dbcUrl.contains(MYSQL_PROTOCOL))
r2dbcUrl = r2dbcUrl.replaceFirst(MYSQL_PROTOCOL, evalMySQLProtocol());


properties.put("spring.r2dbc.url", r2dbcUrl);
properties.put("spring.r2dbc.username", cfJdbcService.getUsername());
properties.put("spring.r2dbc.password", cfJdbcService.getPassword());

if (splitJDBCUrl.length == 2)
{
Map<String, String> queryOptions = parseQueryString(splitJDBCUrl[1]);

properties.put("spring.r2dbc.password", cfJdbcService.getPassword());

if (splitJDBCUrl.length == 2) {
Map<String, String> queryOptions = parseQueryString(splitJDBCUrl[1]);

if (queryOptions.size() > 0) {
queryOptions.forEach((key, value) -> {

switch (key)
{

switch (key) {
case "enabledTLSProtocols":
properties.put("spring.r2dbc.properties.tlsVersion", value);
break;
break;
default:
properties.put(String.format("spring.r2dbc.properties.%s", key) , value);
properties.put(String.format("spring.r2dbc.properties.%s", key), value);
}
});
};
}
}

MutablePropertySources propertySources = environment.getPropertySources();
Expand All @@ -169,27 +153,6 @@ public void postProcessEnvironment(ConfigurableEnvironment environment,
}
}

private String evalMySQLProtocol()
{
// Default to "mysql"
String connectionProtocol = MYSQL_PROTOCOL;

/* In Spring Boot 2.7.0, the previous MySQL r2dbc driver is no longer supported and
* documentation suggests using the MariaDB R2DBC driver as an alternative. Some versions
* of the MariaDB R2DBC driver do not support "mysql" as part of the connection
* protocol; "mariadb" should be used instead when the MariaDB R2DBC driver class is on the classpath.
*/

try {
Class.forName("org.mariadb.r2dbc.MariadbConnection");
connectionProtocol = MARIADB_PROTOCOL;
}
catch (ClassNotFoundException ignored) {
}

return connectionProtocol;
}

private Map<String, String> parseQueryString(String queryParams) {

if (queryParams == null || queryParams.equals(""))
Expand All @@ -200,12 +163,12 @@ private Map<String, String> parseQueryString(String queryParams) {
String[] options = queryParams.split("&");
for (String option : options) {

String[] keyval = option.split("=");
if (keyval.length != 2 || keyval[0].length() == 0 || keyval[1].length() == 0) {
String[] keyValue = option.split("=");
if (keyValue.length != 2 || keyValue[0].length() == 0 || keyValue[1].length() == 0) {
continue;
}

retVal.put(keyval[0], keyval[1]);
retVal.put(keyValue[0], keyValue[1]);
}

return retVal;
Expand Down

0 comments on commit dd0fd67

Please sign in to comment.