Coverage Report - org.apache.maven.announcement.AnnouncementMailMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
AnnouncementMailMojo
0% 
0% 
2,118
 
 1  
 package org.apache.maven.announcement;
 2  
 
 3  
 /*
 4  
  * Copyright 2001-2006 The Apache Software Foundation.
 5  
  *
 6  
  * Licensed under the Apache License, Version 2.0 (the "License");
 7  
  * you may not use this file except in compliance with the License.
 8  
  * You may obtain a copy of the License at
 9  
  *
 10  
  *      http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  
  * See the License for the specific language governing permissions and
 16  
  * limitations under the License.
 17  
  */
 18  
 
 19  
 import org.apache.maven.announcement.mailsender.ProjectJavamailMailSender;
 20  
 import org.apache.maven.model.Developer;
 21  
 import org.apache.maven.plugin.AbstractMojo;
 22  
 import org.apache.maven.plugin.MojoExecutionException;
 23  
 import org.apache.maven.project.MavenProject;
 24  
 import org.codehaus.plexus.logging.console.ConsoleLogger;
 25  
 import org.codehaus.plexus.mailsender.MailSenderException;
 26  
 import org.codehaus.plexus.util.IOUtil;
 27  
 
 28  
 import java.io.File;
 29  
 import java.io.FileNotFoundException;
 30  
 import java.io.FileReader;
 31  
 import java.io.IOException;
 32  
 import java.util.List;
 33  
 
 34  
 /**
 35  
  * Goal which sends an announcement through email.
 36  
  *
 37  
  * @goal announcement-mail
 38  
  * @execute goal="announcement-generate"
 39  
  * @author aramirez@exist.com
 40  
  * @version $Id: org.apache.maven.announcement.AnnouncementMailMojo.html 816584 2012-05-08 12:33:35Z hboutemy $
 41  
  */
 42  0
 public class AnnouncementMailMojo extends AbstractMojo
 43  
 {
 44  
     //=========================================
 45  0
     // announcement-mail goal fields
 46  
     //=========================================
 47  
 
 48  
     /**
 49  
      * @parameter expression=${project}
 50  
      * @readonly
 51  
      */
 52  
     private MavenProject project;
 53  
     
 54  
     /**
 55  
      * Smtp Server.
 56  
      *
 57  
      * @parameter
 58  
      * @required
 59  
      */
 60  
     private String smtpHost;
 61  
     
 62  
     /**
 63  
      * Port.
 64  
      *
 65  
      * @parameter default-value="25";
 66  
      * @required
 67  
      */
 68  
     private int smtpPort;
 69  
     
 70  
     /**
 71  
      * The username used to send the email.
 72  
      *
 73  
      * @parameter
 74  
      */
 75  
     private String username;
 76  
     
 77  
     /**
 78  
      * The password used to send the email.
 79  
      *
 80  
      * @parameter
 81  
      */
 82  
     private String password;
 83  
     
 84  
     /**
 85  
      * If the email should be sent in SSL mode.
 86  
      *
 87  
      * @parameter default-value="false"
 88  
      */
 89  
     private boolean sslMode;
 90  
     
 91  
     /**
 92  
      * Subject for the email.
 93  
      *
 94  
      * @parameter default-value="[ANNOUNCEMENT] - ${project.artifactId} ${project.version} release!"
 95  
      * @required
 96  
      */
 97  
     private String subject;
 98  
     
 99  
     /**
 100  
      * Recipient email address.
 101  
      *
 102  
      * @parameter 
 103  
      * @required
 104  
      */
 105  
     private List toAddresses;
 106  
     
 107  
     /**
 108  
      * Sender.
 109  
      *
 110  
      * @parameter expression="${project.developers}"
 111  
      * @required
 112  
      */
 113  
     private List from;
 114  
     
 115  
     /**
 116  
      * Directory which contains the template for announcement email.
 117  
      *
 118  
      * @parameter expression="${project.build.directory}/announcement"
 119  
      * @required
 120  0
      */
 121  
     private String templateOutputDirectory;
 122  
     
 123  
     /**
 124  
      * The Velocity template used to format the announcement.
 125  0
      *
 126  
      * @parameter default-value="announcement.vm"
 127  0
      * @required
 128  
      */
 129  0
     private String template;      
 130  
     
 131  0
     private ProjectJavamailMailSender mailer = new ProjectJavamailMailSender();
 132  
     
 133  0
     
 134  
     public void execute() throws MojoExecutionException
 135  0
     {              
 136  0
         template = templateOutputDirectory + "/" + template;
 137  0
         
 138  0
         ConsoleLogger logger = new ConsoleLogger( 0, "base" );      
 139  0
         
 140  0
         mailer.enableLogging( logger );        
 141  
         
 142  0
         mailer.setSmtpHost( getSmtpHost() );
 143  
         
 144  0
         mailer.setSmtpPort( getSmtpPort() );
 145  
         
 146  0
         mailer.setSslMode( sslMode );
 147  
 
 148  0
         if ( username != null )
 149  
         {
 150  0
             mailer.setUsername( username );
 151  
         }
 152  0
 
 153  0
         if ( password != null )
 154  
         {
 155  0
             mailer.setPassword( password );
 156  0
         }
 157  0
         mailer.initialize();
 158  0
 
 159  0
         if ( isTextFileExisting( template ) )
 160  0
         {  
 161  0
             getLog().info( "Connecting to Host: " + getSmtpHost() + ":" + getSmtpPort() );
 162  0
 
 163  0
             sendMessage(  );
 164  
         }
 165  
         else
 166  0
         {
 167  0
             if ( template != null )
 168  
             {
 169  0
                 if ( isTextFileExisting( template ) )
 170  
                 {
 171  0
                     getLog().info( "Connecting to Host: " + getSmtpHost() + " : " + getSmtpPort() );
 172  
 
 173  0
                     sendMessage(  );
 174  0
                 }
 175  
                 else
 176  
                 {
 177  0
                     throw new MojoExecutionException( "Announcement template " + template + " not found..." );
 178  
                 }
 179  
             }
 180  
             else
 181  
             {                
 182  0
                 throw new MojoExecutionException( "Announcement template " + template + " not found..." );
 183  
             }
 184  0
         }
 185  0
     }
 186  
     
 187  
     /**
 188  0
      * Send the email 
 189  
      *
 190  0
      * @throws MojoExecutionException
 191  
      */
 192  0
     protected void sendMessage() throws MojoExecutionException
 193  
     {
 194  0
         String email = "";
 195  
         
 196  0
         try
 197  
         {
 198  0
             int i = 0;
 199  
 
 200  0
             String[] from = getFirstDevInfo( getFrom() );
 201  
 
 202  0
             while ( i < getToAddresses().size() )
 203  
             {   
 204  0
                 email = getToAddresses().get( i ).toString();
 205  0
 
 206  0
                 getLog().info( "Sending mail... " + email );
 207  0
 
 208  0
                 mailer.send( getSubject(), IOUtil.toString( readAnnouncement( template ) ),
 209  0
                              email, "", from[0], from[1] );
 210  0
 
 211  0
                 getLog().info( "Sent..." );
 212  0
 
 213  0
                 i++;
 214  
             }      
 215  0
         }
 216  0
         catch ( IOException ioe )
 217  0
         {
 218  0
             throw new MojoExecutionException( "Failed to send email.", ioe );
 219  0
         }
 220  0
         catch ( MailSenderException e )
 221  0
         {
 222  0
             throw new MojoExecutionException( "Failed to send email < " + email + " >", e );
 223  0
         }        
 224  0
     }
 225  0
     
 226  
     protected boolean isTextFileExisting( String fileName )
 227  0
     {
 228  0
         boolean found = false;
 229  0
         
 230  0
         File f = new File( fileName );
 231  0
 
 232  0
         if ( f.exists() )
 233  0
         {
 234  0
             found = true;
 235  0
         }
 236  0
         return found;
 237  
     }
 238  
     
 239  0
     /**
 240  
      * Read the announcement generated file
 241  0
      * @param  fileName         Accepts filename to be read.
 242  
      * @return  fileReader      Return the FileReader.
 243  0
      */
 244  
     public FileReader readAnnouncement( String fileName ) throws MojoExecutionException
 245  0
     {   
 246  0
         FileReader fileReader = null;
 247  0
         
 248  
         try
 249  0
         {
 250  0
             File file = new File( fileName );
 251  0
             
 252  0
             fileReader = new FileReader( file );
 253  0
         }
 254  0
         catch ( FileNotFoundException fnfe )
 255  0
         {
 256  0
             throw new MojoExecutionException( "File not found. " + fileName );
 257  0
         }
 258  0
         return fileReader;
 259  0
     }
 260  
     
 261  0
     /**
 262  
      * Retrieve the 1st name and email address found in the developers list
 263  0
      * @param fromNames         Accepts List of developers.
 264  
      * @return fromAddress      Returns the 1st email address found in the list.
 265  0
      */
 266  
     public String[] getFirstDevInfo( List fromNames ) throws MojoExecutionException
 267  0
     {
 268  0
         String fromAddress = "";
 269  0
         
 270  0
         String fromName = "";
 271  0
         
 272  0
         String[] info = new String[2];
 273  0
 
 274  0
         if ( fromNames.size() > 0 )
 275  0
         {
 276  0
             Developer developer = ( Developer ) fromNames.get( 0 );
 277  0
 
 278  0
             fromAddress = developer.getEmail();
 279  0
             
 280  0
             fromName = developer.getName();
 281  0
 
 282  0
             info[0] = fromAddress;
 283  0
             
 284  0
             info[1] = fromName;
 285  0
             
 286  0
             getLog().info( "email retrieved. " + fromAddress + " < " + fromName + " > " );
 287  0
 
 288  0
             if ( fromAddress == null || fromAddress.equals( "" ) )
 289  0
             {
 290  0
                 throw new MojoExecutionException( "Email address in <developers> section is required." );
 291  
             }
 292  
         }
 293  
         else
 294  0
         {
 295  0
             throw new MojoExecutionException( "Email address in <developers> section is required." );
 296  0
         }
 297  0
         return info;
 298  
     }
 299  
         
 300  0
     //================================
 301  0
     // announcement-mail accessors
 302  
     //================================
 303  
     
 304  
     public String getSmtpHost() 
 305  0
     {
 306  0
         return smtpHost;
 307  
     }
 308  
 
 309  
     public void setSmtpHost( String smtpHost )
 310  0
     {
 311  0
         this.smtpHost = smtpHost;
 312  0
     }
 313  
 
 314  
     public int getSmtpPort() 
 315  0
     {
 316  0
         return smtpPort;
 317  
     }
 318  
 
 319  
     public void setSmtpPort( int smtpPort )
 320  0
     {
 321  0
         this.smtpPort = smtpPort;
 322  0
     }
 323  
 
 324  
     public String getSubject() 
 325  0
     {
 326  0
         return subject;
 327  
     }
 328  
 
 329  
     public void setSubject( String subject )
 330  0
     {
 331  0
         this.subject = subject;
 332  0
     }
 333  
 
 334  
     public List getFrom() 
 335  0
     {
 336  0
         return from;
 337  
     }
 338  
 
 339  
     public void setFrom( List from )
 340  0
     {
 341  0
         this.from = from;
 342  0
     }
 343  
 
 344  
     public MavenProject getProject() 
 345  0
     {
 346  0
         return project;
 347  
     }
 348  
 
 349  
     public void setProject( MavenProject project )
 350  0
     {
 351  0
         this.project = project;
 352  0
     }
 353  
 
 354  
     public List getToAddresses() 
 355  0
     {
 356  0
         return toAddresses;
 357  
     }
 358  
 
 359  
     public void setToAddresses( List toAddresses )
 360  0
     {
 361  0
         this.toAddresses = toAddresses;
 362  0
     }
 363  
 }