james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From matth...@apache.org
Subject svn commit: r1711978 - in /james/project/trunk/server: data/data-api/src/main/java/org/apache/james/rrt/lib/ data/data-file/src/test/java/org/apache/james/rrt/file/ data/data-hbase/src/main/java/org/apache/james/rrt/hbase/ data/data-jdbc/src/main/java/...
Date Mon, 02 Nov 2015 13:44:40 GMT
Author: matthieu
Date: Mon Nov  2 13:44:40 2015
New Revision: 1711978

URL: http://svn.apache.org/viewvc?rev=1711978&view=rev
Log:
JAMES-1595 create a Builder to handle add and addAll on Mappings

Modified:
    james/project/trunk/server/data/data-api/src/main/java/org/apache/james/rrt/lib/Mappings.java
    james/project/trunk/server/data/data-file/src/test/java/org/apache/james/rrt/file/XMLRecipientRewriteTableTest.java
    james/project/trunk/server/data/data-hbase/src/main/java/org/apache/james/rrt/hbase/HBaseRecipientRewriteTable.java
    james/project/trunk/server/data/data-jdbc/src/main/java/org/apache/james/rrt/jdbc/JDBCRecipientRewriteTable.java
    james/project/trunk/server/data/data-jpa/src/main/java/org/apache/james/rrt/jpa/JPARecipientRewriteTable.java
    james/project/trunk/server/data/data-library/src/main/java/org/apache/james/rrt/lib/AbstractRecipientRewriteTable.java
    james/project/trunk/server/data/data-library/src/main/java/org/apache/james/rrt/lib/MappingsImpl.java
    james/project/trunk/server/data/data-library/src/main/java/org/apache/james/user/lib/AbstractJamesUsersRepository.java
    james/project/trunk/server/data/data-library/src/test/java/org/apache/james/rrt/lib/mock/MockRecipientRewriteTableManagementImpl.java
    james/project/trunk/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/ValidRcptHandlerTest.java

Modified: james/project/trunk/server/data/data-api/src/main/java/org/apache/james/rrt/lib/Mappings.java
URL: http://svn.apache.org/viewvc/james/project/trunk/server/data/data-api/src/main/java/org/apache/james/rrt/lib/Mappings.java?rev=1711978&r1=1711977&r2=1711978&view=diff
==============================================================================
--- james/project/trunk/server/data/data-api/src/main/java/org/apache/james/rrt/lib/Mappings.java
(original)
+++ james/project/trunk/server/data/data-api/src/main/java/org/apache/james/rrt/lib/Mappings.java
Mon Nov  2 13:44:40 2015
@@ -28,8 +28,6 @@ public interface Mappings extends Iterab
     
     Mappings addAll(Mappings toAdd);
 
-    void add(String mapping);
-
     boolean contains(String mapping);
 
     int size();

Modified: james/project/trunk/server/data/data-file/src/test/java/org/apache/james/rrt/file/XMLRecipientRewriteTableTest.java
URL: http://svn.apache.org/viewvc/james/project/trunk/server/data/data-file/src/test/java/org/apache/james/rrt/file/XMLRecipientRewriteTableTest.java?rev=1711978&r1=1711977&r2=1711978&view=diff
==============================================================================
--- james/project/trunk/server/data/data-file/src/test/java/org/apache/james/rrt/file/XMLRecipientRewriteTableTest.java
(original)
+++ james/project/trunk/server/data/data-file/src/test/java/org/apache/james/rrt/file/XMLRecipientRewriteTableTest.java
Mon Nov  2 13:44:40 2015
@@ -28,10 +28,13 @@ import org.apache.james.rrt.lib.Abstract
 import org.apache.james.rrt.lib.AbstractRecipientRewriteTableTest;
 import org.apache.james.rrt.lib.Mappings;
 import org.apache.james.rrt.lib.MappingsImpl;
+import org.apache.james.rrt.lib.MappingsImpl.Builder;
 import org.apache.james.rrt.lib.RecipientRewriteTableUtil;
 import org.junit.Before;
 import org.slf4j.LoggerFactory;
 
+import com.google.common.base.Optional;
+
 /**
  * Test the XML Virtual User Table implementation.
  */
@@ -59,31 +62,33 @@ public class XMLRecipientRewriteTableTes
 
         Mappings mappings = virtualUserTable.getUserDomainMappings(user, domain);
 
