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

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

commit b0c6a5dff04f015ae058832a2c4abd80cecbb39c
Author: Jean Helou <[email protected]>
AuthorDate: Wed Oct 5 22:15:41 2022 +0200

    [JAMES-3836] introduces MailRepositoryFactory
    
    This new interface in intended to facilitate injection by separating the 
infrastructure dependencies (cassandra, blob) from the functional dependency on 
the URL to build the actual store.
    This will be leverage in the next commits by a new guice loader 
implementation which doesn't need to dynamically create a subcontext for each 
new MailRepository instance creation.
---
 .../CassandraMailRepositoryModule.java             |  6 ++-
 .../james/modules/data/MemoryDataModule.java       |  6 +++
 .../mailrepository/api/MailRepositoryFactory.java  | 26 ++++++++++
 .../blob/BlobMailRepositoryFactory.scala           | 38 +++++++++++++++
 .../blob/BlobMailRepositoryTest.java               | 25 ++++------
 .../cassandra/CassandraMailRepositoryFactory.java  | 57 ++++++++++++++++++++++
 .../memory/MemoryMailRepositoryFactory.java        | 36 ++++++++++++++
 7 files changed, 177 insertions(+), 17 deletions(-)

diff --git 
a/server/container/guice/mailrepository-cassandra/src/main/java/org/apache/james/modules/mailrepository/CassandraMailRepositoryModule.java
 
b/server/container/guice/mailrepository-cassandra/src/main/java/org/apache/james/modules/mailrepository/CassandraMailRepositoryModule.java
index 242424ea19..198011d1c5 100644
--- 
a/server/container/guice/mailrepository-cassandra/src/main/java/org/apache/james/modules/mailrepository/CassandraMailRepositoryModule.java
+++ 
b/server/container/guice/mailrepository-cassandra/src/main/java/org/apache/james/modules/mailrepository/CassandraMailRepositoryModule.java
@@ -22,10 +22,12 @@ package org.apache.james.modules.mailrepository;
 import org.apache.commons.configuration2.BaseHierarchicalConfiguration;
 import org.apache.james.backends.cassandra.components.CassandraModule;
 import org.apache.james.blob.api.BlobReferenceSource;
+import org.apache.james.mailrepository.api.MailRepositoryFactory;
 import org.apache.james.mailrepository.api.MailRepositoryUrlStore;
 import org.apache.james.mailrepository.api.Protocol;
 import org.apache.james.mailrepository.cassandra.CassandraMailRepository;
 import 
org.apache.james.mailrepository.cassandra.CassandraMailRepositoryCountDAO;
+import 
org.apache.james.mailrepository.cassandra.CassandraMailRepositoryFactory;
 import 
org.apache.james.mailrepository.cassandra.CassandraMailRepositoryKeysDAO;
 import 
org.apache.james.mailrepository.cassandra.CassandraMailRepositoryMailDaoV2;
 import 
org.apache.james.mailrepository.cassandra.CassandraMailRepositoryUrlModule;
@@ -62,6 +64,8 @@ public class CassandraMailRepositoryModule extends 
AbstractModule {
 
         Multibinder.newSetBinder(binder(), BlobReferenceSource.class)
             .addBinding().to(MailRepositoryBlobReferenceSource.class);
-    }
 
+        Multibinder.newSetBinder(binder(), MailRepositoryFactory.class)
+                .addBinding().to(CassandraMailRepositoryFactory.class);
+    }
 }
diff --git 
a/server/container/guice/memory/src/main/java/org/apache/james/modules/data/MemoryDataModule.java
 
b/server/container/guice/memory/src/main/java/org/apache/james/modules/data/MemoryDataModule.java
index cb3293057d..42e8cb262b 100644
--- 
a/server/container/guice/memory/src/main/java/org/apache/james/modules/data/MemoryDataModule.java
+++ 
b/server/container/guice/memory/src/main/java/org/apache/james/modules/data/MemoryDataModule.java
@@ -27,10 +27,12 @@ import 
org.apache.james.dlp.eventsourcing.EventSourcingDLPConfigurationStore;
 import org.apache.james.domainlist.api.DomainList;
 import org.apache.james.domainlist.lib.DomainListConfiguration;
 import org.apache.james.domainlist.memory.MemoryDomainList;
