KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > net > axis > transport > mailto > AbstractMailTransportService


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  *
7  * Created on Mar 16, 2004
8  */

9 package org.jboss.net.axis.transport.mailto;
10
11 import javax.mail.AuthenticationFailedException JavaDoc;
12 import javax.mail.FetchProfile JavaDoc;
13 import javax.mail.Folder JavaDoc;
14 import javax.mail.Message JavaDoc;
15 import javax.mail.MessagingException JavaDoc;
16 import javax.mail.NoSuchProviderException JavaDoc;
17 import javax.mail.Session JavaDoc;
18 import javax.mail.Store JavaDoc;
19 import javax.naming.Context JavaDoc;
20 import javax.naming.InitialContext JavaDoc;
21 import javax.naming.NamingException JavaDoc;
22
23 import org.jboss.system.ServiceMBeanSupport;
24
25 /**
26  * <dl>
27  * <dt><b>Title: </b><dd>Abstract Mail Transport Service</dd>
28  * <p>
29  * <dt><b>Description: </b><dd>Both the client and server side email transport services are based off of this class.
30  * </dd>
31  * <p>
32  * </dl>
33  * @author <a HREF="mailto:jasone@greenrivercomputing.com">Jason Essington</a>
34  * @version $Revision: 1.1 $
35  *
36  * @jmx.mbean
37  * description="Abstract Mail Transport Service"
38  * extends="org.jboss.system.ServiceMBean"
39  */

