You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@james.apache.org by rc...@apache.org on 2023/11/15 06:46:44 UTC

(james-project) 03/04: JAMES-2586 SimpleJamesPostgresConnectionFactory – set empty attribute value when without domain

This is an automated email from the ASF dual-hosted git repository.

rcordier pushed a commit to branch postgresql
in repository https://gitbox.apache.org/repos/asf/james-project.git

commit a92be0136616ddcb42b12714c99784fe78761aa7
Author: Tung Tran <vt...@linagora.com>
AuthorDate: Mon Nov 13 17:16:02 2023 +0700

    JAMES-2586 SimpleJamesPostgresConnectionFactory – set empty attribute value when without domain
---
 .../utils/SimpleJamesPostgresConnectionFactory.java     | 17 +++++++++++------
 .../postgres/JamesPostgresConnectionFactoryTest.java    |  4 ++--
 2 files changed, 13 insertions(+), 8 deletions(-)

diff --git a/backends-common/postgres/src/main/java/org/apache/james/backends/postgres/utils/SimpleJamesPostgresConnectionFactory.java b/backends-common/postgres/src/main/java/org/apache/james/backends/postgres/utils/SimpleJamesPostgresConnectionFactory.java
index 385f32012c..7bd1cf0122 100644
--- a/backends-common/postgres/src/main/java/org/apache/james/backends/postgres/utils/SimpleJamesPostgresConnectionFactory.java
+++ b/backends-common/postgres/src/main/java/org/apache/james/backends/postgres/utils/SimpleJamesPostgresConnectionFactory.java
@@ -36,6 +36,7 @@ import reactor.core.publisher.Mono;
 public class SimpleJamesPostgresConnectionFactory implements JamesPostgresConnectionFactory {
     private static final Logger LOGGER = LoggerFactory.getLogger(SimpleJamesPostgresConnectionFactory.class);
     private static final Domain DEFAULT = Domain.of("default");
+    private static final String DEFAULT_DOMAIN_ATTRIBUTE_VALUE = "";
 
     private final ConnectionFactory connectionFactory;
     private final Map<Domain, Connection> mapDomainToConnection = new ConcurrentHashMap<>();
@@ -63,7 +64,7 @@ public class SimpleJamesPostgresConnectionFactory implements JamesPostgresConnec
     }
 
     private Mono<Connection> getAndSetConnection(Domain domain, Connection newConnection) {
-        return Mono.justOrEmpty(mapDomainToConnection.putIfAbsent(domain, newConnection))
+        return Mono.fromCallable(() -> mapDomainToConnection.putIfAbsent(domain, newConnection))
             .map(postgresqlConnection -> {
                 //close redundant connection
                 Mono.from(newConnection.close())
@@ -74,13 +75,17 @@ public class SimpleJamesPostgresConnectionFactory implements JamesPostgresConnec
     }
 
     private static Mono<Connection> setDomainAttributeForConnection(Domain domain, Connection newConnection) {
+        return Mono.from(newConnection.createStatement("SET " + DOMAIN_ATTRIBUTE + " TO '" + getDomainAttributeValue(domain) + "'") // It should be set value via Bind, but it doesn't work
+                .execute())
+            .doOnError(e -> LOGGER.error("Error while setting domain attribute for domain {}", domain, e))
+            .then(Mono.just(newConnection));
+    }
+
+    private static String getDomainAttributeValue(Domain domain) {
         if (DEFAULT.equals(domain)) {
-            return Mono.just(newConnection);
+            return DEFAULT_DOMAIN_ATTRIBUTE_VALUE;
         } else {
-            return Mono.from(newConnection.createStatement("SET " + DOMAIN_ATTRIBUTE + " TO '" + domain.asString() + "'") // It should be set value via Bind, but it doesn't work
-                .execute())
-                .doOnError(e -> LOGGER.error("Error while setting domain attribute for domain {}", domain, e))
-                .then(Mono.just(newConnection));
+            return domain.asString();
         }
     }
 }
diff --git a/backends-common/postgres/src/test/java/org/apache/james/backends/postgres/JamesPostgresConnectionFactoryTest.java b/backends-common/postgres/src/test/java/org/apache/james/backends/postgres/JamesPostgresConnectionFactoryTest.java
index ab68dd611a..98fb54de43 100644
--- a/backends-common/postgres/src/test/java/org/apache/james/backends/postgres/JamesPostgresConnectionFactoryTest.java
+++ b/backends-common/postgres/src/test/java/org/apache/james/backends/postgres/JamesPostgresConnectionFactoryTest.java
@@ -71,7 +71,7 @@ public abstract class JamesPostgresConnectionFactoryTest {
     }
 
     @Test
-    void getConnectionWithoutDomainShouldNotSetCurrentDomainAttribute() {
+    void getConnectionWithoutDomainShouldReturnEmptyAttribute() {
         Connection connection = jamesPostgresConnectionFactory().getConnection(Optional.empty()).block();
 
         String message = Flux.from(connection.createStatement("show " + JamesPostgresConnectionFactory.DOMAIN_ATTRIBUTE)
@@ -82,7 +82,7 @@ public abstract class JamesPostgresConnectionFactoryTest {
             .onErrorResume(throwable -> Mono.just(throwable.getMessage()))
             .block();
 
-        assertThat(message).isEqualTo("unrecognized configuration parameter \"" + JamesPostgresConnectionFactory.DOMAIN_ATTRIBUTE + "\"");
+        assertThat(message).isEqualTo("");
     }
 
     String getDomainAttributeValue(Connection connection) {


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org