package org.jboss.net.axis.transport.mailto;
import javax.mail.AuthenticationFailedException;
import javax.mail.FetchProfile;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Store;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.jboss.system.ServiceMBeanSupport;
public abstract class AbstractMailTransportService extends ServiceMBeanSupport implements MailConstants
{
public static final String FOLDER_NAME = "INBOX";
public static final String SESSION_NAME = "java:/Mail";
public static final String ENGINE_NAME = "jboss.net:service=Axis";
private String mailFolderName = FOLDER_NAME;
private String mailSessionName = SESSION_NAME;
private String EngineName = ENGINE_NAME;
private boolean deleteMail = true;
public void setSessionName(String name)
{
this.mailSessionName = name;
}
public String getSessionName()
{
return this.mailSessionName;
}
public void setFolderName(String name)
{
this.mailFolderName = name;
}
public String getFolderName()
{
return this.mailFolderName;
}
public void setEngineName(String name)
{
this.EngineName = name;
}
public String getEngineName()
{
return this.EngineName;
}
public void setDeleteMail(boolean delete)
{
this.deleteMail = delete;
}
public boolean getDeleteMail()
{
return this.deleteMail;
}
public void pollMail()
{
if (log.isDebugEnabled())
log.debug("Entering: pollMail()");
Session mail = getMailSession();
Store store = null;
if ((store = getMailStore(mail)) == null)
return;
Folder folder = null;
if ((folder = getMailFolder(store)) == null)
return;
try
{
Message[] msgs = fetchMessages(folder);
if (msgs.length > 0)
processMessages(msgs);
}
finally
{
closeFolder(folder);
closeStore(store);
}
if (log.isDebugEnabled())
log.debug("Leaving: pollMail()");
}
protected abstract void processMessages(Message[] msgs);
protected Session getMailSession()
{
if (log.isDebugEnabled())
log.debug("Entering: getMailSession()");
Context ctx = null;
Session mail = null;
try
{
ctx = new InitialContext();
mail = (Session) ctx.lookup(getSessionName());
}
catch (NamingException ne)
{
log.fatal("NamingException: getMailSession()\n", ne);
}
finally
{
if (ctx != null)
try
{
ctx.close();
}
catch (NamingException ne)
{
}
}
if (log.isDebugEnabled())
log.debug("Leaving: getMailSession()");
return mail;
}
protected Store getMailStore(Session mail)
{
Store store = null;
try
{
store = mail.getStore();
if (log.isDebugEnabled())
log.debug(store.getClass().getName());
}
catch (NoSuchProviderException e)
{
log.fatal("The mail session does not have a default provider! Check the mail.store.protocal "
+ "property in the mail-service.xml file", e);
}
try
{
if (store != null)
store.connect();
}
catch (AuthenticationFailedException e)
{
log.fatal("The User and/or Password defined in the mail-service.xml file could be wrong or missing", e);
closeStore(store);
store = null;
}
catch (MessagingException e)
{
log.fatal("Unable to connect to the mail store.", e);
closeStore(store);
store = null;
}
catch (IllegalStateException e)
{
log.warn("The store is already connected!", e);
}
return store;
}
protected Folder getMailFolder(Store store)
{
Folder folder = null;
try
{
folder = store.getFolder(getFolderName());
if (log.isDebugEnabled())
log.debug(folder.getClass().getName());
if (!folder.exists())
{
log.fatal("The folder '" + getFolderName() + "' doe not exist. Check the FolderName attribute in the "
+ "jboss-service.xml file.");
closeStore(store);
folder = null;
}
}
catch (MessagingException e)
{
log.fatal("Unable to retrieve the folder '" + getFolderName() + "' from the store. Check the FolderName "
+ "attribute in the jboss-service.xml file.", e);
closeStore(store);
folder = null;
}
try
{
folder.open(Folder.READ_WRITE);
}
catch (MessagingException e)
{
log.fatal("Failed to open the folder'" + getFolderName() + "'.", e);
closeStore(store);
folder = null;
}
return folder;
}
protected Message[] fetchMessages(Folder folder)
{
if (log.isDebugEnabled())
log.debug("Entering: fetchMessages(Folder folder)");
Message[] messages = new Message[0];
Store store = null;
try
{
if (log.isDebugEnabled())
log.debug("\nMessageCount: " + folder.getMessageCount());
if (folder.getMessageCount() > 0)
{
messages = folder.getMessages();
FetchProfile fp = new FetchProfile();
fp.add(FetchProfile.Item.CONTENT_INFO);
fp.add(FetchProfile.Item.ENVELOPE);
folder.fetch(messages, fp);
if (log.isDebugEnabled())
{
log.debug("getMailMessages(Session mail)\n\t retrieved " + messages.length + " message(s)");
}
}
else if (log.isDebugEnabled())
log.debug("getMailMessages(Session mail)\n\t no mail!");
}
catch (NoSuchProviderException e)
{
log.error("NoSuchProviderException: getMailMessages(Session mail)\n", e);
}
catch (MessagingException e)
{
log.error("MessagingException: getMailMessages(Session mail)\n", e);
}
finally
{
try
{
if (store != null)
store.close();
}
catch (MessagingException e)
{
}
}
if (log.isDebugEnabled())
log.debug("Leaving: fetchMessages(Folder folder)");
return messages;
}
protected void closeStore(Store store)
{
try
{
if (store != null)
store.close();
}
catch (Exception ignore)
{
}
return;
}
protected void closeFolder(Folder folder)
{
try
{
folder.close(true);
}
catch (Exception ignore)
{
}
return;
}
}