1 package org.kuali.ole.deliver.batch;
2
3 import org.apache.commons.lang.StringUtils;
4 import org.kuali.ole.deliver.processor.LoanProcessor;
5 import org.kuali.rice.core.api.config.property.ConfigContext;
6 import org.springframework.beans.factory.config.AbstractFactoryBean;
7 import org.springframework.mail.javamail.JavaMailSenderImpl;
8
9 import javax.mail.Authenticator;
10 import javax.mail.PasswordAuthentication;
11 import javax.mail.Session;
12 import java.util.Properties;
13
14
15
16
17
18
19
20
21 public class OleMailSenderFactoryBean extends AbstractFactoryBean {
22 protected final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OleMailSenderFactoryBean.class);
23 private static final String MAIL_PREFIX = "mail";
24 private static final String USERNAME_PROPERTY = "mail.smtp.username";
25 private static final String PASSWORD_PROPERTY = "mail.smtp.password";
26 private static final String HOST_PROPERTY = "mail.smtp.host";
27 private static final String PORT_PROPERTY = "mail.smtp.port";
28 private static final String PROTOCOL_PROPERTY = "mail.transport.protocol";
29 private Session mailSession;
30 private String host;
31 private String port;
32 private String userName;
33 private String password;
34 private LoanProcessor loanProcessor = new LoanProcessor();
35
36 public LoanProcessor getLoanProcessor() {
37 return loanProcessor;
38 }
39
40 public void setLoanProcessor(LoanProcessor loanProcessor) {
41 this.loanProcessor = loanProcessor;
42 }
43
44 public String getHost() {
45 return host;
46 }
47
48 public void setHost(String host) {
49 this.host = host;
50 }
51
52 public String getPort() {
53 return port;
54 }
55
56 public void setPort(String port) {
57 this.port = port;
58 }
59
60 public String getUserName() {
61 return userName;
62 }
63
64 public void setUserName(String userName) {
65 this.userName = userName;
66 }
67
68 public String getPassword() {
69 return password;
70 }
71
72 public void setPassword(String password) {
73 this.password = password;
74 }
75
76 @Override
77 protected Object createInstance() throws Exception {
78
79 Properties properties = new Properties();
80 Properties configProps = ConfigContext.getCurrentContextConfig().getProperties();
81 LOG.debug("createInstance(): collecting mail properties.");
82 for (Object keyObj : configProps.keySet()) {
83 if (keyObj instanceof String) {
84 String key = (String) keyObj;
85 if (key.startsWith(MAIL_PREFIX)) {
86 properties.put(key, configProps.get(key));
87 }
88 }
89 }
90
91
92
93
94
95 if (host == null || (host != null && host.trim().isEmpty())) {
96 host = properties.getProperty(HOST_PROPERTY);
97 }
98 if (port == null || (port != null && port.trim().isEmpty())) {
99 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
109
110 if (userName != null && password != null) {
111 mailSession = Session.getInstance(properties, new SimpleAuthenticator(userName, password));
112 LOG.info("createInstance(): Initializing mail session using SMTP authentication.");
113 } else {
114 mailSession = Session.getInstance(properties);
115 LOG.info("createInstance(): Initializing mail session. No SMTP authentication.");
116 }
117
118
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 LOG.debug("createInstance(): setting mail transport protocol = " + protocol);
130 mailSender.setProtocol(protocol);
131 }
132 mailSender.setSession(mailSession);
133
134 LOG.info("createInstance(): Mail Sender Factory Bean initialized.");
135 return mailSender;
136 }
137
138 private class SimpleAuthenticator extends Authenticator {
139
140 private final PasswordAuthentication passwordAuthentication;
141
142 private SimpleAuthenticator(String username, String password) {
143 this.passwordAuthentication = new PasswordAuthentication(username, password);
144 }
145
146 public PasswordAuthentication getPasswordAuthentication() {
147 return passwordAuthentication;
148 }
149 }
150
151 @SuppressWarnings("unchecked")
152 @Override
153 public Class getObjectType() {
154 return JavaMailSenderImpl.class;
155 }
156
157
158 }