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/09 04:45:33 UTC

(james-project) 05/09: JAMES-2586 Guice binding for PostgresTableManager

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 ca9e57cf7418c7f32b333114d1caa544716d583f
Author: Quan Tran <hq...@linagora.com>
AuthorDate: Tue Nov 7 14:33:30 2023 +0700

    JAMES-2586 Guice binding for PostgresTableManager
    
    tested manually the binding with the subscription module -> create subscription table upon James startup successfully.
---
 backends-common/postgres/pom.xml                   |  4 +++
 .../james/backends/postgres/PostgresModule.java    |  5 ++--
 .../backends/postgres/PostgresTableManager.java    | 17 +++++++++++-
 .../james/modules/data/PostgresCommonModule.java   | 31 ++++++++++++++++++++++
 4 files changed, 54 insertions(+), 3 deletions(-)

diff --git a/backends-common/postgres/pom.xml b/backends-common/postgres/pom.xml
index 019e2b5c84..499f3b42a7 100644
--- a/backends-common/postgres/pom.xml
+++ b/backends-common/postgres/pom.xml
@@ -45,6 +45,10 @@
             <type>test-jar</type>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>${james.groupId}</groupId>
+            <artifactId>james-server-lifecycle-api</artifactId>
+        </dependency>
         <dependency>
             <groupId>${james.groupId}</groupId>
             <artifactId>james-server-util</artifactId>
diff --git a/backends-common/postgres/src/main/java/org/apache/james/backends/postgres/PostgresModule.java b/backends-common/postgres/src/main/java/org/apache/james/backends/postgres/PostgresModule.java
index 6df91b894e..8f1725fe4b 100644
--- a/backends-common/postgres/src/main/java/org/apache/james/backends/postgres/PostgresModule.java
+++ b/backends-common/postgres/src/main/java/org/apache/james/backends/postgres/PostgresModule.java
@@ -20,6 +20,7 @@
 package org.apache.james.backends.postgres;
 
 
+import java.util.Collection;
 import java.util.List;
 
 import com.google.common.collect.ImmutableList;
@@ -32,7 +33,7 @@ public interface PostgresModule {
             .build();
     }
 
