1 23 24 package org.glassfish.grizzly.async.javamail; 25 26 import com.sun.enterprise.web.connector.grizzly.AsyncExecutor; 27 import com.sun.enterprise.web.connector.grizzly.AsyncFilter; 28 import com.sun.enterprise.web.connector.grizzly.AsyncHandler; 29 import com.sun.enterprise.web.connector.grizzly.async.AsyncProcessorTask; 30 import com.sun.enterprise.web.connector.grizzly.ProcessorTask; 31 32 import java.util.Properties ; 33 import java.util.concurrent.ConcurrentHashMap ; 34 import java.util.concurrent.ConcurrentLinkedQueue ; 35 import java.util.concurrent.ScheduledThreadPoolExecutor ; 36 import java.util.concurrent.TimeUnit ; 37 import javax.mail.Folder ; 38 import javax.mail.Message ; 39 import javax.mail.Session ; 40 import javax.mail.Store ; 41 import javax.naming.InitialContext ; 42 43 import com.sun.enterprise.deployment.MailConfiguration; 44 import com.sun.mail.pop3.POP3SSLStore; 45 import javax.mail.URLName ; 46 import org.apache.coyote.Request; 47 48 56 public class JavaMailAsyncFilter implements AsyncFilter { 57 58 61 private static ConcurrentHashMap <String ,JavaMailAsyncFilterHandler> handlers 62 = new ConcurrentHashMap <String ,JavaMailAsyncFilterHandler>(); 63 64 65 68 private static ConcurrentLinkedQueue <MailFetcher> mailFetcherCache = 69 new ConcurrentLinkedQueue <MailFetcher>(); 70 71 72 76 private final static ScheduledThreadPoolExecutor scheduler 77 = new ScheduledThreadPoolExecutor (1); 78 79 80 83 private Session mailSession; 84 85 86 89 private Properties props; 90 91 92 94 95 98 public JavaMailAsyncFilter() { 99 try { 100 InitialContext ic = new InitialContext (); 101 String snName = "mail/MailSession"; 102 MailConfiguration mailConfig = 103 (MailConfiguration)ic.lookup(snName); 104 105 props = mailConfig.getMailProperties(); 106 props.setProperty("mail.pop3.ssl", "true"); 107 String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory"; 108 props.setProperty("mail.pop3.socketFactory.class", SSL_FACTORY); 109 props.setProperty("mail.pop3.socketFactory.fallback", "false"); 110 111 } catch (Throwable ex) { 112 ex.printStackTrace(); 113 } 114 } 115 116 117 120 public static void register(String contextPath, 121 JavaMailAsyncFilterHandler handler){ 122 handlers.put(contextPath,handler); 123 } 124 125 126 133 public boolean doFilter(AsyncExecutor asyncExecutor) { 134 AsyncProcessorTask asyncProcessorTask 135 = asyncExecutor.getAsyncProcessorTask(); 136 ProcessorTask processorTask = asyncProcessorTask.getProcessorTask(); 137 AsyncHandler asyncHandler = processorTask.getAsyncHandler(); 138 139 Request request = processorTask.getRequest(); 140 String contextPath = request.requestURI().toString(); 141 contextPath = contextPath.substring(contextPath.lastIndexOf("/")); 142 143 JavaMailAsyncFilterHandler handler = handlers.get(contextPath); 144 if ( handler == null){ 145 processorTask.invokeAdapter(); 146 return true; 147 } 148 MailFetcher mf = mailFetcherCache.poll(); 149 if ( mf == null ){ 150 mf = new MailFetcher(); 151 } 152 mf.handler = handler; 153 mf.processorTask = processorTask; 154 mf.asyncProcessorTask = asyncProcessorTask; 155 mf.asyncHandler = asyncHandler; 156 157 scheduler.schedule(mf,1L,TimeUnit.SECONDS); 158 return false; 159 } 160 161 162 166 private class MailFetcher implements Runnable { 167 168 171 JavaMailAsyncFilterHandler handler; 172 173 174 178 JavaMailAsyncFilterEvent event = new JavaMailAsyncFilterEvent(); 179 180 181 185 private ProcessorTask processorTask; 186 187 188 191 private AsyncProcessorTask asyncProcessorTask; 192 193 194 199 private AsyncHandler asyncHandler; 200 201 202 207 public void run(){ 208 Message [] messages = checkMail(); 209 210 event.setMessages(messages); 211 boolean continueCheckMail = handler.handleEvent(event); 212 if ( !continueCheckMail ) { 213 processorTask.invokeAdapter(); 214 asyncHandler.handle(asyncProcessorTask); 215 mailFetcherCache.offer(this); 216 } else { 217 scheduler.schedule(this,10L,TimeUnit.SECONDS); 218 } 219 } 220 221 222 225 private Message [] checkMail(){ 226 try{ 227 props.setProperty("mail.pop3.user", handler.getUserName()); 228 props.setProperty("mail.pop3.passwd", handler.getPassword()); 229 props.setProperty("mail.pop3.host", handler.getMailServer()); 230 props.setProperty("mail.pop3.port", handler.getMailServerPort()); 231 props.setProperty("mail.pop3.socketFactory.port", 232 handler.getMailServerPort()); 233 234 URLName url = new URLName ("pop3://" 235 + handler.getUserName() 236 +":"+ handler.getPassword() 237 +"@"+ handler.getMailServer() 238 +":"+ handler.getMailServerPort()); 239 240 mailSession = Session.getInstance(props, null); 241 Store store = new POP3SSLStore(mailSession, url); 242 store.connect(handler.getMailServer(), 243 handler.getUserName(), handler.getPassword()); 244 245 Folder folder = store.getDefaultFolder(); 246 folder = folder.getFolder("INBOX"); 247 248 folder.open(Folder.READ_ONLY); 249 250 return folder.getMessages(); 251 } catch (Throwable ex) { 252 ex.printStackTrace(); 253 return null; 254 } 255 } 256 } 257 } 258 | Popular Tags |