Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ff526ef

Browse files
committedOct 9, 2024·
Remove PostgresJdbcUrl.java
1 parent 5511f49 commit ff526ef

File tree

3 files changed

+28
-179
lines changed

3 files changed

+28
-179
lines changed
 

‎spring-boot-project/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/service/connection/postgres/PostgresJdbcDockerComposeConnectionDetailsFactory.java

+28-7
Original file line numberDiff line numberDiff line change
@@ -87,17 +87,38 @@ public String getJdbcUrl() {
8787
}
8888

8989
private static String getJdbcUrl(RunningService service, String database, Environment environment) {
90-
PostgresJdbcUrl jdbcUrl = new PostgresJdbcUrl(jdbcUrlBuilder.build(service, database));
91-
addApplicationNameIfNecessary(jdbcUrl, environment);
92-
return jdbcUrl.toString();
90+
String jdbcUrl = jdbcUrlBuilder.build(service, database);
91+
return addApplicationNameIfNecessary(jdbcUrl, environment);
9392
}
9493

95-
private static void addApplicationNameIfNecessary(PostgresJdbcUrl jdbcUrl, Environment environment) {
94+
private static String addApplicationNameIfNecessary(String jdbcUrl, Environment environment) {
95+
if (hasParameter(jdbcUrl, APPLICATION_NAME)) {
96+
return jdbcUrl;
97+
}
9698
String applicationName = environment.getProperty("spring.application.name");
97-
if (StringUtils.hasText(applicationName)) {
98-
jdbcUrl.addParameterIfDoesNotExist(APPLICATION_NAME,
99-
URLEncoder.encode(applicationName, StandardCharsets.UTF_8));
99+
if (!StringUtils.hasText(applicationName)) {
100+
return jdbcUrl;
101+
}
102+
return addParameter(jdbcUrl, APPLICATION_NAME, applicationName);
103+
}
104+
105+
private static String addParameter(String jdbcUrl, String name, String value) {
106+
StringBuilder jdbcUrlBuilder = new StringBuilder(jdbcUrl);
107+
if (!jdbcUrl.contains("?")) {
108+
jdbcUrlBuilder.append('?');
100109
}
110+
else if (!jdbcUrl.endsWith("&")) {
111+
jdbcUrlBuilder.append('&');
112+
}
113+
return jdbcUrlBuilder.append(name)
114+
.append('=')
115+
.append(URLEncoder.encode(value, StandardCharsets.UTF_8))
116+
.toString();
117+
}
118+
119+
private static boolean hasParameter(String jdbcUrl, String parameterName) {
120+
return jdbcUrl.contains("?%s=".formatted(parameterName))
121+
|| jdbcUrl.contains("&%s=".formatted(parameterName));
101122
}
102123

103124
}

‎spring-boot-project/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/service/connection/postgres/PostgresJdbcUrl.java

-103
This file was deleted.

‎spring-boot-project/spring-boot-docker-compose/src/test/java/org/springframework/boot/docker/compose/service/connection/postgres/PostgresJdbcUrlTests.java

-69
This file was deleted.

0 commit comments

Comments
 (0)
Please sign in to comment.