james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From btell...@apache.org
Subject [4/6] james-project git commit: JAMES-2441 Get ride of UnionConfigurationProvider to inject LDAP host
Date Tue, 03 Jul 2018 02:48:22 GMT
JAMES-2441 Get ride of UnionConfigurationProvider to inject LDAP host

We rather should inject the POJO first!


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

Branch: refs/heads/master
Commit: 6430724b34d5c3a8e0d0ead454cc508a6e640051
Parents: fcd09ee
Author: benwa <btellier@linagora.com>
Authored: Wed Jun 27 11:06:22 2018 +0700
Committer: benwa <btellier@linagora.com>
Committed: Tue Jul 3 09:47:14 2018 +0700

----------------------------------------------------------------------
 .../james/data/LdapUsersRepositoryModule.java   |  18 ++-
 .../apache/james/CassandraLdapJmapTestRule.java |  70 ++++------
 .../user/ldap/LdapRepositoryConfiguration.java  | 130 ++++++++++++++++++-
 3 files changed, 164 insertions(+), 54 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/6430724b/server/container/guice/cassandra-ldap-guice/src/main/java/org/apache/james/data/LdapUsersRepositoryModule.java
----------------------------------------------------------------------
diff --git a/server/container/guice/cassandra-ldap-guice/src/main/java/org/apache/james/data/LdapUsersRepositoryModule.java
b/server/container/guice/cassandra-ldap-guice/src/main/java/org/apache/james/data/LdapUsersRepositoryModule.java
index 6fcab3c..cab8827 100644
--- a/server/container/guice/cassandra-ldap-guice/src/main/java/org/apache/james/data/LdapUsersRepositoryModule.java
+++ b/server/container/guice/cassandra-ldap-guice/src/main/java/org/apache/james/data/LdapUsersRepositoryModule.java
@@ -20,15 +20,18 @@ package org.apache.james.data;
 
 import java.util.List;
 
+import org.apache.commons.configuration.ConfigurationException;
 import org.apache.james.lifecycle.api.Configurable;
 import org.apache.james.server.core.configuration.ConfigurationProvider;
 import org.apache.james.user.api.UsersRepository;
+import org.apache.james.user.ldap.LdapRepositoryConfiguration;
 import org.apache.james.user.ldap.ReadOnlyUsersLDAPRepository;
 import org.apache.james.utils.ConfigurationPerformer;
 
 import com.google.common.collect.ImmutableList;
 import com.google.inject.AbstractModule;
 import com.google.inject.Inject;
+import com.google.inject.Provides;
 import com.google.inject.Scopes;
 import com.google.inject.Singleton;
 import com.google.inject.multibindings.Multibinder;
@@ -43,22 +46,29 @@ public class LdapUsersRepositoryModule extends AbstractModule {
         Multibinder.newSetBinder(binder(), ConfigurationPerformer.class).addBinding().to(LdapUsersRepositoryConfigurationPerformer.class);
     }
 
+    @Provides
+    @Singleton
+    public LdapRepositoryConfiguration provideConfiguration(ConfigurationProvider configurationProvider)
throws ConfigurationException {
+        return LdapRepositoryConfiguration.from(
+            configurationProvider.getConfiguration("usersrepository"));
+    }
+
     @Singleton
     public static class LdapUsersRepositoryConfigurationPerformer implements ConfigurationPerformer
{
 
-        private final ConfigurationProvider configurationProvider;
+        private final LdapRepositoryConfiguration configuration;
         private final ReadOnlyUsersLDAPRepository usersRepository;
 
