[jbpm-commits] JBoss JBPM SVN: r2980 - jbpm3/trunk/modules/core/src/main/java/org/jbpm/mail.

do-not-reply at jboss.org do-not-reply at jboss.org
Tue Nov 18 03:38:25 EST 2008


Author: thomas.diesler at jboss.com
Date: 2008-11-18 03:38:25 -0500 (Tue, 18 Nov 2008)
New Revision: 2980

Modified:
   jbpm3/trunk/modules/core/src/main/java/org/jbpm/mail/Mail.java
Log:
Retry to send mail

Modified: jbpm3/trunk/modules/core/src/main/java/org/jbpm/mail/Mail.java
===================================================================
--- jbpm3/trunk/modules/core/src/main/java/org/jbpm/mail/Mail.java	2008-11-18 08:20:30 UTC (rev 2979)
+++ jbpm3/trunk/modules/core/src/main/java/org/jbpm/mail/Mail.java	2008-11-18 08:38:25 UTC (rev 2980)
@@ -14,6 +14,7 @@
 import java.util.StringTokenizer;
 
 import javax.mail.Message;
+import javax.mail.MessagingException;
 import javax.mail.Session;
 import javax.mail.Transport;
 import javax.mail.internet.InternetAddress;
@@ -31,10 +32,10 @@
 import org.jbpm.util.ClassLoaderUtil;
 import org.jbpm.util.XmlUtil;
 
