View Javadoc

1   /**
2    * This file Copyright (c) 2003-2011 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.module.mail.handlers;
35  
36  import info.magnolia.module.mail.templates.MgnlEmail;
37  
38  import java.util.ArrayList;
39  import java.util.Arrays;
40  
41  import javax.mail.Transport;
42  
43  import org.slf4j.Logger;
44  import org.slf4j.LoggerFactory;
45  
46  
47  /**
48   * This class is meant to allow async emails ... The mail is posted and send. Note that mail success or failure is only
49   * displayed in the logs. Date: Mar 31, 2006 Time: 5:51:38 PM
50   * @author <a href="mailto:niko@macnica.com">Nicolas Modrzyk</a>
51   */
52  public class ThreadedMailHandler implements MgnlMailHandler {
53  
54      Logger log = LoggerFactory.getLogger(ThreadedMailHandler.class);
55  
56      ArrayList<MgnlEmail> emails = new ArrayList<MgnlEmail>();
57  
58      MailThread thread; // can be replaced with a pool of thread someday
59  
60      private ThreadedMailHandler() {
61          this.thread = new MailThread();
62          Thread bb = new Thread(this.thread);
63          bb.start();
64      }
65  
66      protected void finalize() throws Throwable {
67          this.thread.setStop(true);
68          super.finalize();
69      }
70  
71      /**
72       * Prepare the email (format it) and send it.
73       * @param email the email to send
74       * @throws Exception if fails
75       */
76      public void prepareAndSendMail(MgnlEmail email) throws Exception {
77          email.setBodyNotSetFlag(true);
78          synchronized (this) {
79              this.emails.add(email);
80          }
81          this.thread.notify();
82      }
83  
84      /**
85       * Send the email as is, without touching it.
86       * @param email the email to send
87       * @throws Exception if fails
88       */
89      public void sendMail(MgnlEmail email) throws Exception {
90          synchronized (this) {
91              this.emails.add(email);
92          }
93          this.thread.notify();
94      }
95  
96      /**
97       * Thread doing all the job for sending emails and formatting the body.
98       */
99      class MailThread implements Runnable {
100 
101         boolean stop = false;
102 
103         public void run() {
104             while (!this.stop) {
105                 if (ThreadedMailHandler.this.emails.size() == 0) { // nothing to do just sleep
106                     synchronized (this) {
107                         try {
108                             wait();
109                         }
110                         catch (InterruptedException e) {
111                             ThreadedMailHandler.this.log.info("Mail Thread was interrupted");
112                         }
113                     }
114                 }
115                 else {
116                     MgnlEmail email = null;
117                     synchronized (this) {
118                         if (ThreadedMailHandler.this.emails.size() > 0) {
119                             email = ThreadedMailHandler.this.emails.remove(0);
120                         }
121                     }
122                     if (email != null) {
123                         try {
124                             if (email.isBodyNotSetFlag()) {
125                              //   email.setBody();
126                             }
127                             try {
128                                 Transport.send(email);
129                                 ThreadedMailHandler.this.log.info("Mail has been sent to: ["
130                                     + Arrays.asList(email.getAllRecipients())
131                                     + "]");
132                             }
133                             catch (Exception e) {
134                                 ThreadedMailHandler.this.log.error("Email to: ["
135                                     + Arrays.asList(email.getAllRecipients())
136                                     + "] was not sent because of an error", e);
137                             }
138                         }
139                         catch (Exception e) {
140                             e.printStackTrace();
141                         }
142                     }
143                 }
144 
145             }
146         }
147 
148         public boolean isStop() {
149             return this.stop;
150         }
151 
152         public void setStop(boolean stop) {
153             this.stop = stop;
154             this.notify();
155         }
156     }
157 }