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 61632984229e19bb3d21d7038542286ef435a045
Author: Rene Cordier <[email protected]>
AuthorDate: Tue Nov 21 15:30:52 2023 +0700

    JAMES-2586 Guice bindings and package renaming for domain postgres 
implementation
---
 .../src/main/resources/META-INF/persistence.xml    |   1 -
 .../james/modules/data/PostgresDataModule.java     |   2 +-
 ...stModule.java => PostgresDomainListModule.java} |  19 ++-
 server/data/data-postgres/pom.xml                  |   2 -
 .../apache/james/domainlist/jpa/JPADomainList.java | 178 ---------------------
 .../james/domainlist/jpa/PostgresDomainList.java   |  61 -------
 .../james/domainlist/jpa/PostgresDomainModule.java |  27 ----
 .../james/domainlist/jpa/model/JPADomain.java      |  69 --------
 .../domainlist/postgres/PostgresDomainList.java    |  79 +++++++++
 .../domainlist/postgres/PostgresDomainModule.java} |  32 ++--
 .../domainlist/jpa/PostgresDomainListTest.java     |  29 ----
 .../PostgresDomainListTest.java}                   |  56 +++----
 .../src/test/resources/persistence.xml             |   1 -
 13 files changed, 133 insertions(+), 423 deletions(-)

diff --git 
a/server/apps/postgres-app/src/main/resources/META-INF/persistence.xml 
b/server/apps/postgres-app/src/main/resources/META-INF/persistence.xml
index 9573f6e5f6..165c6456cd 100644
--- a/server/apps/postgres-app/src/main/resources/META-INF/persistence.xml
+++ b/server/apps/postgres-app/src/main/resources/META-INF/persistence.xml
@@ -30,7 +30,6 @@
         
<class>org.apache.james.mailbox.postgres.mail.model.openjpa.JPAMailboxMessage</class>
         <class>org.apache.james.mailbox.postgres.mail.model.JPAProperty</class>
 
-        <class>org.apache.james.domainlist.jpa.model.JPADomain</class>
         <class>org.apache.james.mailrepository.jpa.model.JPAUrl</class>
         <class>org.apache.james.mailrepository.jpa.model.JPAMail</class>
         <class>org.apache.james.rrt.jpa.model.JPARecipientRewrite</class>
diff --git 
a/server/container/guice/postgres-common/src/main/java/org/apache/james/modules/data/PostgresDataModule.java
 
