You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by ba...@apache.org on 2006/10/15 19:21:33 UTC

svn commit: r464230 - in /james/server/trunk/src/java/org/apache/james: core/MailImpl.java transport/mailets/AbstractRedirect.java transport/mailets/AbstractVirtualUserTable.java transport/mailets/DSNBounce.java util/Misc.java

Author: bago
Date: Sun Oct 15 10:21:32 2006
New Revision: 464230

URL: http://svn.apache.org/viewvc?view=rev&rev=464230
Log:
Refactored the newName calls. Norman isolaeted the static generation of a new name to a new Misc class, Noel didn't like it and made few alternative proposals, this is a further possible solution. Imho the most clean and I expect this to be liked by both Noel and Norman ;-)

Removed:
    james/server/trunk/src/java/org/apache/james/util/Misc.java
Modified:
    james/server/trunk/src/java/org/apache/james/core/MailImpl.java
    james/server/trunk/src/java/org/apache/james/transport/mailets/AbstractRedirect.java
    james/server/trunk/src/java/org/apache/james/transport/mailets/AbstractVirtualUserTable.java
    james/server/trunk/src/java/org/apache/james/transport/mailets/DSNBounce.java

Modified: james/server/trunk/src/java/org/apache/james/core/MailImpl.java
URL: http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/core/MailImpl.java?view=diff&rev=464230&r1=464229&r2=464230
==============================================================================
--- james/server/trunk/src/java/org/apache/james/core/MailImpl.java (original)
+++ james/server/trunk/src/java/org/apache/james/core/MailImpl.java Sun Oct 15 10:21:32 2006
@@ -137,6 +137,16 @@
             }
         }
     }
+    
+    /**
+     * Create a copy of the input mail and assign it a new name
+     * 
+     * @param mail original mail
+     * @throws MessagingException when the message is not clonable
+     */
+    public MailImpl(Mail mail) throws MessagingException {
+        this(mail, newName(mail));
+    }
 
     /**
      * @param mail
@@ -635,4 +645,45 @@
         Object no = in.readObject();
         return no;
     }
+    
+
+    private static final java.util.Random random = new java.util.Random();  // Used to generate new mail names
+    
+    /**
+     * Create a unique new primary key name for the given MailObject.
+     *
+     * @param mail the mail to use as the basis for the new mail name
+     * @return a new name
+     */
+    public static String newName(Mail mail) throws MessagingException {
+        String oldName = mail.getName();
+        
+        // Checking if the original mail name is too long, perhaps because of a
+        // loop caused by a configuration error.
+        // it could cause a "null pointer exception" in AvalonMailRepository much
+        // harder to understand.
+        if (oldName.length() > 76) {
+            int count = 0;
+            int index = 0;
+            while ((index = oldName.indexOf('!', index + 1)) >= 0) {
+                count++;
+            }
+            // It looks like a configuration loop. It's better to stop.
+            if (count > 7) {
+                throw new MessagingException("Unable to create a new message name: too long."
+                                             + " Possible loop in config.xml.");
+            }
+            else {
+                oldName = oldName.substring(0, 76);
+            }
+        }
+        
+        StringBuffer nameBuffer =
+                                 new StringBuffer(64)
+                                 .append(oldName)
+                                 .append("-!")
+                                 .append(random.nextInt(1048576));
+        return nameBuffer.toString();
+    }
+
 }

Modified: james/server/trunk/src/java/org/apache/james/transport/mailets/AbstractRedirect.java
URL: http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/transport/mailets/AbstractRedirect.java?view=diff&rev=464230&r1=464229&r2=464230
==============================================================================
--- james/server/trunk/src/java/org/apache/james/transport/mailets/AbstractRedirect.java (original)
+++ james/server/trunk/src/java/org/apache/james/transport/mailets/AbstractRedirect.java Sun Oct 15 10:21:32 2006
@@ -46,7 +46,6 @@
 import org.apache.mailet.dates.RFC822DateFormat;
 import org.apache.james.core.MailImpl;
 import org.apache.james.core.MimeMessageUtil;