-public class Mail implements ActionHandler {
+public class Mail implements ActionHandler
+{
+  private static final long serialVersionUID = 1L;
 
-  private static final long serialVersionUID = 1L;
-  
   String template = null;
   String actors = null;
   String to = null;
@@ -42,31 +43,24 @@
   String bccActors = null;
   String subject = null;
   String text = null;
-  
+
   ExecutionContext executionContext = null;
-  
-  public Mail() {
+
+  public Mail()
+  {
   }
 
-  public Mail(String template,
-              String actors,
-              String to,
-              String subject,
-              String text) {
+  public Mail(String template, String actors, String to, String subject, String text)
+  {
     this.template = template;
     this.actors = actors;
     this.to = to;
     this.subject = subject;
     this.text = text;
   }
-  
-  public Mail(String template,
-      String actors,
-      String to,
-      String bccActors,
-      String bcc,
-      String subject,
-      String text) {
+
+  public Mail(String template, String actors, String to, String bccActors, String bcc, String subject, String text)
+  {
     this.template = template;
     this.actors = actors;
     this.to = to;
@@ -74,193 +68,260 @@
     this.bcc = bcc;
     this.subject = subject;
     this.text = text;
-  }  
-  
-  public void execute(ExecutionContext executionContext) {
+  }
+
+  public void execute(ExecutionContext executionContext)
+  {
     this.executionContext = executionContext;
     send();
   }
 
-  public List getRecipients() {
+  public List getRecipients()
+  {
     List recipients = new ArrayList();
-    if (actors!=null) {
+    if (actors != null)
+    {
       String evaluatedActors = evaluate(actors);
       List tokenizedActors = tokenize(evaluatedActors);
-      if (tokenizedActors!=null) {
+      if (tokenizedActors != null)
+      {
         recipients.addAll(resolveAddresses(tokenizedActors));
       }
     }
-    if (to!=null) {
+    if (to != null)
+    {
       String resolvedTo = evaluate(to);
       recipients.addAll(tokenize(resolvedTo));
     }
     return recipients;
   }
 
-  public List getBccRecipients() {
+  public List getBccRecipients()
+  {
     List recipients = new ArrayList();
-    if (bccActors!=null) {
+    if (bccActors != null)
+    {
       String evaluatedActors = evaluate(bccActors);
       List tokenizedActors = tokenize(evaluatedActors);
-      if (tokenizedActors!=null) {
+      if (tokenizedActors != null)
+      {
         recipients.addAll(resolveAddresses(tokenizedActors));
       }
     }
-    if (bcc!=null) {
+    if (bcc != null)
+    {
       String resolvedTo = evaluate(bcc);
       recipients.addAll(tokenize(resolvedTo));
     }
-    if (JbpmConfiguration.Configs.hasObject("jbpm.mail.bcc.address")) {
-      recipients.addAll(tokenize(
-          JbpmConfiguration.Configs.getString("jbpm.mail.bcc.address")));
+    if (JbpmConfiguration.Configs.hasObject("jbpm.mail.bcc.address"))
+    {
+      recipients.addAll(tokenize(JbpmConfiguration.Configs.getString("jbpm.mail.bcc.address")));
     }
     return recipients;
   }
-  
-  public String getSubject() {
-    if (subject==null) return null;
+
+  public String getSubject()
+  {
+    if (subject == null)
+      return null;
     return evaluate(subject);
   }
 
-  public String getText() {
-    if (text==null) return null;
+  public String getText()
+  {
+    if (text == null)
+      return null;
     return evaluate(text);
   }
 
-  public String getFromAddress() {
-    if (JbpmConfiguration.Configs.hasObject("jbpm.mail.from.address")) {
+  public String getFromAddress()
+  {
+    if (JbpmConfiguration.Configs.hasObject("jbpm.mail.from.address"))
+    {
       return JbpmConfiguration.Configs.getString("jbpm.mail.from.address");
-    } 
+    }
     return "jbpm at noreply";
   }
 
-  public void send() {
-    if (template!=null) {
+  public void send()
+  {
+    if (template != null)
+    {
       Properties properties = getMailTemplateProperties(template);
-      if (actors==null) {
+      if (actors == null)
+      {
         actors = properties.getProperty("actors");
       }
-      if (to==null) {
+      if (to == null)
+      {
         to = properties.getProperty("to");
       }
-      if (subject==null) {
+      if (subject == null)
+      {
         subject = properties.getProperty("subject");
       }
-      if (text==null) {
+      if (text == null)
+      {
         text = properties.getProperty("text");
       }
-      if (bcc==null) {
+      if (bcc == null)
+      {
         bcc = properties.getProperty("bcc");
       }
-      if (bccActors==null) {
+      if (bccActors == null)
+      {
         bccActors = properties.getProperty("bccActors");
       }
     }
-    
-    send(getMailServerProperties(), 
-            getFromAddress(), 
-            getRecipients(), 
-            getBccRecipients(),
-            getSubject(), 
-            getText());
+
+    send(getMailServerProperties(), getFromAddress(), getRecipients(), getBccRecipients(), getSubject(), getText());
   }
-  
-  public static void send(Properties mailServerProperties, String fromAddress, List recipients, String subject, String text) {
+
+  public static void send(Properties mailServerProperties, String fromAddress, List recipients, String subject, String text)
+  {
     send(mailServerProperties, fromAddress, recipients, null, subject, text);
   }
-  
-  public static void send(Properties mailServerProperties, String fromAddress, List recipients, List bccRecipients, String subject, String text) {
-    if ( ((recipients==null) || (recipients.isEmpty()))
-         && ((bccRecipients==null) || (bccRecipients.isEmpty()))
-       ) {
+
+  public static void send(Properties mailServerProperties, String fromAddress, List recipients, List bccRecipients, String subject, String text)
+  {
+    if (((recipients == null) || (recipients.isEmpty())) && ((bccRecipients == null) || (bccRecipients.isEmpty())))
+    {
       log.debug("skipping mail because there are no recipients");
       return;
     }
-    log.debug("sending email to '"+recipients+"' "+(bccRecipients!=null ? "and in bcc to '"+bccRecipients+"' " : "")+"about '"+subject+"'");
+    log.debug("sending email to '" + recipients + "' " + (bccRecipients != null ? "and in bcc to '" + bccRecipients + "' " : "") + "about '" + subject + "'");
     Session session = Session.getDefaultInstance(mailServerProperties, null);
     MimeMessage message = new MimeMessage(session);
-    try {
-      if (fromAddress!=null) {
+    try
+    {
+      if (fromAddress != null)
+      {
         message.setFrom(new InternetAddress(fromAddress));
       }
       Iterator iter = recipients.iterator();
-      while (iter.hasNext()) {
-        InternetAddress recipient = new InternetAddress((String) iter.next());
+      while (iter.hasNext())
+      {
+        InternetAddress recipient = new InternetAddress((String)iter.next());
         message.addRecipient(Message.RecipientType.TO, recipient);
       }
-      if (bccRecipients!=null) {
+      if (bccRecipients != null)
+      {
         iter = bccRecipients.iterator();
-        while (iter.hasNext()) {
-          InternetAddress recipient = new InternetAddress((String) iter.next());
+        while (iter.hasNext())
+        {
+          InternetAddress recipient = new InternetAddress((String)iter.next());
           message.addRecipient(Message.RecipientType.BCC, recipient);
-        }        
+        }
       }
-      if (subject!=null) {
+      if (subject != null)
+      {
         message.setSubject(subject);
       }
-      if (text!=null) {
+      if (text != null)
+      {
         message.setText(text);
       }
       message.setSentDate(new Date());
 
-      Transport.send(message);
-    } catch (Exception e) {
-      throw new JbpmException("couldn't send email", e);
+      int retries = 5;
+      while (0 < retries)
+      {
+        retries--;
+        try
+        {
+          Transport.send(message);
+        }
+        catch (MessagingException msgex)
+        {
+          Thread.sleep(500);
+          if (0 < retries)
+            log.error("Cannot send mail, now retrying: " + msgex);
+          else
+            throw msgex;
+        }
+      }
     }
+    catch (Exception e)
+    {
+      throw new JbpmException("Cannot send email", e);
+    }
   }
 
-  protected List tokenize(String text) {
-    if (text==null) {
+  protected List tokenize(String text)
+  {
+    if (text == null)
+    {
       return null;
     }
     List list = new ArrayList();
     StringTokenizer tokenizer = new StringTokenizer(text, ";:");
-    while (tokenizer.hasMoreTokens()) {
+    while (tokenizer.hasMoreTokens())
+    {
       list.add(tokenizer.nextToken());
     }
     return list;
   }
 
-  protected Collection resolveAddresses(List actorIds) {
+  protected Collection resolveAddresses(List actorIds)
+  {
     List emailAddresses = new ArrayList();
     Iterator iter = actorIds.iterator();
-    while (iter.hasNext()) {
-      String actorId = (String) iter.next();
-      AddressResolver addressResolver = (AddressResolver) JbpmConfiguration.Configs.getObject("jbpm.mail.address.resolver");
+    while (iter.hasNext())
+    {
+      String actorId = (String)iter.next();
+      AddressResolver addressResolver = (AddressResolver)JbpmConfiguration.Configs.getObject("jbpm.mail.address.resolver");
       Object resolvedAddresses = addressResolver.resolveAddress(actorId);
-      if (resolvedAddresses!=null) {
-        if (resolvedAddresses instanceof String) {
+      if (resolvedAddresses != null)
+      {
+        if (resolvedAddresses instanceof String)
+        {
           emailAddresses.add((String)resolvedAddresses);
-        } else if (resolvedAddresses instanceof Collection) {
+        }
+        else if (resolvedAddresses instanceof Collection)
+        {
           emailAddresses.addAll((Collection)resolvedAddresses);
-        } else if (resolvedAddresses instanceof String[]) {
+        }
+        else if (resolvedAddresses instanceof String[])
+        {
           emailAddresses.addAll(Arrays.asList((String[])resolvedAddresses));
-        } else {
-          throw new JbpmException("Address resolver '"+addressResolver+"' returned '"+resolvedAddresses.getClass().getName()+"' instead of a String, Collection or String-array: "+resolvedAddresses);
         }
+        else
+        {
+          throw new JbpmException("Address resolver '" + addressResolver + "' returned '" + resolvedAddresses.getClass().getName()
+              + "' instead of a String, Collection or String-array: " + resolvedAddresses);
+        }
       }
     }
     return emailAddresses;
   }
 
-  Properties getMailServerProperties() {
+  Properties getMailServerProperties()
+  {
     Properties mailServerProperties = new Properties();
 
-    if (JbpmConfiguration.Configs.hasObject("resource.mail.properties")) {
+    if (JbpmConfiguration.Configs.hasObject("resource.mail.properties"))
+    {
       String mailServerPropertiesResource = JbpmConfiguration.Configs.getString("resource.mail.properties");
-      try {
+      try
+      {
         InputStream mailServerStream = ClassLoaderUtil.getStream(mailServerPropertiesResource);
         mailServerProperties.load(mailServerStream);
-      } catch (Exception e) {
-        throw new JbpmException("couldn't get configuration properties for jbpm mail server from resource '"+mailServerPropertiesResource+"'", e);
       }
-    
-    } else if (JbpmConfiguration.Configs.hasObject("jbpm.mail.smtp.host")) {
+      catch (Exception e)
+      {
+        throw new JbpmException("couldn't get configuration properties for jbpm mail server from resource '" + mailServerPropertiesResource + "'", e);
+      }
+
+    }
+    else if (JbpmConfiguration.Configs.hasObject("jbpm.mail.smtp.host"))
+    {
       String smtpServer = JbpmConfiguration.Configs.getString("jbpm.mail.smtp.host");
       mailServerProperties.put("mail.smtp.host", smtpServer);
-      
-    } else {
-      
+
+    }
+    else
+    {
+
       log.error("couldn't get mail properties");
     }
 
@@ -269,15 +330,19 @@
 
   static Map templates = null;
   static Map templateVariables = null;
-  synchronized Properties getMailTemplateProperties(String templateName) {
-    if (templates==null) {
+
+  synchronized Properties getMailTemplateProperties(String templateName)
+  {
+    if (templates == null)
+    {
       templates = new HashMap();
       String mailTemplatesResource = JbpmConfiguration.Configs.getString("resource.mail.templates");
       org.w3c.dom.Element mailTemplatesElement = XmlUtil.parseXmlResource(mailTemplatesResource, false).getDocumentElement();
       List mailTemplateElements = XmlUtil.elements(mailTemplatesElement, "mail-template");
       Iterator iter = mailTemplateElements.iterator();
-      while (iter.hasNext()) {
-        org.w3c.dom.Element mailTemplateElement = (org.w3c.dom.Element) iter.next();
+      while (iter.hasNext())
+      {
+        org.w3c.dom.Element mailTemplateElement = (org.w3c.dom.Element)iter.next();
 
         Properties templateProperties = new Properties();
         addTemplateProperty(mailTemplateElement, "actors", templateProperties);
@@ -293,47 +358,54 @@
       templateVariables = new HashMap();
       List variableElements = XmlUtil.elements(mailTemplatesElement, "variable");
       iter = variableElements.iterator();
-      while (iter.hasNext()) {
-        org.w3c.dom.Element variableElement = (org.w3c.dom.Element) iter.next();
+      while (iter.hasNext())
+      {
+        org.w3c.dom.Element variableElement = (org.w3c.dom.Element)iter.next();
         templateVariables.put(variableElement.getAttribute("name"), variableElement.getAttribute("value"));
       }
     }
-    return (Properties) templates.get(templateName);
+    return (Properties)templates.get(templateName);
   }
 
-  void addTemplateProperty(org.w3c.dom.Element mailTemplateElement, String property, Properties templateProperties) {
+  void addTemplateProperty(org.w3c.dom.Element mailTemplateElement, String property, Properties templateProperties)
+  {
     org.w3c.dom.Element element = XmlUtil.element(mailTemplateElement, property);
-    if (element!=null) {
+    if (element != null)
+    {
       templateProperties.put(property, XmlUtil.getContentText(element));
     }
   }
-  
-  String evaluate(String expression) {
-    if (expression==null) {
+
+  String evaluate(String expression)
+  {
+    if (expression == null)
+    {
       return null;
     }
     VariableResolver variableResolver = JbpmExpressionEvaluator.getUsedVariableResolver();
-    if (variableResolver!=null) {
+    if (variableResolver != null)
+    {
       variableResolver = new MailVariableResolver(templateVariables, variableResolver);
     }
-    return (String) JbpmExpressionEvaluator.evaluate(expression, executionContext, variableResolver,
-    	JbpmExpressionEvaluator.getUsedFunctionMapper());
+    return (String)JbpmExpressionEvaluator.evaluate(expression, executionContext, variableResolver, JbpmExpressionEvaluator.getUsedFunctionMapper());
   }
 
-  class MailVariableResolver implements VariableResolver, Serializable {
+  class MailVariableResolver implements VariableResolver, Serializable
+  {
     private static final long serialVersionUID = 1L;
     Map templateVariables = null;
     VariableResolver variableResolver = null;
 
-    public MailVariableResolver(Map templateVariables, VariableResolver variableResolver) {
+    public MailVariableResolver(Map templateVariables, VariableResolver variableResolver)
+    {
       this.templateVariables = templateVariables;
       this.variableResolver = variableResolver;
     }
 
-    public Object resolveVariable(String pName) throws ELException {
-      if ( (templateVariables!=null)
-           && (templateVariables.containsKey(pName))
-         ){
+    public Object resolveVariable(String pName) throws ELException
+    {
+      if ((templateVariables != null) && (templateVariables.containsKey(pName)))
+      {
         return templateVariables.get(pName);
       }
       return variableResolver.resolveVariable(pName);




More information about the jbpm-commits mailing list