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 no...@apache.org on 2006/06/20 11:54:05 UTC

svn commit: r415593 - in /james/server/trunk/src: java/org/apache/james/transport/mailets/RemoteDelivery.java java/org/apache/james/util/TimeConverter.java test/org/apache/james/util/TimeConverterTest.java

Author: norman
Date: Tue Jun 20 02:54:05 2006
New Revision: 415593

URL: http://svn.apache.org/viewvc?rev=415593&view=rev
Log:
Move the TimeConvertion in a seperate util class to use it later in other mailets. See JAMES-546

Added:
    james/server/trunk/src/java/org/apache/james/util/TimeConverter.java
    james/server/trunk/src/test/org/apache/james/util/TimeConverterTest.java
Modified:
    james/server/trunk/src/java/org/apache/james/transport/mailets/RemoteDelivery.java

Modified: james/server/trunk/src/java/org/apache/james/transport/mailets/RemoteDelivery.java
URL: http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/transport/mailets/RemoteDelivery.java?rev=415593&r1=415592&r2=415593&view=diff
==============================================================================
--- james/server/trunk/src/java/org/apache/james/transport/mailets/RemoteDelivery.java (original)
+++ james/server/trunk/src/java/org/apache/james/transport/mailets/RemoteDelivery.java Tue Jun 20 02:54:05 2006
@@ -24,6 +24,7 @@
 import org.apache.james.Constants;
 import org.apache.james.services.DNSServer;
 import org.apache.james.services.SpoolRepository;
+import org.apache.james.util.TimeConverter;
 import org.apache.mailet.GenericMailet;
 import org.apache.mailet.HostAddress;
 import org.apache.mailet.Mail;
@@ -62,7 +63,6 @@
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.Date;
-import java.util.HashMap;
 import java.util.Hashtable;
 import java.util.Iterator;
 import java.util.Locale;
@@ -102,8 +102,7 @@
                                                                  //[attempts*]delay[units]
                                             
     private static Pattern PATTERN = null; //the compiled pattern of the above String
-    private static final HashMap MULTIPLIERS = new HashMap (10); //holds allowed units for delaytime together with
-                                                                //the factor to turn it into the equivalent time in msec
+    
     // The DNSServer
     private DNSServer dnsServer;
     
@@ -118,20 +117,8 @@
             PATTERN = compiler.compile(PATTERN_STRING, Perl5Compiler.READ_ONLY_MASK);
         } catch(MalformedPatternException mpe) {
             //this should not happen as the pattern string is hardcoded.
-            System.err.println ("Malformed pattern: " + PATTERN_STRING);
             mpe.printStackTrace (System.err);
         }
