1 10 11 package org.mule.providers.email; 12 13 import javax.mail.Flags ; 14 import javax.mail.Folder ; 15 import javax.mail.Message ; 16 import javax.mail.MessagingException ; 17 import javax.mail.Session ; 18 import javax.mail.Store ; 19 import javax.mail.URLName ; 20 21 import org.mule.impl.MuleMessage; 22 import org.mule.providers.AbstractMessageDispatcher; 23 import org.mule.umo.UMOEvent; 24 import org.mule.umo.UMOException; 25 import org.mule.umo.UMOMessage; 26 import org.mule.umo.endpoint.UMOEndpointURI; 27 import org.mule.umo.endpoint.UMOImmutableEndpoint; 28 import org.mule.umo.provider.UMOConnector; 29 30 35 36 public class Pop3MessageDispatcher extends AbstractMessageDispatcher 37 { 38 private Pop3Connector connector; 39 40 private Folder folder; 41 42 private Session session = null; 43 44 public Pop3MessageDispatcher(UMOImmutableEndpoint endpoint) 45 { 46 super(endpoint); 47 this.connector = (Pop3Connector)endpoint.getConnector(); 48 } 49 50 protected void doConnect(UMOImmutableEndpoint endpoint) throws Exception 51 { 52 53 if (folder == null || !folder.isOpen()) 54 { 55 String inbox = (String )endpoint.getProperty("folder"); 56 57 if (inbox == null || endpoint.getProtocol().toLowerCase().startsWith("pop3")) 58 { 59 inbox = Pop3Connector.MAILBOX; 60 } 61 62 UMOEndpointURI uri = endpoint.getEndpointURI(); 63 URLName url = new URLName (uri.getScheme(), uri.getHost(), uri.getPort(), inbox, 64 uri.getUsername(), uri.getPassword()); 65 66 session = MailUtils.createMailSession(url, connector); 67 session.setDebug(logger.isDebugEnabled()); 68 69 Store store = session.getStore(url); 70 store.connect(uri.getHost(), uri.getPort(), uri.getUsername(), uri.getPassword()); 71 72 folder = store.getFolder(inbox); 73 if (!folder.isOpen()) 74 { 75 try 76 { 77 folder.open(Folder.READ_WRITE); 82 } 83 catch (MessagingException e) 84 { 85 logger.warn("Failed to open folder: " + folder.getFullName(), e); 86 } 87 } 88 } 89 } 90 91 protected void doDisconnect() throws Exception 92 { 93 try 95 { 96 if (folder != null) 97 { 98 try 99 { 100 folder.expunge(); 101 } 102 catch (MessagingException e) 103 { 104 } 106 if (folder.isOpen()) 107 { 108 folder.close(true); 109 } 110 } 111 session = null; 112 } 113 catch (Exception e) 114 { 115 logger.error("Failed to close inbox: " + e.getMessage(), e); 116 } 117 } 118 119 123 protected void doDispatch(UMOEvent event) throws Exception 124 { 125 throw new UnsupportedOperationException ("Cannot dispatch from a Pop3 connection"); 126 } 127 128 133 protected UMOMessage doSend(UMOEvent event) throws Exception 134 { 135 throw new UnsupportedOperationException ("Cannot send from a Pop3 connection"); 136 } 137 138 151 protected synchronized UMOMessage doReceive(UMOImmutableEndpoint endpoint, long timeout) throws Exception 152 { 153 long t0 = System.currentTimeMillis(); 154 if (timeout < 0) 155 { 156 timeout = Long.MAX_VALUE; 157 } 158 159 do 160 { 161 if (hasMessages(folder)) 162 { 163 int count = getMessageCount(folder); 164 if (count > 0) 165 { 166 Message message = getNextMessage(folder); 167 flagMessage(folder, message); 169 170 return new MuleMessage(connector.getMessageAdapter(message)); 171 } 172 else if (count == -1) 173 { 174 throw new MessagingException ("Cannot monitor folder: " + folder.getFullName() 175 + " as folder is closed"); 176 } 177 } 178 179 long sleep = Math.min(this.connector.getCheckFrequency(), timeout 180 - (System.currentTimeMillis() - t0)); 181 182 if (sleep > 0) 183 { 184 if (logger.isDebugEnabled()) 185 { 186 logger.debug("No results, sleeping for " + sleep); 187 } 188 Thread.sleep(sleep); 189 } 190 else 191 { 192 logger.debug("Timeout"); 193 return null; 194 } 195 196 } 197 while (true); 198 } 199 200 208 protected void flagMessage(Folder folder, Message message) throws MessagingException 209 { 210 message.setFlag(Flags.Flag.DELETED, true); 211 } 212 213 protected Message getNextMessage(Folder folder) throws MessagingException 214 { 215 return folder.getMessage(1); 216 } 217 218 protected int getMessageCount(Folder folder) throws MessagingException 219 { 220 return folder.getMessageCount(); 221 } 222 223 232 protected boolean hasMessages(Folder folder) throws MessagingException 233 { 234 return getMessageCount(folder) > 0; 235 } 236 237 public Object getDelegateSession() throws UMOException 238 { 239 return session; 240 } 241 242 public UMOConnector getConnector() 243 { 244 return connector; 245 } 246 247 protected void doDispose() 248 { 249 } 251 } 252 | Popular Tags |