40 public abstract class AbstractMailTransportService extends ServiceMBeanSupport implements MailConstants
41 {
42    public static final String JavaDoc FOLDER_NAME = "INBOX";
43    public static final String JavaDoc SESSION_NAME = "java:/Mail";
44    public static final String JavaDoc ENGINE_NAME = "jboss.net:service=Axis";
45
46    private String JavaDoc mailFolderName = FOLDER_NAME;
47    private String JavaDoc mailSessionName = SESSION_NAME;
48    private String JavaDoc EngineName = ENGINE_NAME;
49
50    private boolean deleteMail = true;
51
52    /**
53     * The jndi name under which the javax.mail.Session object is stored.
54     * @jmx.managed-attribute
55     */

56    public void setSessionName(String JavaDoc name)
57    {
58       this.mailSessionName = name;
59    }
60
61    /**
62     * @jmx.managed-attribute
63     * description = "The JNDI name of the mail session that will be used by this transport"
64     * value = "java:/Mail"
65     */

66    public String JavaDoc getSessionName()
67    {
68       return this.mailSessionName;
69    }
70
71    /**
72     * The name of the folder in which the SOAP messages will reside (likely INBOX).
73     * @jmx.managed-attribute
74     */

75    public void setFolderName(String JavaDoc name)
76    {
77       this.mailFolderName = name;
78    }
79
80    /**
81     * @jmx.managed-attribute
82     * description = "Folder in which incoming SOAP messages will be found"
83     * value = "INBOX"
84     */

85    public String JavaDoc getFolderName()
86    {
87       return this.mailFolderName;
88    }
89
90    /**
91     * The jndi name under which the org.apache.axis.server.AxisServer object is stored.
92     * @jmx.managed-attribute
93     */

94    public void setEngineName(String JavaDoc name)
95    {
96       this.EngineName = name;
97    }
98
99    /**
100     * @jmx.managed-attribute
101     * description = "The jmx ObjectName of the Axis Server"
102     * value = "jboss.net:service=Axis"
103     */

104    public String JavaDoc getEngineName()
105    {
106       return this.EngineName;
107    }
108
109    /**
110     * Flag instructing the transport to delete processed messages, or not.
111     * @jmx.managed-attribute
112     */

113    public void setDeleteMail(boolean delete)
114    {
115       this.deleteMail = delete;
116    }
117
118    /**
119     * @jmx.managed-attribute
120     * description = "Should processed messages be deleted?"
121     * value = "true"
122     */

123    public boolean getDeleteMail()
124    {
125       return this.deleteMail;
126    }
127
128    /**
129     * Check our pop mail box for new emails.
130     *
131     * maybe an exception of some sort should be thrown if something bad happens? currently all exceptions are logged
132     * and execution of this method terminates with nothing else happening.
133     *
134     * @jmx.managed-operation
135     */

136    public void pollMail()
137    {
138       if (log.isDebugEnabled())
139          log.debug("Entering: pollMail()");
140       Session JavaDoc mail = getMailSession();
141
142       // bail if there is a problem with our store or folder
143
Store JavaDoc store = null;
144       if ((store = getMailStore(mail)) == null)
145          return;
146
147       Folder JavaDoc folder = null;
148       if ((folder = getMailFolder(store)) == null)
149          return;
150
151       try
152       {
153          Message JavaDoc[] msgs = fetchMessages(folder);
154          //leave if there are no messages
155
if (msgs.length > 0)
156             processMessages(msgs);
157       }
158       finally
159       {
160          // now we are done, get out of here
161
closeFolder(folder);
162          closeStore(store);
163       }
164
165       if (log.isDebugEnabled())
166          log.debug("Leaving: pollMail()");
167    }
168
169    protected abstract void processMessages(Message JavaDoc[] msgs);
170
171    /**
172     * Fetch the mail session stored in jndi.
173     * @return Session
174     */

175    protected Session JavaDoc getMailSession()
176    {
177       if (log.isDebugEnabled())
178          log.debug("Entering: getMailSession()");
179
180       Context JavaDoc ctx = null;
181       Session JavaDoc mail = null;
182
183       try
184       {
185          ctx = new InitialContext JavaDoc();
186          mail = (Session JavaDoc) ctx.lookup(getSessionName());
187       }
188       catch (NamingException JavaDoc ne)
189       {
190          /** @TODO we should probably turn around and throw an exception of some sort here
191           * since there was some problem fetching the mail session from jndi */

192          log.fatal("NamingException: getMailSession()\n", ne);
193       }
194       finally
195       {
196          if (ctx != null)
197             try
198             {
199                ctx.close();
200             }
201             catch (NamingException JavaDoc ne)
202             {
203             }
204       }
205
206       if (log.isDebugEnabled())
207          log.debug("Leaving: getMailSession()");
208       return mail;
209    }
210
211    protected Store JavaDoc getMailStore(Session JavaDoc mail)
212    {
213       Store JavaDoc store = null;
214       try
215       {
216          store = mail.getStore();
217          if (log.isDebugEnabled())
218             log.debug(store.getClass().getName());
219       }
220       catch (NoSuchProviderException JavaDoc e)
221       {
222          // Could happen if <parameter name="mail.store.protocol" value="pop3"/> is not set
223
log.fatal("The mail session does not have a default provider! Check the mail.store.protocal "
224             + "property in the mail-service.xml file", e);
225       }
226
227       try
228       {
229          if (store != null)
230             store.connect();
231       }
232       catch (AuthenticationFailedException JavaDoc e)
233       {
234          // This could happen if the username and password defined in the mail-service.xml file are wrong or missing
235
log.fatal("The User and/or Password defined in the mail-service.xml file could be wrong or missing", e);
236          closeStore(store);
237          store = null;
238       }
239       catch (MessagingException JavaDoc e)
240       {
241          // This is a bad thing. We couldn't connect to the mail store for some reason.
242
log.fatal("Unable to connect to the mail store.", e);
243          closeStore(store);
244          store = null;
245       }
246       catch (IllegalStateException JavaDoc e)
247       {
248          // This means the store is already connected! I suppose since this is what we wanted any way we could
249
// go along our merry way?
250
log.warn("The store is already connected!", e);
251       }
252
253       return store;
254    }
255
256    protected Folder JavaDoc getMailFolder(Store JavaDoc store)
257    {
258       Folder JavaDoc folder = null;
259       try
260       {
261          folder = store.getFolder(getFolderName());
262
263          if (log.isDebugEnabled())
264             log.debug(folder.getClass().getName());
265
266          // we can only continue if this folder actually exists!
267
if (!folder.exists())
268          {
269             log.fatal("The folder '" + getFolderName() + "' doe not exist. Check the FolderName attribute in the "
270                + "jboss-service.xml file.");
271             // This was fatal, lets clean up and leave.
272
closeStore(store);
273             folder = null;
274          }
275       }
276       catch (MessagingException JavaDoc e)
277       {
278          log.fatal("Unable to retrieve the folder '" + getFolderName() + "' from the store. Check the FolderName "
279             + "attribute in the jboss-service.xml file.", e);
280          closeStore(store);
281          folder = null;
282       }
283
284       try
285       {
286          folder.open(Folder.READ_WRITE);
287       }
288       catch (MessagingException JavaDoc e)
289       {
290          // not sure what would cause this, but if it happens, it can't be good so log and leave
291
log.fatal("Failed to open the folder'" + getFolderName() + "'.", e);
292          closeStore(store);
293          folder = null;
294       }
295
296       return folder;
297    }
298
299    /**
300     * Lets preload some information about our mail messages into message objects.
301     *
302     * @param mail
303     * @return Message[]
304     */

305    protected Message JavaDoc[] fetchMessages(Folder JavaDoc folder)
306    {
307       if (log.isDebugEnabled())
308          log.debug("Entering: fetchMessages(Folder folder)");
309
310       Message JavaDoc[] messages = new Message JavaDoc[0];
311
312       Store JavaDoc store = null;
313
314       try
315       {
316          if (log.isDebugEnabled())
317             log.debug("\nMessageCount: " + folder.getMessageCount());
318
319          if (folder.getMessageCount() > 0)
320          {
321             messages = folder.getMessages();
322             FetchProfile JavaDoc fp = new FetchProfile JavaDoc();
323             fp.add(FetchProfile.Item.CONTENT_INFO);
324
325             //address information
326
fp.add(FetchProfile.Item.ENVELOPE);
327
328             folder.fetch(messages, fp);
329
330             if (log.isDebugEnabled())
331             {
332                log.debug("getMailMessages(Session mail)\n\t retrieved " + messages.length + " message(s)");
333             }
334          }
335          else if (log.isDebugEnabled())
336             log.debug("getMailMessages(Session mail)\n\t no mail!");
337       }
338       catch (NoSuchProviderException JavaDoc e)
339       {
340          log.error("NoSuchProviderException: getMailMessages(Session mail)\n", e);
341       }
342       catch (MessagingException JavaDoc e)
343       {
344          log.error("MessagingException: getMailMessages(Session mail)\n", e);
345       }
346       finally
347       {
348          try
349          {
350             if (store != null)
351                store.close();
352          }
353          catch (MessagingException JavaDoc e)
354          {
355          }
356       }
357
358       if (log.isDebugEnabled())
359          log.debug("Leaving: fetchMessages(Folder folder)");
360       return messages;
361    }
362
363    /**
364     * This closes the mail store and suppresses any exceptions. For use when bailing out due to an error of some sort.
365     *
366     * @param store
367     */

368    protected void closeStore(Store JavaDoc store)
369    {
370       try
371       {
372          if (store != null)
373             store.close();
374       }
375       catch (Exception JavaDoc ignore)
376       {
377       }
378       return;
379    }
380
381    /**
382     * This closes a mail folder and handles exceptions by pretending they didn't happen.
383     * Only do this if you are leaving and the resulting state of the folder is of no consequence to you.
384     *
385     * @param folder
386     */

387    protected void closeFolder(Folder JavaDoc folder)
388    {
389       try
390       {
391          // expunge deleted messages on close
392
folder.close(true);
393       }
394       catch (Exception JavaDoc ignore)
395       {
396       }
397       return;
398    }
399 }
400
Popular Tags