JAMES-1999 ElasticSearchModule can bind configuration file

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

Branch: refs/heads/master
Commit: 5e1f5727f48bae035661892fdc256543e0da168c
Parents: bb68be1
Author: quynhn <qngu...@linagora.com>
Authored: Tue Apr 18 17:31:45 2017 +0700
Committer: benwa <btell...@linagora.com>
Committed: Fri Apr 21 07:52:23 2017 +0700

----------------------------------------------------------------------
 .../mailbox/ElasticSearchConfiguration.java     | 31 ++++++++++++++++++++
 .../mailbox/ElasticSearchMailboxModule.java     | 31 ++++++++++++++------
 2 files changed, 53 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/5e1f5727/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/mailbox/ElasticSearchConfiguration.java
----------------------------------------------------------------------
diff --git 
a/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/mailbox/ElasticSearchConfiguration.java
 
b/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/mailbox/ElasticSearchConfiguration.java
new file mode 100644
index 0000000..0c39e81
--- /dev/null
+++ 
b/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/mailbox/ElasticSearchConfiguration.java
@@ -0,0 +1,31 @@
+/****************************************************************
+ * 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.modules.mailbox;
+
+import org.apache.commons.configuration.ConfigurationException;
+import org.apache.commons.configuration.PropertiesConfiguration;
+
+import java.io.FileNotFoundException;
+
+public interface ElasticSearchConfiguration {
+
+    PropertiesConfiguration getConfiguration() throws FileNotFoundException, 
ConfigurationException;
+
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/5e1f5727/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/mailbox/ElasticSearchMailboxModule.java
----------------------------------------------------------------------
diff --git 
a/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/mailbox/ElasticSearchMailboxModule.java
 
b/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/mailbox/ElasticSearchMailboxModule.java
index 48c4145..e114938 100644
--- 
a/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/mailbox/ElasticSearchMailboxModule.java
+++ 
b/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/mailbox/ElasticSearchMailboxModule.java
@@ -27,7 +27,6 @@ import javax.inject.Singleton;
 
 import org.apache.commons.configuration.ConfigurationException;
 import org.apache.commons.configuration.PropertiesConfiguration;
-import org.apache.james.backends.es.ClientProvider;
 import org.apache.james.backends.es.ClientProviderImpl;
 import org.apache.james.backends.es.IndexCreationFactory;
 import org.apache.james.backends.es.IndexName;
@@ -79,8 +78,8 @@ public class ElasticSearchMailboxModule extends 
AbstractModule {
 
     @Provides
     @Singleton
-    protected Client provideClientProvider(FileSystem fileSystem, 
AsyncRetryExecutor executor) throws ConfigurationException, 
FileNotFoundException, ExecutionException, InterruptedException {
-        PropertiesConfiguration propertiesReader = new 
PropertiesConfiguration(fileSystem.getFile(ES_CONFIG_FILE));
+    protected Client provideClientProvider(ElasticSearchConfiguration 
elasticSearchConfiguration, AsyncRetryExecutor executor) throws 
ConfigurationException, FileNotFoundException, ExecutionException, 
InterruptedException {
+        PropertiesConfiguration propertiesReader = 
elasticSearchConfiguration.getConfiguration();
         int maxRetries = 
propertiesReader.getInt("elasticsearch.retryConnection.maxRetries", 
DEFAULT_CONNECTION_MAX_RETRIES);
         int minDelay = 
propertiesReader.getInt("elasticsearch.retryConnection.minDelay", 
DEFAULT_CONNECTION_MIN_DELAY);
 
@@ -89,9 +88,17 @@ public class ElasticSearchMailboxModule extends 
AbstractModule {
             .get();
     }
 
-    private Client createClientAndIndex(ClientProvider clientProvider, 
PropertiesConfiguration propertiesReader) {
-        LOGGER.info("Trying to connect to ElasticSearch service");
-        Client client = clientProvider.get();
+    @Provides
+    @Singleton
+    ElasticSearchConfiguration getElasticSearchConfiguration(FileSystem 
fileSystem) {
+        return () -> getConfiguration(fileSystem);
+    }
+
+    private PropertiesConfiguration getConfiguration(FileSystem fileSystem) 
throws FileNotFoundException, ConfigurationException {
+        return new PropertiesConfiguration(fileSystem.getFile(ES_CONFIG_FILE));
+    }
+
+    private Client createIndexAndMapping(Client client, 
PropertiesConfiguration propertiesReader) {
         IndexCreationFactory.createIndex(client,
             MailboxElasticsearchConstants.MAILBOX_INDEX,
             propertiesReader.getInt("elasticsearch.nb.shards"),
@@ -103,7 +110,13 @@ public class ElasticSearchMailboxModule extends 
AbstractModule {
         return client;
     }
 
-    private static ClientProvider connectToCluster(PropertiesConfiguration 
propertiesReader) throws ConfigurationException {
+    private Client connectToCluster(PropertiesConfiguration propertiesReader) 
throws ConfigurationException {
+        LOGGER.info("Trying to connect to ElasticSearch service");
+
+        return createIndexAndMapping(createClient(propertiesReader), 
propertiesReader);
+    }
+
+    private Client createClient(PropertiesConfiguration propertiesReader) 
throws ConfigurationException {
         Optional<String> monoHostAddress = 
Optional.ofNullable(propertiesReader.getString(ELASTICSEARCH_MASTER_HOST, 
null));
         Optional<Integer> monoHostPort = 
Optional.ofNullable(propertiesReader.getInteger(ELASTICSEARCH_PORT, null));
         Optional<String> multiHosts = 
Optional.ofNullable(propertiesReader.getString(ELASTICSEARCH_HOSTS, null));
@@ -111,9 +124,9 @@ public class ElasticSearchMailboxModule extends 
AbstractModule {
         validateHostsConfigurationOptions(monoHostAddress, monoHostPort, 
multiHosts);
 
         if (monoHostAddress.isPresent()) {
-            return ClientProviderImpl.forHost(monoHostAddress.get(), 
monoHostPort.get());
+            return ClientProviderImpl.forHost(monoHostAddress.get(), 
monoHostPort.get()).get();
         } else {
-            return ClientProviderImpl.fromHostsString(multiHosts.get());
+            return ClientProviderImpl.fromHostsString(multiHosts.get()).get();
         }
     }
 


---------------------------------------------------------------------
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