+import org.apache.james.mailrepository.api.MailRepositoryFactory;
 import org.apache.james.mailrepository.api.MailRepositoryUrlStore;
 import org.apache.james.mailrepository.api.Protocol;
 import org.apache.james.mailrepository.memory.MailRepositoryStoreConfiguration;
 import org.apache.james.mailrepository.memory.MemoryMailRepository;
+import org.apache.james.mailrepository.memory.MemoryMailRepositoryFactory;
 import org.apache.james.mailrepository.memory.MemoryMailRepositoryUrlStore;
 import org.apache.james.rrt.api.AliasReverseResolver;
 import org.apache.james.rrt.api.CanSendFrom;
@@ -52,6 +54,7 @@ import 
org.apache.james.vacation.memory.MemoryVacationRepository;
 import com.google.common.collect.ImmutableList;
 import com.google.inject.AbstractModule;
 import com.google.inject.Scopes;
+import com.google.inject.multibindings.Multibinder;
 import com.google.inject.multibindings.ProvidesIntoSet;
 
 public class MemoryDataModule extends AbstractModule {
@@ -99,6 +102,9 @@ public class MemoryDataModule extends AbstractModule {
                 ImmutableList.of(new Protocol("memory")),
                 MemoryMailRepository.class.getName(),
                 new BaseHierarchicalConfiguration()));
+
+        Multibinder.newSetBinder(binder(), MailRepositoryFactory.class)
+                .addBinding().to(MemoryMailRepositoryFactory.class);
     }
 
     @ProvidesIntoSet
diff --git 
a/server/mailrepository/mailrepository-api/src/main/java/org/apache/james/mailrepository/api/MailRepositoryFactory.java
 
