JAMES-2582 make use of Guice automatic Provider generation to simplify 
BlobStoreChoosingModule further


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/59fd99f8
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/59fd99f8
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/59fd99f8

Branch: refs/heads/master
Commit: 59fd99f8d32f1dfb44aabc801d103a604f396e54
Parents: 642555e
Author: Matthieu Baechler <matth...@apache.org>
Authored: Mon Nov 12 09:29:14 2018 +0100
Committer: Benoit Tellier <btell...@linagora.com>
Committed: Wed Nov 14 11:19:19 2018 +0700

----------------------------------------------------------------------
 .../objectstore/BlobStoreChoosingModule.java    | 59 +++-----------------
 .../BlobStoreChoosingModuleTest.java            | 30 +++++-----
 2 files changed, 24 insertions(+), 65 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/59fd99f8/server/container/guice/cassandra-rabbitmq-guice/src/main/java/org/apache/james/modules/objectstore/BlobStoreChoosingModule.java
----------------------------------------------------------------------
diff --git 
a/server/container/guice/cassandra-rabbitmq-guice/src/main/java/org/apache/james/modules/objectstore/BlobStoreChoosingModule.java
 
b/server/container/guice/cassandra-rabbitmq-guice/src/main/java/org/apache/james/modules/objectstore/BlobStoreChoosingModule.java
index fce77e6..2acd8c1 100644
--- 
a/server/container/guice/cassandra-rabbitmq-guice/src/main/java/org/apache/james/modules/objectstore/BlobStoreChoosingModule.java
+++ 
b/server/container/guice/cassandra-rabbitmq-guice/src/main/java/org/apache/james/modules/objectstore/BlobStoreChoosingModule.java
@@ -21,26 +21,22 @@ package org.apache.james.modules.objectstore;
 
 import java.io.FileNotFoundException;
 
-import javax.inject.Inject;
 import javax.inject.Provider;
 import javax.inject.Singleton;
 
 import org.apache.commons.configuration.Configuration;
 import org.apache.commons.configuration.ConfigurationException;
 import org.apache.james.backends.cassandra.components.CassandraModule;
-import 
org.apache.james.backends.cassandra.init.configuration.CassandraConfiguration;
 import org.apache.james.blob.api.BlobStore;
-import org.apache.james.blob.api.HashBlobId;
 import org.apache.james.blob.cassandra.CassandraBlobModule;
 import org.apache.james.blob.cassandra.CassandraBlobsDAO;
+import org.apache.james.blob.objectstorage.ObjectStorageBlobsDAO;
 import org.apache.james.blob.objectstorage.PayloadCodec;
-import org.apache.james.modules.objectstorage.ObjectStorageBlobsDAOProvider;
 import org.apache.james.modules.objectstorage.PayloadCodecProvider;
 import org.apache.james.utils.PropertiesProvider;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.datastax.driver.core.Session;
 import com.google.common.annotations.VisibleForTesting;
 import com.google.inject.AbstractModule;
 import com.google.inject.Provides;