-        if (mappings == null) {
-            mappings = MappingsImpl.empty();
-        } else {
+        if (mappings != null) {
             removeMappingsFromConfig(user, domain, mappings);
         }
 
+        Builder builder = MappingsImpl.from(Optional.fromNullable(mappings).or(MappingsImpl.empty()));
+        
         if (type == ERROR_TYPE) {
-            mappings.add(RecipientRewriteTable.ERROR_PREFIX + mapping);
+            builder.add(RecipientRewriteTable.ERROR_PREFIX + mapping);
         } else if (type == REGEX_TYPE) {
-            mappings.add(RecipientRewriteTable.REGEX_PREFIX + mapping);
+            builder.add(RecipientRewriteTable.REGEX_PREFIX + mapping);
         } else if (type == ADDRESS_TYPE) {
-            mappings.add(mapping);
+            builder.add(mapping);
         } else if (type == ALIASDOMAIN_TYPE) {
-            mappings.add(RecipientRewriteTable.ALIASDOMAIN_PREFIX + mapping);
+            builder.add(RecipientRewriteTable.ALIASDOMAIN_PREFIX + mapping);
         }
 
-        if (mappings.size() > 0) {
+        Mappings updatedMappings = builder.build();
+        
+        if (!updatedMappings.isEmpty()) {
             defaultConfiguration.addProperty("mapping", user + "@" + domain + "=" + RecipientRewriteTableUtil.
-                    CollectionToMapping(mappings));
+                    CollectionToMapping(updatedMappings));
         }
 
         try {
             virtualUserTable.configure(defaultConfiguration);
         } catch (Exception e) {
-            return mappings.size() <= 0;
+            return updatedMappings.size() <= 0;
         }
 
         return true;

Modified: james/project/trunk/server/data/data-hbase/src/main/java/org/apache/james/rrt/hbase/HBaseRecipientRewriteTable.java
URL: http://svn.apache.org/viewvc/james/project/trunk/server/data/data-hbase/src/main/java/org/apache/james/rrt/hbase/HBaseRecipientRewriteTable.java?rev=1711978&r1=1711977&r2=1711978&view=diff
==============================================================================
--- james/project/trunk/server/data/data-hbase/src/main/java/org/apache/james/rrt/hbase/HBaseRecipientRewriteTable.java
(original)
+++ james/project/trunk/server/data/data-hbase/src/main/java/org/apache/james/rrt/hbase/HBaseRecipientRewriteTable.java
Mon Nov  2 13:44:40 2015
@@ -37,11 +37,14 @@ import org.apache.james.rrt.hbase.def.HR
 import org.apache.james.rrt.lib.AbstractRecipientRewriteTable;
 import org.apache.james.rrt.lib.Mappings;
 import org.apache.james.rrt.lib.MappingsImpl;
+import org.apache.james.rrt.lib.MappingsImpl.Builder;
 import org.apache.james.rrt.lib.RecipientRewriteTableUtil;
 import org.apache.james.system.hbase.TablePool;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import com.google.common.base.Optional;
+
 /**
  * Implementation of the RecipientRewriteTable for a HBase persistence.
  */
@@ -62,8 +65,8 @@ public class HBaseRecipientRewriteTable
         String fixedDomain = getFixedDomain(domain);
         Mappings map = getUserDomainMappings(fixedUser, fixedDomain);
         if (map != null && map.size() != 0) {
-            map.add(mapping);
-            doUpdateMapping(fixedUser, fixedDomain, RecipientRewriteTableUtil.CollectionToMapping(map));
+            Mappings updatedMappings = MappingsImpl.from(map).add(mapping).build();
+            doUpdateMapping(fixedUser, fixedDomain, RecipientRewriteTableUtil.CollectionToMapping(updatedMappings.getMappings()));
         } else {
             doAddMapping(fixedUser, fixedDomain, mapping);
         }
@@ -131,12 +134,14 @@ public class HBaseRecipientRewriteTable
                         if (map == null) {
                             map = new HashMap<String, Mappings>();
                         }
-                        Mappings list = map.get(email);
-                        if (list == null) {
-                            list = MappingsImpl.empty();
-                        }
-                        list.add(Bytes.toString(keyValue.getRow()));
-                        map.put(email, list);
+                        Mappings mappings = 
+                                MappingsImpl.from(
+                                    Optional.fromNullable(
+                                        map.get(email))
+                                        .or(MappingsImpl.empty()))
+                                .add(Bytes.toString(keyValue.getRow()))
+                                .build();
+                        map.put(email, mappings);
                     }
                 }
             }

