KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > binding > mail > listeners > MailBCExternalListener


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2006 EBM Websourcing, http://www.ebmwebsourcing.com/
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * -------------------------------------------------------------------------
19  * $Id: MailBCExternalListener.java 154 27 sept. 06 ofabre $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.binding.mail.listeners;
23
24 import java.util.Timer JavaDoc;
25 import java.util.TimerTask JavaDoc;
26 import java.util.logging.Level JavaDoc;
27 import java.util.logging.Logger JavaDoc;
28
29 import javax.jbi.JBIException;
30 import javax.mail.Folder JavaDoc;
31 import javax.mail.Message JavaDoc;
32 import javax.mail.MessagingException JavaDoc;
33 import javax.mail.Session JavaDoc;
34 import javax.mail.Store JavaDoc;
35 import javax.mail.Flags.Flag;
36
37 /**
38  * This class is used to monitor a mailbox for new mails (accept() method). It
39  * acts as a Listener. After new mail retrieval, the listener calls a
40  * 'process(MimeMessage)' method on the MimeMessageManager, which acts as a
41  * handler of MimeMessages.
42  *
43  * @author ofabre - EBM Websourcing
44  *
45  */

46 public class MailBCExternalListener extends TimerTask JavaDoc {
47
48     protected Timer JavaDoc t;
49
50     protected Logger JavaDoc logger;
51
52     protected String JavaDoc address;
53
54     protected SessionDescriptor sessionDescriptor;
55
56     protected MimeMessageManager processor;
57
58     protected MailSessionManager mailSessionManager;
59
60     /**
61      * Instantiate a {@link MailBCExternalListener} for a specific address
62      *
63      * @param t
64      * the timer use to schedule this listener
65      * @param log
66      * the component logger
67      * @param sessionDescriptor
68      * information about the mail session to open
69      * @param mailSessionManager
70      * the {@link MailSessionManager}
71      * @param processor
72      * the {@link MimeMessageManager}
73      * @param address
74      * URI linked to this listener
75      */

76     public MailBCExternalListener(Timer JavaDoc t, Logger JavaDoc log,
77             SessionDescriptor sessionDescriptor,
78             MailSessionManager mailSessionManager,
79             MimeMessageManager processor, String JavaDoc address) {
80         super();
81         this.logger = log;
82         this.t = t;
83         this.address = address;
84         this.mailSessionManager = mailSessionManager;
85         this.sessionDescriptor = sessionDescriptor;
86         this.processor = processor;
87     }
88
89     /**
90      * Create mail store and folder. Retrieve new mails and process them. Mail
91      * store and folder are opened (connected) and closed each time the run
92      * method is called. If the processing of a message fails, it is skipped and
93      * the processing of other messages continue. All processed messages are
94      * deleted from the folder.
95      */

96     public void run() {
97         Message JavaDoc[] messages = null;
98         Folder JavaDoc folder = null;
99         Store JavaDoc store = null;
100         try {
101             // Create a Session
102
Session JavaDoc session = mailSessionManager
103                     .createSessionPropertiesFromDescriptor(sessionDescriptor);
104
105             // Get mail store and connect to it
106
store = mailSessionManager.getStoreAndConnect(session,
107                     sessionDescriptor);
108
109             // Get mail folder and open it
110
folder = mailSessionManager.getFolderAndOpen(store,
111                     sessionDescriptor);
112
113             // receive messages
114
messages = accept(folder);
115
116             // Process messages. If the processing of a message fails, it is
117
// skipped and the processing of other messages continue
118
for (Message JavaDoc message : messages) {
119                 process(message, address);
120             }
121
122             // Delete message from the mail folder
123
folder.expunge();
124         } catch (JBIException e) {
125             logError("Error retrieving new mails", e);
126         } catch (MessagingException JavaDoc e) {
127             logWarning("Processed messages can't be deleted from the folder : "
128                     + folder, e);
129         } finally {
130             closeFolderAndStore(folder, store);
131         }
132
133     }
134
135     /**
136      * Search for new mails at linked address
137      *
138      * @return the new Messages
139      * @throws JBIException
140      * if new mail retrieval fails
141      */

142     protected Message JavaDoc[] accept(Folder JavaDoc folder) throws JBIException {
143         return mailSessionManager.getNewMails(folder);
144     }
145
146     protected void logError(String JavaDoc msg, Throwable JavaDoc e) {
147         if (logger != null)
148             logger.log(Level.SEVERE, e.getMessage(), e);
149         else
150             e.printStackTrace();
151     }
152
153     protected void logWarning(String JavaDoc msg, Throwable JavaDoc e) {
154         if (logger != null)
155             logger.log(Level.WARNING, e.getMessage(), e);
156         else
157             e.printStackTrace();
158     }
159
160     /**
161      * Call the processor's process() method. If the processing of a message
162      * fails, it is skipped (just log error) and the processing of other
163      * messages continue. In all cases (success or failure), the message is
164      * deleted from the mail folder.
165      *
166      * @param exchange
167      */

168     protected void process(Message JavaDoc message, String JavaDoc address) {
169         if (processor != null)
170             try {
171                 processor.process(message, address);
172             } catch (JBIException e) {
173                 String JavaDoc msg = "Error processing a mail : "
174                         + message.getMessageNumber();
175                 logError(msg, e);
176             } finally {
177                 try {
178                     message.setFlag(Flag.DELETED, true);
179                 } catch (MessagingException JavaDoc e) {
180                     logWarning("Mail message can't be marked as deleted. "
181                             + "It will not be deleted from the store", e);
182                 }
183             }
184     }
185
186     /**
187      * Close previously opened folder and store.
188      *
189      * @param folder
190      * the folder to close
191      * @param store
192      * the store to close
193      */

194     protected void closeFolderAndStore(Folder JavaDoc folder, Store JavaDoc store) {
195         try {
196             if ((folder != null) && (folder.isOpen())) {
197                 folder.close(false);
198             }
199         } catch (Exception JavaDoc ex) {
200             if (folder != null) {
201                 logWarning("Error closing mail Folder :" + folder.toString(),
202                         ex);
203             }
204         }
205         try {
206
207             if ((store != null) && (store.isConnected())) {
208                 store.close();
209             }
210         } catch (Exception JavaDoc ex) {
211             if (store != null) {
212                 logWarning("Error closing mail Store : " + store.toString(), ex);
213             }
214         }
215     }
216
217     public Timer JavaDoc getT() {
218         return t;
219     }
220
221 }
222
Popular Tags