1 9 package org.jboss.net.axis.transport.mailto; 10 11 import javax.mail.AuthenticationFailedException ; 12 import javax.mail.FetchProfile ; 13 import javax.mail.Folder ; 14 import javax.mail.Message ; 15 import javax.mail.MessagingException ; 16 import javax.mail.NoSuchProviderException ; 17 import javax.mail.Session ; 18 import javax.mail.Store ; 19 import javax.naming.Context ; 20 import javax.naming.InitialContext ; 21 import javax.naming.NamingException ; 22 23 import org.jboss.system.ServiceMBeanSupport; 24 25 40 public abstract class AbstractMailTransportService extends ServiceMBeanSupport implements MailConstants 41 { 42 public static final String FOLDER_NAME = "INBOX"; 43 public static final String SESSION_NAME = "java:/Mail"; 44 public static final String ENGINE_NAME = "jboss.net:service=Axis"; 45 46 private String mailFolderName = FOLDER_NAME; 47 private String mailSessionName = SESSION_NAME; 48 private String EngineName = ENGINE_NAME; 49 50 private boolean deleteMail = true; 51 52 56 public void setSessionName(String name) 57 { 58 this.mailSessionName = name; 59 } 60 61 66 public String getSessionName() 67 { 68 return this.mailSessionName; 69 } 70 71 75 public void setFolderName(String name) 76 { 77 this.mailFolderName = name; 78 } 79 80 85 public String getFolderName() 86 { 87 return this.mailFolderName; 88 } 89 90 94 public void setEngineName(String name) 95 { 96 this.EngineName = name; 97 } 98 99 104 public String getEngineName() 105 { 106 return this.EngineName; 107 } 108 109 113 public void setDeleteMail(boolean delete) 114 { 115 this.deleteMail = delete; 116 } 117 118 123 public boolean getDeleteMail() 124 { 125 return this.deleteMail; 126 } 127 128 136 public void pollMail() 137 { 138 if (log.isDebugEnabled()) 139 log.debug("Entering: pollMail()"); 140 Session mail = getMailSession(); 141 142 Store store = null; 144 if ((store = getMailStore(mail)) == null) 145 return; 146 147 Folder folder = null; 148 if ((folder = getMailFolder(store)) == null) 149 return; 150 151 try 152 { 153 Message [] msgs = fetchMessages(folder); 154 if (msgs.length > 0) 156 processMessages(msgs); 157 } 158 finally 159 { 160 closeFolder(folder); 162 closeStore(store); 163 } 164 165 if (log.isDebugEnabled()) 166 log.debug("Leaving: pollMail()"); 167 } 168 169 protected abstract void processMessages(Message [] msgs); 170 171 175 protected Session getMailSession() 176 { 177 if (log.isDebugEnabled()) 178 log.debug("Entering: getMailSession()"); 179 180 Context ctx = null; 181 Session mail = null; 182 183 try 184 { 185 ctx = new InitialContext (); 186 mail = (Session ) ctx.lookup(getSessionName()); 187 } 188 catch (NamingException ne) 189 { 190 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 ne) 202 { 203 } 204 } 205 206 if (log.isDebugEnabled()) 207 log.debug("Leaving: getMailSession()"); 208 return mail; 209 } 210 211 protected Store getMailStore(Session mail) 212 { 213 Store store = null; 214 try 215 { 216 store = mail.getStore(); 217 if (log.isDebugEnabled()) 218 log.debug(store.getClass().getName()); 219 } 220 catch (NoSuchProviderException e) 221 { 222 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 e) 233 { 234 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 e) 240 { 241 log.fatal("Unable to connect to the mail store.", e); 243 closeStore(store); 244 store = null; 245 } 246 catch (IllegalStateException e) 247 { 248 log.warn("The store is already connected!", e); 251 } 252 253 return store; 254 } 255 256 protected Folder getMailFolder(Store store) 257 { 258 Folder folder = null; 259 try 260 { 261 folder = store.getFolder(getFolderName()); 262 263 if (log.isDebugEnabled()) 264 log.debug(folder.getClass().getName()); 265 266 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 closeStore(store); 273 folder = null; 274 } 275 } 276 catch (MessagingException 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 e) 289 { 290 log.fatal("Failed to open the folder'" + getFolderName() + "'.", e); 292 closeStore(store); 293 folder = null; 294 } 295 296 return folder; 297 } 298 299 305 protected Message [] fetchMessages(Folder folder) 306 { 307 if (log.isDebugEnabled()) 308 log.debug("Entering: fetchMessages(Folder folder)"); 309 310 Message [] messages = new Message [0]; 311 312 Store 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 fp = new FetchProfile (); 323 fp.add(FetchProfile.Item.CONTENT_INFO); 324 325 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 e) 339 { 340 log.error("NoSuchProviderException: getMailMessages(Session mail)\n", e); 341 } 342 catch (MessagingException 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 e) 354 { 355 } 356 } 357 358 if (log.isDebugEnabled()) 359 log.debug("Leaving: fetchMessages(Folder folder)"); 360 return messages; 361 } 362 363 368 protected void closeStore(Store store) 369 { 370 try 371 { 372 if (store != null) 373 store.close(); 374 } 375 catch (Exception ignore) 376 { 377 } 378 return; 379 } 380 381 387 protected void closeFolder(Folder folder) 388 { 389 try 390 { 391 folder.close(true); 393 } 394 catch (Exception ignore) 395 { 396 } 397 return; 398 } 399 } 400 | Popular Tags |