Modified: james/project/trunk/server/data/data-jdbc/src/main/java/org/apache/james/rrt/jdbc/JDBCRecipientRewriteTable.java
URL: http://svn.apache.org/viewvc/james/project/trunk/server/data/data-jdbc/src/main/java/org/apache/james/rrt/jdbc/JDBCRecipientRewriteTable.java?rev=1711978&r1=1711977&r2=1711978&view=diff
==============================================================================
--- james/project/trunk/server/data/data-jdbc/src/main/java/org/apache/james/rrt/jdbc/JDBCRecipientRewriteTable.java
(original)
+++ james/project/trunk/server/data/data-jdbc/src/main/java/org/apache/james/rrt/jdbc/JDBCRecipientRewriteTable.java
Mon Nov  2 13:44:40 2015
@@ -209,8 +209,8 @@ public class JDBCRecipientRewriteTable e
         String fixedDomain = getFixedDomain(domain);
         Mappings map = getUserDomainMappings(fixedUser, fixedDomain);
         if (map != null && map.size() != 0) {
-            map.add(regex);
-            doUpdateMapping(fixedUser, fixedDomain, RecipientRewriteTableUtil.CollectionToMapping(map));
+            Mappings updatedMappings = MappingsImpl.from(map).add(regex).build();
+            doUpdateMapping(fixedUser, fixedDomain, RecipientRewriteTableUtil.CollectionToMapping(updatedMappings));
         }
         doAddMapping(fixedUser, fixedDomain, regex);
     }

Modified: james/project/trunk/server/data/data-jpa/src/main/java/org/apache/james/rrt/jpa/JPARecipientRewriteTable.java
URL: http://svn.apache.org/viewvc/james/project/trunk/server/data/data-jpa/src/main/java/org/apache/james/rrt/jpa/JPARecipientRewriteTable.java?rev=1711978&r1=1711977&r2=1711978&view=diff
==============================================================================
--- james/project/trunk/server/data/data-jpa/src/main/java/org/apache/james/rrt/jpa/JPARecipientRewriteTable.java
(original)
+++ james/project/trunk/server/data/data-jpa/src/main/java/org/apache/james/rrt/jpa/JPARecipientRewriteTable.java
Mon Nov  2 13:44:40 2015
@@ -65,8 +65,8 @@ public class JPARecipientRewriteTable ex
         String fixedDomain = getFixedDomain(domain);
         Mappings map = getUserDomainMappings(fixedUser, fixedDomain);
         if (map != null && map.size() != 0) {
-            map.add(mapping);
-            doUpdateMapping(fixedUser, fixedDomain, RecipientRewriteTableUtil.CollectionToMapping(map));
+            Mappings updatedMappings = MappingsImpl.from(map).add(mapping).build();
+            doUpdateMapping(fixedUser, fixedDomain, RecipientRewriteTableUtil.CollectionToMapping(updatedMappings));
         } else {
             doAddMapping(fixedUser, fixedDomain, mapping);
         }

Modified: james/project/trunk/server/data/data-library/src/main/java/org/apache/james/rrt/lib/AbstractRecipientRewriteTable.java
URL: http://svn.apache.org/viewvc/james/project/trunk/server/data/data-library/src/main/java/org/apache/james/rrt/lib/AbstractRecipientRewriteTable.java?rev=1711978&r1=1711977&r2=1711978&view=diff
==============================================================================
--- james/project/trunk/server/data/data-library/src/main/java/org/apache/james/rrt/lib/AbstractRecipientRewriteTable.java
(original)
+++ james/project/trunk/server/data/data-library/src/main/java/org/apache/james/rrt/lib/AbstractRecipientRewriteTable.java
Mon Nov  2 13:44:40 2015
@@ -126,7 +126,7 @@ public abstract class AbstractRecipientR
 
         // Only non-null mappings are translated
         if (targetString != null) {
-            Mappings mappings = MappingsImpl.empty();
+            MappingsImpl.Builder mappings = MappingsImpl.builder();
             if (targetString.startsWith(RecipientRewriteTable.ERROR_PREFIX)) {
                 throw new ErrorMappingException(targetString.substring(RecipientRewriteTable.ERROR_PREFIX.length()));
 
@@ -189,7 +189,7 @@ public abstract class AbstractRecipientR
                 }
             }
 