@@ -49,80 +45,39 @@ import com.google.inject.multibindings.Multibinder;
 
 public class BlobStoreChoosingModule extends AbstractModule {
 
-    interface BlobStoreFactory extends Provider<BlobStore> {}
-
-    static class CassandraBlobStoreFactory implements BlobStoreFactory {
-        private final Session session;
-        private final CassandraConfiguration configuration;
-        private final HashBlobId.Factory blobIdFactory;
-
-        @Inject
-        CassandraBlobStoreFactory(Session session, CassandraConfiguration 
configuration, HashBlobId.Factory blobIdFactory) {
-            this.session = session;
-            this.configuration = configuration;
-            this.blobIdFactory = blobIdFactory;
-        }
-
-        @Override
-        public BlobStore get() {
-            return new CassandraBlobsDAO(
-                session,
-                configuration,
-                blobIdFactory);
-        }
-    }
-
-    static class SwiftBlobStoreFactory implements BlobStoreFactory {
-        private final ObjectStorageBlobsDAOProvider blobsDAOProvider;
-
-        @Inject
-        SwiftBlobStoreFactory(ObjectStorageBlobsDAOProvider blobsDAOProvider) {
-            this.blobsDAOProvider = blobsDAOProvider;
-        }
-
-        @Override
-        public BlobStore get() {
-            return blobsDAOProvider.get();
-        }
-    }
-
     private static final Logger LOGGER = 
LoggerFactory.getLogger(BlobStoreChoosingModule.class);
 
     static final String BLOBSTORE_CONFIGURATION_NAME = "objectstore";
 
     @Override
     protected void configure() {
-        bind(SwiftBlobStoreFactory.class).in(Scopes.SINGLETON);
         
bind(PayloadCodec.class).toProvider(PayloadCodecProvider.class).in(Scopes.SINGLETON);
 
-        bind(CassandraBlobStoreFactory.class).in(Scopes.SINGLETON);
         Multibinder<CassandraModule> cassandraDataDefinitions = 
Multibinder.newSetBinder(binder(), CassandraModule.class);
         
cassandraDataDefinitions.addBinding().toInstance(CassandraBlobModule.MODULE);
-
-        
bind(BlobStore.class).toProvider(BlobStoreFactory.class).in(Scopes.SINGLETON);
     }
 
     @VisibleForTesting
     @Provides
     @Singleton
-    BlobStoreFactory provideBlobStoreFactory(PropertiesProvider 
propertiesProvider,
-                                             
Provider<CassandraBlobStoreFactory> cassandraBlobStoreFactoryProvider,
-                                             Provider<SwiftBlobStoreFactory> 
swiftBlobStoreFactoryProvider) throws ConfigurationException {
+    BlobStore provideBlobStore(PropertiesProvider propertiesProvider,
+                               Provider<CassandraBlobsDAO> 
cassandraBlobStoreProvider,
+                               Provider<ObjectStorageBlobsDAO> 
swiftBlobStoreProvider) throws ConfigurationException {
         try {
             Configuration configuration = 
propertiesProvider.getConfiguration(BLOBSTORE_CONFIGURATION_NAME);
             BlobStoreChoosingConfiguration choosingConfiguration = 
BlobStoreChoosingConfiguration.from(configuration);
             switch (choosingConfiguration.getImplementation()) {
                 case SWIFT:
-                    return swiftBlobStoreFactoryProvider.get();
+                    return swiftBlobStoreProvider.get();
                 case CASSANDRA:
-                    return cassandraBlobStoreFactoryProvider.get();
+                    return cassandraBlobStoreProvider.get();
                 default:
                     throw new RuntimeException(String.format("can not get the 
right blobstore provider with configuration %s",
                         choosingConfiguration.toString()));
             }
         } catch (FileNotFoundException e) {
             LOGGER.warn("Could not find " + BLOBSTORE_CONFIGURATION_NAME + " 
configuration file, using cassandra blobstore as the default");
-            return cassandraBlobStoreFactoryProvider.get();
+            return cassandraBlobStoreProvider.get();
         }
     }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/59fd99f8/server/container/guice/cassandra-rabbitmq-guice/src/test/java/org/apache/james/modules/objectstore/BlobStoreChoosingModuleTest.java
----------------------------------------------------------------------
diff --git 
a/server/container/guice/cassandra-rabbitmq-guice/src/test/java/org/apache/james/modules/objectstore/BlobStoreChoosingModuleTest.java
 
b/server/container/guice/cassandra-rabbitmq-guice/src/test/java/org/apache/james/modules/objectstore/BlobStoreChoosingModuleTest.java
index 72d931f..0fb619c 100644
--- 
a/server/container/guice/cassandra-rabbitmq-guice/src/test/java/org/apache/james/modules/objectstore/BlobStoreChoosingModuleTest.java
+++ 
b/server/container/guice/cassandra-rabbitmq-guice/src/test/java/org/apache/james/modules/objectstore/BlobStoreChoosingModuleTest.java
@@ -25,16 +25,20 @@ import static 
org.assertj.core.api.Assertions.assertThatThrownBy;
 import static org.mockito.Mockito.mock;
 
 import org.apache.commons.configuration.PropertiesConfiguration;
+import org.apache.james.blob.cassandra.CassandraBlobsDAO;
+import org.apache.james.blob.objectstorage.ObjectStorageBlobsDAO;
 import org.apache.james.modules.objectstorage.FakePropertiesProvider;
 import 
org.apache.james.modules.objectstore.BlobStoreChoosingConfiguration.BlobStoreImplName;
-import 
org.apache.james.modules.objectstore.BlobStoreChoosingModule.CassandraBlobStoreFactory;
-import 
org.apache.james.modules.objectstore.BlobStoreChoosingModule.SwiftBlobStoreFactory;
 import org.junit.jupiter.api.Test;
 
+import com.google.inject.Provider;
+
 class BlobStoreChoosingModuleTest {
 
-    private static CassandraBlobStoreFactory CASSANDRA_BLOBSTORE_FACTORY = 
mock(CassandraBlobStoreFactory.class);
-    private static SwiftBlobStoreFactory SWIFT_BLOBSTORE_FACTORY = 
mock(SwiftBlobStoreFactory.class);
+    private static CassandraBlobsDAO CASSANDRA_BLOBSTORE = 
mock(CassandraBlobsDAO.class);
+    private static Provider<CassandraBlobsDAO> CASSANDRA_BLOBSTORE_PROVIDER = 
() -> CASSANDRA_BLOBSTORE;
+    private static ObjectStorageBlobsDAO SWIFT_BLOBSTORE = 
mock(ObjectStorageBlobsDAO.class);
+    private static Provider<ObjectStorageBlobsDAO> SWIFT_BLOBSTORE_PROVIDER = 
() -> SWIFT_BLOBSTORE;
 
     @Test
     void provideBlobStoreFactoryShouldThrowWhenMissingPropertyField() throws 
Exception {
@@ -45,7 +49,7 @@ class BlobStoreChoosingModuleTest {
             .register(BLOBSTORE_CONFIGURATION_NAME, configuration)
             .build();
 
-        assertThatThrownBy(() -> 
module.provideBlobStoreFactory(propertyProvider, () -> 
CASSANDRA_BLOBSTORE_FACTORY, () -> SWIFT_BLOBSTORE_FACTORY))
+        assertThatThrownBy(() -> module.provideBlobStore(propertyProvider, 
CASSANDRA_BLOBSTORE_PROVIDER, SWIFT_BLOBSTORE_PROVIDER))
             .isInstanceOf(IllegalStateException.class);
     }
 
@@ -58,7 +62,7 @@ class BlobStoreChoosingModuleTest {
             .register(BLOBSTORE_CONFIGURATION_NAME, configuration)
             .build();
 
-        assertThatThrownBy(() -> 
module.provideBlobStoreFactory(propertyProvider, () -> 
CASSANDRA_BLOBSTORE_FACTORY, () -> SWIFT_BLOBSTORE_FACTORY))
+        assertThatThrownBy(() -> module.provideBlobStore(propertyProvider, 
CASSANDRA_BLOBSTORE_PROVIDER, SWIFT_BLOBSTORE_PROVIDER))
             .isInstanceOf(IllegalStateException.class);
     }
 
@@ -71,7 +75,7 @@ class BlobStoreChoosingModuleTest {
             .register(BLOBSTORE_CONFIGURATION_NAME, configuration)
             .build();
 
-        assertThatThrownBy(() -> 
module.provideBlobStoreFactory(propertyProvider, () -> 
CASSANDRA_BLOBSTORE_FACTORY, () -> SWIFT_BLOBSTORE_FACTORY))
+        assertThatThrownBy(() -> module.provideBlobStore(propertyProvider, 
CASSANDRA_BLOBSTORE_PROVIDER, SWIFT_BLOBSTORE_PROVIDER))
             .isInstanceOf(IllegalArgumentException.class);
     }
 
@@ -82,8 +86,8 @@ class BlobStoreChoosingModuleTest {
             .register("other_configuration_file", new 
PropertiesConfiguration())
             .build();
 
-        assertThat(module.provideBlobStoreFactory(propertyProvider, () -> 
CASSANDRA_BLOBSTORE_FACTORY, () -> SWIFT_BLOBSTORE_FACTORY))
-            .isEqualTo(CASSANDRA_BLOBSTORE_FACTORY);
+        assertThat(module.provideBlobStore(propertyProvider, 
CASSANDRA_BLOBSTORE_PROVIDER, SWIFT_BLOBSTORE_PROVIDER))
+            .isEqualTo(CASSANDRA_BLOBSTORE);
     }
 
     @Test
@@ -95,8 +99,8 @@ class BlobStoreChoosingModuleTest {
             .register(BLOBSTORE_CONFIGURATION_NAME, configuration)
             .build();
 
-        assertThat(module.provideBlobStoreFactory(propertyProvider, () -> 
CASSANDRA_BLOBSTORE_FACTORY, () -> SWIFT_BLOBSTORE_FACTORY))
-            .isEqualTo(SWIFT_BLOBSTORE_FACTORY);
+        assertThat(module.provideBlobStore(propertyProvider, 
CASSANDRA_BLOBSTORE_PROVIDER, SWIFT_BLOBSTORE_PROVIDER))
+            .isEqualTo(SWIFT_BLOBSTORE);
     }
 
     @Test
@@ -108,7 +112,7 @@ class BlobStoreChoosingModuleTest {
             .register(BLOBSTORE_CONFIGURATION_NAME, configuration)
             .build();
 
-        assertThat(module.provideBlobStoreFactory(propertyProvider, () -> 
CASSANDRA_BLOBSTORE_FACTORY, () -> SWIFT_BLOBSTORE_FACTORY))
-            .isEqualTo(CASSANDRA_BLOBSTORE_FACTORY);
+        assertThat(module.provideBlobStore(propertyProvider, 
CASSANDRA_BLOBSTORE_PROVIDER, SWIFT_BLOBSTORE_PROVIDER))
+            .isEqualTo(CASSANDRA_BLOBSTORE);
     }
 }
\ No newline at end of file


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org
For additional commands, e-mail: server-dev-h...@james.apache.org

Reply via email to