         @Inject
-        public LdapUsersRepositoryConfigurationPerformer(ConfigurationProvider configurationProvider,
ReadOnlyUsersLDAPRepository usersRepository) {
-            this.configurationProvider = configurationProvider;
+        public LdapUsersRepositoryConfigurationPerformer(LdapRepositoryConfiguration configuration,
ReadOnlyUsersLDAPRepository usersRepository) {
+            this.configuration = configuration;
             this.usersRepository = usersRepository;
         }
 
         @Override
         public void initModule() {
             try {
-                usersRepository.configure(configurationProvider.getConfiguration("usersrepository"));
+                usersRepository.configure(configuration);
                 usersRepository.init();
             } catch (Exception e) {
                 throw new RuntimeException(e);

http://git-wip-us.apache.org/repos/asf/james-project/blob/6430724b/server/container/guice/cassandra-ldap-guice/src/test/java/org/apache/james/CassandraLdapJmapTestRule.java
----------------------------------------------------------------------
diff --git a/server/container/guice/cassandra-ldap-guice/src/test/java/org/apache/james/CassandraLdapJmapTestRule.java
b/server/container/guice/cassandra-ldap-guice/src/test/java/org/apache/james/CassandraLdapJmapTestRule.java
index 3a81da6..b4b8fd0 100644
--- a/server/container/guice/cassandra-ldap-guice/src/test/java/org/apache/james/CassandraLdapJmapTestRule.java
+++ b/server/container/guice/cassandra-ldap-guice/src/test/java/org/apache/james/CassandraLdapJmapTestRule.java
@@ -21,22 +21,16 @@ package org.apache.james;
 
 import java.io.IOException;
 
-import org.apache.commons.configuration.HierarchicalConfiguration;
-import org.apache.commons.configuration.plist.PropertyListConfiguration;
-import org.apache.james.http.jetty.ConfigurationException;
+import org.apache.commons.configuration.ConfigurationException;
 import org.apache.james.modules.TestJMAPServerModule;
 import org.apache.james.server.core.configuration.Configuration;
-import org.apache.james.server.core.configuration.ConfigurationProvider;
-import org.apache.james.server.core.configuration.FileConfigurationProvider;
+import org.apache.james.user.ldap.LdapRepositoryConfiguration;
 import org.junit.rules.TemporaryFolder;
 import org.junit.rules.TestRule;
 import org.junit.runner.Description;
 import org.junit.runners.model.Statement;
 
-import com.google.inject.Inject;
 import com.google.inject.Module;
-import com.google.inject.name.Named;
-import com.google.inject.name.Names;
 
 public class CassandraLdapJmapTestRule implements TestRule {
     private static final int LIMIT_TO_3_MESSAGES = 3;
@@ -65,12 +59,31 @@ public class CassandraLdapJmapTestRule implements TestRule {
             .build();
 
         return new GuiceJamesServer(configuration)
-            .combineWith(CassandraLdapJamesServerMain.cassandraLdapServerModule,
-                binder -> binder.bind(String.class).annotatedWith(Names.named("ldapIp")).toInstance(ldapIp))
+            .combineWith(CassandraLdapJamesServerMain.cassandraLdapServerModule)
             .overrideWith(new TestJMAPServerModule(LIMIT_TO_3_MESSAGES))
             .overrideWith(guiceModuleTestRule.getModule())
             .overrideWith(additionals)
-            .overrideWith(binder -> binder.bind(ConfigurationProvider.class).to(UnionConfigurationProvider.class));
+            .overrideWith(binder -> binder.bind(LdapRepositoryConfiguration.class)
+                .toInstance(computeConfiguration(ldapIp)));
+    }
+
+    private LdapRepositoryConfiguration computeConfiguration(String ldapIp) {
+        try {
+            return LdapRepositoryConfiguration.builder()
+                .ldapHost(ldapIp)
+                .principal("cn=admin,dc=james,dc=org")
+                .credentials("mysecretpassword")
+                .userBase("ou=People,dc=james,dc=org")
+                .userIdAttribute("uid")
+                .userObjectClass("inetOrgPerson")
+                .maxRetries(4)
+                .retryStartInterval(0)
+                .retryMaxInterval(8)
+                .scale(1000)
+                .build();
+        } catch (ConfigurationException e) {
+            throw new RuntimeException(e);
+        }
     }
 
     @Override
@@ -81,39 +94,4 @@ public class CassandraLdapJmapTestRule implements TestRule {
     public void await() {
         guiceModuleTestRule.await();
     }
-
-    private static class UnionConfigurationProvider implements ConfigurationProvider {
-        private final FileConfigurationProvider fileConfigurationProvider;
-        private final String ldapIp;
-
-        @Inject
-        public UnionConfigurationProvider(FileConfigurationProvider fileConfigurationProvider,
-            @Named("ldapIp") String ldapIp) {
-            this.fileConfigurationProvider = fileConfigurationProvider;
-            this.ldapIp = ldapIp;
-        }
-
-        @Override
-        public HierarchicalConfiguration getConfiguration(String component) throws org.apache.commons.configuration.ConfigurationException
{
-            if (component.equals("usersrepository")) {
-                return ldapRepositoryConfiguration();
-            }
-            return fileConfigurationProvider.getConfiguration(component);
-        }
-
-        private HierarchicalConfiguration ldapRepositoryConfiguration() throws ConfigurationException
{
-            PropertyListConfiguration configuration = new PropertyListConfiguration();
-            configuration.addProperty("[@ldapHost]", ldapIp);
-            configuration.addProperty("[@principal]", "cn=admin\\,dc=james\\,dc=org");
-            configuration.addProperty("[@credentials]", "mysecretpassword");
-            configuration.addProperty("[@userBase]", "ou=People\\,dc=james\\,dc=org");
-            configuration.addProperty("[@userIdAttribute]", "uid");
-            configuration.addProperty("[@userObjectClass]", "inetOrgPerson");
-            configuration.addProperty("[@maxRetries]", "4");
-            configuration.addProperty("[@retryStartInterval]", "0");
-            configuration.addProperty("[@retryMaxInterval]", "8");
-            configuration.addProperty("[@retryIntervalScale]", "1000");
-            return configuration;
-        }
-    }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/6430724b/server/data/data-ldap/src/main/java/org/apache/james/user/ldap/LdapRepositoryConfiguration.java
----------------------------------------------------------------------
diff --git a/server/data/data-ldap/src/main/java/org/apache/james/user/ldap/LdapRepositoryConfiguration.java
b/server/data/data-ldap/src/main/java/org/apache/james/user/ldap/LdapRepositoryConfiguration.java
index fe230f0..9546ec4 100644
--- a/server/data/data-ldap/src/main/java/org/apache/james/user/ldap/LdapRepositoryConfiguration.java
+++ b/server/data/data-ldap/src/main/java/org/apache/james/user/ldap/LdapRepositoryConfiguration.java
@@ -25,9 +25,131 @@ import java.util.Optional;
 import org.apache.commons.configuration.ConfigurationException;
 import org.apache.commons.configuration.HierarchicalConfiguration;
 
+import com.google.common.base.Preconditions;
+
 public class LdapRepositoryConfiguration {
     public static final String SUPPORTS_VIRTUAL_HOSTING = "supportsVirtualHosting";
 
+    private static final boolean USE_CONNECTION_POOL = true;
+    private static final int NO_CONNECTION_TIMEOUT = -1;
+    private static final int NO_READ_TIME_OUT = -1;
+    private static final boolean ENABLE_VIRTUAL_HOSTING = true;
+    private static final ReadOnlyLDAPGroupRestriction NO_RESTRICTION = new ReadOnlyLDAPGroupRestriction(null);
+    private static final String NO_FILTER = null;
+    private static final Optional<String> NO_ADMINISTRATOR_ID = Optional.empty();
+
+    public static class Builder {
+        private Optional<String> ldapHost;
+        private Optional<String> principal;
+        private Optional<String> credentials;
+        private Optional<String> userBase;
+        private Optional<String> userIdAttribute;
+        private Optional<String> userObjectClass;
+        private Optional<Integer> maxRetries;
+        private Optional<Long> retryStartInterval;
+        private Optional<Long> retryMaxInterval;
+        private Optional<Integer> scale;
+
+        public Builder() {
+            ldapHost = Optional.empty();
+            principal = Optional.empty();
+            credentials = Optional.empty();
+            userBase = Optional.empty();
+            userIdAttribute = Optional.empty();
+            userObjectClass = Optional.empty();
+            maxRetries = Optional.empty();
+            retryStartInterval = Optional.empty();
+            retryMaxInterval = Optional.empty();
+            scale = Optional.empty();
+        }
+
+        public Builder ldapHost(String ldapHost) {
+            this.ldapHost = Optional.of(ldapHost);
+            return this;
+        }
+
+        public Builder principal(String principal) {
+            this.principal = Optional.of(principal);
+            return this;
+        }
+
+        public Builder credentials(String credentials) {
+            this.credentials = Optional.of(credentials);
+            return this;
+        }
+
+        public Builder userBase(String userBase) {
+            this.userBase = Optional.of(userBase);
+            return this;
+        }
+
+        public Builder userIdAttribute(String userIdAttribute) {
+            this.userIdAttribute = Optional.of(userIdAttribute);
+            return this;
+        }
+
+        public Builder userObjectClass(String userObjectClass) {
+            this.userObjectClass = Optional.of(userObjectClass);
+            return this;
+        }
+
+        public Builder maxRetries(int maxRetries) {
+            this.maxRetries = Optional.of(maxRetries);
+            return this;
+        }
+
+        public Builder retryStartInterval(long retryStartInterval) {
+            this.retryStartInterval = Optional.of(retryStartInterval);
+            return this;
+        }
+
+        public Builder retryMaxInterval(long retryMaxInterval) {
+            this.retryMaxInterval = Optional.of(retryMaxInterval);
+            return this;
+        }
+
+        public Builder scale(int scale) {
+            this.scale = Optional.of(scale);
+            return this;
+        }
+
+        public LdapRepositoryConfiguration build() throws ConfigurationException {
+            Preconditions.checkState(ldapHost.isPresent(), "'ldapHost' is mandatory");
+            Preconditions.checkState(principal.isPresent(), "'principal' is mandatory");
+            Preconditions.checkState(credentials.isPresent(), "'credentials' is mandatory");
+            Preconditions.checkState(userBase.isPresent(), "'userBase' is mandatory");
+            Preconditions.checkState(userIdAttribute.isPresent(), "'userIdAttribute' is mandatory");
+            Preconditions.checkState(userObjectClass.isPresent(), "'userObjectClass' is mandatory");
+            Preconditions.checkState(maxRetries.isPresent(), "'maxRetries' is mandatory");
+            Preconditions.checkState(retryStartInterval.isPresent(), "'retryStartInterval'
is mandatory");
+            Preconditions.checkState(retryMaxInterval.isPresent(), "'retryMaxInterval' is
mandatory");
+            Preconditions.checkState(scale.isPresent(), "'scale' is mandatory");
+
+            return new LdapRepositoryConfiguration(
+                ldapHost.get(),
+                principal.get(),
+                credentials.get(),
+                userBase.get(),
+                userIdAttribute.get(),
+                userObjectClass.get(),
+                USE_CONNECTION_POOL,
+                NO_CONNECTION_TIMEOUT,
+                NO_READ_TIME_OUT,
+                maxRetries.get(),
+                !ENABLE_VIRTUAL_HOSTING,
+                retryStartInterval.get(),
+                retryMaxInterval.get(),
+                scale.get(),
+                NO_RESTRICTION,
+                NO_FILTER,
+                NO_ADMINISTRATOR_ID);
+        }
+    }
+
+    public static Builder builder() {
+        return new Builder();
+    }
+
     public static LdapRepositoryConfiguration from(HierarchicalConfiguration configuration)
throws ConfigurationException {
         String ldapHost = configuration.getString("[@ldapHost]", "");
         String principal = configuration.getString("[@principal]", "");
@@ -36,13 +158,13 @@ public class LdapRepositoryConfiguration {
         String userIdAttribute = configuration.getString("[@userIdAttribute]");
         String userObjectClass = configuration.getString("[@userObjectClass]");
         // Default is to use connection pooling
-        boolean useConnectionPool = configuration.getBoolean("[@useConnectionPool]", true);
-        int connectionTimeout = configuration.getInt("[@connectionTimeout]", -1);
-        int readTimeout = configuration.getInt("[@readTimeout]", -1);
+        boolean useConnectionPool = configuration.getBoolean("[@useConnectionPool]", USE_CONNECTION_POOL);
+        int connectionTimeout = configuration.getInt("[@connectionTimeout]", NO_CONNECTION_TIMEOUT);
+        int readTimeout = configuration.getInt("[@readTimeout]", NO_READ_TIME_OUT);
         // Default maximum retries is 1, which allows an alternate connection to
         // be found in a multi-homed environment
         int maxRetries = configuration.getInt("[@maxRetries]", 1);
-        boolean supportsVirtualHosting = configuration.getBoolean(SUPPORTS_VIRTUAL_HOSTING,
false);
+        boolean supportsVirtualHosting = configuration.getBoolean(SUPPORTS_VIRTUAL_HOSTING,
!ENABLE_VIRTUAL_HOSTING);
         // Default retry start interval is 0 second
         long retryStartInterval = configuration.getLong("[@retryStartInterval]", 0);
         // Default maximum retry interval is 60 seconds


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