-        //add allowed units and their respective multiplier
-        MULTIPLIERS.put ("msec", new Integer (1));
-        MULTIPLIERS.put ("msecs", new Integer (1));
-        MULTIPLIERS.put ("sec",  new Integer (1000));
-        MULTIPLIERS.put ("secs",  new Integer (1000));
-        MULTIPLIERS.put ("minute", new Integer (1000*60));
-        MULTIPLIERS.put ("minutes", new Integer (1000*60));
-        MULTIPLIERS.put ("hour", new Integer (1000*60*60));
-        MULTIPLIERS.put ("hours", new Integer (1000*60*60));
-        MULTIPLIERS.put ("day", new Integer (1000*60*60*24));
-        MULTIPLIERS.put ("days", new Integer (1000*60*60*24));
     }
     
     /**
@@ -1244,11 +1231,10 @@
             } else {
                 throw new MessagingException(init_string+" does not match "+PATTERN_STRING);
             }
-            if (MULTIPLIERS.get (unit)!=null) {
-                int multiplier = ((Integer)MULTIPLIERS.get (unit)).intValue();
-                delayTime *= multiplier;
-            } else {
-                throw new MessagingException("Unknown unit: "+unit);
+                try {
+                delayTime = TimeConverter.getMilliSeconds(delayTime, unit);
+            } catch (NumberFormatException e) {
+                throw new MessagingException(e.getMessage());
             }
         }
 

Added: james/server/trunk/src/java/org/apache/james/util/TimeConverter.java
URL: http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/util/TimeConverter.java?rev=415593&view=auto
==============================================================================
--- james/server/trunk/src/java/org/apache/james/util/TimeConverter.java (added)
+++ james/server/trunk/src/java/org/apache/james/util/TimeConverter.java Tue Jun 20 02:54:05 2006
@@ -0,0 +1,128 @@
+/***********************************************************************
+ * Copyright (c) 2006 The Apache Software Foundation.                  *
+ * All rights reserved.                                                *
+ * ------------------------------------------------------------------- *
+ * Licensed 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.util;
+
+import java.util.HashMap;
+
+import org.apache.oro.text.regex.MalformedPatternException;
+import org.apache.oro.text.regex.MatchResult;
+import org.apache.oro.text.regex.Pattern;
+import org.apache.oro.text.regex.Perl5Compiler;
+import org.apache.oro.text.regex.Perl5Matcher;
+
+public class TimeConverter {
+
+    private static HashMap multipliers = new HashMap(10);
+
+    private static final String PATTERN_STRING = "\\s*([0-9]+)\\s*([a-z,A-Z]+)\\s*";
+
+    private static Pattern PATTERN = null;
+
+    static {
+        // add allowed units and their respective multiplier
+        multipliers.put("msec", new Integer(1));
+        multipliers.put("msecs", new Integer(1));
+        multipliers.put("sec", new Integer(1000));
+        multipliers.put("secs", new Integer(1000));
+        multipliers.put("minute", new Integer(1000 * 60));
+        multipliers.put("minutes", new Integer(1000 * 60));
+        multipliers.put("hour", new Integer(1000 * 60 * 60));
+        multipliers.put("hours", new Integer(1000 * 60 * 60));
+        multipliers.put("day", new Integer(1000 * 60 * 60 * 24));
+        multipliers.put("days", new Integer(1000 * 60 * 60 * 24));
+
+        try {
+            Perl5Compiler compiler = new Perl5Compiler();
+            PATTERN = compiler.compile(PATTERN_STRING,
+                    Perl5Compiler.READ_ONLY_MASK);
+        } catch (MalformedPatternException mpe) {
+            // Will never happen cause its hardcoded
+        }
+
+    }
+
+    // Get sure it can not be instanciated
+    private TimeConverter(String rawString) {
+    }
+
+    /**
+     * Helper method to get the milliseconds for the given amount and unit
+     * 
+     * @param amount
+     *            The amount for use with the unit
+     * @param unit
+     *            The unit
+     * @return The time in milliseconds
+     * @throws NumberFormatException
+     *             Get thrown if an illegal unit was used
+     */
+    public static long getMilliSeconds(long amount, String unit)
+            throws NumberFormatException {
+        Object multiplierObject = multipliers.get(unit);
+        if (multiplierObject == null) {
+            throw new NumberFormatException("Unknown unit: " + unit);
+        }
+        int multiplier = ((Integer) multiplierObject).intValue();
+        return (amount * multiplier);
+    }
+
+    /**
+     * Helper method to get the milliseconds for the given rawstring. Allowed
+     * rawstrings must mach pattern: "\\s*([0-9]+)\\s*([a-z,A-Z]+)\\s*"
+     * 
+     * @param rawString
+     *            The rawstring which we use to extract the amount and unit
+     * @return The time in milliseconds
+     * @throws NumberFormatException
+     *             Get thrown if an illegal rawString was used
+     */
+    public static long getMilliSeconds(String rawString)
+            throws NumberFormatException {
+        Perl5Matcher matcher = new Perl5Matcher();
+
+        try {
+            Perl5Compiler compiler = new Perl5Compiler();
+            PATTERN = compiler.compile(PATTERN_STRING,
+                    Perl5Compiler.READ_ONLY_MASK);
+        } catch (MalformedPatternException mpe) {
+            // Will never happen
+        }
+
+        if (matcher.matches(rawString, PATTERN)) {
+            MatchResult res = matcher.getMatch();
+
+            if (res.group(1) != null && res.group(2) != null) {
+                long time = Integer.parseInt(res.group(1).trim());
+                String unit = res.group(2);
+                return getMilliSeconds(time, unit);
+            } else {
+
+                // This should never Happen anyway throw an exception
+                throw new NumberFormatException(
+                        "The supplied String is not a supported format "
+                                + rawString);
+            }
+        } else {
+            // The rawString not match our pattern. So its not supported
+            throw new NumberFormatException(
+                    "The supplied String is not a supported format "
+                            + rawString);
+        }
+    }
+
+}

