Scarab commit: svn commit: r10919 - trunk/src/java/org/tigris/scarab/services/email/TemplateHtmlEmail.java

Hussayn Dabbous <[email protected]>
Newsgroups gmane.comp.java.scarab.cvs
Message-ID <[email protected]>
Author: dabbous
Date: 2010-02-24 13:29:17-0800
New Revision: 10919

Modified:
   trunk/src/java/org/tigris/scarab/services/email/TemplateHtmlEmail.java

Log:
Improved Exception handling and logging during sending of emails.

Modified: trunk/src/java/org/tigris/scarab/services/email/TemplateHtmlEmail.java
Url: http://scarab.tigris.org/source/browse/scarab/trunk/src/java/org/tigris/scarab/services/email/TemplateHtmlEmail.java?view=diff&pathrev=10919&r1=10918&r2=10919
==============================================================================
--- trunk/src/java/org/tigris/scarab/services/email/TemplateHtmlEmail.java	(original)
+++ trunk/src/java/org/tigris/scarab/services/email/TemplateHtmlEmail.java	2010-02-24 13:29:17-0800
@@ -68,9 +68,11 @@
 import org.apache.fulcrum.velocity.ContextAdapter;
 import org.apache.fulcrum.template.TemplateContext;
 import org.apache.fulcrum.template.TurbineTemplate;
+import org.apache.log4j.Logger;
 
 import org.tigris.scarab.services.email.VelocityEmail;
 import org.tigris.scarab.tools.ScarabLocalizationTool;
+import org.tigris.scarab.util.Email;
 import org.tigris.scarab.util.Log;
 import org.tigris.scarab.util.ScarabConstants;
 
@@ -138,6 +140,8 @@
     /** The map of embedded files. */
     private Hashtable embmap = null;
 
+    public static Logger log = Log.get(Email.class.getName());
+    
     /**
      * Constructor, sets the TemplateContext object.
      *
@@ -245,7 +249,7 @@
         String htmlbody = "";
         String textbody = "";
 
-        // Process the templates.
+        // Create MessageBody from template:
         try
         {
             if (htmlTemplate != null)
@@ -259,50 +263,76 @@
         }
         catch( Exception e)
         {
-            Log.get().error(e.getMessage());
-            Log.get().warn("This was the Email context:");
-            try
+            dumpContextToLog(e);
+            throw new EmailException("Cannot parse email template", e);
+        }
+
+        // Set appropriate Message body:
+        try
+        {
+            if (StringUtils.isNotEmpty(htmlbody) && StringUtils.isNotEmpty(textbody))
             {
-                Object[] keys = context.getKeys();
-                for(int i=0; i < keys.length; i++)
-                {
-                    String keystring = keys[i].toString();
-                    Object val = context.get(keystring);
-                    Log.get().warn("    " + keystring + "=" + val.toString());                    
-                }
+                // We have both plain text and HTML message bodys
+                setHtmlMsg(htmlbody); 
+                setTextMsg(textbody); 
             }
-            catch(Exception ex)
+            else if (StringUtils.isEmpty(htmlbody)) 
             {
-                Log.get().error("Double error: Can not dump Email context. ");
+                // We have only a text body.
+                setTextMsg(textbody);
+                /* Note that we don't use setMsg() because that would put the plain
+                 * text into a <html><pre>[plain text]</pre></html> stansa in the
+                 * HTML part. Not particularly useful. Worse, it also defeats any 
+                 * hyperlinks in the message.
+                 */
             }
-            Log.get().warn("End of Email context dump");
-            throw new EmailException("Cannot parse email template", e);
+            else
+            {
+                // We have only a HTML message. Recipients with a text-only client
+                // won't like this.
+                setHtmlMsg(htmlbody); 
+            }
+        }
+        catch( Exception e)
+        {
+            dumpContextToLog(e);
+            throw new EmailException("Cannot set message body", e);
         }
 
-        if (StringUtils.isNotEmpty(htmlbody) && StringUtils.isNotEmpty(textbody))
+
+        // Send message to recipients:
+        String mimeMessageId = null;
+        try
         {
-            // We have both plain text and HTML message bodys
-            setHtmlMsg(htmlbody); 
-            setTextMsg(textbody); 
-        }
-        else if (StringUtils.isEmpty(htmlbody)) 
-        {
-            // We have only a text body.
-            setTextMsg(textbody);
-            /* Note that we don't use setMsg() because that would put the plain
-             * text into a <html><pre>[plain text]</pre></html> stansa in the
-             * HTML part. Not particularly useful. Worse, it also defeats any 
-             * hyperlinks in the message.
-             */
+            mimeMessageId = super.send();
         }
-        else
+        catch( Exception e)
         {
-            // We have only a HTML message. Recipients with a text-only client
-            // won't like this.
-            setHtmlMsg(htmlbody); 
+            dumpContextToLog(e);
+            throw new EmailException("Cannot send email template", e);
         }
+        
+        return mimeMessageId;
+    }
 
-        return super.send();
+    private void dumpContextToLog(Exception e) {
+        log.error(e.getMessage());
+        log.error("This was the Email context:");
+        try
+        {
+            Object[] keys = context.getKeys();
+            for(int i=0; i < keys.length; i++)
+            {
+                String keystring = keys[i].toString();
+                Object val = context.get(keystring);
+                log.error("    " + keystring + "=" + val.toString());                 
+            }
+        }
+        catch(Exception ex)
+        {
+            log.error("Double error: Can not dump Email context. ");
+        }
+        log.error("End of Email context dump");
     }
 
     /**

------------------------------------------------------
http://scarab.tigris.org/ds/viewMessage.do?dsForumId=3577&dsMessageId=2451707
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.