james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From btell...@apache.org
Subject [3/5] james-project git commit: JAMES-1944 transform ClusterFactory into a Builder
Date Thu, 02 Mar 2017 11:21:03 GMT
JAMES-1944 transform ClusterFactory into a Builder


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

Branch: refs/heads/master
Commit: 4eb8aaf4017b38e07bc970591cc7683b120bee1d
Parents: afe1558
Author: Luc DUZAN <lduzan@linagora.com>
Authored: Thu Feb 16 18:17:44 2017 +0100
Committer: benwa <btellier@linagora.com>
Committed: Thu Mar 2 18:20:35 2017 +0700

----------------------------------------------------------------------
 .../backends/cassandra/init/ClusterBuilder.java | 155 +++++++++++++++++++
 .../backends/cassandra/init/ClusterFactory.java |  72 ---------
 .../resources/META-INF/cassandra-session.xml    |   2 +-
 .../backends/cassandra/CassandraCluster.java    |   7 +-
 .../modules/mailbox/CassandraSessionModule.java |   8 +-
 5 files changed, 166 insertions(+), 78 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/4eb8aaf4/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/init/ClusterBuilder.java
----------------------------------------------------------------------
diff --git a/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/init/ClusterBuilder.java
b/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/init/ClusterBuilder.java
new file mode 100644
index 0000000..3761da1
--- /dev/null
+++ b/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/init/ClusterBuilder.java
@@ -0,0 +1,155 @@
+/****************************************************************
+ * 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.backends.cassandra.init;
+
+import java.util.Collection;
+import java.util.Optional;
+
+import org.apache.james.util.Host;
+
+import com.datastax.driver.core.Cluster;
+import com.datastax.driver.core.QueryOptions;
+import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableList;
+
+public class ClusterBuilder {
+
+    private final static String DEFAULT_CLUSTER_IP = "localhost";
+    public static final int DEFAULT_CASSANDRA_PORT = 9042;
+
+    public static ClusterBuilder builder() {
+        return new ClusterBuilder();
+    }
+
+
+    private Optional<String> username;
+    private Optional<String> password;
+
+    private Optional<String> host;
+    private Optional<Integer> port;
+    private Optional<Collection<Host>> servers;
+
+    private Optional<Integer> refreshSchemaIntervalMillis;
+    private boolean forTest;
+
+    private ClusterBuilder() {
+        username = Optional.empty();
+        password = Optional.empty();
+
+        host = Optional.empty();
+        port = Optional.empty();
+        servers = Optional.empty();
+
+        refreshSchemaIntervalMillis = Optional.empty();
+        forTest = false;
+    }
+
+    public ClusterBuilder username(String username) {
+        this.username = Optional.of(username);
+
+        return this;
+    }
+
+    public ClusterBuilder password(String password) {
+        this.password = Optional.of(password);
+
+        return this;
+    }
+
+    public ClusterBuilder host(String host) {
+        this.host = Optional.of(host);
+
+        return this;
+    }
+
+    public ClusterBuilder port(int port) {
+        this.port = Optional.of(port);
+
+        return this;
+    }
+
+    public ClusterBuilder refreshSchemaIntervalMillis(int refreshSchemaIntervalMillis) {
+        this.refreshSchemaIntervalMillis = Optional.of(refreshSchemaIntervalMillis);
+
+        return this;
+    }
+
+    public ClusterBuilder servers(Host... servers) {
+        this.servers = Optional.of(ImmutableList.copyOf(servers));
+
+        return this;
+    }
+
+    public ClusterBuilder servers(Collection<Host> servers) {
+        this.servers = Optional.of(servers);
+
+        return this;
+    }
+
+    public ClusterBuilder forTest() {
+        this.forTest = true;
+
+        return this;
+    }
+
+    public Cluster build() {
+        Preconditions.checkState(!(servers.isPresent() && host.isPresent()), "You
can't specify a list of servers and a host at the same time");
+        Preconditions.checkState(!(servers.isPresent() && port.isPresent()), "You
can't specify a list of servers and a port at the same time");
+        Preconditions.checkState(username.isPresent() == password.isPresent(), "If you specify
username, you must specify password");
+        Preconditions.checkState(forTest == refreshSchemaIntervalMillis.isPresent(), "You
can't specify refreshSchemaIntervalMillis for test");
+
+        Cluster.Builder clusterBuilder = Cluster.builder();
+        getServers().forEach(
+                (server) -> clusterBuilder.addContactPoint(server.getHostName()).withPort(server.getPort())
+        );
+
+        username.map(username ->
+            password.map(password ->
+                clusterBuilder.withCredentials(username, password)
+            )
+        );
+
+        getRefreshSchemaIntervalMillis().map(refreshSchemaIntervalMillis ->
+            clusterBuilder.withQueryOptions(
+                new QueryOptions()
+                    .setRefreshSchemaIntervalMillis(refreshSchemaIntervalMillis)
+            )
+        );
+
+        Cluster cluster = clusterBuilder.build();
+
+        return cluster;
+    }
+
+    private Optional<Integer> getRefreshSchemaIntervalMillis() {
+        return forTest ? Optional.of(0) : refreshSchemaIntervalMillis;
+    }
+
+    private Collection<Host> getServers() {
+        return servers.orElse(getServersFromHostAndPort());
+    }
+
+    private Collection<Host> getServersFromHostAndPort() {
+        String host = this.host.orElse(DEFAULT_CLUSTER_IP);
+        int port = this.port.orElse(DEFAULT_CASSANDRA_PORT);
+
+        return ImmutableList.of(Host.from(host, port));
+    }
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/4eb8aaf4/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/init/ClusterFactory.java
----------------------------------------------------------------------
diff --git a/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/init/ClusterFactory.java
b/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/init/ClusterFactory.java
deleted file mode 100644
index 56eeebc..0000000
--- a/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/init/ClusterFactory.java
+++ /dev/null
@@ -1,72 +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.backends.cassandra.init;
-
-import java.util.List;
-import java.util.Optional;
-
-import org.apache.james.util.Host;
-
-import com.datastax.driver.core.Cluster;
-import com.datastax.driver.core.QueryOptions;
-import com.google.common.base.Strings;
-import com.google.common.collect.ImmutableList;
-
-public class ClusterFactory {
-
-    private final static String DEFAULT_CLUSTER_IP = "localhost";
-    public static final int DEFAULT_CASSANDRA_PORT = 9042;
-
-    public static Cluster createClusterForClusterWithPassWord(List<Host> servers, String
userName, String password,
-                                                              Optional<Integer> refreshSchemaIntervalMillis)
{
-
-        Cluster.Builder clusterBuilder = Cluster.builder();
-        servers.forEach(
-            (server) -> clusterBuilder.addContactPoint(server.getHostName()).withPort(server.getPort())
-        );
-        if(!Strings.isNullOrEmpty(userName) && !Strings.isNullOrEmpty(password))
{
-            clusterBuilder.withCredentials(userName, password);
-        }
-        if (refreshSchemaIntervalMillis.isPresent()) {
-            clusterBuilder.withQueryOptions(new QueryOptions().setRefreshSchemaIntervalMillis(refreshSchemaIntervalMillis.get()));
-        }
-        return clusterBuilder.build();
-    }
-
-    public static Cluster createClusterForClusterWithoutPassWord(List<Host> servers)
{
-        return createClusterForClusterWithPassWord(servers, null, null, Optional.empty());
-    }
-
-    public static Cluster createClusterForSingleServerWithPassWord(String ip, int port, String
userName, String password) {
-        return createClusterForClusterWithPassWord(ImmutableList.of(Host.from(ip, port)),
userName, password, Optional.empty());
-    }
-
-    public static Cluster createClusterForSingleServerWithoutPassWord(String ip, int port)
{
-        return createClusterForClusterWithPassWord(ImmutableList.of(Host.from(ip, port)),
null, null, Optional.empty());
-    }
-
-    public static Cluster createTestingCluster(String ip, int port) {
-        return createClusterForClusterWithPassWord(ImmutableList.of(Host.from(ip, port)),
null, null, Optional.of(0));
-    }
-
-    public static Cluster createDefaultSession() {
-        return createClusterForSingleServerWithoutPassWord(DEFAULT_CLUSTER_IP, DEFAULT_CASSANDRA_PORT);
-    }
-}

http://git-wip-us.apache.org/repos/asf/james-project/blob/4eb8aaf4/backends-common/cassandra/src/main/resources/META-INF/cassandra-session.xml
----------------------------------------------------------------------
diff --git a/backends-common/cassandra/src/main/resources/META-INF/cassandra-session.xml b/backends-common/cassandra/src/main/resources/META-INF/cassandra-session.xml
index e8bda8d..3fe26f0 100644
--- a/backends-common/cassandra/src/main/resources/META-INF/cassandra-session.xml
+++ b/backends-common/cassandra/src/main/resources/META-INF/cassandra-session.xml
@@ -109,7 +109,7 @@
     </bean>
 
     <bean id="cassandra-cluster"
-          class="org.apache.james.backends.cassandra.init.ClusterFactory"
+          class="org.apache.james.backends.cassandra.init.ClusterBuilder"
           factory-method="createClusterForSingleServerWithoutPassWord"
           lazy-init="true">
         <constructor-arg index="0" value="${cassandra.ip}"/>

http://git-wip-us.apache.org/repos/asf/james-project/blob/4eb8aaf4/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 fbf3e8e..6e987fb 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
@@ -26,7 +26,7 @@ import javax.inject.Inject;
 import org.apache.james.backends.cassandra.components.CassandraModule;
 import org.apache.james.backends.cassandra.init.CassandraTableManager;
 import org.apache.james.backends.cassandra.init.CassandraTypesProvider;
-import org.apache.james.backends.cassandra.init.ClusterFactory;
+import org.apache.james.backends.cassandra.init.ClusterBuilder;
 import org.apache.james.backends.cassandra.init.ClusterWithKeyspaceCreatedFactory;
 import org.apache.james.backends.cassandra.init.SessionWithInitializedTablesFactory;
 import org.apache.james.backends.cassandra.utils.FunctionRunnerWithRetry;
@@ -92,7 +92,10 @@ public final class CassandraCluster {
     }
 
     public Cluster getCluster() {
-        return ClusterFactory.createTestingCluster(CLUSTER_IP, CLUSTER_PORT_TEST);
+        return ClusterBuilder.builder()
+                .host(CLUSTER_IP)
+                .port(CLUSTER_PORT_TEST)
+                .build();
     }
 
     private void sleep(long sleepMs) {

http://git-wip-us.apache.org/repos/asf/james-project/blob/4eb8aaf4/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 640472f..a464bc7 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
@@ -30,7 +30,7 @@ import org.apache.commons.configuration.PropertiesConfiguration;
 import org.apache.james.backends.cassandra.components.CassandraModule;
 import org.apache.james.backends.cassandra.init.CassandraModuleComposite;
 import org.apache.james.backends.cassandra.init.CassandraZonedDateTimeModule;
-import org.apache.james.backends.cassandra.init.ClusterFactory;
+import org.apache.james.backends.cassandra.init.ClusterBuilder;
 import org.apache.james.backends.cassandra.init.ClusterWithKeyspaceCreatedFactory;
 import org.apache.james.backends.cassandra.init.SessionWithInitializedTablesFactory;
 import org.apache.james.filesystem.api.FileSystem;
@@ -88,7 +88,9 @@ public class CassandraSessionModule extends AbstractModule {
         return getRetryer(executor, configuration)
                 .getWithRetry(ctx -> ClusterWithKeyspaceCreatedFactory
                         .config(
-                            ClusterFactory.createClusterForClusterWithoutPassWord(servers),
+                            ClusterBuilder.builder()
+                                .servers(servers)
+                                .build(),
                             configuration.getString("cassandra.keyspace"))
                         .replicationFactor(configuration.getInt("cassandra.replication.factor"))
                         .clusterWithInitializedKeyspace())
@@ -99,7 +101,7 @@ public class CassandraSessionModule extends AbstractModule {
         String[] ipAndPorts = configuration.getStringArray("cassandra.nodes");
 
         return Arrays.stream(ipAndPorts)
-                .map(string -> Host.parseConfString(string, ClusterFactory.DEFAULT_CASSANDRA_PORT))
+                .map(string -> Host.parseConfString(string, ClusterBuilder.DEFAULT_CASSANDRA_PORT))
                 .collect(Guavate.toImmutableList());
     }
 


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


Mime
View raw message