-            return mappings;
+            return mappings.build();
 
         }
 

Modified: james/project/trunk/server/data/data-library/src/main/java/org/apache/james/rrt/lib/MappingsImpl.java
URL: http://svn.apache.org/viewvc/james/project/trunk/server/data/data-library/src/main/java/org/apache/james/rrt/lib/MappingsImpl.java?rev=1711978&r1=1711977&r2=1711978&view=diff
==============================================================================
--- james/project/trunk/server/data/data-library/src/main/java/org/apache/james/rrt/lib/MappingsImpl.java
(original)
+++ james/project/trunk/server/data/data-library/src/main/java/org/apache/james/rrt/lib/MappingsImpl.java
Mon Nov  2 13:44:40 2015
@@ -23,9 +23,9 @@ package org.apache.james.rrt.lib;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Iterator;
-import java.util.List;
 import java.util.StringTokenizer;
 
+import com.google.common.collect.ImmutableList;
 import com.google.common.collect.Iterables;
 import com.google.common.collect.Lists;
 
@@ -53,6 +53,40 @@ public class MappingsImpl implements Map
         return new MappingsImpl(mappings);
     }
     
+    public static Builder from(Mappings from) {
+        Builder builder = new Builder();
+        builder.addAll(from);
+        return builder;
+    }
+    
+    public static Builder builder() {
+        return new Builder();
+    }
+    
+    public static class Builder {
+        
+        private final ImmutableList.Builder<String> mappings;
+        
+        private Builder() {
+            mappings = ImmutableList.builder();
+        }
+
+        public Builder add(String mapping) {
+            mappings.add(mapping);
+            return this;
+        }
+
+        public Builder addAll(Mappings mappings) {
+            this.mappings.addAll(mappings);
+            return this;
+        }
+        
+        public Mappings build() {
+            return new MappingsImpl(mappings.build());
+        }
+        
+    }
+    
     private final Collection<String> mappings;
 
     private MappingsImpl(Collection<String> mappings) {
@@ -75,11 +109,6 @@ public class MappingsImpl implements Map
     }
 
     @Override
-    public void add(String mapping) {
-       mappings.add(mapping); 
-    }
-
-    @Override
     public boolean contains(String mapping) {
         return mappings.contains(mapping);
     }
@@ -94,10 +123,6 @@ public class MappingsImpl implements Map
         return mappings.remove(mapping);
     }
 
-    public void addAll(List<String> target) {
-        mappings.addAll(target);
-    }
-
     @Override
     public boolean isEmpty() {
         return mappings.isEmpty();

Modified: james/project/trunk/server/data/data-library/src/main/java/org/apache/james/user/lib/AbstractJamesUsersRepository.java
URL: http://svn.apache.org/viewvc/james/project/trunk/server/data/data-library/src/main/java/org/apache/james/user/lib/AbstractJamesUsersRepository.java?rev=1711978&r1=1711977&r2=1711978&view=diff
==============================================================================
--- james/project/trunk/server/data/data-library/src/main/java/org/apache/james/user/lib/AbstractJamesUsersRepository.java
(original)
+++ james/project/trunk/server/data/data-library/src/main/java/org/apache/james/user/lib/AbstractJamesUsersRepository.java
Mon Nov  2 13:44:40 2015
@@ -29,6 +29,7 @@ import org.apache.james.rrt.api.Recipien
 import org.apache.james.rrt.api.RecipientRewriteTableException;
 import org.apache.james.rrt.lib.Mappings;
 import org.apache.james.rrt.lib.MappingsImpl;
+import org.apache.james.rrt.lib.MappingsImpl.Builder;
 import org.apache.james.user.api.JamesUsersRepository;
 import org.apache.james.user.api.UsersRepository;
 import org.apache.james.user.api.UsersRepositoryException;
@@ -121,7 +122,7 @@ public abstract class AbstractJamesUsers
      *      java.lang.String)
      */
     public Mappings getMappings(String username, String domain) throws ErrorMappingException,