-    static PostgresModule aggregateModules(List<PostgresModule> modules) {
+    static PostgresModule aggregateModules(Collection<PostgresModule> modules) {
         return builder()
             .modules(modules)
             .build();
@@ -93,7 +94,7 @@ public interface PostgresModule {
             return this;
         }
 
-        public Builder modules(List<PostgresModule> modules) {
+        public Builder modules(Collection<PostgresModule> modules) {
             modules.forEach(module -> {
                 addTable(module.tables());
                 addIndex(module.tableIndexes());
diff --git a/backends-common/postgres/src/main/java/org/apache/james/backends/postgres/PostgresTableManager.java b/backends-common/postgres/src/main/java/org/apache/james/backends/postgres/PostgresTableManager.java
index c563b5918b..b140a83790 100644
--- a/backends-common/postgres/src/main/java/org/apache/james/backends/postgres/PostgresTableManager.java
+++ b/backends-common/postgres/src/main/java/org/apache/james/backends/postgres/PostgresTableManager.java
@@ -19,20 +19,35 @@
 
 package org.apache.james.backends.postgres;
 
+import java.util.Optional;
+
+import javax.inject.Inject;
+
+import org.apache.james.backends.postgres.utils.JamesPostgresConnectionFactory;
 import org.apache.james.backends.postgres.utils.PostgresExecutor;
+import org.apache.james.lifecycle.api.Startable;
 import org.jooq.exception.DataAccessException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import com.google.common.annotations.VisibleForTesting;
+
 import io.r2dbc.spi.Result;
 import reactor.core.publisher.Flux;
 import reactor.core.publisher.Mono;
 
-public class PostgresTableManager {
+public class PostgresTableManager implements Startable {
     private static final Logger LOGGER = LoggerFactory.getLogger(PostgresTableManager.class);
     private final PostgresExecutor postgresExecutor;
     private final PostgresModule module;
 
+    @Inject
+    public PostgresTableManager(JamesPostgresConnectionFactory postgresConnectionFactory, PostgresModule module) {
+        this.postgresExecutor = new PostgresExecutor(postgresConnectionFactory.getConnection(Optional.empty()));
+        this.module = module;
+    }
+
+    @VisibleForTesting
     public PostgresTableManager(PostgresExecutor postgresExecutor, PostgresModule module) {
         this.postgresExecutor = postgresExecutor;
         this.module = module;
diff --git a/server/container/guice/postgres-common/src/main/java/org/apache/james/modules/data/PostgresCommonModule.java b/server/container/guice/postgres-common/src/main/java/org/apache/james/modules/data/PostgresCommonModule.java
index b8b1fbcdc1..88119bd8d3 100644
--- a/server/container/guice/postgres-common/src/main/java/org/apache/james/modules/data/PostgresCommonModule.java
+++ b/server/container/guice/postgres-common/src/main/java/org/apache/james/modules/data/PostgresCommonModule.java
@@ -19,18 +19,25 @@
 package org.apache.james.modules.data;
 
 import java.io.FileNotFoundException;
+import java.util.Set;
 
 import javax.inject.Singleton;
 
 import org.apache.commons.configuration2.ex.ConfigurationException;
 import org.apache.james.backends.postgres.PostgresConfiguration;
+import org.apache.james.backends.postgres.PostgresModule;
+import org.apache.james.backends.postgres.PostgresTableManager;
 import org.apache.james.backends.postgres.utils.JamesPostgresConnectionFactory;
 import org.apache.james.backends.postgres.utils.SimpleJamesPostgresConnectionFactory;
+import org.apache.james.utils.InitializationOperation;
+import org.apache.james.utils.InitilizationOperationBuilder;
 import org.apache.james.utils.PropertiesProvider;
 
 import com.google.inject.AbstractModule;
 import com.google.inject.Provides;
 import com.google.inject.Scopes;
+import com.google.inject.multibindings.Multibinder;
+import com.google.inject.multibindings.ProvidesIntoSet;
 
 import io.r2dbc.postgresql.PostgresqlConnectionConfiguration;
 import io.r2dbc.postgresql.PostgresqlConnectionFactory;
@@ -41,6 +48,8 @@ public class PostgresCommonModule extends AbstractModule {
     public void configure() {
         bind(JamesPostgresConnectionFactory.class).to(SimpleJamesPostgresConnectionFactory.class);
 
+        Multibinder.newSetBinder(binder(), PostgresModule.class);
+
         bind(SimpleJamesPostgresConnectionFactory.class).in(Scopes.SINGLETON);
     }
 
@@ -62,4 +71,26 @@ public class PostgresCommonModule extends AbstractModule {
             .schema(postgresConfiguration.getDatabaseSchema())
             .build());
     }
+
+    @Provides
+    @Singleton
+    PostgresModule composePostgresDataDefinitions(Set<PostgresModule> modules) {
+        return PostgresModule.aggregateModules(modules);
+    }
+
+    @Provides
+    @Singleton
+    PostgresTableManager postgresTableManager(JamesPostgresConnectionFactory jamesPostgresConnectionFactory,
+                                              PostgresModule postgresModule) {
+        return new PostgresTableManager(jamesPostgresConnectionFactory, postgresModule);
+    }
+
+    @ProvidesIntoSet
+    InitializationOperation provisionPostgresTablesAndIndexes(PostgresTableManager postgresTableManager) {
+        return InitilizationOperationBuilder
+            .forClass(PostgresTableManager.class)
+            .init(() -> postgresTableManager.initializeTables()
+                .then(postgresTableManager.initializeTableIndexes())
+                .block());
+    }
 }


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