-import org.apache.james.util.Misc;
 
 import org.apache.mailet.GenericMailet;
 import org.apache.mailet.Mail;
@@ -978,7 +977,7 @@
         boolean keepMessageId = false;
 
         // duplicates the Mail object, to be able to modify the new mail keeping the original untouched
-        MailImpl newMail = new MailImpl(originalMail,Misc.newName(originalMail,random));
+        MailImpl newMail = new MailImpl(originalMail);
         try {
             // We don't need to use the original Remote Address and Host,
             // and doing so would likely cause a loop with spam detecting
@@ -1080,8 +1079,6 @@
             originalMail.setState(Mail.GHOST);
         }
     }
-
-    private static final java.util.Random random = new java.util.Random();  // Used to generate new mail names
 
 
     /**

Modified: james/server/trunk/src/java/org/apache/james/transport/mailets/AbstractVirtualUserTable.java
URL: http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/transport/mailets/AbstractVirtualUserTable.java?view=diff&rev=464230&r1=464229&r2=464230
==============================================================================
--- james/server/trunk/src/java/org/apache/james/transport/mailets/AbstractVirtualUserTable.java (original)
+++ james/server/trunk/src/java/org/apache/james/transport/mailets/AbstractVirtualUserTable.java Sun Oct 15 10:21:32 2006
@@ -22,7 +22,6 @@
 package org.apache.james.transport.mailets;
 
 import org.apache.james.core.MailImpl;
-import org.apache.james.util.Misc;
 import org.apache.james.util.VirtualUserTableUtil;
 import org.apache.mailet.GenericMailet;
 import org.apache.mailet.Mail;
@@ -163,7 +162,7 @@
             // getMailetContext().sendMail(mail.getSender(), recipientsToAddForward, mail.getMessage());
 
             // duplicates the Mail object, to be able to modify the new mail keeping the original untouched
-            MailImpl newMail = new MailImpl(mail,Misc.newName(mail,random));
+            MailImpl newMail = new MailImpl(mail);
             try {
                 try {
                     newMail.setRemoteAddr(java.net.InetAddress.getLocalHost().getHostAddress());
@@ -236,7 +235,5 @@
   private String getSeparator(String targetString) {
       return (targetString.indexOf(',') > -1 ? "," : (targetString.indexOf(';') > -1 ? ";" : (targetString.indexOf("regex:") > -1? "" : ":" )));
   }
-
-  private static final java.util.Random random = new java.util.Random();  // Used to generate new mail names
 
 }

Modified: james/server/trunk/src/java/org/apache/james/transport/mailets/DSNBounce.java
URL: http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/transport/mailets/DSNBounce.java?view=diff&rev=464230&r1=464229&r2=464230
==============================================================================
--- james/server/trunk/src/java/org/apache/james/transport/mailets/DSNBounce.java (original)
+++ james/server/trunk/src/java/org/apache/james/transport/mailets/DSNBounce.java Sun Oct 15 10:21:32 2006
@@ -23,7 +23,6 @@
 
 import org.apache.james.Constants;
 import org.apache.james.core.MailImpl;
-import org.apache.james.util.Misc;
 import org.apache.james.util.mail.MimeMultipartReport;
 import org.apache.james.util.mail.dsn.DSNStatus;
 import org.apache.mailet.Mail;
@@ -94,9 +93,6 @@
 
     private static final RFC822DateFormat rfc822DateFormat = new RFC822DateFormat();
 
-    //  Used to generate new mail names
-    private static final java.util.Random random = new java.util.Random();
-
     // regexp pattern for scaning status code from exception message
     private static Pattern statusPattern;
 
@@ -151,7 +147,7 @@
 
 
         // duplicates the Mail object, to be able to modify the new mail keeping the original untouched
-        MailImpl newMail = new MailImpl(originalMail,Misc.newName(originalMail,random));
+        MailImpl newMail = new MailImpl(originalMail);
         try {
             // We don't need to use the original Remote Address and Host,
             // and doing so would likely cause a loop with spam detecting



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