[jboss-cvs] JBossAS SVN: r104877 - branches/JBPAPP_4_3_0_GA_CP08_JBPAPP-4321/connector/src/main/org/jboss/resource/adapter/mail/inflow.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Mon May 17 13:11:29 EDT 2010
Author: jbertram at redhat.com
Date: 2010-05-17 13:11:29 -0400 (Mon, 17 May 2010)
New Revision: 104877
Added:
branches/JBPAPP_4_3_0_GA_CP08_JBPAPP-4321/connector/src/main/org/jboss/resource/adapter/mail/inflow/IMAPMailFolder.java
branches/JBPAPP_4_3_0_GA_CP08_JBPAPP-4321/connector/src/main/org/jboss/resource/adapter/mail/inflow/IMAPsMailFolder.java
branches/JBPAPP_4_3_0_GA_CP08_JBPAPP-4321/connector/src/main/org/jboss/resource/adapter/mail/inflow/POP3MailFolder.java
branches/JBPAPP_4_3_0_GA_CP08_JBPAPP-4321/connector/src/main/org/jboss/resource/adapter/mail/inflow/POP3sMailFolder.java
Modified:
branches/JBPAPP_4_3_0_GA_CP08_JBPAPP-4321/connector/src/main/org/jboss/resource/adapter/mail/inflow/MailActivation.java
branches/JBPAPP_4_3_0_GA_CP08_JBPAPP-4321/connector/src/main/org/jboss/resource/adapter/mail/inflow/MailActivationSpec.java
branches/JBPAPP_4_3_0_GA_CP08_JBPAPP-4321/connector/src/main/org/jboss/resource/adapter/mail/inflow/MailFolder.java
Log:
JBPAPP-4321
Copied: branches/JBPAPP_4_3_0_GA_CP08_JBPAPP-4321/connector/src/main/org/jboss/resource/adapter/mail/inflow/IMAPMailFolder.java (from rev 104749, branches/JBPAPP_4_2_0_GA_CP/connector/src/main/org/jboss/resource/adapter/mail/inflow/IMAPMailFolder.java)
===================================================================
--- branches/JBPAPP_4_3_0_GA_CP08_JBPAPP-4321/connector/src/main/org/jboss/resource/adapter/mail/inflow/IMAPMailFolder.java (rev 0)
+++ branches/JBPAPP_4_3_0_GA_CP08_JBPAPP-4321/connector/src/main/org/jboss/resource/adapter/mail/inflow/IMAPMailFolder.java 2010-05-17 17:11:29 UTC (rev 104877)
@@ -0,0 +1,50 @@
+package org.jboss.resource.adapter.mail.inflow;
+
+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.mail.Flags.Flag;
+
+public class IMAPMailFolder extends MailFolder {
+
+ public IMAPMailFolder(MailActivationSpec spec) {
+ super(spec);
+ }
+
+ protected Message[] getMessages(Folder folder) throws MessagingException {
+ if (folder.getUnreadMessageCount() > 0) {
+ int newCount = folder.getUnreadMessageCount();
+ int messageCount = folder.getMessageCount();
+ // folder.getMessages indexes from 1 and uses an inclusive range (ffs)
+ return folder.getMessages(messageCount - newCount + 1, messageCount);
+ }
+ else
+ {
+ return new Message[0];
+ }
+ }
+
+ protected Store openStore(Session session) throws NoSuchProviderException {
+ return session.getStore("imap");
+ }
+
+ protected void markMessageSeen(Message message) throws MessagingException {
+ message.setFlag(Flag.SEEN, true);
+ }
+
+ protected void closeStore(boolean success, Store store, Folder folder) throws MessagingException {
+ try {
+ if (folder != null && folder.isOpen()) {
+ folder.close(success);
+ }
+ } finally {
+ if (store != null && store.isConnected()) {
+ store.close();
+ }
+ }
+ }
+
+}
Copied: branches/JBPAPP_4_3_0_GA_CP08_JBPAPP-4321/connector/src/main/org/jboss/resource/adapter/mail/inflow/IMAPsMailFolder.java (from rev 104749, branches/JBPAPP_4_2_0_GA_CP/connector/src/main/org/jboss/resource/adapter/mail/inflow/IMAPsMailFolder.java)
===================================================================
--- branches/JBPAPP_4_3_0_GA_CP08_JBPAPP-4321/connector/src/main/org/jboss/resource/adapter/mail/inflow/IMAPsMailFolder.java (rev 0)
+++ branches/JBPAPP_4_3_0_GA_CP08_JBPAPP-4321/connector/src/main/org/jboss/resource/adapter/mail/inflow/IMAPsMailFolder.java 2010-05-17 17:11:29 UTC (rev 104877)
@@ -0,0 +1,38 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.resource.adapter.mail.inflow;
+
+import javax.mail.NoSuchProviderException;
+import javax.mail.Session;
+import javax.mail.Store;
+
+public class IMAPsMailFolder extends IMAPMailFolder {
+
+ public IMAPsMailFolder(MailActivationSpec spec) {
+ super(spec);
+ }
+
+ protected Store openStore(Session session) throws NoSuchProviderException {
+ return session.getStore("imaps");
+ }
+}
Modified: branches/JBPAPP_4_3_0_GA_CP08_JBPAPP-4321/connector/src/main/org/jboss/resource/adapter/mail/inflow/MailActivation.java
===================================================================
--- branches/JBPAPP_4_3_0_GA_CP08_JBPAPP-4321/connector/src/main/org/jboss/resource/adapter/mail/inflow/MailActivation.java 2010-05-17 16:52:18 UTC (rev 104876)
+++ branches/JBPAPP_4_3_0_GA_CP08_JBPAPP-4321/connector/src/main/org/jboss/resource/adapter/mail/inflow/MailActivation.java 2010-05-17 17:11:29 UTC (rev 104877)
@@ -117,12 +117,11 @@
log.trace("Begin new msgs check");
try
{
- MailFolder mailFolder = new MailFolder(spec);
+ MailFolder mailFolder = MailFolder.getInstance(spec);
mailFolder.open();
- Message[] msgs = mailFolder.getNewMessages();
- for(int n = 0; released == false && n < msgs.length; n ++)
+ while(mailFolder.hasNext())
{
- Message msg = msgs[n];
+ Message msg = (Message) mailFolder.next();
deliverMsg(msg);
}
mailFolder.close();
Modified: branches/JBPAPP_4_3_0_GA_CP08_JBPAPP-4321/connector/src/main/org/jboss/resource/adapter/mail/inflow/MailActivationSpec.java
===================================================================
--- branches/JBPAPP_4_3_0_GA_CP08_JBPAPP-4321/connector/src/main/org/jboss/resource/adapter/mail/inflow/MailActivationSpec.java 2010-05-17 16:52:18 UTC (rev 104876)
+++ branches/JBPAPP_4_3_0_GA_CP08_JBPAPP-4321/connector/src/main/org/jboss/resource/adapter/mail/inflow/MailActivationSpec.java 2010-05-17 17:11:29 UTC (rev 104877)
@@ -56,6 +56,13 @@
private long pollingInterval = 60000;
/** The maximum number of messages */
private int maxMessages = 1;
+ /** Enable JavaMail debugging*/
+ private boolean debug;
+ /** flush - for pop3 flush the mailbox after checking */
+ private boolean flush = true;
+ /** starttls - ssl */
+ private boolean starttls;
+ private int port;
public String getMailServer()
{
@@ -128,7 +135,47 @@
{
this.maxMessages = maxMessages;
}
+
+ public void setDebug(boolean debug)
+ {
+ this.debug = debug;
+ }
+
+ public boolean isDebug()
+ {
+ return debug;
+ }
+
+ public int getPort()
+ {
+ return port;
+ }
+
+ public void setPort(int port)
+ {
+ this.port = port;
+ }
+
+ public void setStarttls(boolean starttls)
+ {
+ this.starttls = starttls;
+ }
+
+ public boolean isStarttls()
+ {
+ return starttls;
+ }
+ public void setFlush(boolean flush)
+ {
+ this.flush = flush;
+ }
+
+ public boolean isFlush()
+ {
+ return flush;
+ }
+
public ResourceAdapter getResourceAdapter()
{
return ra;
@@ -161,6 +208,12 @@
tmp.append(userName);
tmp.append(", maxMessages=");
tmp.append(maxMessages);
+ tmp.append(", debug=");
+ tmp.append(debug);
+ tmp.append(", starttls=");
+ tmp.append(starttls);
+ tmp.append(", port=");
+ tmp.append(port);
tmp.append(")");
return tmp.toString();
}
Modified: branches/JBPAPP_4_3_0_GA_CP08_JBPAPP-4321/connector/src/main/org/jboss/resource/adapter/mail/inflow/MailFolder.java
===================================================================
--- branches/JBPAPP_4_3_0_GA_CP08_JBPAPP-4321/connector/src/main/org/jboss/resource/adapter/mail/inflow/MailFolder.java 2010-05-17 16:52:18 UTC (rev 104876)
+++ branches/JBPAPP_4_3_0_GA_CP08_JBPAPP-4321/connector/src/main/org/jboss/resource/adapter/mail/inflow/MailFolder.java 2010-05-17 17:11:29 UTC (rev 104877)
@@ -21,7 +21,10 @@
*/
package org.jboss.resource.adapter.mail.inflow;
+import java.util.Iterator;
import java.util.Properties;
+
+import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.Folder;
@@ -34,7 +37,7 @@
* @author Scott.Stark at jboss.org
* @version $Revision$
*/
-public class MailFolder
+public abstract class MailFolder implements Iterator
{
private Session session;
private Store store;
@@ -43,32 +46,49 @@
private String folderName;
private String userName;
private String password;
+ private int port;
+ private boolean debug;
+ private boolean starttls;
private Properties sessionProps;
+
+ private Message[] msgs = {};
+ private int messagePosition;
public MailFolder(MailActivationSpec spec)
- throws Exception
{
mailServer = spec.getMailServer();
- String protocol = spec.getStoreProtocol();
folderName = spec.getMailFolder();
userName = spec.getUserName();
password = spec.getPassword();
+ debug = spec.isDebug();
+ starttls = spec.isStarttls();
+ port = spec.getPort();
sessionProps = new Properties();
sessionProps.setProperty("mail.transport.protocol", "smtp");
- sessionProps.setProperty("mail.store.protocol", protocol);
sessionProps.setProperty("mail.smtp.host", mailServer);
- }
+ sessionProps.setProperty("mail.debug", debug + "");
+
+ // JavaMail doesn't implement POP3 STARTTLS
+ sessionProps.setProperty("mail.imap.starttls.enable", starttls + "");
+ }
public void open()
throws Exception
{
// Get a session object
session = Session.getInstance(sessionProps);
-
+ session.setDebug(debug);
// Get a store object
- store = session.getStore();
- store.connect(mailServer, userName, password);
+ store = openStore(session);
+ if (port == 0)
+ {
+ store.connect(mailServer, userName, password);
+ }
+ else
+ {
+ store.connect(mailServer, port, userName, password);
+ }
folder = store.getFolder(folderName);
if (folder == null || (!this.folder.exists()))
@@ -78,33 +98,61 @@
}
folder.open(Folder.READ_WRITE);
+ msgs = getMessages(folder);
}
+
+ protected abstract Store openStore(Session session) throws NoSuchProviderException;
+
+ protected abstract void closeStore(boolean success, Store store, Folder folder) throws MessagingException;
+
+ protected abstract Message[] getMessages(Folder folder) throws MessagingException;
+
+ protected abstract void markMessageSeen(Message message) throws MessagingException;
- public Message[] getNewMessages()
- throws Exception
- {
- Message msgs[] = {};
- /* This does not seem to be the most reliable new msg check. This should
- probably be unread msgs with the msgs marked as read on successful
- delivery.
- */
- if( folder.hasNewMessages() )
- {
- int newCount = folder.getNewMessageCount();
- int msgCount = folder.getMessageCount();
- msgs = folder.getMessages(msgCount - newCount + 1, msgCount);
- return msgs;
- }
- return msgs;
- }
-
public void close() throws MessagingException
{
- this.folder.close(true);
- this.store.close();
- this.folder = null;
- this.store = null;
- this.session = null;
+ close(true);
}
+ public boolean hasNext() {
+ return messagePosition < msgs.length;
+ }
+
+ public Object next() {
+ try {
+ Message m = msgs[messagePosition++];
+ markMessageSeen(m);
+ return m;
+ } catch (MessagingException e) {
+ close(false);
+ throw new RuntimeException(e);
+ }
+ }
+
+ public void remove() {
+ throw new UnsupportedOperationException();
+ }
+
+ protected void close(boolean checkSuccessful) {
+ try {
+ closeStore(checkSuccessful, store, folder);
+ } catch (MessagingException e) {
+ throw new RuntimeException("Error closing mail store", e);
+ }
+ }
+
+ public static MailFolder getInstance(MailActivationSpec mailActivationSpec) {
+ if ("pop3".equals(mailActivationSpec.getStoreProtocol())) {
+ return new POP3MailFolder(mailActivationSpec);
+ } else if ("imap".equals(mailActivationSpec.getStoreProtocol())) {
+ return new IMAPMailFolder(mailActivationSpec);
+ } else if ("pop3s".equals(mailActivationSpec.getStoreProtocol())) {
+ return new POP3sMailFolder(mailActivationSpec);
+ } else if ("imaps".equals(mailActivationSpec.getStoreProtocol())) {
+ return new IMAPsMailFolder(mailActivationSpec);
+ } else {
+ return null;
+ }
+ }
+
}
Copied: branches/JBPAPP_4_3_0_GA_CP08_JBPAPP-4321/connector/src/main/org/jboss/resource/adapter/mail/inflow/POP3MailFolder.java (from rev 104749, branches/JBPAPP_4_2_0_GA_CP/connector/src/main/org/jboss/resource/adapter/mail/inflow/POP3MailFolder.java)
===================================================================
--- branches/JBPAPP_4_3_0_GA_CP08_JBPAPP-4321/connector/src/main/org/jboss/resource/adapter/mail/inflow/POP3MailFolder.java (rev 0)
+++ branches/JBPAPP_4_3_0_GA_CP08_JBPAPP-4321/connector/src/main/org/jboss/resource/adapter/mail/inflow/POP3MailFolder.java 2010-05-17 17:11:29 UTC (rev 104877)
@@ -0,0 +1,45 @@
+package org.jboss.resource.adapter.mail.inflow;
+
+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.mail.Flags.Flag;
+
+public class POP3MailFolder extends MailFolder {
+
+ private boolean flush;
+
+ public POP3MailFolder(MailActivationSpec spec) {
+ super(spec);
+ this.flush = spec.isFlush();
+ }
+
+ protected Message[] getMessages(Folder folder) throws MessagingException {
+ return folder.getMessages();
+ }
+
+ protected Store openStore(Session session) throws NoSuchProviderException {
+ return session.getStore("pop3");
+ }
+
+ protected void markMessageSeen(Message message) throws MessagingException {
+ message.setFlag(Flag.DELETED, true);
+ }
+
+ protected void closeStore(boolean success, Store store, Folder folder) throws MessagingException {
+ try {
+ if (folder != null && folder.isOpen()) {
+ folder.close(success && flush);
+ }
+ } finally {
+ if (store != null && store.isConnected()) {
+ store.close();
+ }
+ }
+
+ }
+
+}
Copied: branches/JBPAPP_4_3_0_GA_CP08_JBPAPP-4321/connector/src/main/org/jboss/resource/adapter/mail/inflow/POP3sMailFolder.java (from rev 104749, branches/JBPAPP_4_2_0_GA_CP/connector/src/main/org/jboss/resource/adapter/mail/inflow/POP3sMailFolder.java)
===================================================================
--- branches/JBPAPP_4_3_0_GA_CP08_JBPAPP-4321/connector/src/main/org/jboss/resource/adapter/mail/inflow/POP3sMailFolder.java (rev 0)
+++ branches/JBPAPP_4_3_0_GA_CP08_JBPAPP-4321/connector/src/main/org/jboss/resource/adapter/mail/inflow/POP3sMailFolder.java 2010-05-17 17:11:29 UTC (rev 104877)
@@ -0,0 +1,38 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.resource.adapter.mail.inflow;
+
+import javax.mail.NoSuchProviderException;
+import javax.mail.Session;
+import javax.mail.Store;
+
+public class POP3sMailFolder extends POP3MailFolder {
+
+ public POP3sMailFolder(MailActivationSpec spec) {
+ super(spec);
+ }
+
+ protected Store openStore(Session session) throws NoSuchProviderException {
+ return session.getStore("pop3s");
+ }
+}
More information about the jboss-cvs-commits
mailing list