Added: james/server/trunk/src/test/org/apache/james/util/TimeConverterTest.java
URL: http://svn.apache.org/viewvc/james/server/trunk/src/test/org/apache/james/util/TimeConverterTest.java?rev=415593&view=auto
==============================================================================
--- james/server/trunk/src/test/org/apache/james/util/TimeConverterTest.java (added)
+++ james/server/trunk/src/test/org/apache/james/util/TimeConverterTest.java Tue Jun 20 02:54:05 2006
@@ -0,0 +1,135 @@
+/***********************************************************************
+ * Copyright (c) 2006 The Apache Software Foundation.                  *
+ * All rights reserved.                                                *
+ * ------------------------------------------------------------------- *
+ * Licensed 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.util;
+
+import junit.framework.TestCase;
+
+public class TimeConverterTest extends TestCase {
+    
+    private final long AMOUNT = 2;
+    
+
+    public TimeConverterTest(String arg0) {
+        super(arg0);
+    }
+
+    
+    public void testGetMilliSecondsMsec() {     
+        long time = 2;
+        String unit = "msec";
+        
+        assertEquals(TimeConverter.getMilliSeconds(AMOUNT,unit),time);
+        assertEquals(TimeConverter.getMilliSeconds(AMOUNT +" " + unit),time);
+    }
+    
+    public void testGetMilliSecondsMsecs() {
+        long time = 2;
+        String unit = "msecs";
+        
+        assertEquals(TimeConverter.getMilliSeconds(AMOUNT,unit),time);
+        assertEquals(TimeConverter.getMilliSeconds(AMOUNT +" " + unit),time);
+    }
+    
+    public void testGetMilliSecondsSec() {   
+        long time = 2000;
+        String unit = "sec";
+        
+        assertEquals(TimeConverter.getMilliSeconds(AMOUNT,unit),time);
+        assertEquals(TimeConverter.getMilliSeconds(AMOUNT +" " + unit),time);
+    }
+    
+    public void testGetMilliSecondsSecs() {   
+        long time = 2000;
+        String unit = "secs";
+        
+        assertEquals(TimeConverter.getMilliSeconds(AMOUNT,unit),time);
+        assertEquals(TimeConverter.getMilliSeconds(AMOUNT +" " + unit),time);
+    }
+    
+    public void testGetMilliSecondsMinute() {  
+        long time = 120000;
+        String unit = "minute";
+        
+        assertEquals(TimeConverter.getMilliSeconds(AMOUNT,unit),time);
+        assertEquals(TimeConverter.getMilliSeconds(AMOUNT +" " + unit),time);
+    }
+    
+    public void testGetMilliSecondsMinutes() {     
+        long time = 120000;
+        String unit = "minutes";
+        
+        assertEquals(TimeConverter.getMilliSeconds(AMOUNT,unit),time);
+        assertEquals(TimeConverter.getMilliSeconds(AMOUNT +" " + unit),time);
+    }
+    
+    public void testGetMilliSecondsHour() {    
+        long time = 7200000;
+        String unit = "hour";
+        
+        assertEquals(TimeConverter.getMilliSeconds(AMOUNT,unit),time);
+        assertEquals(TimeConverter.getMilliSeconds(AMOUNT +" " + unit),time);
+    }
+    
+    public void testGetMilliSecondsHours() {     
+        long time = 7200000;
+        String unit = "hours";
+        
+        assertEquals(TimeConverter.getMilliSeconds(AMOUNT,unit),time);
+        assertEquals(TimeConverter.getMilliSeconds(AMOUNT +" " + unit),time);
+    }
+    
+    public void testGetMilliSecondsDay() {  
+        long time = 172800000;
+        String unit = "day";
+        
+        assertEquals(TimeConverter.getMilliSeconds(AMOUNT,unit),time);
+        assertEquals(TimeConverter.getMilliSeconds(AMOUNT +" " + unit),time);
+    }
+    
+    public void testGetMilliSecondsDays() {     
+        long time = 172800000;
+        String unit = "days";
+        
+        assertEquals(TimeConverter.getMilliSeconds(AMOUNT,unit),time);
+        assertEquals(TimeConverter.getMilliSeconds(AMOUNT +" " + unit),time);
+    }
+    
+    public void testIllegalUnit() {     
+        boolean exceptionThrown = false;
+        try {
+            TimeConverter.getMilliSeconds(2,"week");
+            TimeConverter.getMilliSeconds(2 +" week");
+        } catch (NumberFormatException e) {
+            exceptionThrown = true;
+        }
+        
+        assertTrue(exceptionThrown);
+    }
+    
+    public void testIllegalPattern() {     
+        boolean exceptionThrown = false;
+        try {
+            TimeConverter.getMilliSeconds("illegal pattern");
+        } catch (NumberFormatException e) {
+            exceptionThrown = true;
+        }
+        
+        assertTrue(exceptionThrown);
+    }
+    
+}



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