JAMES-1772 Make something like a builder in ClusterWithKeyspaceCreatedFactory


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

Branch: refs/heads/master
Commit: 68ddd58d2155e2ae26a2c1b0b58738a2e60d1053
Parents: 1590e60
Author: Raphael Ouazana <[email protected]>
Authored: Tue Jun 21 11:57:17 2016 +0200
Committer: Raphael Ouazana <[email protected]>
Committed: Tue Jun 21 11:57:17 2016 +0200

----------------------------------------------------------------------
 .../init/ClusterWithKeyspaceCreatedFactory.java | 48 +++++++++++++-------
 .../backends/cassandra/CassandraCluster.java    |  5 +-
 .../modules/mailbox/CassandraSessionModule.java | 14 +++---
 3 files changed, 44 insertions(+), 23 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/68ddd58d/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/init/ClusterWithKeyspaceCreatedFactory.java
----------------------------------------------------------------------
diff --git 
a/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/init/ClusterWithKeyspaceCreatedFactory.java
 
b/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/init/ClusterWithKeyspaceCreatedFactory.java
index f56a3af..5266c35 100644
--- 
a/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/init/ClusterWithKeyspaceCreatedFactory.java
+++ 
b/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/init/ClusterWithKeyspaceCreatedFactory.java
@@ -25,24 +25,40 @@ import com.datastax.driver.core.Session;
 public class ClusterWithKeyspaceCreatedFactory {
 
     private final static int DEFAULT_REPLICATION_FACTOR = 1;
-
-    public static Cluster 
clusterWithInitializedKeyspaceWithoutDurableWrites(Cluster cluster, String 
keyspace, int replicationFactor) {
-        return clusterWithInitializedKeyspace(cluster, keyspace, 
replicationFactor, false);
-    }
-
-    public static Cluster clusterWithInitializedKeyspace(Cluster cluster, 
String keyspace, int replicationFactor) {
-        return clusterWithInitializedKeyspace(cluster, keyspace, 
replicationFactor, true);
+    
+    public static Configuration config(Cluster cluster, String keyspace) {
+        return new Configuration(cluster, keyspace);
     }
-
-    private static Cluster clusterWithInitializedKeyspace(Cluster cluster, 
String keyspace, int replicationFactor, boolean durableWrites) {
-        if (isKeyspacePresent(cluster, keyspace)) {
-            createKeyspace(cluster, keyspace, replicationFactor, 
durableWrites);
+    
+    public static class Configuration {
+        private Cluster cluster;
+        private String keyspace;
+        private boolean durableWrites;
+        private int replicationFactor;
+        
+        private Configuration(Cluster cluster, String keyspace) {
+            this.cluster = cluster;
+            this.keyspace = keyspace;
+            this.durableWrites = true;
+            this.replicationFactor = DEFAULT_REPLICATION_FACTOR;
+        }
+        
+        public Configuration disableDurableWrites() {
+            this.durableWrites = false;
+            return this;
+        }
+        
+        public Configuration replicationFactor(int replicationFactor) {
+            this.replicationFactor = replicationFactor;
+            return this;
+        }
+        
+        public Cluster clusterWithInitializedKeyspace() {
+            if (isKeyspacePresent(cluster, keyspace)) {
+                createKeyspace(cluster, keyspace, replicationFactor, 
durableWrites);
+            }
+            return cluster;
         }
-        return cluster;
-    }
-
-    public static Cluster clusterWithInitializedKeyspace(Cluster cluster, 
String keyspace) {
-        return clusterWithInitializedKeyspace(cluster, keyspace, 
DEFAULT_REPLICATION_FACTOR);
     }
 
     private static boolean isKeyspacePresent(Cluster cluster, String keyspace) 
{

http://git-wip-us.apache.org/repos/asf/james-project/blob/68ddd58d/backends-common/cassandra/src/test/java/org/apache/james/backends/cassandra/CassandraCluster.java
----------------------------------------------------------------------
diff --git 
a/backends-common/cassandra/src/test/java/org/apache/james/backends/cassandra/CassandraCluster.java
 
b/backends-common/cassandra/src/test/java/org/apache/james/backends/cassandra/CassandraCluster.java
index b5bc160..fbf3e8e 100644
--- 
a/backends-common/cassandra/src/test/java/org/apache/james/backends/cassandra/CassandraCluster.java
+++ 
b/backends-common/cassandra/src/test/java/org/apache/james/backends/cassandra/CassandraCluster.java
@@ -80,7 +80,10 @@ public final class CassandraCluster {
     private Optional<Session> tryInitializeSession() {
         try {
             Cluster clusterWithInitializedKeyspace = 
ClusterWithKeyspaceCreatedFactory
-                
.clusterWithInitializedKeyspaceWithoutDurableWrites(getCluster(), 
KEYSPACE_NAME, REPLICATION_FACTOR);
+                .config(getCluster(), KEYSPACE_NAME)
+                .replicationFactor(REPLICATION_FACTOR)
+                .disableDurableWrites()
+                .clusterWithInitializedKeyspace();
             return Optional.of(new 
SessionWithInitializedTablesFactory(module).createSession(clusterWithInitializedKeyspace,
 KEYSPACE_NAME));
         } catch (NoHostAvailableException exception) {
             sleep(SLEEP_BEFORE_RETRY);

http://git-wip-us.apache.org/repos/asf/james-project/blob/68ddd58d/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/mailbox/CassandraSessionModule.java
----------------------------------------------------------------------
diff --git 
a/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/mailbox/CassandraSessionModule.java
 
b/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/mailbox/CassandraSessionModule.java
index fe234b6..716b95f 100644
--- 
a/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/mailbox/CassandraSessionModule.java
+++ 
b/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/mailbox/CassandraSessionModule.java
@@ -76,12 +76,14 @@ public class CassandraSessionModule extends AbstractModule {
         PropertiesConfiguration configuration = getConfiguration(fileSystem);
 
         return getRetryer(executor, configuration)
-                .getWithRetry(ctx -> 
ClusterWithKeyspaceCreatedFactory.clusterWithInitializedKeyspace(
-                        
ClusterFactory.createClusterForSingleServerWithoutPassWord(
-                                configuration.getString("cassandra.ip"),
-                                configuration.getInt("cassandra.port")),
-                        configuration.getString("cassandra.keyspace"),
-                        configuration.getInt("cassandra.replication.factor")))
+                .getWithRetry(ctx -> ClusterWithKeyspaceCreatedFactory
+                        .config(
+                            
ClusterFactory.createClusterForSingleServerWithoutPassWord(
+                                    configuration.getString("cassandra.ip"),
+                                    configuration.getInt("cassandra.port")),
+                            configuration.getString("cassandra.keyspace"))
+                        
.replicationFactor(configuration.getInt("cassandra.replication.factor"))
+                        .clusterWithInitializedKeyspace())
                 .get();
     }
 


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

Reply via email to