RecipientRewriteTableException {
-        MappingsImpl mappings = MappingsImpl.empty();
+        Builder mappingsBuilder = MappingsImpl.builder();
         try {
             User user = getUserByName(username);
 
@@ -131,14 +132,14 @@ public abstract class AbstractJamesUsers
                 if (enableAliases && jUser.getAliasing()) {
                     String alias = jUser.getAlias();
                     if (alias != null) {
-                        mappings.add(alias + "@" + domain);
+                        mappingsBuilder.add(alias + "@" + domain);
                     }
                 }
 
                 if (enableForwarding && jUser.getForwarding()) {
                     String forward;
                     if (jUser.getForwardingDestination() != null && ((forward = jUser.getForwardingDestination().toString())
!= null)) {
-                        mappings.add(forward);
+                        mappingsBuilder.add(forward);
                     } else {
                         String errorBuffer = "Forwarding was enabled for " + username + "
but no forwarding address was set for this account.";
                         getLogger().error(errorBuffer);
@@ -148,6 +149,7 @@ public abstract class AbstractJamesUsers
         } catch (UsersRepositoryException e) {
             throw new RecipientRewriteTableException("Unable to lookup forwards/aliases",
e);
         }
+        Mappings mappings = mappingsBuilder.build();
         if (mappings.size() == 0) {
             return null;
         } else {

Modified: james/project/trunk/server/data/data-library/src/test/java/org/apache/james/rrt/lib/mock/MockRecipientRewriteTableManagementImpl.java
URL: http://svn.apache.org/viewvc/james/project/trunk/server/data/data-library/src/test/java/org/apache/james/rrt/lib/mock/MockRecipientRewriteTableManagementImpl.java?rev=1711978&r1=1711977&r2=1711978&view=diff
==============================================================================
--- james/project/trunk/server/data/data-library/src/test/java/org/apache/james/rrt/lib/mock/MockRecipientRewriteTableManagementImpl.java
(original)
+++ james/project/trunk/server/data/data-library/src/test/java/org/apache/james/rrt/lib/mock/MockRecipientRewriteTableManagementImpl.java
Mon Nov  2 13:44:40 2015
@@ -109,18 +109,17 @@ public class MockRecipientRewriteTableMa
     }
 
     private void addRawMapping(String user, String domain, String mapping) throws RecipientRewriteTableException
{
-        MappingsImpl map;
         String key = user + "@" + domain;
         String mappings = (String) store.get(key);
 
         if (mappings != null) {
-            map = MappingsImpl.fromRawString(mappings);
+            MappingsImpl map = MappingsImpl.fromRawString(mappings);
 
             if (map.contains(mapping)) {
                 throw new RecipientRewriteTableException("Mapping " + mapping + " already
exist!");
             } else {
-                map.add(mapping);
-                store.put(key, RecipientRewriteTableUtil.CollectionToMapping(map));
+                Mappings updateMappings = MappingsImpl.from(map).add(mapping).build();
+                store.put(key, RecipientRewriteTableUtil.CollectionToMapping(updateMappings));
             }
         } else {
             store.put(key, mapping);

Modified: james/project/trunk/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/ValidRcptHandlerTest.java
URL: http://svn.apache.org/viewvc/james/project/trunk/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/ValidRcptHandlerTest.java?rev=1711978&r1=1711977&r2=1711978&view=diff
==============================================================================
--- james/project/trunk/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/ValidRcptHandlerTest.java
(original)
+++ james/project/trunk/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/ValidRcptHandlerTest.java
Mon Nov  2 13:44:40 2015
@@ -21,6 +21,7 @@ package org.apache.james.smtpserver;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNull;
 
+import java.util.Arrays;
 import java.util.HashMap;
 import java.util.Map;
 
@@ -115,13 +116,12 @@ public class ValidRcptHandlerTest {
             @Override
             public Mappings getMappings(String user, String domain) throws ErrorMappingException,
                     RecipientRewriteTableException {
-                Mappings mappings = MappingsImpl.empty();
                 if (user.equals(USER1)) {
-                    mappings.add("address@localhost");
+                    return MappingsImpl.fromCollection(Arrays.asList("address@localhost"));
                 } else if (user.equals(USER2)) {
                     throw new ErrorMappingException("554 BOUNCE");
                 }
-                return mappings;
+                return MappingsImpl.empty();
             }
 
             @Override



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