001package org.kuali.ole.deliver.batch; 002 003import org.apache.commons.lang.StringUtils; 004import org.kuali.ole.deliver.processor.LoanProcessor; 005import org.kuali.rice.core.api.config.property.ConfigContext; 006import org.springframework.beans.factory.config.AbstractFactoryBean; 007import org.springframework.mail.javamail.JavaMailSenderImpl; 008 009import javax.mail.Authenticator; 010import javax.mail.PasswordAuthentication; 011import javax.mail.Session; 012import java.util.Properties; 013 014/** 015 * Created with IntelliJ IDEA. 016 * User: maheswarang 017 * Date: 4/23/13 018 * Time: 6:49 PM 019 * To change this template use File | Settings | File Templates. 020 */ 021public class OleMailSenderFactoryBean extends AbstractFactoryBean { 022 protected final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OleMailSenderFactoryBean.class); 023 private static final String MAIL_PREFIX = "mail"; 024 private static final String USERNAME_PROPERTY = "mail.smtp.username"; 025 private static final String PASSWORD_PROPERTY = "mail.smtp.password"; 026 private static final String HOST_PROPERTY = "mail.smtp.host"; 027 private static final String PORT_PROPERTY = "mail.smtp.port"; 028 private static final String PROTOCOL_PROPERTY = "mail.transport.protocol"; 029 private Session mailSession; 030 private String host; 031 private String port; 032 private String userName; 033 private String password; 034 private LoanProcessor loanProcessor = new LoanProcessor(); 035 036 public LoanProcessor getLoanProcessor() { 037 return loanProcessor; 038 } 039 040 public void setLoanProcessor(LoanProcessor loanProcessor) { 041 this.loanProcessor = loanProcessor; 042 } 043 044 public String getHost() { 045 return host; 046 } 047 048 public void setHost(String host) { 049 this.host = host; 050 } 051 052 public String getPort() { 053 return port; 054 } 055 056 public void setPort(String port) { 057 this.port = port; 058 } 059 060 public String getUserName() { 061 return userName; 062 } 063 064 public void setUserName(String userName) { 065 this.userName = userName; 066 } 067 068 public String getPassword() { 069 return password; 070 } 071 072 public void setPassword(String password) { 073 this.password = password; 074 } 075 076 @Override 077 protected Object createInstance() throws Exception { 078 // Retrieve "mail.*" properties from the configuration system and construct a Properties object 079 Properties properties = new Properties(); 080 Properties configProps = ConfigContext.getCurrentContextConfig().getProperties(); 081 LOG.debug("createInstance(): collecting mail properties."); 082 for (Object keyObj : configProps.keySet()) { 083 if (keyObj instanceof String) { 084 String key = (String) keyObj; 085 if (key.startsWith(MAIL_PREFIX)) { 086 properties.put(key, configProps.get(key)); 087 } 088 } 089 } 090 091 // Construct an appropriate Java Mail Session 092 // If username and password properties are found, construct a Session with SMTP authentication 093 094 095 if (host == null || (host != null && host.trim().isEmpty())) { 096 host = properties.getProperty(HOST_PROPERTY); 097 } 098 if (port == null || (port != null && port.trim().isEmpty())) { 099 port = properties.getProperty(PORT_PROPERTY); 100 } 101 if ((userName == null || (userName != null && userName.trim().isEmpty())) || ((password == null || (password != null && password.trim().isEmpty())))) { 102 userName = properties.getProperty(USERNAME_PROPERTY); 103 password = properties.getProperty(PASSWORD_PROPERTY); 104 } 105 106 107 108 /* String username = properties.getProperty(USERNAME_PROPERTY); 109 String password = properties.getProperty(PASSWORD_PROPERTY);*/ 110 if (userName != null && password != null) { 111 mailSession = Session.getInstance(properties, new SimpleAuthenticator(userName, password)); 112 LOG.debug("createInstance(): Initializing mail session using SMTP authentication."); 113 } else { 114 mailSession = Session.getInstance(properties); 115 LOG.debug("createInstance(): Initializing mail session. No SMTP authentication."); 116 } 117 118 // Construct and return a Spring Java Mail Sender 119 JavaMailSenderImpl mailSender = new JavaMailSenderImpl(); 120 LOG.debug("createInstance(): setting SMTP host."); 121 mailSender.setHost(host); 122 if (port != null) { 123 LOG.debug("createInstance(): setting SMTP port."); 124 int smtpPort = Integer.parseInt(port.trim()); 125 mailSender.setPort(smtpPort); 126 } 127 String protocol = properties.getProperty(PROTOCOL_PROPERTY); 128 if (StringUtils.isNotBlank(protocol)) { 129 if (LOG.isDebugEnabled()){ 130 LOG.debug("createInstance( ): setting mail transport protocol = " + protocol); 131 } 132 mailSender.setProtocol(protocol); 133 } 134 mailSender.setSession(mailSession); 135 136 LOG.debug("createInstance(): Mail Sender Factory Bean initialized."); 137 return mailSender; 138 } 139 140 private class SimpleAuthenticator extends Authenticator { 141 142 private final PasswordAuthentication passwordAuthentication; 143 144 private SimpleAuthenticator(String username, String password) { 145 this.passwordAuthentication = new PasswordAuthentication(username, password); 146 } 147 148 public PasswordAuthentication getPasswordAuthentication() { 149 return passwordAuthentication; 150 } 151 } 152 153 @SuppressWarnings("unchecked") 154 @Override 155 public Class getObjectType() { 156 return JavaMailSenderImpl.class; 157 } 158 159 160}