b/server/mailrepository/mailrepository-api/src/main/java/org/apache/james/mailrepository/api/MailRepositoryFactory.java
new file mode 100644
index 0000000000..7d3f9c47f7
--- /dev/null
+++ 
b/server/mailrepository/mailrepository-api/src/main/java/org/apache/james/mailrepository/api/MailRepositoryFactory.java
@@ -0,0 +1,26 @@
+/****************************************************************
+ * 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.mailrepository.api;
+
+public interface MailRepositoryFactory {
+    Class<? extends MailRepository> mailRepositoryClass();
+
+    MailRepository create(MailRepositoryUrl url);
+}
diff --git 
a/server/mailrepository/mailrepository-blob/src/main/scala/org/apache/james/mailrepository/blob/BlobMailRepositoryFactory.scala
 
b/server/mailrepository/mailrepository-blob/src/main/scala/org/apache/james/mailrepository/blob/BlobMailRepositoryFactory.scala
new file mode 100644
index 0000000000..9ebfd10665
--- /dev/null
+++ 
b/server/mailrepository/mailrepository-blob/src/main/scala/org/apache/james/mailrepository/blob/BlobMailRepositoryFactory.scala
@@ -0,0 +1,38 @@
+/** **************************************************************
+ * 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.mailrepository.blob
+
+import org.apache.james.blob.api.{BlobId, BlobStoreDAO, BucketName}
+import org.apache.james.blob.mail.MimeMessageStore
+import org.apache.james.mailrepository.api.{MailRepository, 
MailRepositoryFactory, MailRepositoryUrl}
+
+class BlobMailRepositoryFactory(blobStore: BlobStoreDAO,
+                                blobIdFactory: BlobId.Factory,
+                                mimeFactory: MimeMessageStore.Factory) extends 
MailRepositoryFactory {
+  override val mailRepositoryClass: Class[_ <: MailRepository] = 
classOf[BlobMailRepository]
+
+  override def create(url: MailRepositoryUrl): MailRepository = {
+    val metadataBucketName = BucketName.of(url.getPath.asString() + 
"/mailMetadata")
+    val mailDataBucketName = BucketName.of(url.getPath.asString() + 
"/mimeMessageData")
+    val mimeMessageStore = mimeFactory.mimeMessageStore(mailDataBucketName)
+
+    new BlobMailRepository(blobStore, blobIdFactory, mimeMessageStore, 
metadataBucketName)
+  }
+}
diff --git 
a/server/mailrepository/mailrepository-blob/src/test/java/org/apache/james/mailrepository/blob/BlobMailRepositoryTest.java
 
b/server/mailrepository/mailrepository-blob/src/test/java/org/apache/james/mailrepository/blob/BlobMailRepositoryTest.java
index df396d3b90..41c48aba68 100644
--- 
a/server/mailrepository/mailrepository-blob/src/test/java/org/apache/james/mailrepository/blob/BlobMailRepositoryTest.java
+++ 
b/server/mailrepository/mailrepository-blob/src/test/java/org/apache/james/mailrepository/blob/BlobMailRepositoryTest.java
@@ -32,6 +32,8 @@ import org.apache.james.blob.memory.MemoryBlobStoreFactory;
 import org.apache.james.mailrepository.MailRepositoryContract;
 import org.apache.james.mailrepository.api.MailRepository;
 import org.apache.james.mailrepository.api.MailRepositoryPath;
+import org.apache.james.mailrepository.api.MailRepositoryUrl;
+import org.apache.james.mailrepository.api.Protocol;
 import org.jetbrains.annotations.NotNull;
 import org.apache.james.mailrepository.api.MailRepositoryPath;
 import org.junit.jupiter.api.BeforeEach;
@@ -39,10 +41,11 @@ import org.junit.jupiter.api.Disabled;
 
 class BlobMailRepositoryTest implements MailRepositoryContract {
 
-    private BlobMailRepository blobMailRepository;
+    private MailRepository blobMailRepository;
     private HashBlobId.Factory blobIdFactory;
     private MemoryBlobStoreDAO blobStore;
     private MimeMessageStore.Factory mimeMessageStoreFactory;
+    private BlobMailRepositoryFactory blobMailRepositoryFactory;
 
     @BeforeEach
     void setup() {
@@ -54,23 +57,13 @@ class BlobMailRepositoryTest implements 
MailRepositoryContract {
                 .passthrough();
         mimeMessageStoreFactory = new 
MimeMessageStore.Factory(mimeMessageBlobStore);
         MailRepositoryPath path = MailRepositoryPath.from("/foo");
-
-        blobMailRepository = buildBlobMailRepository(blobIdFactory, blobStore, 
path, mimeMessageStoreFactory);
+        blobMailRepositoryFactory = new BlobMailRepositoryFactory(blobStore, 
blobIdFactory, mimeMessageStoreFactory);
+        blobMailRepository = buildBlobMailRepository(path);
     }
 
     @NotNull
-    private BlobMailRepository buildBlobMailRepository(HashBlobId.Factory 
blobIdFactory,
-                                                       MemoryBlobStoreDAO 
blobStore,
-                                                       MailRepositoryPath path,
-                                                       
MimeMessageStore.Factory mimeMessageStoreFactory) {
-        Store<MimeMessage, MimeMessagePartsId> mimeMessageStore =
-                
mimeMessageStoreFactory.mimeMessageStore(BucketName.of(path+"/mimeMessageData"));
-        return new BlobMailRepository(
-                blobStore,
-                blobIdFactory,
-                mimeMessageStore,
-                BucketName.of(path + "/mailMetadata")
-        );
+    private MailRepository buildBlobMailRepository(MailRepositoryPath path) {
+        return 
blobMailRepositoryFactory.create(MailRepositoryUrl.fromPathAndProtocol(new 
Protocol("blob"), path));
     }
 
     @Override
@@ -80,6 +73,6 @@ class BlobMailRepositoryTest implements 
MailRepositoryContract {
 
     @Override
     public MailRepository retrieveRepository(MailRepositoryPath path) {
-        return buildBlobMailRepository(blobIdFactory, blobStore, path, 
mimeMessageStoreFactory);
+        return buildBlobMailRepository(path);
     }
 }
\ No newline at end of file
diff --git 
a/server/mailrepository/mailrepository-cassandra/src/main/java/org/apache/james/mailrepository/cassandra/CassandraMailRepositoryFactory.java
 
b/server/mailrepository/mailrepository-cassandra/src/main/java/org/apache/james/mailrepository/cassandra/CassandraMailRepositoryFactory.java
new file mode 100644
index 0000000000..98a3d58d3e
--- /dev/null
+++ 
b/server/mailrepository/mailrepository-cassandra/src/main/java/org/apache/james/mailrepository/cassandra/CassandraMailRepositoryFactory.java
@@ -0,0 +1,57 @@
+/****************************************************************
+ * 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.mailrepository.cassandra;
+
+import javax.inject.Inject;
+
+import org.apache.james.blob.mail.MimeMessageStore;
+import org.apache.james.mailrepository.api.MailRepository;
+import org.apache.james.mailrepository.api.MailRepositoryFactory;
+import org.apache.james.mailrepository.api.MailRepositoryUrl;
+
+public class CassandraMailRepositoryFactory implements MailRepositoryFactory {
+    private final CassandraMailRepositoryKeysDAO keysDAO;
+    private final CassandraMailRepositoryCountDAO countDAO;
+    private final CassandraMailRepositoryMailDaoV2 mailDAO;
+    private final MimeMessageStore.Factory mimeMessageStoreFactory;
+
+    @Inject
+    public CassandraMailRepositoryFactory(
+            CassandraMailRepositoryKeysDAO keysDAO,
+            CassandraMailRepositoryCountDAO countDAO,
+            CassandraMailRepositoryMailDaoV2 mailDAO,
+            MimeMessageStore.Factory mimeMessageStoreFactory
+    ) {
+        this.keysDAO = keysDAO;
+        this.countDAO = countDAO;
+        this.mailDAO = mailDAO;
+        this.mimeMessageStoreFactory = mimeMessageStoreFactory;
+    }
+
+    @Override
+    public Class<? extends MailRepository> mailRepositoryClass() {
+        return CassandraMailRepository.class;
+    }
+
+    @Override
+    public MailRepository create(MailRepositoryUrl url) {
+        return new CassandraMailRepository(url, keysDAO, countDAO, mailDAO, 
mimeMessageStoreFactory);
+    }
+}
diff --git 
a/server/mailrepository/mailrepository-memory/src/main/java/org/apache/james/mailrepository/memory/MemoryMailRepositoryFactory.java
 
b/server/mailrepository/mailrepository-memory/src/main/java/org/apache/james/mailrepository/memory/MemoryMailRepositoryFactory.java
new file mode 100644
index 0000000000..f3d95ec099
--- /dev/null
+++ 
b/server/mailrepository/mailrepository-memory/src/main/java/org/apache/james/mailrepository/memory/MemoryMailRepositoryFactory.java
@@ -0,0 +1,36 @@
+/****************************************************************
+ * 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.mailrepository.memory;
+
+import org.apache.james.mailrepository.api.MailRepository;
+import org.apache.james.mailrepository.api.MailRepositoryFactory;
+import org.apache.james.mailrepository.api.MailRepositoryUrl;
+
+public class MemoryMailRepositoryFactory implements MailRepositoryFactory {
+    @Override
+    public Class<? extends MailRepository> mailRepositoryClass() {
+        return MemoryMailRepository.class;
+    }
+
+    @Override
+    public MailRepository create(MailRepositoryUrl url) {
+        return new MemoryMailRepository();
+    }
+}


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

Reply via email to