b/server/container/guice/postgres-common/src/main/java/org/apache/james/modules/data/PostgresDataModule.java
index 125746063b..39cec08889 100644
--- 
a/server/container/guice/postgres-common/src/main/java/org/apache/james/modules/data/PostgresDataModule.java
+++ 
b/server/container/guice/postgres-common/src/main/java/org/apache/james/modules/data/PostgresDataModule.java
@@ -27,7 +27,7 @@ public class PostgresDataModule extends AbstractModule {
     @Override
     protected void configure() {
         install(new CoreDataModule());
-        install(new JPADomainListModule());
+        install(new PostgresDomainListModule());
         install(new JPARecipientRewriteTableModule());
         install(new JPAMailRepositoryModule());
     }
diff --git 
a/server/container/guice/postgres-common/src/main/java/org/apache/james/modules/data/JPADomainListModule.java
 
b/server/container/guice/postgres-common/src/main/java/org/apache/james/modules/data/PostgresDomainListModule.java
similarity index 71%
rename from 
server/container/guice/postgres-common/src/main/java/org/apache/james/modules/data/JPADomainListModule.java
rename to 
server/container/guice/postgres-common/src/main/java/org/apache/james/modules/data/PostgresDomainListModule.java
index 116fd4b8ac..728c1ad051 100644
--- 
a/server/container/guice/postgres-common/src/main/java/org/apache/james/modules/data/JPADomainListModule.java
+++ 
b/server/container/guice/postgres-common/src/main/java/org/apache/james/modules/data/PostgresDomainListModule.java
@@ -16,29 +16,34 @@
  * specific language governing permissions and limitations      *
  * under the License.                                           *
  ****************************************************************/
+
 package org.apache.james.modules.data;
 
+import org.apache.james.backends.postgres.PostgresModule;
 import org.apache.james.domainlist.api.DomainList;
-import org.apache.james.domainlist.jpa.JPADomainList;
 import org.apache.james.domainlist.lib.DomainListConfiguration;
+import org.apache.james.domainlist.postgres.PostgresDomainList;
+import org.apache.james.domainlist.postgres.PostgresDomainModule;
 import org.apache.james.utils.InitializationOperation;
 import org.apache.james.utils.InitilizationOperationBuilder;
 
 import com.google.inject.AbstractModule;
 import com.google.inject.Scopes;
+import com.google.inject.multibindings.Multibinder;
 import com.google.inject.multibindings.ProvidesIntoSet;
 
-public class JPADomainListModule extends AbstractModule {
+public class PostgresDomainListModule extends AbstractModule {
     @Override
     public void configure() {
-        bind(JPADomainList.class).in(Scopes.SINGLETON);
-        bind(DomainList.class).to(JPADomainList.class);
+        bind(PostgresDomainList.class).in(Scopes.SINGLETON);
+        bind(DomainList.class).to(PostgresDomainList.class);
+        Multibinder.newSetBinder(binder(), 
PostgresModule.class).addBinding().toInstance(PostgresDomainModule.MODULE);
     }
 
     @ProvidesIntoSet
-    InitializationOperation configureDomainList(DomainListConfiguration 
configuration, JPADomainList jpaDomainList) {
+    InitializationOperation configureDomainList(DomainListConfiguration 
configuration, PostgresDomainList postgresDomainList) {
         return InitilizationOperationBuilder
-            .forClass(JPADomainList.class)
-            .init(() -> jpaDomainList.configure(configuration));
+            .forClass(PostgresDomainList.class)
+            .init(() -> postgresDomainList.configure(configuration));
     }
 }
diff --git a/server/data/data-postgres/pom.xml 
b/server/data/data-postgres/pom.xml
index 223cf0a802..f5a2a5226e 100644
--- a/server/data/data-postgres/pom.xml
+++ b/server/data/data-postgres/pom.xml
@@ -157,7 +157,6 @@
                 <configuration>
                     
<includes>org/apache/james/sieve/postgres/model/JPASieveScript.class,
                         
org/apache/james/rrt/jpa/model/JPARecipientRewrite.class,
-                        org/apache/james/domainlist/jpa/model/JPADomain.class,
                         org/apache/james/mailrepository/jpa/model/JPAUrl.class,
                         
org/apache/james/mailrepository/jpa/model/JPAMail.class</includes>
                     <addDefaultConstructor>true</addDefaultConstructor>
@@ -171,7 +170,6 @@
                             <name>metaDataFactory</name>
                             
<value>jpa(Types=org.apache.james.sieve.postgres.model.JPASieveScript;
                                 
org.apache.james.rrt.jpa.model.JPARecipientRewrite;
-                                
org.apache.james.domainlist.jpa.model.JPADomain;
                                 
org.apache.james.mailrepository.jpa.model.JPAUrl;
                                 
org.apache.james.mailrepository.jpa.model.JPAMail)</value>
                         </property>
diff --git 
a/server/data/data-postgres/src/main/java/org/apache/james/domainlist/jpa/JPADomainList.java
 
b/server/data/data-postgres/src/main/java/org/apache/james/domainlist/jpa/JPADomainList.java
deleted file mode 100644
index 1432b211b8..0000000000
--- 
a/server/data/data-postgres/src/main/java/org/apache/james/domainlist/jpa/JPADomainList.java
+++ /dev/null
@@ -1,178 +0,0 @@
-/****************************************************************
- * Licensed to the Apache Software Foundation (ASF) under one   *
- * or more contributor license agreements.  See the NOTICE file *
- * distributed with this work for additional information        *
- * regarding copyright ownership.  The ASF licenses this file   *
- * to you under the Apache License, Version 2.0 (the            *
- * "License"); you may not use this file except in compliance   *
- * with the License.  You may obtain a copy of the License at   *
- *                                                              *
- *   http://www.apache.org/licenses/LICENSE-2.0                 *
- *                                                              *
- * Unless required by applicable law or agreed to in writing,   *
- * software distributed under the License is distributed on an  *
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
- * KIND, either express or implied.  See the License for the    *
- * specific language governing permissions and limitations      *
- * under the License.                                           *
- ****************************************************************/
-package org.apache.james.domainlist.jpa;
-
-import java.util.List;
-
-import javax.annotation.PostConstruct;
-import javax.inject.Inject;
-import javax.persistence.EntityManager;
-import javax.persistence.EntityManagerFactory;
-import javax.persistence.EntityTransaction;
-import javax.persistence.NoResultException;
-import javax.persistence.PersistenceException;
-import javax.persistence.PersistenceUnit;
-
-import org.apache.james.backends.jpa.EntityManagerUtils;
-import org.apache.james.core.Domain;
-import org.apache.james.dnsservice.api.DNSService;
-import org.apache.james.domainlist.api.DomainListException;
-import org.apache.james.domainlist.jpa.model.JPADomain;
-import org.apache.james.domainlist.lib.AbstractDomainList;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import com.google.common.collect.ImmutableList;
-
-/**
- * JPA implementation of the DomainList.<br>
- * This implementation is compatible with the JDBCDomainList, meaning same
- * database schema can be reused.
- */
-public class JPADomainList extends AbstractDomainList {
-    private static final Logger LOGGER = 
LoggerFactory.getLogger(JPADomainList.class);
-
-    /**
-     * The entity manager to access the database.
-     */
-    private EntityManagerFactory entityManagerFactory;
-
-    @Inject
-    public JPADomainList(DNSService dns, EntityManagerFactory 
entityManagerFactory) {
-        super(dns);
-        this.entityManagerFactory = entityManagerFactory;
-    }
-
-    /**
-     * Set the entity manager to use.
-     */
-    @Inject
-    @PersistenceUnit(unitName = "James")
-    public void setEntityManagerFactory(EntityManagerFactory 
entityManagerFactory) {
-        this.entityManagerFactory = entityManagerFactory;
-    }
-
-    @PostConstruct
-    public void init() {
-        EntityManagerUtils.safelyClose(createEntityManager());
-    }
-
-    @SuppressWarnings("unchecked")
-    @Override
-    protected List<Domain> getDomainListInternal() throws DomainListException {
-        EntityManager entityManager = 
entityManagerFactory.createEntityManager();
-        try {
-            List<String> resultList = entityManager
-                    .createNamedQuery("listDomainNames")
-                    .getResultList();
-            return resultList
-                    .stream()
-                    .map(Domain::of)
-                    .collect(ImmutableList.toImmutableList());
-        } catch (PersistenceException e) {
-            LOGGER.error("Failed to list domains", e);
-            throw new DomainListException("Unable to retrieve domains", e);
-        } finally {
-            EntityManagerUtils.safelyClose(entityManager);
-        }
-    }
-
-    @Override
-    protected boolean containsDomainInternal(Domain domain) throws 
DomainListException {
-        EntityManager entityManager = 
entityManagerFactory.createEntityManager();
-        try {
-            return containsDomainInternal(domain, entityManager);
-        } catch (PersistenceException e) {
-            LOGGER.error("Failed to find domain", e);
-            throw new DomainListException("Unable to retrieve domains", e);
-        } finally {
-            EntityManagerUtils.safelyClose(entityManager);
-        }
-    }
-
-    @Override
-    public void addDomain(Domain domain) throws DomainListException {
-        EntityManager entityManager = 
entityManagerFactory.createEntityManager();
-        final EntityTransaction transaction = entityManager.getTransaction();
-        try {
-            transaction.begin();
-            if (containsDomainInternal(domain, entityManager)) {
-                transaction.commit();
-                throw new DomainListException(domain.name() + " already 
exists.");
-            }
-            JPADomain jpaDomain = new JPADomain(domain);
-            entityManager.persist(jpaDomain);
-            transaction.commit();
-        } catch (PersistenceException e) {
-            LOGGER.error("Failed to save domain", e);
-            rollback(transaction);
-            throw new DomainListException("Unable to add domain " + 
domain.name(), e);
-        } finally {
-            EntityManagerUtils.safelyClose(entityManager);
-        }
-    }
-
-    @Override
-    public void doRemoveDomain(Domain domain) throws DomainListException {
-        EntityManager entityManager = 
entityManagerFactory.createEntityManager();
-        final EntityTransaction transaction = entityManager.getTransaction();
-        try {
-            transaction.begin();
-            if (!containsDomainInternal(domain, entityManager)) {
-                transaction.commit();
-                throw new DomainListException(domain.name() + " was not 
found.");
-            }
-            
entityManager.createNamedQuery("deleteDomainByName").setParameter("name", 
domain.asString()).executeUpdate();
-            transaction.commit();
-        } catch (PersistenceException e) {
-            LOGGER.error("Failed to remove domain", e);
-            rollback(transaction);
-            throw new DomainListException("Unable to remove domain " + 
domain.name(), e);
-        } finally {
-            EntityManagerUtils.safelyClose(entityManager);
-        }
-    }
-
-    private void rollback(EntityTransaction transaction) {
-        if (transaction.isActive()) {
-            transaction.rollback();
-        }
-    }
-
-    private boolean containsDomainInternal(Domain domain, EntityManager 
entityManager) {
-        try {
-            return entityManager.createNamedQuery("findDomainByName")
-                .setParameter("name", domain.asString())
-                .getSingleResult() != null;
-        } catch (NoResultException e) {
-            LOGGER.debug("No domain found", e);
-            return false;
-        }
-    }
-
-    /**
-     * Return a new {@link EntityManager} instance
-     *
-     * @return manager
-     */
-    private EntityManager createEntityManager() {
-        return entityManagerFactory.createEntityManager();
-    }
-
-}
diff --git 
a/server/data/data-postgres/src/main/java/org/apache/james/domainlist/jpa/PostgresDomainList.java
 
b/server/data/data-postgres/src/main/java/org/apache/james/domainlist/jpa/PostgresDomainList.java
deleted file mode 100644
index 2135c84ed5..0000000000
--- 
a/server/data/data-postgres/src/main/java/org/apache/james/domainlist/jpa/PostgresDomainList.java
+++ /dev/null
@@ -1,61 +0,0 @@
-package org.apache.james.domainlist.jpa;
-
-import static 
org.apache.james.domainlist.jpa.PostgresDomainModule.PostgresDomainTable.DOMAIN;
-import static 
org.apache.james.domainlist.jpa.PostgresDomainModule.PostgresDomainTable.TABLE_NAME;
-
-import java.util.List;
-
-import javax.inject.Inject;
-
-import org.apache.james.backends.postgres.utils.PostgresExecutor;
-import org.apache.james.core.Domain;
-import org.apache.james.dnsservice.api.DNSService;
-import org.apache.james.domainlist.api.DomainListException;
-import org.apache.james.domainlist.lib.AbstractDomainList;
-import org.jooq.exception.DataAccessException;
-
-import reactor.core.publisher.Flux;
-import reactor.core.publisher.Mono;
-
-public class PostgresDomainList extends AbstractDomainList {
-    private final PostgresExecutor postgresExecutor;
-
-    @Inject
-    public PostgresDomainList(DNSService dnsService, PostgresExecutor 
postgresExecutor) {
-        super(dnsService);
-        this.postgresExecutor = postgresExecutor;
-    }
-
-    @Override
-    public void addDomain(Domain domain) throws DomainListException {
-        postgresExecutor.executeVoid(dslContext -> 
Mono.from(dslContext.insertInto(TABLE_NAME, DOMAIN)
-                .values(domain.asString())))
-            .onErrorMap(DataAccessException.class, e -> new 
DomainListException(domain.name() + " already exists."))
-            .block();
-
-    }
-
-    @Override
-    protected List<Domain> getDomainListInternal() throws DomainListException {
-        return postgresExecutor.executeRows(dsl -> 
Flux.from(dsl.selectFrom(TABLE_NAME)))
-            .map(record -> Domain.of(record.get(DOMAIN)))
-            .collectList()
-            .block();
-    }
-
-    @Override
-    protected boolean containsDomainInternal(Domain domain) throws 
DomainListException {
-        return postgresExecutor.executeRow(dsl -> 
Mono.from(dsl.selectFrom(TABLE_NAME)
-            .where(DOMAIN.eq(domain.asString()))))
-            .blockOptional()
-            .isPresent();
-    }
-
-    @Override
-    protected void doRemoveDomain(Domain domain) throws DomainListException {
-        postgresExecutor.executeVoid(dslContext -> 
Mono.from(dslContext.deleteFrom(TABLE_NAME)
-            .where(DOMAIN.eq(domain.asString()))))
-            .onErrorMap(DataAccessException.class, e -> new 
DomainListException(domain.name() + " was not found"))
-            .block();
-    }
-}
diff --git 
a/server/data/data-postgres/src/main/java/org/apache/james/domainlist/jpa/PostgresDomainModule.java
 
b/server/data/data-postgres/src/main/java/org/apache/james/domainlist/jpa/PostgresDomainModule.java
deleted file mode 100644
index 9a99e5e785..0000000000
--- 
a/server/data/data-postgres/src/main/java/org/apache/james/domainlist/jpa/PostgresDomainModule.java
+++ /dev/null
@@ -1,27 +0,0 @@
-package org.apache.james.domainlist.jpa;
-
-import org.apache.james.backends.postgres.PostgresModule;
-import org.apache.james.backends.postgres.PostgresTable;
-import org.jooq.Field;
-import org.jooq.Record;
-import org.jooq.Table;
-import org.jooq.impl.DSL;
-import org.jooq.impl.SQLDataType;
-
-public interface PostgresDomainModule {
-    interface PostgresDomainTable {
-        Table<Record> TABLE_NAME = DSL.table("domains");
-
-        Field<String> DOMAIN = DSL.field("domain", 
SQLDataType.VARCHAR.notNull());
-
-        PostgresTable TABLE = PostgresTable.name(TABLE_NAME.getName())
-            .createTableStep(((dsl, tableName) -> 
dsl.createTableIfNotExists(tableName)
-                .column(DOMAIN)
-                .constraint(DSL.primaryKey(DOMAIN))))
-            .disableRowLevelSecurity();
-    }
-
-    PostgresModule MODULE = PostgresModule.builder()
-        .addTable(PostgresDomainTable.TABLE)
-        .build();
-}
diff --git 
a/server/data/data-postgres/src/main/java/org/apache/james/domainlist/jpa/model/JPADomain.java
 
b/server/data/data-postgres/src/main/java/org/apache/james/domainlist/jpa/model/JPADomain.java
deleted file mode 100644
index 3b4367494c..0000000000
--- 
a/server/data/data-postgres/src/main/java/org/apache/james/domainlist/jpa/model/JPADomain.java
+++ /dev/null
@@ -1,69 +0,0 @@
-/****************************************************************
- * Licensed to the Apache Software Foundation (ASF) under one   *
- * or more contributor license agreements.  See the NOTICE file *
- * distributed with this work for additional information        *
- * regarding copyright ownership.  The ASF licenses this file   *
- * to you under the Apache License, Version 2.0 (the            *
- * "License"); you may not use this file except in compliance   *
- * with the License.  You may obtain a copy of the License at   *
- *                                                              *
- *   http://www.apache.org/licenses/LICENSE-2.0                 *
- *                                                              *
- * Unless required by applicable law or agreed to in writing,   *
- * software distributed under the License is distributed on an  *
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
- * KIND, either express or implied.  See the License for the    *
- * specific language governing permissions and limitations      *
- * under the License.                                           *
- ****************************************************************/
-package org.apache.james.domainlist.jpa.model;
-
-import javax.persistence.Column;
-import javax.persistence.Entity;
-import javax.persistence.Id;
-import javax.persistence.NamedQueries;
-import javax.persistence.NamedQuery;
-import javax.persistence.Table;
-
-import org.apache.james.core.Domain;
-
-/**
- * Domain class for the James Domain to be used for JPA persistence.
- */
-@Entity(name = "JamesDomain")
-@Table(name = "JAMES_DOMAIN")
-@NamedQueries({ 
-    @NamedQuery(name = "findDomainByName", query = "SELECT domain FROM 
JamesDomain domain WHERE domain.name=:name"), 
-    @NamedQuery(name = "containsDomain", query = "SELECT COUNT(domain) FROM 
JamesDomain domain WHERE domain.name=:name"),
-    @NamedQuery(name = "listDomainNames", query = "SELECT domain.name FROM 
JamesDomain domain"), 
-    @NamedQuery(name = "deleteDomainByName", query = "DELETE FROM JamesDomain 
domain WHERE domain.name=:name") })
-public class JPADomain {
-
-    /**
-     * The name of the domain. column name is chosen to be compatible with the
-     * JDBCDomainList.
-     */
-    @Id
-    @Column(name = "DOMAIN_NAME", nullable = false, length = 100)
-    private String name;
-
-    /**
-     * Default no-args constructor for JPA class enhancement.
-     * The constructor need to be public or protected to be used by JPA.
-     * See:  http://docs.oracle.com/javaee/6/tutorial/doc/bnbqa.html
-     * Do not us this constructor, it is for JPA only.
-     */
-    protected JPADomain() {
-    }
-
-    /**
-     * Use this simple constructor to create a new Domain.
-     * 
-     * @param name
-     *            the name of the Domain
-     */
-    public JPADomain(Domain name) {
-        this.name = name.asString();
-    }
-
-}
diff --git 
a/server/data/data-postgres/src/main/java/org/apache/james/domainlist/postgres/PostgresDomainList.java
 
b/server/data/data-postgres/src/main/java/org/apache/james/domainlist/postgres/PostgresDomainList.java
new file mode 100644
index 0000000000..6074b6babc
--- /dev/null
+++ 
b/server/data/data-postgres/src/main/java/org/apache/james/domainlist/postgres/PostgresDomainList.java
@@ -0,0 +1,79 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.domainlist.postgres;
+
+import java.util.List;
+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.core.Domain;
+import org.apache.james.dnsservice.api.DNSService;
+import org.apache.james.domainlist.api.DomainListException;
+import org.apache.james.domainlist.lib.AbstractDomainList;
+import org.jooq.exception.DataAccessException;
+
+import reactor.core.publisher.Flux;
+import reactor.core.publisher.Mono;
+
+public class PostgresDomainList extends AbstractDomainList {
+    private final PostgresExecutor postgresExecutor;
+
+    @Inject
+    public PostgresDomainList(DNSService dnsService, 
JamesPostgresConnectionFactory postgresConnectionFactory) {
+        super(dnsService);
+        this.postgresExecutor = new 
PostgresExecutor(postgresConnectionFactory.getConnection(Optional.empty()));;
+    }
+
+    @Override
+    public void addDomain(Domain domain) throws DomainListException {
+        postgresExecutor.executeVoid(dslContext -> 
Mono.from(dslContext.insertInto(PostgresDomainModule.PostgresDomainTable.TABLE_NAME,
 PostgresDomainModule.PostgresDomainTable.DOMAIN)
+                .values(domain.asString())))
+            .onErrorMap(DataAccessException.class, e -> new 
DomainListException(domain.name() + " already exists."))
+            .block();
+
+    }
+
+    @Override
+    protected List<Domain> getDomainListInternal() throws DomainListException {
+        return postgresExecutor.executeRows(dsl -> 
Flux.from(dsl.selectFrom(PostgresDomainModule.PostgresDomainTable.TABLE_NAME)))
+            .map(record -> 
Domain.of(record.get(PostgresDomainModule.PostgresDomainTable.DOMAIN)))
+            .collectList()
+            .block();
+    }
+
+    @Override
+    protected boolean containsDomainInternal(Domain domain) throws 
DomainListException {
+        return postgresExecutor.executeRow(dsl -> 
Mono.from(dsl.selectFrom(PostgresDomainModule.PostgresDomainTable.TABLE_NAME)
+            
.where(PostgresDomainModule.PostgresDomainTable.DOMAIN.eq(domain.asString()))))
+            .blockOptional()
+            .isPresent();
+    }
+
+    @Override
+    protected void doRemoveDomain(Domain domain) throws DomainListException {
+        postgresExecutor.executeVoid(dslContext -> 
Mono.from(dslContext.deleteFrom(PostgresDomainModule.PostgresDomainTable.TABLE_NAME)
+            
.where(PostgresDomainModule.PostgresDomainTable.DOMAIN.eq(domain.asString()))))
+            .onErrorMap(DataAccessException.class, e -> new 
DomainListException(domain.name() + " was not found"))
+            .block();
+    }
+}
diff --git 
a/server/container/guice/postgres-common/src/main/java/org/apache/james/modules/data/PostgresDataModule.java
 
b/server/data/data-postgres/src/main/java/org/apache/james/domainlist/postgres/PostgresDomainModule.java
similarity index 56%
copy from 
server/container/guice/postgres-common/src/main/java/org/apache/james/modules/data/PostgresDataModule.java
copy to 
server/data/data-postgres/src/main/java/org/apache/james/domainlist/postgres/PostgresDomainModule.java
index 125746063b..aa80839f9f 100644
--- 
a/server/container/guice/postgres-common/src/main/java/org/apache/james/modules/data/PostgresDataModule.java
+++ 
b/server/data/data-postgres/src/main/java/org/apache/james/domainlist/postgres/PostgresDomainModule.java
@@ -17,18 +17,30 @@
  * under the License.                                           *
  ****************************************************************/
 
-package org.apache.james.modules.data;
+package org.apache.james.domainlist.postgres;
 
-import org.apache.james.CoreDataModule;
+import org.apache.james.backends.postgres.PostgresModule;
+import org.apache.james.backends.postgres.PostgresTable;
+import org.jooq.Field;
+import org.jooq.Record;
+import org.jooq.Table;
+import org.jooq.impl.DSL;
+import org.jooq.impl.SQLDataType;
 
-import com.google.inject.AbstractModule;
+public interface PostgresDomainModule {
+    interface PostgresDomainTable {
+        Table<Record> TABLE_NAME = DSL.table("domains");
 
-public class PostgresDataModule extends AbstractModule {
-    @Override
-    protected void configure() {
-        install(new CoreDataModule());
-        install(new JPADomainListModule());
-        install(new JPARecipientRewriteTableModule());
-        install(new JPAMailRepositoryModule());
+        Field<String> DOMAIN = DSL.field("domain", 
SQLDataType.VARCHAR.notNull());
+
+        PostgresTable TABLE = PostgresTable.name(TABLE_NAME.getName())
+            .createTableStep(((dsl, tableName) -> 
dsl.createTableIfNotExists(tableName)
+                .column(DOMAIN)
+                .constraint(DSL.primaryKey(DOMAIN))))
+            .disableRowLevelSecurity();
     }
+
+    PostgresModule MODULE = PostgresModule.builder()
+        .addTable(PostgresDomainTable.TABLE)
+        .build();
 }
diff --git 
a/server/data/data-postgres/src/test/java/org/apache/james/domainlist/jpa/PostgresDomainListTest.java
 
b/server/data/data-postgres/src/test/java/org/apache/james/domainlist/jpa/PostgresDomainListTest.java
deleted file mode 100644
index 909a1e8286..0000000000
--- 
a/server/data/data-postgres/src/test/java/org/apache/james/domainlist/jpa/PostgresDomainListTest.java
+++ /dev/null
@@ -1,29 +0,0 @@
-package org.apache.james.domainlist.jpa;
-
-import org.apache.james.backends.postgres.PostgresExtension;
-import org.apache.james.domainlist.api.DomainList;
-import org.apache.james.domainlist.lib.DomainListConfiguration;
-import org.apache.james.domainlist.lib.DomainListContract;
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.extension.RegisterExtension;
-
-public class PostgresDomainListTest implements DomainListContract {
-    @RegisterExtension
-    static PostgresExtension postgresExtension = 
PostgresExtension.withoutRowLevelSecurity(PostgresDomainModule.MODULE);
-
-    PostgresDomainList domainList;
-
-    @BeforeEach
-    public void setup() throws Exception {
-        domainList = new PostgresDomainList(getDNSServer("localhost"), 
postgresExtension.getPostgresExecutor());
-        domainList.configure(DomainListConfiguration.builder()
-            .autoDetect(false)
-            .autoDetectIp(false)
-            .build());
-    }
-
-    @Override
-    public DomainList domainList() {
-        return domainList;
-    }
-}
diff --git 
a/server/data/data-postgres/src/test/java/org/apache/james/domainlist/jpa/JPADomainListTest.java
 
b/server/data/data-postgres/src/test/java/org/apache/james/domainlist/postgres/PostgresDomainListTest.java
similarity index 55%
rename from 
server/data/data-postgres/src/test/java/org/apache/james/domainlist/jpa/JPADomainListTest.java
rename to 
server/data/data-postgres/src/test/java/org/apache/james/domainlist/postgres/PostgresDomainListTest.java
index 2a9bb30fd3..a3b969b338 100644
--- 
a/server/data/data-postgres/src/test/java/org/apache/james/domainlist/jpa/JPADomainListTest.java
+++ 
b/server/data/data-postgres/src/test/java/org/apache/james/domainlist/postgres/PostgresDomainListTest.java
@@ -16,56 +16,38 @@
  * specific language governing permissions and limitations      *
  * under the License.                                           *
  ****************************************************************/
-package org.apache.james.domainlist.jpa;
 
-import org.apache.james.backends.jpa.JpaTestCluster;
-import org.apache.james.core.Domain;
+package org.apache.james.domainlist.postgres;
+
+import org.apache.james.backends.postgres.PostgresExtension;
+import 
org.apache.james.backends.postgres.utils.SinglePostgresConnectionFactory;
 import org.apache.james.domainlist.api.DomainList;
-import org.apache.james.domainlist.jpa.model.JPADomain;
 import org.apache.james.domainlist.lib.DomainListConfiguration;
 import org.apache.james.domainlist.lib.DomainListContract;
-import org.junit.jupiter.api.AfterEach;
 import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.extension.RegisterExtension;
 
-/**
- * Test the JPA implementation of the DomainList.
- */
-class JPADomainListTest implements DomainListContract {
+import io.r2dbc.spi.Connection;
+import reactor.core.publisher.Mono;
 
-    static final JpaTestCluster JPA_TEST_CLUSTER = 
JpaTestCluster.create(JPADomain.class);
+public class PostgresDomainListTest implements DomainListContract {
+    @RegisterExtension
+    static PostgresExtension postgresExtension = 
PostgresExtension.withoutRowLevelSecurity(PostgresDomainModule.MODULE);
 
-    JPADomainList jpaDomainList;
+    PostgresDomainList domainList;
 
     @BeforeEach
-    public void setUp() throws Exception {
-        jpaDomainList = createDomainList();
-    }
-
-    @AfterEach
-    public void tearDown() throws Exception {
-        DomainList domainList = createDomainList();
-        for (Domain domain: domainList.getDomains()) {
-            try {
-                domainList.removeDomain(domain);
-            } catch (Exception e) {
-                // silent: exception arise where clearing auto detected domains
-            }
-        }
-    }
-
-    @Override
-    public DomainList domainList() {
-        return jpaDomainList;
-    }
-
-    private JPADomainList createDomainList() throws Exception {
-        JPADomainList jpaDomainList = new 
JPADomainList(getDNSServer("localhost"),
-            JPA_TEST_CLUSTER.getEntityManagerFactory());
-        jpaDomainList.configure(DomainListConfiguration.builder()
+    public void setup() throws Exception {
+        Connection connection = 
Mono.from(postgresExtension.getConnectionFactory().create()).block();
+        domainList = new PostgresDomainList(getDNSServer("localhost"), new 
SinglePostgresConnectionFactory(connection));
+        domainList.configure(DomainListConfiguration.builder()
             .autoDetect(false)
             .autoDetectIp(false)
             .build());
+    }
 
-        return jpaDomainList;
+    @Override
+    public DomainList domainList() {
+        return domainList;
     }
 }
diff --git a/server/data/data-postgres/src/test/resources/persistence.xml 
b/server/data/data-postgres/src/test/resources/persistence.xml
index 962146a543..4a6b7c3c5b 100644
--- a/server/data/data-postgres/src/test/resources/persistence.xml
+++ b/server/data/data-postgres/src/test/resources/persistence.xml
@@ -26,7 +26,6 @@
     <persistence-unit name="James" transaction-type="JTA">
        
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
         
<jta-data-source>osgi:service/javax.sql.DataSource/(osgi.jndi.service.name=jdbc/james)</jta-data-source>
-        <class>org.apache.james.domainlist.jpa.model.JPADomain</class>
         <class>org.apache.james.rrt.jpa.model.JPARecipientRewrite</class>
         <class>org.apache.james.mailrepository.jpa.model.JPAUrl</class>
         <class>org.apache.james.mailrepository.jpa.model.JPAMail</class>


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to