buildframework/helium/sf/java/signaling/src/com/nokia/helium/signal/ant/types/EMAILNotifier.java
changeset 628 7c4a911dc066
parent 588 c7c26511138f
child 645 b8d81fa19e7d
equal deleted inserted replaced
588:c7c26511138f 628:7c4a911dc066
    16  */
    16  */
    17 
    17 
    18 package com.nokia.helium.signal.ant.types;
    18 package com.nokia.helium.signal.ant.types;
    19 
    19 
    20 import com.nokia.helium.core.EmailDataSender;
    20 import com.nokia.helium.core.EmailDataSender;
    21 import java.util.Iterator;
    21 
       
    22 import com.nokia.helium.core.EmailSendException;
    22 import com.nokia.helium.core.PropertiesSource;
    23 import com.nokia.helium.core.PropertiesSource;
    23 import com.nokia.helium.core.TemplateInputSource;
    24 import com.nokia.helium.core.TemplateInputSource;
    24 import com.nokia.helium.core.XMLTemplateSource;
       
    25 import com.nokia.helium.signal.Notifier;
    25 import com.nokia.helium.signal.Notifier;
    26 import com.nokia.helium.signal.ant.SignalListener;
       
    27 import com.nokia.helium.core.TemplateProcessor;
    26 import com.nokia.helium.core.TemplateProcessor;
    28 import com.nokia.helium.core.HlmAntLibException;
       
    29 import java.util.List;
    27 import java.util.List;
    30 import java.util.Hashtable;
    28 import java.util.Hashtable;
    31 import java.util.ArrayList;
    29 import java.util.ArrayList;
    32 import java.io.IOException;
    30 import java.io.IOException;
       
    31 
       
    32 import org.apache.tools.ant.BuildException;
    33 import org.apache.tools.ant.Project;
    33 import org.apache.tools.ant.Project;
    34 import org.apache.tools.ant.types.DataType;
    34 import org.apache.tools.ant.types.DataType;
    35 import org.apache.log4j.Logger;
       
    36 import java.io.File;
    35 import java.io.File;
    37 
    36 
    38 /**
    37 /**
    39  * The emailNotifier provides you an easy way to send a you and email containing
    38  * The emailNotifier provides you an easy way to send a you and email containing
    40  * the summary of a build failure.
    39  * the summary of a build failure.
    41  * 
    40  * 
    42  * @ant.type name="emailNotifier" category="Signaling"
    41  * @ant.type name="emailNotifier" category="Signaling"
    43  */
    42  */
    44 public class EMAILNotifier extends DataType implements Notifier {
    43 public class EMAILNotifier extends DataType implements Notifier {
    45 
    44 
    46     private Logger log = Logger.getLogger(EmailDataSender.class);
       
    47     private TemplateProcessor templateProcessor = new TemplateProcessor();
    45     private TemplateProcessor templateProcessor = new TemplateProcessor();
    48     private File defaultTemplate;
    46     private File defaultTemplate;
    49     private File templateSrc; // Deprecated    
       
    50     private String title;
    47     private String title;
    51     private String smtp;
    48     private String smtp;
    52     private String ldap;
    49     private String ldap;
    53     private String rootdn;
    50     private String rootdn;
    54     private String notifyWhen = "never";
    51     private String notifyWhen = "never";
    55     private String from;
    52     private String from;
    56     private String additionalRecipients;
    53     private String additionalRecipients;
    57 
    54 
    58     /**
    55     /**
    59      * Rendering the template, and sending the result through email.
    56      * Rendering the template, and sending the result through email.
    60      * @deprecated
    57      * 
    61      * @param signalName
    58      * @param signalName
    62      *            - Name of the signal that has been raised.
    59      *            - is the name of the signal that has been raised.
    63      */
    60      * @param failStatus
       
    61      *            - indicates whether to fail the build or not
       
    62      * @param notifierInput
       
    63      *            - contains signal notifier info
       
    64      * @param message
       
    65      *            - is the message from the signal that has been raised.
       
    66      */
       
    67 
    64     @SuppressWarnings("unchecked")
    68     @SuppressWarnings("unchecked")
    65     public void sendData(String signalName, boolean failStatus,
    69     public void sendData(String signalName, boolean failStatus,
    66             List<String> fileList) {
    70             NotifierInput notifierInput, String message) {
    67         if (notifyWhen != null
    71         if (notifyWhen != null
    68                 && (notifyWhen.equals("always") || (notifyWhen.equals("fail") && failStatus)
    72                 && (notifyWhen.equals("always")
    69                         || (notifyWhen.equals("pass") && !failStatus))) {
    73                         || (notifyWhen.equals("fail") && failStatus) || (notifyWhen
    70             if (templateSrc == null) {
    74                         .equals("pass") && !failStatus))) {
    71                 throw new HlmAntLibException(SignalListener.MODULE_NAME,
       
    72                         "templateSrc attribute has not been defined.");
       
    73             }
       
    74 
       
    75             if (title == null) {
    75             if (title == null) {
    76                 throw new HlmAntLibException(SignalListener.MODULE_NAME,
    76                 throw new BuildException(
    77                         "title attribute has not been defined.");
    77                         "The 'title' attribute has not been defined.");
    78             }
    78             }
    79 
    79 
    80             if (smtp == null) {
    80             if (smtp == null) {
    81                 throw new HlmAntLibException(SignalListener.MODULE_NAME,
    81                 throw new BuildException(
    82                         "smtp attribute has not been defined.");
    82                         "The 'smtp' attribute has not been defined.");
    83             }
    83             }
    84 
    84 
    85             if (ldap == null) {
    85             if (ldap == null && from == null) {
    86                 throw new HlmAntLibException(SignalListener.MODULE_NAME,
    86                 throw new BuildException(
    87                         "ldap attribute has not been defined.");
    87                         "The 'ldap' attribute has not been defined.");
    88             }
    88             }
    89 
    89             EmailDataSender emailSender = createEmailDataSender();
    90             log.debug("Sending data by e-mail.");
    90 
    91             File emailOutputFile;
       
    92             try {
       
    93                 emailOutputFile = File.createTempFile("helium_", "email.html");
       
    94                 emailOutputFile.deleteOnExit();
       
    95                 log.debug("sending data by e-mail:outputDir: "
       
    96                         + emailOutputFile.getAbsolutePath());
       
    97 
       
    98                 List<TemplateInputSource> sourceList = new ArrayList<TemplateInputSource>();
       
    99                 sourceList.add(new PropertiesSource("ant", getProject()
       
   100                         .getProperties()));
       
   101                 Iterator iter = fileList.iterator();
       
   102                 String sourceBaseName = "doc";
       
   103                 int count = 0;
       
   104                 while (iter.hasNext()) {
       
   105                     String srcFile = (String) iter.next();
       
   106                     sourceList.add(new XMLTemplateSource(sourceBaseName + count, 
       
   107                             new File(srcFile)));
       
   108                     count++;
       
   109                 }
       
   110                 Hashtable<String, String> signalProperties = new Hashtable<String, String>();
       
   111                 signalProperties.put("signal.name", signalName);
       
   112                 signalProperties.put("signal.status", "" + failStatus);
       
   113                 sourceList.add(new PropertiesSource("signaling",
       
   114                         signalProperties));
       
   115 
       
   116                 templateProcessor.convertTemplate(templateSrc, emailOutputFile,
       
   117                         sourceList);
       
   118                 EmailDataSender emailSender;
       
   119                 if (rootdn != null)
       
   120                 {
       
   121                     String[] to = null;
       
   122                     if (additionalRecipients != null)
       
   123                     {
       
   124                         to = additionalRecipients.split(",");
       
   125                     }
       
   126                     emailSender = new EmailDataSender(to, smtp, ldap, rootdn);
       
   127                 }
       
   128                 else
       
   129                 {
       
   130                     emailSender = new EmailDataSender(
       
   131                         additionalRecipients, smtp, ldap);
       
   132                 }
       
   133                 if (from != null)
       
   134                 {
       
   135                     emailSender.setFrom(from);
       
   136                 }
       
   137                 log.debug("EmailNotifier:arlist: " + additionalRecipients);
       
   138                 Project subProject = getProject().createSubProject();
       
   139                 subProject.setProperty("signal.name", signalName);
       
   140                 subProject.setProperty("signal.status", "" + failStatus);
       
   141                 emailSender.addCurrentUserToAddressList();
       
   142                 emailSender.sendData("signaling", emailOutputFile
       
   143                         .getAbsolutePath(), "application/html", subProject
       
   144                         .replaceProperties(title), null);
       
   145             } catch (IOException e) {
       
   146                 log.debug("EmailNotifier:IOexception: ", e);
       
   147             }
       
   148         }
       
   149     }
       
   150             
       
   151     
       
   152     /**
       
   153      * Rendering the template, and sending the result through email.
       
   154      * 
       
   155      * @param signalName - is the name of the signal that has been raised.
       
   156      * @param failStatus - indicates whether to fail the build or not
       
   157      * @param notifierInput - contains signal notifier info
       
   158      * @param message - is the message from the signal that has been raised. 
       
   159      */
       
   160 
       
   161     @SuppressWarnings("unchecked")
       
   162     public void sendData(String signalName, boolean failStatus,
       
   163             NotifierInput notifierInput, String message ) {
       
   164         if (notifyWhen != null
       
   165                 && (notifyWhen.equals("always") || (notifyWhen.equals("fail") && failStatus)
       
   166                         || (notifyWhen.equals("pass") && !failStatus))) {
       
   167             if (title == null) {
       
   168                 throw new HlmAntLibException(SignalListener.MODULE_NAME,
       
   169                         "title attribute has not been defined.");
       
   170             }
       
   171 
       
   172             if (smtp == null) {
       
   173                 throw new HlmAntLibException(SignalListener.MODULE_NAME,
       
   174                         "smtp attribute has not been defined.");
       
   175             }
       
   176 
       
   177             if (ldap == null) {
       
   178                 throw new HlmAntLibException(SignalListener.MODULE_NAME,
       
   179                         "ldap attribute has not been defined.");
       
   180             }
       
   181             
       
   182             String smtpUpdated = getProject().replaceProperties(smtp);
       
   183             String ldapUpdated = getProject().replaceProperties(ldap);
       
   184             String rootdnUpdated = getProject().replaceProperties(rootdn);
       
   185             String additionalRecipientsUpdated = getProject().replaceProperties(additionalRecipients);
       
   186 
       
   187             log.debug("Sending data by e-mail.");
       
   188             EmailDataSender emailSender;
       
   189             if (rootdnUpdated != null)
       
   190             {
       
   191                 String[] to = null;
       
   192                 if (additionalRecipientsUpdated != null)
       
   193                 {
       
   194                     to = additionalRecipientsUpdated.split(",");
       
   195                 }
       
   196                 emailSender = new EmailDataSender(to, smtpUpdated, ldapUpdated, rootdnUpdated);
       
   197             }
       
   198             else
       
   199             {
       
   200                 emailSender = new EmailDataSender(
       
   201                     additionalRecipientsUpdated, smtpUpdated, ldapUpdated);
       
   202             }
       
   203             if (from != null)
       
   204             {
       
   205                 emailSender.setFrom(from);
       
   206             }
       
   207             log.debug("EmailNotifier:arlist: " + additionalRecipientsUpdated);
       
   208             Project subProject = getProject().createSubProject();
    91             Project subProject = getProject().createSubProject();
   209             subProject.setProperty("signal.name", signalName);
    92             subProject.setProperty("signal.name", signalName);
   210             subProject.setProperty("signal.status", "" + failStatus);
    93             subProject.setProperty("signal.status", "" + failStatus);
   211             subProject.setProperty("signal.message", "" + message);
    94             subProject.setProperty("signal.message", "" + message);
   212             
    95             try {
   213             emailSender.addCurrentUserToAddressList();
    96                 
   214             String filePath = "";
    97                 File fileToSend = null;
   215             File fileToSend = null;
    98                 if (notifierInput != null) {
   216             if (notifierInput != null) {
    99                     fileToSend = notifierInput.getFile(".*.html");
   217                 fileToSend = notifierInput.getFile(".*.html");
       
   218                 if (fileToSend != null) {
       
   219                     filePath = fileToSend.toString();
       
   220                 }
   100                 }
   221                 
   101                 if (fileToSend == null) {
   222             } 
   102                     if (defaultTemplate != null && defaultTemplate.exists()) {
   223             if (fileToSend == null) {
   103                         File emailOutputFile;
   224                 File emailOutputFile;
   104                         try {
   225                 try {
   105                             emailOutputFile = File.createTempFile("helium_",
   226                     emailOutputFile = File.createTempFile("helium_", "email.html");
   106                                     "email.html");
   227                     emailOutputFile.deleteOnExit();
   107                             emailOutputFile.deleteOnExit();
   228                     log.debug("sending data by e-mail:outputDir: "
   108                             log("sending data by e-mail:outputDir: "
   229                             + emailOutputFile.getAbsolutePath());
   109                                     + emailOutputFile.getAbsolutePath(), Project.MSG_DEBUG);
   230 
   110 
   231                     List<TemplateInputSource> sourceList = new ArrayList<TemplateInputSource>();
   111                             List<TemplateInputSource> sourceList = new ArrayList<TemplateInputSource>();
   232                     sourceList.add(new PropertiesSource("ant", getProject()
   112                             sourceList.add(new PropertiesSource("ant",
   233                             .getProperties()));
   113                                     getProject().getProperties()));
   234                     Hashtable<String, String> signalProperties = new Hashtable<String, String>();
   114                             Hashtable<String, String> signalProperties = new Hashtable<String, String>();
   235                     signalProperties.put("signal.name", signalName);
   115                             signalProperties.put("signal.name", signalName);
   236                     signalProperties.put("signal.status", "" + failStatus);
   116                             signalProperties.put("signal.status", ""
   237                     signalProperties.put("signal.message", "" + message);
   117                                     + failStatus);
   238                     sourceList.add(new PropertiesSource("signaling",
   118                             signalProperties
   239                             signalProperties));
   119                                     .put("signal.message", "" + message);
   240 
   120                             sourceList.add(new PropertiesSource("signaling",
   241                     templateProcessor.convertTemplate(defaultTemplate, emailOutputFile,
   121                                     signalProperties));
   242                             sourceList);
   122 
   243                     filePath = emailOutputFile.toString();
   123                             templateProcessor.convertTemplate(defaultTemplate,
   244                 } catch (IOException e) {
   124                                     emailOutputFile, sourceList);
   245                     log.debug("EmailNotifier: IOexception: ", e);
   125                             fileToSend = emailOutputFile;
       
   126                         } catch (IOException e) {
       
   127                             log("EmailNotifier: IOexception: " + e.getMessage(), Project.MSG_DEBUG);
       
   128                         }
       
   129                     } else {
       
   130                         if (defaultTemplate == null) {
       
   131                             log("The 'defaultTemplate' has not been defined.",
       
   132                                     Project.MSG_WARN);
       
   133                         } else if (!defaultTemplate.exists()) {
       
   134                             log("Could not find default template: "
       
   135                                     + defaultTemplate.getAbsolutePath(),
       
   136                                     Project.MSG_WARN);
       
   137                         }
       
   138                     }
   246                 }
   139                 }
   247             }
   140                 emailSender.sendData("signaling", fileToSend, "application/html",
   248             emailSender.sendData("signaling", filePath, 
   141                         subProject.replaceProperties(title), null);
   249                     "application/html", subProject
   142             } catch (EmailSendException ese) {
   250                     .replaceProperties(title), null);
   143                 log(this.getDataTypeName() + " Warning: " + ese.getMessage(), Project.MSG_WARN);
   251         }
   144             }
   252     }
   145         }
   253 
   146     }
   254     /**
   147 
   255      * Set when the notifier should emit the massage. Possible values are: never, always, fail, pass.
   148     /**
       
   149      * Create an EmailDataSender base on this type settings.
       
   150      * @return
       
   151      */
       
   152     private EmailDataSender createEmailDataSender() {
       
   153         String smtpUpdated = getProject().replaceProperties(smtp);
       
   154         String ldapUpdated = getProject().replaceProperties(ldap);
       
   155         String additionalRecipientsUpdated = getProject()
       
   156                 .replaceProperties(additionalRecipients);
       
   157 
       
   158         log("Sending data by e-mail.", Project.MSG_DEBUG);
       
   159         EmailDataSender emailSender;
       
   160         if (additionalRecipientsUpdated == null) { 
       
   161             additionalRecipientsUpdated = from;
       
   162         } else {
       
   163             additionalRecipientsUpdated += (from != null) ? (additionalRecipientsUpdated.length() > 0 ? "," : "") + from : "";
       
   164         }
       
   165         if (rootdn != null) {
       
   166             String[] to = null;
       
   167             if (additionalRecipientsUpdated != null) {
       
   168                 to = additionalRecipientsUpdated.split(",");
       
   169             }
       
   170             emailSender = new EmailDataSender(to, smtpUpdated, ldapUpdated,
       
   171                     getProject().replaceProperties(rootdn));
       
   172         } else {
       
   173             emailSender = new EmailDataSender(additionalRecipientsUpdated,
       
   174                     smtpUpdated, ldapUpdated);
       
   175         }
       
   176         if (from == null) {
       
   177             try {
       
   178                 emailSender.addCurrentUserToAddressList();
       
   179             } catch (EmailSendException ex) {
       
   180                 // Consider the error as a warning, let's try to send the email anyway
       
   181                 log(this.getDataTypeName() + " Warning: " + ex.getMessage(), Project.MSG_WARN);
       
   182             }
       
   183         }
       
   184         if (from != null) {
       
   185             log("Setting from: " + from);
       
   186             emailSender.setFrom(from);
       
   187         }
       
   188         log("EmailNotifier:arlist: " + additionalRecipientsUpdated, Project.MSG_DEBUG);
       
   189         return emailSender;
       
   190     }
       
   191     
       
   192     /**
       
   193      * Set when the notifier should emit the massage. Possible values are:
       
   194      * never, always, fail, pass.
       
   195      * 
   256      * @ant.not-required Default is never.
   196      * @ant.not-required Default is never.
   257      */
   197      */
   258     public void setNotifyWhen(String ntfyWhen) {
   198     public void setNotifyWhen(NotifyWhenEnum notifyWhen) {
   259         notifyWhen = ntfyWhen;
   199         this.notifyWhen = notifyWhen.getValue();
   260     }
   200     }
   261 
   201 
       
   202     /**
       
   203      * When do we need to notify the user?
       
   204      * 
       
   205      * @return
       
   206      */
   262     public String getNotifyWhen() {
   207     public String getNotifyWhen() {
   263         return notifyWhen;
   208         return notifyWhen;
   264     }
   209     }
   265 
   210 
   266     /**
   211     /**
   272         this.defaultTemplate = template;
   217         this.defaultTemplate = template;
   273     }
   218     }
   274 
   219 
   275     /**
   220     /**
   276      * Define the template source file to use while rendering the message.
   221      * Define the template source file to use while rendering the message.
       
   222      * 
   277      * @deprecated
   223      * @deprecated
   278      * @ant.required
   224      * @ant.required
   279      */
   225      */
       
   226     @Deprecated
   280     public void setTemplateSrc(File template) {
   227     public void setTemplateSrc(File template) {
   281         this.templateSrc = template;
   228         log(
   282     }
   229                 "The usage of the templateSrc attribute is deprecated,"
   283     
   230                         + " please consider using the defaultTemplate attribute instead.",
       
   231                 Project.MSG_ERR);
       
   232         this.defaultTemplate = template;
       
   233     }
       
   234 
   284     /**
   235     /**
   285      * The title of the email.
   236      * The title of the email.
   286      * 
   237      * 
   287      * @ant.required
   238      * @ant.required
   288      */
   239      */
   296      * @ant.required
   247      * @ant.required
   297      */
   248      */
   298     public void setSmtp(String smtp) {
   249     public void setSmtp(String smtp) {
   299         this.smtp = smtp;
   250         this.smtp = smtp;
   300     }
   251     }
   301     
   252 
   302     /**
   253     /**
   303      * Who the email is sent from.
   254      * Who the email is sent from.
   304      * 
   255      * 
   305      * @ant.not-required
   256      * @ant.not-required
   306      */
   257      */
   318     }
   269     }
   319 
   270 
   320     /**
   271     /**
   321      * The LDAP server URL.
   272      * The LDAP server URL.
   322      * 
   273      * 
   323      * @ant.required
   274      * @ant.required (or from attribute can be used)
   324      */
   275      */
   325     public void setLdap(String ldap) {
   276     public void setLdap(String ldap) {
   326         this.ldap = ldap;
   277         this.ldap = ldap;
   327     }
   278     }
   328     
   279 
       
   280     /**
       
   281      * The LDAP rootdn.
       
   282      * @param rootdn
       
   283      * @ant.required
       
   284      */
   329     public void setRootdn(String rootdn) {
   285     public void setRootdn(String rootdn) {
   330         this.rootdn = rootdn;
   286         this.rootdn = rootdn;
   331     }
   287     }
   332 
   288 
   333 }
   289 }